@axi-engine/fields 0.3.10 → 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -789,7 +789,7 @@ interface FieldSnapshot {
789
789
  * to resolve class constructors and a `PolicySerializer` to handle the state
790
790
  * of any attached policies.
791
791
  */
792
- declare class FieldSerializer {
792
+ declare class FieldHydrator {
793
793
  private readonly fieldRegistry;
794
794
  private readonly policySerializer;
795
795
  /**
@@ -798,13 +798,6 @@ declare class FieldSerializer {
798
798
  * @param {PolicySerializer} policySerializer - A serializer dedicated to handling Policy instances.
799
799
  */
800
800
  constructor(fieldRegistry: FieldRegistry, policySerializer: PolicySerializer);
801
- /**
802
- * Creates a serializable snapshot of a Field instance.
803
- * The snapshot includes the field's type, name, current value, and the state of all its policies.
804
- * @param {Field<any>} field - The Field instance to serialize.
805
- * @returns {FieldSnapshot} A plain object ready for JSON serialization.
806
- */
807
- snapshot(field: Field<any>): FieldSnapshot;
808
801
  /**
809
802
  * Restores a Field instance from its snapshot representation.
810
803
  * It uses the `__type` property to find the correct constructor and hydrates
@@ -828,6 +821,18 @@ declare class FieldSerializer {
828
821
  private hydratePolicies;
829
822
  }
830
823
 
824
+ declare class FieldSnapshotter {
825
+ readonly policySerializer: PolicySerializer;
826
+ constructor(policySerializer: PolicySerializer);
827
+ /**
828
+ * Creates a serializable snapshot of a Field instance.
829
+ * The snapshot includes the field's type, name, current value, and the state of all its policies.
830
+ * @param {Field<any>} field - The Field instance to serialize.
831
+ * @returns {FieldSnapshot} A plain object ready for JSON serialization.
832
+ */
833
+ snapshot(field: Field<any>): FieldSnapshot;
834
+ }
835
+
831
836
  /**
832
837
  * A plain object representation of a Fields container's state for serialization.
833
838
  */
@@ -837,25 +842,53 @@ interface FieldsSnapshot {
837
842
  }
838
843
 
839
844
  /**
840
- * Orchestrates the serialization and deserialization of `Fields` container instances.
841
- *
842
- * This class acts as a high-level composer, responsible for converting an entire `Fields` object
843
- * into a storable snapshot and back.
844
- * It delegates the actual serialization of each `Field` and `Policy` to their respective serializers.
845
- *
846
- * @todo Implement a `patch(fields, snapshot)` method. It should perform a non-destructive
847
- * update, creating new fields, removing missing ones, and patching existing ones
848
- * in place, preserving the container instance itself.
845
+ * Deserialization of `Fields` container instances.
846
+ * Responsible for converting snapshot of `Fields` object into a `Fields` instance.
849
847
  */
850
- declare class FieldsSerializer<TFields extends Fields> {
848
+ declare class FieldsHydrator<TFields extends Fields> {
851
849
  private readonly fieldsFactory;
852
- private readonly fieldSerializer;
850
+ private readonly fieldHydrator;
853
851
  /**
854
852
  * Creates an instance of FieldsSerializer.
855
853
  * @param {FieldsFactory} fieldsFactory - A registry that maps string type names to Field constructors.
856
- * @param {FieldSerializer} fieldSerializer - A serializer of field instances.
854
+ * @param {FieldHydrator} fieldHydrator - A hydrator of field instances.
855
+ */
856
+ constructor(fieldsFactory: FieldsFactory<TFields>, fieldHydrator: FieldHydrator);
857
+ /**
858
+ * Restores a `Fields` container instance from its snapshot representation.
859
+ *
860
+ * It iterates through the field snapshots and hydrates them individually, adding them to the new container.
861
+ * @param {FieldsSnapshot} snapshot - The plain object snapshot to deserialize.
862
+ * @returns {Fields} A new `DefaultFields` instance populated with the restored fields.
863
+ */
864
+ hydrate(snapshot: FieldsSnapshot): TFields;
865
+ /**
866
+ * Synchronizes an existing `Fields` container with a snapshot.
867
+ *
868
+ * This method performs a "smart update":
869
+ * 1. **Removes** fields from the container that are missing in the snapshot.
870
+ * 2. **Patches** existing fields in-place using {@link FieldHydrator.patch}, preserving object references.
871
+ * 3. **Creates** (hydrates) and adds new fields that exist in the snapshot but not in the container.
872
+ *
873
+ * @param {Fields} fields - The target `Fields` container to update.
874
+ * @param {FieldsSnapshot} snapshot - The source snapshot containing the desired state.
857
875
  */
858
- constructor(fieldsFactory: FieldsFactory<TFields>, fieldSerializer: FieldSerializer);
876
+ patch(fields: TFields, snapshot: FieldsSnapshot): void;
877
+ }
878
+
879
+ /**
880
+ * The serialization of `Fields` container instances.
881
+ *
882
+ * This class responsible for converting an entire `Fields` object
883
+ * into a storable snapshot.
884
+ */
885
+ declare class FieldsSnapshotter {
886
+ private readonly fieldSnapshotter;
887
+ /**
888
+ * Creates an instance of FieldsSnapshotter.
889
+ * @param {FieldSnapshotter} fieldSnapshotter - A serializer of field instances.
890
+ */
891
+ constructor(fieldSnapshotter: FieldSnapshotter);
859
892
  /**
860
893
  * Creates a serializable snapshot of a `Fields` container.
861
894
  *
@@ -865,14 +898,6 @@ declare class FieldsSerializer<TFields extends Fields> {
865
898
  * @returns {FieldsSnapshot} A plain object ready for JSON serialization.
866
899
  */
867
900
  snapshot(fields: Fields): FieldsSnapshot;
868
- /**
869
- * Restores a `Fields` container instance from its snapshot representation.
870
- *
871
- * It iterates through the field snapshots and hydrates them individually, adding them to the new container.
872
- * @param {FieldsSnapshot} snapshot - The plain object snapshot to deserialize.
873
- * @returns {Fields} A new `DefaultFields` instance populated with the restored fields.
874
- */
875
- hydrate(snapshot: FieldsSnapshot): TFields;
876
901
  }
877
902
 
878
903
  /**
@@ -890,38 +915,66 @@ interface FieldTreeSnapshot {
890
915
  }
891
916
 
892
917
  /**
893
- * Orchestrates the recursive serialization and deserialization of `FieldTree` instances.
918
+ * Orchestrates the recursive deserialization of `FieldTree` instances.
894
919
  *
895
920
  * This class handles the conversion of an entire `FieldTree` object graph into a
896
921
  * plain, storable snapshot and vice-versa. It delegates the processing of `Fields`
897
- * leaf nodes to a dedicated `FieldsSerializer`.
922
+ * leaf nodes to a dedicated `FieldsHydrator`.
898
923
  * @todo Refactoring: The current implementation uses `if/else` logic in `snapshot` and `hydrate`
899
924
  * to process different node types. A more extensible approach would be to use a
900
925
  * registry of dedicated handlers for each node type.
901
926
  * This would allow new node types to be supported without
902
927
  * modifying this class, adhering to the Open/Closed Principle.
903
- *
904
- * @todo Implement a `patch(tree, snapshot)` method for recursive, non-destructive
905
- * updates. This method should traverse the existing tree and the snapshot,
906
- * patching nodes in place to maintain object references.
907
928
  */
908
- declare class FieldTreeSerializer<TFields extends Fields> {
929
+ declare class FieldTreeHydrator<TFields extends Fields> {
909
930
  _factory: FieldTreeFactory<TFields>;
910
- _fieldsSerializer: FieldsSerializer<TFields>;
931
+ _fieldsHydrator: FieldsHydrator<TFields>;
911
932
  get factory(): FieldTreeFactory<TFields>;
912
- get fieldsSerializer(): FieldsSerializer<TFields>;
913
- constructor(fieldTreeNodeFactory: FieldTreeFactory<TFields>, fieldsSerializer: FieldsSerializer<TFields>);
914
- /**
915
- * Creates a serializable snapshot of the entire tree and its contained fields.
916
- * @returns A plain JavaScript object representing the complete state managed by this tree.
917
- */
918
- snapshot(tree: FieldTree<TFields>): FieldTreeSnapshot;
933
+ get fieldsHydrator(): FieldsHydrator<TFields>;
934
+ constructor(fieldTreeNodeFactory: FieldTreeFactory<TFields>, fieldsHydrator: FieldsHydrator<TFields>);
919
935
  /**
920
936
  * Restores the state of the tree from a snapshot.
921
937
  * It intelligently creates missing nodes based on `__type` metadata and delegates hydration to child nodes.
922
938
  * @param snapshot The snapshot object to load.
923
939
  */
924
940
  hydrate(snapshot: FieldTreeSnapshot): FieldTree<TFields>;
941
+ /**
942
+ * Synchronizes an existing `FieldTree` branch with a snapshot.
943
+ *
944
+ * This method performs a recursive update to match the tree state with the provided snapshot:
945
+ * 1. **Removes** child nodes that are present in the tree but missing in the snapshot.
946
+ * 2. **Creates** new nodes that are present in the snapshot but missing in the tree.
947
+ * 3. **Replaces** nodes if their type has changed (e.g., replacing a `Fields` leaf with a `FieldTree` branch).
948
+ * 4. **Patches** existing matching nodes in-place (recursively).
949
+ *
950
+ * @param {FieldTree} tree - The target tree instance to update.
951
+ * @param {FieldTreeSnapshot} snapshot - The source snapshot containing the desired state.
952
+ */
953
+ patch(tree: FieldTree<TFields>, snapshot: FieldTreeSnapshot): void;
954
+ /**
955
+ * Helper method to instantiate and add a new node to the tree based on the snapshot type.
956
+ *
957
+ * It determines whether to create a nested `FieldTree` or a `Fields` container
958
+ * by inspecting the `__type` property of the snapshot, hydrates it, and attaches it to the parent tree.
959
+ *
960
+ * @param {FieldTree} tree - The parent tree instance where the new node will be added.
961
+ * @param {string} key - The name (key) for the new node.
962
+ * @param {FieldsSnapshot | FieldTreeSnapshot} snapshot - The source snapshot data.
963
+ * @throws If the snapshot contains an unsupported or unknown `__type`.
964
+ */
965
+ private addNodeFromSnapshot;
966
+ }
967
+
968
+ /**
969
+ */
970
+ declare class FieldTreeSnapshotter {
971
+ readonly fieldsSnapshotter: FieldsSnapshotter;
972
+ constructor(fieldsSnapshotter: FieldsSnapshotter);
973
+ /**
974
+ * Creates a serializable snapshot of the entire tree and its contained fields.
975
+ * @returns A plain JavaScript object representing the complete state managed by this tree.
976
+ */
977
+ snapshot(tree: CoreFieldTree): FieldTreeSnapshot;
925
978
  }
926
979
 
927
980
  /**
@@ -1162,6 +1215,14 @@ declare class DataStore implements Store {
1162
1215
  * @internal Used for serialization
1163
1216
  */
1164
1217
  getInternalTree(): CoreFieldTree | undefined;
1218
+ /**
1219
+ * @internal Used for serialization
1220
+ */
1221
+ getOrCreateInternalVariables(): CoreFields;
1222
+ /**
1223
+ * @internal Used for serialization
1224
+ */
1225
+ getOrCreateInternalTree(): CoreFieldTree;
1165
1226
  /**
1166
1227
  * @private
1167
1228
  */
@@ -1173,29 +1234,15 @@ declare class DataStore implements Store {
1173
1234
  }
1174
1235
 
1175
1236
  /**
1176
- * Handles the serialization and deserialization of DataStore instances.
1177
1237
  *
1178
- * This class ensures that both components of a DataStore (the detached variables
1179
- * and the hierarchical tree) are correctly persisted and restored. It delegates
1180
- * the actual serialization of the inner structures to the `FieldTreeSerializer`.
1181
1238
  */
1182
- declare class DataStoreSerializer {
1183
- private readonly fieldTreeSerializer;
1239
+ declare class DataStoreHydrator {
1240
+ private readonly fieldsFieldTreeHydrator;
1184
1241
  /**
1185
1242
  * Creates an instance of DataStoreSerializer.
1186
- * @param {FieldTreeSerializer} fieldTreeSerializer - The serializer used for the underlying tree and fields.
1243
+ * @param {FieldTreeHydrator} fieldsFieldTreeHydrator - The serializer used for the underlying tree and fields.
1187
1244
  */
1188
- constructor(fieldTreeSerializer: FieldTreeSerializer<CoreFields>);
1189
- /**
1190
- * Captures the current state of a DataStore into a serializable snapshot.
1191
- *
1192
- * It checks for the existence of internal variables and the internal tree,
1193
- * serializing them only if they have been initialized (lazy serialization).
1194
- *
1195
- * @param {DataStore} store - The store instance to serialize.
1196
- * @returns {DataStoreSnapshot} The snapshot object.
1197
- */
1198
- snapshot(store: DataStore): DataStoreSnapshot;
1245
+ constructor(fieldsFieldTreeHydrator: FieldTreeHydrator<CoreFields>);
1199
1246
  /**
1200
1247
  * Reconstructs a DataStore instance from a snapshot.
1201
1248
  *
@@ -1207,6 +1254,41 @@ declare class DataStoreSerializer {
1207
1254
  * @returns {DataStore} A new, fully restored DataStore instance.
1208
1255
  */
1209
1256
  hydrate(snapshot: DataStoreSnapshot): DataStore;
1257
+ /**
1258
+ * Synchronizes a DataStore instance with a snapshot.
1259
+ *
1260
+ * This method ensures the DataStore's internal state matches the snapshot by:
1261
+ * 1. **Destroying** internal containers (variables/tree) if they are missing in the snapshot.
1262
+ * 2. **Patching** (updating/creating) contents if they exist in the snapshot.
1263
+ *
1264
+ * This allows for a granular update where only specific parts of the store (e.g., only variables)
1265
+ * are modified if the snapshot contains partial data, or a full reset if parts are missing.
1266
+ *
1267
+ * @param {DataStore} store - The target DataStore to update.
1268
+ * @param {DataStoreSnapshot} snapshot - The source snapshot.
1269
+ */
1270
+ patch(store: DataStore, snapshot: DataStoreSnapshot): void;
1271
+ }
1272
+
1273
+ /**
1274
+ */
1275
+ declare class DataStoreSnapshotter {
1276
+ private readonly treeSnapshotter;
1277
+ /**
1278
+ * Creates an instance of DataStoreSnapshotter.
1279
+ * @param {FieldTreeSnapshotter} treeSnapshotter - The serializer used for the underlying tree and fields.
1280
+ */
1281
+ constructor(treeSnapshotter: FieldTreeSnapshotter);
1282
+ /**
1283
+ * Captures the current state of a DataStore into a serializable snapshot.
1284
+ *
1285
+ * It checks for the existence of internal variables and the internal tree,
1286
+ * serializing them only if they have been initialized (lazy serialization).
1287
+ *
1288
+ * @param {DataStore} store - The store instance to serialize.
1289
+ * @returns {DataStoreSnapshot} The snapshot object.
1290
+ */
1291
+ snapshot(store: DataStore): DataStoreSnapshot;
1210
1292
  }
1211
1293
 
1212
1294
  /**
@@ -1267,20 +1349,20 @@ declare function createCoreTreeNodeFactory(fieldRegistry: FieldRegistry): CoreTr
1267
1349
  * This function composes all necessary serializers (FieldTree, Fields, Field) for a complete setup.
1268
1350
  * @param {CoreTreeNodeFactory} fieldTreeNodeFactory - The factory used to create new tree nodes during deserialization.
1269
1351
  * @param policySerializer
1270
- * @returns {FieldTreeSerializer<CoreFields>} A top-level serializer for the entire field tree.
1352
+ * @returns {FieldTreeHydrator<CoreFields>} A top-level serializer for the entire field tree.
1271
1353
  */
1272
- declare function createCoreTreeSerializer(fieldTreeNodeFactory: CoreTreeNodeFactory, policySerializer?: PolicySerializer): FieldTreeSerializer<CoreFields>;
1354
+ declare function createCoreTreeSerializer(fieldTreeNodeFactory: CoreTreeNodeFactory, policySerializer?: PolicySerializer): FieldTreeHydrator<CoreFields>;
1273
1355
  interface CoreFieldSystemConfig {
1274
1356
  registry?: FieldRegistry;
1275
1357
  policySerializer?: PolicySerializer;
1276
1358
  }
1277
1359
  /**
1278
1360
  * Creates a complete core setup for the field system.
1279
- * @returns {{factory: CoreTreeNodeFactory, serializer: FieldTreeSerializer<CoreFields>}}
1361
+ * @returns {{factory: CoreTreeNodeFactory, serializer: FieldTreeHydrator<CoreFields>}}
1280
1362
  */
1281
1363
  declare function createCoreFieldSystem(config?: CoreFieldSystemConfig): {
1282
1364
  factory: CoreTreeNodeFactory;
1283
- serializer: FieldTreeSerializer<CoreFields>;
1365
+ serializer: FieldTreeHydrator<CoreFields>;
1284
1366
  };
1285
1367
 
1286
- export { type BooleanField, ClampMaxPolicy, ClampMaxPolicySerializerHandler, ClampMinPolicy, ClampMinPolicySerializerHandler, ClampPolicy, ClampPolicySerializerHandler, CoreBooleanField, type CoreBooleanFieldOptions, CoreField, type CoreFieldSystemConfig, CoreFieldTree, CoreFields, CoreFieldsFactory, CoreNumericField, type CoreNumericFieldOptions, CoreStringField, type CoreStringFieldOptions, CoreTreeNodeFactory, DataStore, DataStoreSerializer, type DataStoreSnapshot, type Field, type FieldOptions, FieldRegistry, FieldSerializer, type FieldSnapshot, FieldTree, type FieldTreeFactory, FieldTreeSerializer, type FieldTreeSnapshot, Fields, type FieldsFactory, FieldsSerializer, type FieldsSnapshot, type NumericField, Policies, type Policy, PolicySerializer, type PolicySerializerHandler, type Store, type StoreCreateFieldOptions, type StringField, type TreeNode, clampMaxPolicy, clampMinPolicy, clampPolicy, createCoreFieldRegistry, createCoreFieldSystem, createCorePolicySerializer, createCoreTreeNodeFactory, createCoreTreeSerializer, createTypedMethodsMixin, isDataStore, isFieldTree, isFields };
1368
+ export { type BooleanField, ClampMaxPolicy, ClampMaxPolicySerializerHandler, ClampMinPolicy, ClampMinPolicySerializerHandler, ClampPolicy, ClampPolicySerializerHandler, CoreBooleanField, type CoreBooleanFieldOptions, CoreField, type CoreFieldSystemConfig, CoreFieldTree, CoreFields, CoreFieldsFactory, CoreNumericField, type CoreNumericFieldOptions, CoreStringField, type CoreStringFieldOptions, CoreTreeNodeFactory, DataStore, DataStoreHydrator, type DataStoreSnapshot, DataStoreSnapshotter, type Field, FieldHydrator, type FieldOptions, FieldRegistry, type FieldSnapshot, FieldSnapshotter, FieldTree, type FieldTreeFactory, FieldTreeHydrator, type FieldTreeSnapshot, FieldTreeSnapshotter, Fields, type FieldsFactory, FieldsHydrator, type FieldsSnapshot, FieldsSnapshotter, type NumericField, Policies, type Policy, PolicySerializer, type PolicySerializerHandler, type Store, type StoreCreateFieldOptions, type StringField, type TreeNode, clampMaxPolicy, clampMinPolicy, clampPolicy, createCoreFieldRegistry, createCoreFieldSystem, createCorePolicySerializer, createCoreTreeNodeFactory, createCoreTreeSerializer, createTypedMethodsMixin, isDataStore, isFieldTree, isFields };
package/dist/index.d.ts CHANGED
@@ -789,7 +789,7 @@ interface FieldSnapshot {
789
789
  * to resolve class constructors and a `PolicySerializer` to handle the state
790
790
  * of any attached policies.
791
791
  */
792
- declare class FieldSerializer {
792
+ declare class FieldHydrator {
793
793
  private readonly fieldRegistry;
794
794
  private readonly policySerializer;
795
795
  /**
@@ -798,13 +798,6 @@ declare class FieldSerializer {
798
798
  * @param {PolicySerializer} policySerializer - A serializer dedicated to handling Policy instances.
799
799
  */
800
800
  constructor(fieldRegistry: FieldRegistry, policySerializer: PolicySerializer);
801
- /**
802
- * Creates a serializable snapshot of a Field instance.
803
- * The snapshot includes the field's type, name, current value, and the state of all its policies.
804
- * @param {Field<any>} field - The Field instance to serialize.
805
- * @returns {FieldSnapshot} A plain object ready for JSON serialization.
806
- */
807
- snapshot(field: Field<any>): FieldSnapshot;
808
801
  /**
809
802
  * Restores a Field instance from its snapshot representation.
810
803
  * It uses the `__type` property to find the correct constructor and hydrates
@@ -828,6 +821,18 @@ declare class FieldSerializer {
828
821
  private hydratePolicies;
829
822
  }
830
823
 
824
+ declare class FieldSnapshotter {
825
+ readonly policySerializer: PolicySerializer;
826
+ constructor(policySerializer: PolicySerializer);
827
+ /**
828
+ * Creates a serializable snapshot of a Field instance.
829
+ * The snapshot includes the field's type, name, current value, and the state of all its policies.
830
+ * @param {Field<any>} field - The Field instance to serialize.
831
+ * @returns {FieldSnapshot} A plain object ready for JSON serialization.
832
+ */
833
+ snapshot(field: Field<any>): FieldSnapshot;
834
+ }
835
+
831
836
  /**
832
837
  * A plain object representation of a Fields container's state for serialization.
833
838
  */
@@ -837,25 +842,53 @@ interface FieldsSnapshot {
837
842
  }
838
843
 
839
844
  /**
840
- * Orchestrates the serialization and deserialization of `Fields` container instances.
841
- *
842
- * This class acts as a high-level composer, responsible for converting an entire `Fields` object
843
- * into a storable snapshot and back.
844
- * It delegates the actual serialization of each `Field` and `Policy` to their respective serializers.
845
- *
846
- * @todo Implement a `patch(fields, snapshot)` method. It should perform a non-destructive
847
- * update, creating new fields, removing missing ones, and patching existing ones
848
- * in place, preserving the container instance itself.
845
+ * Deserialization of `Fields` container instances.
846
+ * Responsible for converting snapshot of `Fields` object into a `Fields` instance.
849
847
  */
850
- declare class FieldsSerializer<TFields extends Fields> {
848
+ declare class FieldsHydrator<TFields extends Fields> {
851
849
  private readonly fieldsFactory;
852
- private readonly fieldSerializer;
850
+ private readonly fieldHydrator;
853
851
  /**
854
852
  * Creates an instance of FieldsSerializer.
855
853
  * @param {FieldsFactory} fieldsFactory - A registry that maps string type names to Field constructors.
856
- * @param {FieldSerializer} fieldSerializer - A serializer of field instances.
854
+ * @param {FieldHydrator} fieldHydrator - A hydrator of field instances.
855
+ */
856
+ constructor(fieldsFactory: FieldsFactory<TFields>, fieldHydrator: FieldHydrator);
857
+ /**
858
+ * Restores a `Fields` container instance from its snapshot representation.
859
+ *
860
+ * It iterates through the field snapshots and hydrates them individually, adding them to the new container.
861
+ * @param {FieldsSnapshot} snapshot - The plain object snapshot to deserialize.
862
+ * @returns {Fields} A new `DefaultFields` instance populated with the restored fields.
863
+ */
864
+ hydrate(snapshot: FieldsSnapshot): TFields;
865
+ /**
866
+ * Synchronizes an existing `Fields` container with a snapshot.
867
+ *
868
+ * This method performs a "smart update":
869
+ * 1. **Removes** fields from the container that are missing in the snapshot.
870
+ * 2. **Patches** existing fields in-place using {@link FieldHydrator.patch}, preserving object references.
871
+ * 3. **Creates** (hydrates) and adds new fields that exist in the snapshot but not in the container.
872
+ *
873
+ * @param {Fields} fields - The target `Fields` container to update.
874
+ * @param {FieldsSnapshot} snapshot - The source snapshot containing the desired state.
857
875
  */
858
- constructor(fieldsFactory: FieldsFactory<TFields>, fieldSerializer: FieldSerializer);
876
+ patch(fields: TFields, snapshot: FieldsSnapshot): void;
877
+ }
878
+
879
+ /**
880
+ * The serialization of `Fields` container instances.
881
+ *
882
+ * This class responsible for converting an entire `Fields` object
883
+ * into a storable snapshot.
884
+ */
885
+ declare class FieldsSnapshotter {
886
+ private readonly fieldSnapshotter;
887
+ /**
888
+ * Creates an instance of FieldsSnapshotter.
889
+ * @param {FieldSnapshotter} fieldSnapshotter - A serializer of field instances.
890
+ */
891
+ constructor(fieldSnapshotter: FieldSnapshotter);
859
892
  /**
860
893
  * Creates a serializable snapshot of a `Fields` container.
861
894
  *
@@ -865,14 +898,6 @@ declare class FieldsSerializer<TFields extends Fields> {
865
898
  * @returns {FieldsSnapshot} A plain object ready for JSON serialization.
866
899
  */
867
900
  snapshot(fields: Fields): FieldsSnapshot;
868
- /**
869
- * Restores a `Fields` container instance from its snapshot representation.
870
- *
871
- * It iterates through the field snapshots and hydrates them individually, adding them to the new container.
872
- * @param {FieldsSnapshot} snapshot - The plain object snapshot to deserialize.
873
- * @returns {Fields} A new `DefaultFields` instance populated with the restored fields.
874
- */
875
- hydrate(snapshot: FieldsSnapshot): TFields;
876
901
  }
877
902
 
878
903
  /**
@@ -890,38 +915,66 @@ interface FieldTreeSnapshot {
890
915
  }
891
916
 
892
917
  /**
893
- * Orchestrates the recursive serialization and deserialization of `FieldTree` instances.
918
+ * Orchestrates the recursive deserialization of `FieldTree` instances.
894
919
  *
895
920
  * This class handles the conversion of an entire `FieldTree` object graph into a
896
921
  * plain, storable snapshot and vice-versa. It delegates the processing of `Fields`
897
- * leaf nodes to a dedicated `FieldsSerializer`.
922
+ * leaf nodes to a dedicated `FieldsHydrator`.
898
923
  * @todo Refactoring: The current implementation uses `if/else` logic in `snapshot` and `hydrate`
899
924
  * to process different node types. A more extensible approach would be to use a
900
925
  * registry of dedicated handlers for each node type.
901
926
  * This would allow new node types to be supported without
902
927
  * modifying this class, adhering to the Open/Closed Principle.
903
- *
904
- * @todo Implement a `patch(tree, snapshot)` method for recursive, non-destructive
905
- * updates. This method should traverse the existing tree and the snapshot,
906
- * patching nodes in place to maintain object references.
907
928
  */
908
- declare class FieldTreeSerializer<TFields extends Fields> {
929
+ declare class FieldTreeHydrator<TFields extends Fields> {
909
930
  _factory: FieldTreeFactory<TFields>;
910
- _fieldsSerializer: FieldsSerializer<TFields>;
931
+ _fieldsHydrator: FieldsHydrator<TFields>;
911
932
  get factory(): FieldTreeFactory<TFields>;
912
- get fieldsSerializer(): FieldsSerializer<TFields>;
913
- constructor(fieldTreeNodeFactory: FieldTreeFactory<TFields>, fieldsSerializer: FieldsSerializer<TFields>);
914
- /**
915
- * Creates a serializable snapshot of the entire tree and its contained fields.
916
- * @returns A plain JavaScript object representing the complete state managed by this tree.
917
- */
918
- snapshot(tree: FieldTree<TFields>): FieldTreeSnapshot;
933
+ get fieldsHydrator(): FieldsHydrator<TFields>;
934
+ constructor(fieldTreeNodeFactory: FieldTreeFactory<TFields>, fieldsHydrator: FieldsHydrator<TFields>);
919
935
  /**
920
936
  * Restores the state of the tree from a snapshot.
921
937
  * It intelligently creates missing nodes based on `__type` metadata and delegates hydration to child nodes.
922
938
  * @param snapshot The snapshot object to load.
923
939
  */
924
940
  hydrate(snapshot: FieldTreeSnapshot): FieldTree<TFields>;
941
+ /**
942
+ * Synchronizes an existing `FieldTree` branch with a snapshot.
943
+ *
944
+ * This method performs a recursive update to match the tree state with the provided snapshot:
945
+ * 1. **Removes** child nodes that are present in the tree but missing in the snapshot.
946
+ * 2. **Creates** new nodes that are present in the snapshot but missing in the tree.
947
+ * 3. **Replaces** nodes if their type has changed (e.g., replacing a `Fields` leaf with a `FieldTree` branch).
948
+ * 4. **Patches** existing matching nodes in-place (recursively).
949
+ *
950
+ * @param {FieldTree} tree - The target tree instance to update.
951
+ * @param {FieldTreeSnapshot} snapshot - The source snapshot containing the desired state.
952
+ */
953
+ patch(tree: FieldTree<TFields>, snapshot: FieldTreeSnapshot): void;
954
+ /**
955
+ * Helper method to instantiate and add a new node to the tree based on the snapshot type.
956
+ *
957
+ * It determines whether to create a nested `FieldTree` or a `Fields` container
958
+ * by inspecting the `__type` property of the snapshot, hydrates it, and attaches it to the parent tree.
959
+ *
960
+ * @param {FieldTree} tree - The parent tree instance where the new node will be added.
961
+ * @param {string} key - The name (key) for the new node.
962
+ * @param {FieldsSnapshot | FieldTreeSnapshot} snapshot - The source snapshot data.
963
+ * @throws If the snapshot contains an unsupported or unknown `__type`.
964
+ */
965
+ private addNodeFromSnapshot;
966
+ }
967
+
968
+ /**
969
+ */
970
+ declare class FieldTreeSnapshotter {
971
+ readonly fieldsSnapshotter: FieldsSnapshotter;
972
+ constructor(fieldsSnapshotter: FieldsSnapshotter);
973
+ /**
974
+ * Creates a serializable snapshot of the entire tree and its contained fields.
975
+ * @returns A plain JavaScript object representing the complete state managed by this tree.
976
+ */
977
+ snapshot(tree: CoreFieldTree): FieldTreeSnapshot;
925
978
  }
926
979
 
927
980
  /**
@@ -1162,6 +1215,14 @@ declare class DataStore implements Store {
1162
1215
  * @internal Used for serialization
1163
1216
  */
1164
1217
  getInternalTree(): CoreFieldTree | undefined;
1218
+ /**
1219
+ * @internal Used for serialization
1220
+ */
1221
+ getOrCreateInternalVariables(): CoreFields;
1222
+ /**
1223
+ * @internal Used for serialization
1224
+ */
1225
+ getOrCreateInternalTree(): CoreFieldTree;
1165
1226
  /**
1166
1227
  * @private
1167
1228
  */
@@ -1173,29 +1234,15 @@ declare class DataStore implements Store {
1173
1234
  }
1174
1235
 
1175
1236
  /**
1176
- * Handles the serialization and deserialization of DataStore instances.
1177
1237
  *
1178
- * This class ensures that both components of a DataStore (the detached variables
1179
- * and the hierarchical tree) are correctly persisted and restored. It delegates
1180
- * the actual serialization of the inner structures to the `FieldTreeSerializer`.
1181
1238
  */
1182
- declare class DataStoreSerializer {
1183
- private readonly fieldTreeSerializer;
1239
+ declare class DataStoreHydrator {
1240
+ private readonly fieldsFieldTreeHydrator;
1184
1241
  /**
1185
1242
  * Creates an instance of DataStoreSerializer.
1186
- * @param {FieldTreeSerializer} fieldTreeSerializer - The serializer used for the underlying tree and fields.
1243
+ * @param {FieldTreeHydrator} fieldsFieldTreeHydrator - The serializer used for the underlying tree and fields.
1187
1244
  */
1188
- constructor(fieldTreeSerializer: FieldTreeSerializer<CoreFields>);
1189
- /**
1190
- * Captures the current state of a DataStore into a serializable snapshot.
1191
- *
1192
- * It checks for the existence of internal variables and the internal tree,
1193
- * serializing them only if they have been initialized (lazy serialization).
1194
- *
1195
- * @param {DataStore} store - The store instance to serialize.
1196
- * @returns {DataStoreSnapshot} The snapshot object.
1197
- */
1198
- snapshot(store: DataStore): DataStoreSnapshot;
1245
+ constructor(fieldsFieldTreeHydrator: FieldTreeHydrator<CoreFields>);
1199
1246
  /**
1200
1247
  * Reconstructs a DataStore instance from a snapshot.
1201
1248
  *
@@ -1207,6 +1254,41 @@ declare class DataStoreSerializer {
1207
1254
  * @returns {DataStore} A new, fully restored DataStore instance.
1208
1255
  */
1209
1256
  hydrate(snapshot: DataStoreSnapshot): DataStore;
1257
+ /**
1258
+ * Synchronizes a DataStore instance with a snapshot.
1259
+ *
1260
+ * This method ensures the DataStore's internal state matches the snapshot by:
1261
+ * 1. **Destroying** internal containers (variables/tree) if they are missing in the snapshot.
1262
+ * 2. **Patching** (updating/creating) contents if they exist in the snapshot.
1263
+ *
1264
+ * This allows for a granular update where only specific parts of the store (e.g., only variables)
1265
+ * are modified if the snapshot contains partial data, or a full reset if parts are missing.
1266
+ *
1267
+ * @param {DataStore} store - The target DataStore to update.
1268
+ * @param {DataStoreSnapshot} snapshot - The source snapshot.
1269
+ */
1270
+ patch(store: DataStore, snapshot: DataStoreSnapshot): void;
1271
+ }
1272
+
1273
+ /**
1274
+ */
1275
+ declare class DataStoreSnapshotter {
1276
+ private readonly treeSnapshotter;
1277
+ /**
1278
+ * Creates an instance of DataStoreSnapshotter.
1279
+ * @param {FieldTreeSnapshotter} treeSnapshotter - The serializer used for the underlying tree and fields.
1280
+ */
1281
+ constructor(treeSnapshotter: FieldTreeSnapshotter);
1282
+ /**
1283
+ * Captures the current state of a DataStore into a serializable snapshot.
1284
+ *
1285
+ * It checks for the existence of internal variables and the internal tree,
1286
+ * serializing them only if they have been initialized (lazy serialization).
1287
+ *
1288
+ * @param {DataStore} store - The store instance to serialize.
1289
+ * @returns {DataStoreSnapshot} The snapshot object.
1290
+ */
1291
+ snapshot(store: DataStore): DataStoreSnapshot;
1210
1292
  }
1211
1293
 
1212
1294
  /**
@@ -1267,20 +1349,20 @@ declare function createCoreTreeNodeFactory(fieldRegistry: FieldRegistry): CoreTr
1267
1349
  * This function composes all necessary serializers (FieldTree, Fields, Field) for a complete setup.
1268
1350
  * @param {CoreTreeNodeFactory} fieldTreeNodeFactory - The factory used to create new tree nodes during deserialization.
1269
1351
  * @param policySerializer
1270
- * @returns {FieldTreeSerializer<CoreFields>} A top-level serializer for the entire field tree.
1352
+ * @returns {FieldTreeHydrator<CoreFields>} A top-level serializer for the entire field tree.
1271
1353
  */
1272
- declare function createCoreTreeSerializer(fieldTreeNodeFactory: CoreTreeNodeFactory, policySerializer?: PolicySerializer): FieldTreeSerializer<CoreFields>;
1354
+ declare function createCoreTreeSerializer(fieldTreeNodeFactory: CoreTreeNodeFactory, policySerializer?: PolicySerializer): FieldTreeHydrator<CoreFields>;
1273
1355
  interface CoreFieldSystemConfig {
1274
1356
  registry?: FieldRegistry;
1275
1357
  policySerializer?: PolicySerializer;
1276
1358
  }
1277
1359
  /**
1278
1360
  * Creates a complete core setup for the field system.
1279
- * @returns {{factory: CoreTreeNodeFactory, serializer: FieldTreeSerializer<CoreFields>}}
1361
+ * @returns {{factory: CoreTreeNodeFactory, serializer: FieldTreeHydrator<CoreFields>}}
1280
1362
  */
1281
1363
  declare function createCoreFieldSystem(config?: CoreFieldSystemConfig): {
1282
1364
  factory: CoreTreeNodeFactory;
1283
- serializer: FieldTreeSerializer<CoreFields>;
1365
+ serializer: FieldTreeHydrator<CoreFields>;
1284
1366
  };
1285
1367
 
1286
- export { type BooleanField, ClampMaxPolicy, ClampMaxPolicySerializerHandler, ClampMinPolicy, ClampMinPolicySerializerHandler, ClampPolicy, ClampPolicySerializerHandler, CoreBooleanField, type CoreBooleanFieldOptions, CoreField, type CoreFieldSystemConfig, CoreFieldTree, CoreFields, CoreFieldsFactory, CoreNumericField, type CoreNumericFieldOptions, CoreStringField, type CoreStringFieldOptions, CoreTreeNodeFactory, DataStore, DataStoreSerializer, type DataStoreSnapshot, type Field, type FieldOptions, FieldRegistry, FieldSerializer, type FieldSnapshot, FieldTree, type FieldTreeFactory, FieldTreeSerializer, type FieldTreeSnapshot, Fields, type FieldsFactory, FieldsSerializer, type FieldsSnapshot, type NumericField, Policies, type Policy, PolicySerializer, type PolicySerializerHandler, type Store, type StoreCreateFieldOptions, type StringField, type TreeNode, clampMaxPolicy, clampMinPolicy, clampPolicy, createCoreFieldRegistry, createCoreFieldSystem, createCorePolicySerializer, createCoreTreeNodeFactory, createCoreTreeSerializer, createTypedMethodsMixin, isDataStore, isFieldTree, isFields };
1368
+ export { type BooleanField, ClampMaxPolicy, ClampMaxPolicySerializerHandler, ClampMinPolicy, ClampMinPolicySerializerHandler, ClampPolicy, ClampPolicySerializerHandler, CoreBooleanField, type CoreBooleanFieldOptions, CoreField, type CoreFieldSystemConfig, CoreFieldTree, CoreFields, CoreFieldsFactory, CoreNumericField, type CoreNumericFieldOptions, CoreStringField, type CoreStringFieldOptions, CoreTreeNodeFactory, DataStore, DataStoreHydrator, type DataStoreSnapshot, DataStoreSnapshotter, type Field, FieldHydrator, type FieldOptions, FieldRegistry, type FieldSnapshot, FieldSnapshotter, FieldTree, type FieldTreeFactory, FieldTreeHydrator, type FieldTreeSnapshot, FieldTreeSnapshotter, Fields, type FieldsFactory, FieldsHydrator, type FieldsSnapshot, FieldsSnapshotter, type NumericField, Policies, type Policy, PolicySerializer, type PolicySerializerHandler, type Store, type StoreCreateFieldOptions, type StringField, type TreeNode, clampMaxPolicy, clampMinPolicy, clampPolicy, createCoreFieldRegistry, createCoreFieldSystem, createCorePolicySerializer, createCoreTreeNodeFactory, createCoreTreeSerializer, createTypedMethodsMixin, isDataStore, isFieldTree, isFields };