@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 +149 -67
- package/dist/index.d.ts +149 -67
- package/dist/index.js +274 -117
- package/dist/index.mjs +267 -114
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -35,13 +35,17 @@ __export(index_exports, {
|
|
|
35
35
|
CoreStringField: () => CoreStringField,
|
|
36
36
|
CoreTreeNodeFactory: () => CoreTreeNodeFactory,
|
|
37
37
|
DataStore: () => DataStore,
|
|
38
|
-
|
|
38
|
+
DataStoreHydrator: () => DataStoreHydrator,
|
|
39
|
+
DataStoreSnapshotter: () => DataStoreSnapshotter,
|
|
40
|
+
FieldHydrator: () => FieldHydrator,
|
|
39
41
|
FieldRegistry: () => FieldRegistry,
|
|
40
|
-
|
|
42
|
+
FieldSnapshotter: () => FieldSnapshotter,
|
|
41
43
|
FieldTree: () => FieldTree,
|
|
42
|
-
|
|
44
|
+
FieldTreeHydrator: () => FieldTreeHydrator,
|
|
45
|
+
FieldTreeSnapshotter: () => FieldTreeSnapshotter,
|
|
43
46
|
Fields: () => Fields,
|
|
44
|
-
|
|
47
|
+
FieldsHydrator: () => FieldsHydrator,
|
|
48
|
+
FieldsSnapshotter: () => FieldsSnapshotter,
|
|
45
49
|
Policies: () => Policies,
|
|
46
50
|
PolicySerializer: () => PolicySerializer,
|
|
47
51
|
clampMaxPolicy: () => clampMaxPolicy,
|
|
@@ -469,6 +473,9 @@ var Fields = class _Fields {
|
|
|
469
473
|
*/
|
|
470
474
|
remove(names) {
|
|
471
475
|
const namesToRemove = Array.isArray(names) ? names : [names];
|
|
476
|
+
if (!namesToRemove.length) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
472
479
|
const reallyRemoved = namesToRemove.filter((name) => {
|
|
473
480
|
const field = this._fields.get(name);
|
|
474
481
|
if (!field) {
|
|
@@ -825,7 +832,7 @@ var CoreTreeNodeFactory = class extends CoreFieldsFactory {
|
|
|
825
832
|
}
|
|
826
833
|
};
|
|
827
834
|
|
|
828
|
-
// src/
|
|
835
|
+
// src/serializers/policies/clamp-policy-serializer-handler.ts
|
|
829
836
|
var ClampPolicySerializerHandler = class {
|
|
830
837
|
snapshot(policy) {
|
|
831
838
|
return { min: policy.min, max: policy.max };
|
|
@@ -835,7 +842,7 @@ var ClampPolicySerializerHandler = class {
|
|
|
835
842
|
}
|
|
836
843
|
};
|
|
837
844
|
|
|
838
|
-
// src/
|
|
845
|
+
// src/serializers/policies/clamp-max-policy-serializer-handler.ts
|
|
839
846
|
var ClampMaxPolicySerializerHandler = class {
|
|
840
847
|
snapshot(policy) {
|
|
841
848
|
return { max: policy.max };
|
|
@@ -845,7 +852,7 @@ var ClampMaxPolicySerializerHandler = class {
|
|
|
845
852
|
}
|
|
846
853
|
};
|
|
847
854
|
|
|
848
|
-
// src/
|
|
855
|
+
// src/serializers/policies/clamp-min-policy-serializer-handler.ts
|
|
849
856
|
var ClampMinPolicySerializerHandler = class {
|
|
850
857
|
snapshot(policy) {
|
|
851
858
|
return { min: policy.min };
|
|
@@ -855,7 +862,7 @@ var ClampMinPolicySerializerHandler = class {
|
|
|
855
862
|
}
|
|
856
863
|
};
|
|
857
864
|
|
|
858
|
-
// src/
|
|
865
|
+
// src/serializers/policy-serializer.ts
|
|
859
866
|
var import_utils6 = require("@axi-engine/utils");
|
|
860
867
|
var PolicySerializer = class {
|
|
861
868
|
handlers = new import_utils6.Registry();
|
|
@@ -892,9 +899,9 @@ var PolicySerializer = class {
|
|
|
892
899
|
}
|
|
893
900
|
};
|
|
894
901
|
|
|
895
|
-
// src/
|
|
902
|
+
// src/serializers/field-hydrator.ts
|
|
896
903
|
var import_utils7 = require("@axi-engine/utils");
|
|
897
|
-
var
|
|
904
|
+
var FieldHydrator = class {
|
|
898
905
|
/**
|
|
899
906
|
* Creates an instance of FieldSerializer.
|
|
900
907
|
* @param {FieldRegistry} fieldRegistry - A registry that maps string type names to Field constructors.
|
|
@@ -904,25 +911,6 @@ var FieldSerializer = class {
|
|
|
904
911
|
this.fieldRegistry = fieldRegistry;
|
|
905
912
|
this.policySerializer = policySerializer;
|
|
906
913
|
}
|
|
907
|
-
/**
|
|
908
|
-
* Creates a serializable snapshot of a Field instance.
|
|
909
|
-
* The snapshot includes the field's type, name, current value, and the state of all its policies.
|
|
910
|
-
* @param {Field<any>} field - The Field instance to serialize.
|
|
911
|
-
* @returns {FieldSnapshot} A plain object ready for JSON serialization.
|
|
912
|
-
*/
|
|
913
|
-
snapshot(field) {
|
|
914
|
-
let snapshot = {
|
|
915
|
-
__type: field.typeName,
|
|
916
|
-
name: field.name,
|
|
917
|
-
value: field.value
|
|
918
|
-
};
|
|
919
|
-
if (!field.policies.isEmpty()) {
|
|
920
|
-
const serializedPolicies = [];
|
|
921
|
-
field.policies.items.forEach((policy) => serializedPolicies.push(this.policySerializer.snapshot(policy)));
|
|
922
|
-
snapshot.policies = serializedPolicies;
|
|
923
|
-
}
|
|
924
|
-
return snapshot;
|
|
925
|
-
}
|
|
926
914
|
/**
|
|
927
915
|
* Restores a Field instance from its snapshot representation.
|
|
928
916
|
* It uses the `__type` property to find the correct constructor and hydrates
|
|
@@ -949,48 +937,50 @@ var FieldSerializer = class {
|
|
|
949
937
|
* @param {FieldSnapshot} snapshot - The snapshot containing the new state.
|
|
950
938
|
*/
|
|
951
939
|
patch(field, snapshot) {
|
|
952
|
-
let policies = this.hydratePolicies(snapshot);
|
|
953
940
|
field.policies.clear();
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
}
|
|
941
|
+
const policies = this.hydratePolicies(snapshot);
|
|
942
|
+
policies?.forEach((p) => field.policies.add(p));
|
|
957
943
|
field.value = snapshot.value;
|
|
958
944
|
}
|
|
959
945
|
hydratePolicies(snapshot) {
|
|
960
|
-
|
|
961
|
-
|
|
946
|
+
return (0, import_utils7.isNullOrUndefined)(snapshot.policies) ? void 0 : snapshot.policies.map((p) => this.policySerializer.hydrate(p));
|
|
947
|
+
}
|
|
948
|
+
};
|
|
949
|
+
|
|
950
|
+
// src/serializers/field-snapshotter.ts
|
|
951
|
+
var FieldSnapshotter = class {
|
|
952
|
+
constructor(policySerializer) {
|
|
953
|
+
this.policySerializer = policySerializer;
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Creates a serializable snapshot of a Field instance.
|
|
957
|
+
* The snapshot includes the field's type, name, current value, and the state of all its policies.
|
|
958
|
+
* @param {Field<any>} field - The Field instance to serialize.
|
|
959
|
+
* @returns {FieldSnapshot} A plain object ready for JSON serialization.
|
|
960
|
+
*/
|
|
961
|
+
snapshot(field) {
|
|
962
|
+
let snapshot = {
|
|
963
|
+
__type: field.typeName,
|
|
964
|
+
name: field.name,
|
|
965
|
+
value: field.value
|
|
966
|
+
};
|
|
967
|
+
if (!field.policies.isEmpty()) {
|
|
968
|
+
snapshot.policies = Array.from(field.policies.items.values()).map((policy) => this.policySerializer.snapshot(policy));
|
|
962
969
|
}
|
|
963
|
-
|
|
964
|
-
snapshot.policies.forEach((p) => policies.push(this.policySerializer.hydrate(p)));
|
|
965
|
-
return policies;
|
|
970
|
+
return snapshot;
|
|
966
971
|
}
|
|
967
972
|
};
|
|
968
973
|
|
|
969
|
-
// src/
|
|
970
|
-
var
|
|
974
|
+
// src/serializers/fields-hydrator.ts
|
|
975
|
+
var FieldsHydrator = class {
|
|
971
976
|
/**
|
|
972
977
|
* Creates an instance of FieldsSerializer.
|
|
973
978
|
* @param {FieldsFactory} fieldsFactory - A registry that maps string type names to Field constructors.
|
|
974
|
-
* @param {
|
|
979
|
+
* @param {FieldHydrator} fieldHydrator - A hydrator of field instances.
|
|
975
980
|
*/
|
|
976
|
-
constructor(fieldsFactory,
|
|
981
|
+
constructor(fieldsFactory, fieldHydrator) {
|
|
977
982
|
this.fieldsFactory = fieldsFactory;
|
|
978
|
-
this.
|
|
979
|
-
}
|
|
980
|
-
/**
|
|
981
|
-
* Creates a serializable snapshot of a `Fields` container.
|
|
982
|
-
*
|
|
983
|
-
* The snapshot includes a `__type` identifier (currently hardcoded) and an array of snapshots
|
|
984
|
-
* for each `Field` within the container.
|
|
985
|
-
* @param {Fields} fields - The `Fields` instance to serialize.
|
|
986
|
-
* @returns {FieldsSnapshot} A plain object ready for JSON serialization.
|
|
987
|
-
*/
|
|
988
|
-
snapshot(fields) {
|
|
989
|
-
const res = {
|
|
990
|
-
__type: fields.typeName
|
|
991
|
-
};
|
|
992
|
-
fields.fields.forEach((field) => res[field.name] = this.fieldSerializer.snapshot(field));
|
|
993
|
-
return res;
|
|
983
|
+
this.fieldHydrator = fieldHydrator;
|
|
994
984
|
}
|
|
995
985
|
/**
|
|
996
986
|
* Restores a `Fields` container instance from its snapshot representation.
|
|
@@ -1004,44 +994,80 @@ var FieldsSerializer = class {
|
|
|
1004
994
|
const fields = this.fieldsFactory.fields();
|
|
1005
995
|
for (const fieldName in fieldsData) {
|
|
1006
996
|
const fieldSnapshot = fieldsData[fieldName];
|
|
1007
|
-
const restoredField = this.
|
|
997
|
+
const restoredField = this.fieldHydrator.hydrate(fieldSnapshot);
|
|
1008
998
|
fields.add(restoredField);
|
|
1009
999
|
}
|
|
1010
1000
|
return fields;
|
|
1011
1001
|
}
|
|
1002
|
+
/**
|
|
1003
|
+
* Synchronizes an existing `Fields` container with a snapshot.
|
|
1004
|
+
*
|
|
1005
|
+
* This method performs a "smart update":
|
|
1006
|
+
* 1. **Removes** fields from the container that are missing in the snapshot.
|
|
1007
|
+
* 2. **Patches** existing fields in-place using {@link FieldHydrator.patch}, preserving object references.
|
|
1008
|
+
* 3. **Creates** (hydrates) and adds new fields that exist in the snapshot but not in the container.
|
|
1009
|
+
*
|
|
1010
|
+
* @param {Fields} fields - The target `Fields` container to update.
|
|
1011
|
+
* @param {FieldsSnapshot} snapshot - The source snapshot containing the desired state.
|
|
1012
|
+
*/
|
|
1013
|
+
patch(fields, snapshot) {
|
|
1014
|
+
const { __type, ...fieldsData } = snapshot;
|
|
1015
|
+
const snapshotKeys = new Set(Object.keys(fieldsData));
|
|
1016
|
+
const fieldsToRemove = Array.from(fields.fields.values()).filter((f) => !snapshotKeys.has(f.name)).map((f) => f.name);
|
|
1017
|
+
fields.remove(fieldsToRemove);
|
|
1018
|
+
for (const fieldName in fieldsData) {
|
|
1019
|
+
const fieldSnapshot = fieldsData[fieldName];
|
|
1020
|
+
if (fields.has(fieldName)) {
|
|
1021
|
+
const existingField = fields.get(fieldName);
|
|
1022
|
+
this.fieldHydrator.patch(existingField, fieldSnapshot);
|
|
1023
|
+
} else {
|
|
1024
|
+
const newField = this.fieldHydrator.hydrate(fieldSnapshot);
|
|
1025
|
+
fields.add(newField);
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1012
1029
|
};
|
|
1013
1030
|
|
|
1014
|
-
// src/
|
|
1031
|
+
// src/serializers/fields-snapshotter.ts
|
|
1032
|
+
var FieldsSnapshotter = class {
|
|
1033
|
+
/**
|
|
1034
|
+
* Creates an instance of FieldsSnapshotter.
|
|
1035
|
+
* @param {FieldSnapshotter} fieldSnapshotter - A serializer of field instances.
|
|
1036
|
+
*/
|
|
1037
|
+
constructor(fieldSnapshotter) {
|
|
1038
|
+
this.fieldSnapshotter = fieldSnapshotter;
|
|
1039
|
+
}
|
|
1040
|
+
/**
|
|
1041
|
+
* Creates a serializable snapshot of a `Fields` container.
|
|
1042
|
+
*
|
|
1043
|
+
* The snapshot includes a `__type` identifier (currently hardcoded) and an array of snapshots
|
|
1044
|
+
* for each `Field` within the container.
|
|
1045
|
+
* @param {Fields} fields - The `Fields` instance to serialize.
|
|
1046
|
+
* @returns {FieldsSnapshot} A plain object ready for JSON serialization.
|
|
1047
|
+
*/
|
|
1048
|
+
snapshot(fields) {
|
|
1049
|
+
const res = {
|
|
1050
|
+
__type: fields.typeName
|
|
1051
|
+
};
|
|
1052
|
+
fields.fields.forEach((field) => res[field.name] = this.fieldSnapshotter.snapshot(field));
|
|
1053
|
+
return res;
|
|
1054
|
+
}
|
|
1055
|
+
};
|
|
1056
|
+
|
|
1057
|
+
// src/serializers/field-tree-hydrator.ts
|
|
1015
1058
|
var import_utils8 = require("@axi-engine/utils");
|
|
1016
|
-
var
|
|
1059
|
+
var FieldTreeHydrator = class {
|
|
1017
1060
|
_factory;
|
|
1018
|
-
|
|
1061
|
+
_fieldsHydrator;
|
|
1019
1062
|
get factory() {
|
|
1020
1063
|
return this._factory;
|
|
1021
1064
|
}
|
|
1022
|
-
get
|
|
1023
|
-
return this.
|
|
1065
|
+
get fieldsHydrator() {
|
|
1066
|
+
return this._fieldsHydrator;
|
|
1024
1067
|
}
|
|
1025
|
-
constructor(fieldTreeNodeFactory,
|
|
1068
|
+
constructor(fieldTreeNodeFactory, fieldsHydrator) {
|
|
1026
1069
|
this._factory = fieldTreeNodeFactory;
|
|
1027
|
-
this.
|
|
1028
|
-
}
|
|
1029
|
-
/**
|
|
1030
|
-
* Creates a serializable snapshot of the entire tree and its contained fields.
|
|
1031
|
-
* @returns A plain JavaScript object representing the complete state managed by this tree.
|
|
1032
|
-
*/
|
|
1033
|
-
snapshot(tree) {
|
|
1034
|
-
const res = {
|
|
1035
|
-
__type: tree.typeName
|
|
1036
|
-
};
|
|
1037
|
-
tree.nodes.forEach((node, key) => {
|
|
1038
|
-
if (node.typeName === tree.typeName) {
|
|
1039
|
-
res[key] = this.snapshot(node);
|
|
1040
|
-
} else if (node.typeName === Fields.typeName) {
|
|
1041
|
-
res[key] = this.fieldsSerializer.snapshot(node);
|
|
1042
|
-
}
|
|
1043
|
-
});
|
|
1044
|
-
return res;
|
|
1070
|
+
this._fieldsHydrator = fieldsHydrator;
|
|
1045
1071
|
}
|
|
1046
1072
|
/**
|
|
1047
1073
|
* Restores the state of the tree from a snapshot.
|
|
@@ -1056,14 +1082,93 @@ var FieldTreeSerializer = class {
|
|
|
1056
1082
|
if ((0, import_utils8.isString)(nodeData)) {
|
|
1057
1083
|
continue;
|
|
1058
1084
|
}
|
|
1059
|
-
|
|
1060
|
-
tree.addNode(key, this.hydrate(nodeData));
|
|
1061
|
-
} else if (nodeData.__type === Fields.typeName) {
|
|
1062
|
-
tree.addNode(key, this.fieldsSerializer.hydrate(nodeData));
|
|
1063
|
-
}
|
|
1085
|
+
this.addNodeFromSnapshot(tree, key, nodeData);
|
|
1064
1086
|
}
|
|
1065
1087
|
return tree;
|
|
1066
1088
|
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Synchronizes an existing `FieldTree` branch with a snapshot.
|
|
1091
|
+
*
|
|
1092
|
+
* This method performs a recursive update to match the tree state with the provided snapshot:
|
|
1093
|
+
* 1. **Removes** child nodes that are present in the tree but missing in the snapshot.
|
|
1094
|
+
* 2. **Creates** new nodes that are present in the snapshot but missing in the tree.
|
|
1095
|
+
* 3. **Replaces** nodes if their type has changed (e.g., replacing a `Fields` leaf with a `FieldTree` branch).
|
|
1096
|
+
* 4. **Patches** existing matching nodes in-place (recursively).
|
|
1097
|
+
*
|
|
1098
|
+
* @param {FieldTree} tree - The target tree instance to update.
|
|
1099
|
+
* @param {FieldTreeSnapshot} snapshot - The source snapshot containing the desired state.
|
|
1100
|
+
*/
|
|
1101
|
+
patch(tree, snapshot) {
|
|
1102
|
+
const { __type, ...nodes } = snapshot;
|
|
1103
|
+
const snapshotKeys = new Set(Object.keys(nodes));
|
|
1104
|
+
const nodesToRemove = Array.from(tree.nodes.keys()).filter((key) => !snapshotKeys.has(key));
|
|
1105
|
+
tree.removeNode(nodesToRemove);
|
|
1106
|
+
for (const key in nodes) {
|
|
1107
|
+
const nodeData = nodes[key];
|
|
1108
|
+
if ((0, import_utils8.isString)(nodeData)) {
|
|
1109
|
+
continue;
|
|
1110
|
+
}
|
|
1111
|
+
if (!tree.has(key)) {
|
|
1112
|
+
this.addNodeFromSnapshot(tree, key, nodeData);
|
|
1113
|
+
} else {
|
|
1114
|
+
const treeNode = tree.getNode(key);
|
|
1115
|
+
if (treeNode.typeName !== nodeData.__type) {
|
|
1116
|
+
tree.removeNode(key);
|
|
1117
|
+
this.addNodeFromSnapshot(tree, key, nodeData);
|
|
1118
|
+
} else {
|
|
1119
|
+
if (nodeData.__type === FieldTree.typeName) {
|
|
1120
|
+
this.patch(treeNode, nodeData);
|
|
1121
|
+
} else if (nodeData.__type === Fields.typeName) {
|
|
1122
|
+
this.fieldsHydrator.patch(treeNode, nodeData);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
/**
|
|
1129
|
+
* Helper method to instantiate and add a new node to the tree based on the snapshot type.
|
|
1130
|
+
*
|
|
1131
|
+
* It determines whether to create a nested `FieldTree` or a `Fields` container
|
|
1132
|
+
* by inspecting the `__type` property of the snapshot, hydrates it, and attaches it to the parent tree.
|
|
1133
|
+
*
|
|
1134
|
+
* @param {FieldTree} tree - The parent tree instance where the new node will be added.
|
|
1135
|
+
* @param {string} key - The name (key) for the new node.
|
|
1136
|
+
* @param {FieldsSnapshot | FieldTreeSnapshot} snapshot - The source snapshot data.
|
|
1137
|
+
* @throws If the snapshot contains an unsupported or unknown `__type`.
|
|
1138
|
+
*/
|
|
1139
|
+
addNodeFromSnapshot(tree, key, snapshot) {
|
|
1140
|
+
if (snapshot.__type === FieldTree.typeName) {
|
|
1141
|
+
tree.addNode(key, this.hydrate(snapshot));
|
|
1142
|
+
} else if (snapshot.__type === Fields.typeName) {
|
|
1143
|
+
tree.addNode(key, this.fieldsHydrator.hydrate(snapshot));
|
|
1144
|
+
} else {
|
|
1145
|
+
(0, import_utils8.throwError)(`Can't hydrate node with unsupported type: ${snapshot.__type}`);
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
};
|
|
1149
|
+
|
|
1150
|
+
// src/serializers/field-tree-snapshotter.ts
|
|
1151
|
+
var FieldTreeSnapshotter = class {
|
|
1152
|
+
constructor(fieldsSnapshotter) {
|
|
1153
|
+
this.fieldsSnapshotter = fieldsSnapshotter;
|
|
1154
|
+
}
|
|
1155
|
+
/**
|
|
1156
|
+
* Creates a serializable snapshot of the entire tree and its contained fields.
|
|
1157
|
+
* @returns A plain JavaScript object representing the complete state managed by this tree.
|
|
1158
|
+
*/
|
|
1159
|
+
snapshot(tree) {
|
|
1160
|
+
const res = {
|
|
1161
|
+
__type: tree.typeName
|
|
1162
|
+
};
|
|
1163
|
+
tree.nodes.forEach((node, key) => {
|
|
1164
|
+
if (node.typeName === tree.typeName) {
|
|
1165
|
+
res[key] = this.snapshot(node);
|
|
1166
|
+
} else if (node.typeName === Fields.typeName) {
|
|
1167
|
+
res[key] = this.fieldsSnapshotter.snapshot(node);
|
|
1168
|
+
}
|
|
1169
|
+
});
|
|
1170
|
+
return res;
|
|
1171
|
+
}
|
|
1067
1172
|
};
|
|
1068
1173
|
|
|
1069
1174
|
// src/data-store.ts
|
|
@@ -1286,6 +1391,18 @@ var DataStore = class _DataStore {
|
|
|
1286
1391
|
getInternalTree() {
|
|
1287
1392
|
return this._tree;
|
|
1288
1393
|
}
|
|
1394
|
+
/**
|
|
1395
|
+
* @internal Used for serialization
|
|
1396
|
+
*/
|
|
1397
|
+
getOrCreateInternalVariables() {
|
|
1398
|
+
return this.variables;
|
|
1399
|
+
}
|
|
1400
|
+
/**
|
|
1401
|
+
* @internal Used for serialization
|
|
1402
|
+
*/
|
|
1403
|
+
getOrCreateInternalTree() {
|
|
1404
|
+
return this.tree;
|
|
1405
|
+
}
|
|
1289
1406
|
/**
|
|
1290
1407
|
* @private
|
|
1291
1408
|
*/
|
|
@@ -1305,15 +1422,66 @@ var DataStore = class _DataStore {
|
|
|
1305
1422
|
}
|
|
1306
1423
|
};
|
|
1307
1424
|
|
|
1308
|
-
// src/
|
|
1425
|
+
// src/serializers/data-store-hydrator.ts
|
|
1309
1426
|
var import_utils12 = require("@axi-engine/utils");
|
|
1310
|
-
var
|
|
1427
|
+
var DataStoreHydrator = class {
|
|
1311
1428
|
/**
|
|
1312
1429
|
* Creates an instance of DataStoreSerializer.
|
|
1313
|
-
* @param {
|
|
1430
|
+
* @param {FieldTreeHydrator} fieldsFieldTreeHydrator - The serializer used for the underlying tree and fields.
|
|
1431
|
+
*/
|
|
1432
|
+
constructor(fieldsFieldTreeHydrator) {
|
|
1433
|
+
this.fieldsFieldTreeHydrator = fieldsFieldTreeHydrator;
|
|
1434
|
+
}
|
|
1435
|
+
/**
|
|
1436
|
+
* Reconstructs a DataStore instance from a snapshot.
|
|
1437
|
+
*
|
|
1438
|
+
* If the snapshot contains a tree, the store is initialized with it.
|
|
1439
|
+
* If not, the store is initialized with the factory (lazy mode), and the
|
|
1440
|
+
* detached variables are injected if present.
|
|
1441
|
+
*
|
|
1442
|
+
* @param {DataStoreSnapshot} snapshot - The snapshot to hydrate.
|
|
1443
|
+
* @returns {DataStore} A new, fully restored DataStore instance.
|
|
1314
1444
|
*/
|
|
1315
|
-
|
|
1316
|
-
|
|
1445
|
+
hydrate(snapshot) {
|
|
1446
|
+
const tree = (0, import_utils12.isNullOrUndefined)(snapshot.tree) ? void 0 : this.fieldsFieldTreeHydrator.hydrate(snapshot.tree);
|
|
1447
|
+
const variables = (0, import_utils12.isNullOrUndefined)(snapshot.variables) ? void 0 : this.fieldsFieldTreeHydrator.fieldsHydrator.hydrate(snapshot.variables);
|
|
1448
|
+
return new DataStore(tree ? tree : this.fieldsFieldTreeHydrator.factory, variables);
|
|
1449
|
+
}
|
|
1450
|
+
/**
|
|
1451
|
+
* Synchronizes a DataStore instance with a snapshot.
|
|
1452
|
+
*
|
|
1453
|
+
* This method ensures the DataStore's internal state matches the snapshot by:
|
|
1454
|
+
* 1. **Destroying** internal containers (variables/tree) if they are missing in the snapshot.
|
|
1455
|
+
* 2. **Patching** (updating/creating) contents if they exist in the snapshot.
|
|
1456
|
+
*
|
|
1457
|
+
* This allows for a granular update where only specific parts of the store (e.g., only variables)
|
|
1458
|
+
* are modified if the snapshot contains partial data, or a full reset if parts are missing.
|
|
1459
|
+
*
|
|
1460
|
+
* @param {DataStore} store - The target DataStore to update.
|
|
1461
|
+
* @param {DataStoreSnapshot} snapshot - The source snapshot.
|
|
1462
|
+
*/
|
|
1463
|
+
patch(store, snapshot) {
|
|
1464
|
+
if (!snapshot.variables) {
|
|
1465
|
+
store.getInternalVariables()?.destroy();
|
|
1466
|
+
} else {
|
|
1467
|
+
this.fieldsFieldTreeHydrator.fieldsHydrator.patch(store.getOrCreateInternalVariables(), snapshot.variables);
|
|
1468
|
+
}
|
|
1469
|
+
if (!snapshot.tree) {
|
|
1470
|
+
store.getInternalTree()?.destroy();
|
|
1471
|
+
} else {
|
|
1472
|
+
this.fieldsFieldTreeHydrator.patch(store.getOrCreateInternalTree(), snapshot.tree);
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
};
|
|
1476
|
+
|
|
1477
|
+
// src/serializers/data-store-snapshotter.ts
|
|
1478
|
+
var DataStoreSnapshotter = class {
|
|
1479
|
+
/**
|
|
1480
|
+
* Creates an instance of DataStoreSnapshotter.
|
|
1481
|
+
* @param {FieldTreeSnapshotter} treeSnapshotter - The serializer used for the underlying tree and fields.
|
|
1482
|
+
*/
|
|
1483
|
+
constructor(treeSnapshotter) {
|
|
1484
|
+
this.treeSnapshotter = treeSnapshotter;
|
|
1317
1485
|
}
|
|
1318
1486
|
/**
|
|
1319
1487
|
* Captures the current state of a DataStore into a serializable snapshot.
|
|
@@ -1330,29 +1498,14 @@ var DataStoreSerializer = class {
|
|
|
1330
1498
|
};
|
|
1331
1499
|
const variables = store.getInternalVariables();
|
|
1332
1500
|
if (variables) {
|
|
1333
|
-
snapshot.variables = this.
|
|
1501
|
+
snapshot.variables = this.treeSnapshotter.fieldsSnapshotter.snapshot(variables);
|
|
1334
1502
|
}
|
|
1335
1503
|
const tree = store.getInternalTree();
|
|
1336
1504
|
if (tree) {
|
|
1337
|
-
snapshot.tree = this.
|
|
1505
|
+
snapshot.tree = this.treeSnapshotter.snapshot(tree);
|
|
1338
1506
|
}
|
|
1339
1507
|
return snapshot;
|
|
1340
1508
|
}
|
|
1341
|
-
/**
|
|
1342
|
-
* Reconstructs a DataStore instance from a snapshot.
|
|
1343
|
-
*
|
|
1344
|
-
* If the snapshot contains a tree, the store is initialized with it.
|
|
1345
|
-
* If not, the store is initialized with the factory (lazy mode), and the
|
|
1346
|
-
* detached variables are injected if present.
|
|
1347
|
-
*
|
|
1348
|
-
* @param {DataStoreSnapshot} snapshot - The snapshot to hydrate.
|
|
1349
|
-
* @returns {DataStore} A new, fully restored DataStore instance.
|
|
1350
|
-
*/
|
|
1351
|
-
hydrate(snapshot) {
|
|
1352
|
-
const tree = (0, import_utils12.isNullOrUndefined)(snapshot.tree) ? void 0 : this.fieldTreeSerializer.hydrate(snapshot.tree);
|
|
1353
|
-
const variables = (0, import_utils12.isNullOrUndefined)(snapshot.variables) ? void 0 : this.fieldTreeSerializer.fieldsSerializer.hydrate(snapshot.variables);
|
|
1354
|
-
return new DataStore(tree ? tree : this.fieldTreeSerializer.factory, variables);
|
|
1355
|
-
}
|
|
1356
1509
|
};
|
|
1357
1510
|
|
|
1358
1511
|
// src/setup.ts
|
|
@@ -1375,11 +1528,11 @@ function createCoreTreeNodeFactory(fieldRegistry) {
|
|
|
1375
1528
|
return new CoreTreeNodeFactory(fieldRegistry);
|
|
1376
1529
|
}
|
|
1377
1530
|
function createCoreTreeSerializer(fieldTreeNodeFactory, policySerializer) {
|
|
1378
|
-
return new
|
|
1531
|
+
return new FieldTreeHydrator(
|
|
1379
1532
|
fieldTreeNodeFactory,
|
|
1380
|
-
new
|
|
1533
|
+
new FieldsHydrator(
|
|
1381
1534
|
fieldTreeNodeFactory,
|
|
1382
|
-
new
|
|
1535
|
+
new FieldHydrator(fieldTreeNodeFactory.fieldRegistry, policySerializer ?? createCorePolicySerializer())
|
|
1383
1536
|
)
|
|
1384
1537
|
);
|
|
1385
1538
|
}
|
|
@@ -1406,13 +1559,17 @@ function createCoreFieldSystem(config) {
|
|
|
1406
1559
|
CoreStringField,
|
|
1407
1560
|
CoreTreeNodeFactory,
|
|
1408
1561
|
DataStore,
|
|
1409
|
-
|
|
1562
|
+
DataStoreHydrator,
|
|
1563
|
+
DataStoreSnapshotter,
|
|
1564
|
+
FieldHydrator,
|
|
1410
1565
|
FieldRegistry,
|
|
1411
|
-
|
|
1566
|
+
FieldSnapshotter,
|
|
1412
1567
|
FieldTree,
|
|
1413
|
-
|
|
1568
|
+
FieldTreeHydrator,
|
|
1569
|
+
FieldTreeSnapshotter,
|
|
1414
1570
|
Fields,
|
|
1415
|
-
|
|
1571
|
+
FieldsHydrator,
|
|
1572
|
+
FieldsSnapshotter,
|
|
1416
1573
|
Policies,
|
|
1417
1574
|
PolicySerializer,
|
|
1418
1575
|
clampMaxPolicy,
|