@axi-engine/fields 0.3.8 → 0.3.10

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.ts CHANGED
@@ -75,35 +75,137 @@ declare class Policies<T> {
75
75
  apply(val: T): T;
76
76
  }
77
77
 
78
+ /**
79
+ * Configuration options for creating a new Field instance.
80
+ * @template T The type of the value stored in the field.
81
+ */
78
82
  interface FieldOptions<T> {
83
+ /**
84
+ * An optional array of policies to apply to this field.
85
+ * Policies can enforce validation rules, transform values, or handle constraints.
86
+ */
79
87
  policies?: Policy<T>[];
80
88
  }
89
+ /**
90
+ * Represents a reactive data holder for a specific value type.
91
+ *
92
+ * A Field wraps a raw value, providing features like change observation (`onChange`),
93
+ * policy enforcement (validation/transformation), and metadata management (`name`, `typeName`).
94
+ *
95
+ * @template T The type of the value stored in the field.
96
+ */
81
97
  interface Field<T> {
98
+ /**
99
+ * A unique string identifier for the field type (e.g., 'numeric', 'boolean').
100
+ * Used for serialization and type guards.
101
+ */
82
102
  readonly typeName: string;
103
+ /**
104
+ * The name or key of this field within its parent container.
105
+ */
83
106
  readonly name: string;
107
+ /**
108
+ * The current value of the field.
109
+ * Assigning a new value triggers policies and emits the `onChange` event
110
+ * if the value is different from the current one.
111
+ */
84
112
  value: T;
113
+ /**
114
+ * The collection of policies applied to this field.
115
+ */
85
116
  policies: Policies<T>;
117
+ /**
118
+ * Updates the field's value without triggering the `onChange` event.
119
+ * Useful for internal synchronization or restoring state where side effects are undesirable.
120
+ * @param val The new value to set.
121
+ */
86
122
  setValueSilently(val: T): void;
123
+ /**
124
+ * Performs an atomic-like update using a callback function.
125
+ * The callback receives the current value and should return the new value.
126
+ * @param updateFn A function that transforms the current value into a new one.
127
+ */
87
128
  batchUpdate(updateFn: (currentValue: T) => T): void;
129
+ /**
130
+ * An observable stream that emits an event whenever the value changes.
131
+ * The payload contains the new value and the old value.
132
+ */
88
133
  onChange: Subscribable<[newValue: T, oldValue: T]>;
134
+ /**
135
+ * Cleans up the field, removing all listeners and releasing resources.
136
+ * Should be called when the field is no longer needed.
137
+ */
89
138
  destroy(): void;
90
139
  }
140
+ /**
141
+ * A specialized Field for handling numeric values.
142
+ * Provides capabilities for range clamping (min/max) and arithmetic operations.
143
+ */
91
144
  interface NumericField extends Field<number> {
145
+ /** The minimum allowed value for this field, or undefined if no lower bound exists. */
92
146
  readonly min: number | undefined;
147
+ /** The maximum allowed value for this field, or undefined if no upper bound exists. */
93
148
  readonly max: number | undefined;
149
+ /**
150
+ * Checks if the current value is equal to or less than the minimum limit.
151
+ */
94
152
  isMin(): boolean;
153
+ /**
154
+ * Checks if the current value is equal to or greater than the maximum limit.
155
+ */
95
156
  isMax(): boolean;
157
+ /**
158
+ * Increments the current value by the specified amount.
159
+ * @param val The amount to add.
160
+ */
96
161
  inc(val: number): void;
162
+ /**
163
+ * Decrements the current value by the specified amount.
164
+ * @param val The amount to subtract.
165
+ */
97
166
  dec(val: number): void;
98
167
  }
168
+ /**
169
+ * A specialized Field for handling boolean values.
170
+ * Provides toggle functionality.
171
+ */
99
172
  interface BooleanField extends Field<boolean> {
173
+ /**
174
+ * Inverts the current boolean value (true -> false, false -> true).
175
+ * @returns {boolean} The new value after toggling.
176
+ */
100
177
  toggle(): boolean;
101
178
  }
179
+ /**
180
+ * A specialized Field for handling string values.
181
+ * Provides chainable methods for common string manipulations.
182
+ */
102
183
  interface StringField extends Field<string> {
184
+ /**
185
+ * Appends a string or number to the end of the current value.
186
+ * @param str The value to append.
187
+ * @returns {this} The field instance for chaining.
188
+ */
103
189
  append(str: string | number): this;
190
+ /**
191
+ * Prepends a string or number to the beginning of the current value.
192
+ * @param str The value to prepend.
193
+ * @returns {this} The field instance for chaining.
194
+ */
104
195
  prepend(str: string | number): this;
196
+ /**
197
+ * Removes whitespace from both ends of the current string value.
198
+ * @returns {this} The field instance for chaining.
199
+ */
105
200
  trim(): this;
201
+ /**
202
+ * Checks if the current string is empty (length is 0).
203
+ * @returns {boolean} `true` if the string is empty, otherwise `false`.
204
+ */
106
205
  isEmpty(): boolean;
206
+ /**
207
+ * Sets the value to an empty string.
208
+ */
107
209
  clear(): void;
108
210
  }
109
211
 
@@ -144,6 +246,9 @@ declare class Fields {
144
246
  }]>;
145
247
  /**
146
248
  * Gets the read-only map of all `Field` instances in this container.
249
+ *
250
+ * @internal
251
+ *
147
252
  * @returns {Map<string, Field<any>>} The collection of fields.
148
253
  */
149
254
  get fields(): Map<string, Field<any>>;
@@ -379,9 +484,24 @@ declare class FieldTree<TFields extends Fields> {
379
484
  names: string[];
380
485
  }]>;
381
486
  /**
382
- * Gets the collection of direct child nodes of this tree branch.
487
+ * Provides direct access to the internal node storage.
488
+ *
489
+ * @remarks
490
+ * This is primarily intended for **serialization**, debugging, or low-level iteration.
491
+ * Avoid modifying this map directly to maintain internal consistency; use {@link addNode} or {@link removeNode} instead.
492
+ * @internal
383
493
  */
384
494
  get nodes(): Map<string, TreeNode<TFields>>;
495
+ /**
496
+ * Exposes the internal factory instance used by this tree.
497
+ *
498
+ * @remarks
499
+ * Direct usage of this getter is generally unnecessary.
500
+ * Prefer using {@link createDetachedTree} or {@link createDetachedFields} to create isolated instances.
501
+ *
502
+ * @returns {FieldTreeFactory} The factory instance.
503
+ */
504
+ get factory(): FieldTreeFactory<TFields>;
385
505
  /**
386
506
  * Creates an instance of FieldTree.
387
507
  * @param {FieldTreeFactory} factory - A factory responsible for creating new nodes within the tree.
@@ -488,6 +608,19 @@ declare class FieldTree<TFields extends Fields> {
488
608
  * This method should be called when a node is no longer needed.
489
609
  */
490
610
  destroy(): void;
611
+ /**
612
+ * Creates a new, detached FieldTree instance using the same factory as this tree.
613
+ * This new tree has no parent and is completely isolated.
614
+ *
615
+ * @returns A new instance of the same tree type.
616
+ */
617
+ createDetachedTree(): FieldTree<TFields>;
618
+ /**
619
+ * Creates a new, detached Fields container using the same factory.
620
+ *
621
+ * @returns
622
+ */
623
+ createDetachedFields(): TFields;
491
624
  /**
492
625
  * @private
493
626
  * Navigates the tree to the parent of a target node.
@@ -655,11 +788,6 @@ interface FieldSnapshot {
655
788
  * plain, storable data (snapshots) and vice-versa. It uses a `FieldRegistry`
656
789
  * to resolve class constructors and a `PolicySerializer` to handle the state
657
790
  * of any attached policies.
658
- *
659
- * @todo Implement a `patch(field, snapshot)` method.
660
- * Unlike `hydrate`, which creates a new
661
- * instance, `patch` should update the state of an *existing* field instance
662
- * without breaking external references to it.
663
791
  */
664
792
  declare class FieldSerializer {
665
793
  private readonly fieldRegistry;
@@ -686,6 +814,18 @@ declare class FieldSerializer {
686
814
  * @throws If the snapshot is invalid, missing a `__type`, or if the type is not registered.
687
815
  */
688
816
  hydrate(snapshot: FieldSnapshot): Field<any>;
817
+ /**
818
+ * Updates an existing Field instance with data from a snapshot.
819
+ *
820
+ * This method modifies the field in-place, preserving the object reference.
821
+ * It updates the field's value and completely replaces its current policies
822
+ * with the ones defined in the snapshot.
823
+ *
824
+ * @param {Field<any>} field - The existing Field instance to update.
825
+ * @param {FieldSnapshot} snapshot - The snapshot containing the new state.
826
+ */
827
+ patch(field: Field<any>, snapshot: FieldSnapshot): void;
828
+ private hydratePolicies;
689
829
  }
690
830
 
691
831
  /**
@@ -766,8 +906,10 @@ interface FieldTreeSnapshot {
766
906
  * patching nodes in place to maintain object references.
767
907
  */
768
908
  declare class FieldTreeSerializer<TFields extends Fields> {
769
- private readonly fieldTreeNodeFactory;
770
- private readonly fieldsSerializer;
909
+ _factory: FieldTreeFactory<TFields>;
910
+ _fieldsSerializer: FieldsSerializer<TFields>;
911
+ get factory(): FieldTreeFactory<TFields>;
912
+ get fieldsSerializer(): FieldsSerializer<TFields>;
771
913
  constructor(fieldTreeNodeFactory: FieldTreeFactory<TFields>, fieldsSerializer: FieldsSerializer<TFields>);
772
914
  /**
773
915
  * Creates a serializable snapshot of the entire tree and its contained fields.
@@ -782,6 +924,30 @@ declare class FieldTreeSerializer<TFields extends Fields> {
782
924
  hydrate(snapshot: FieldTreeSnapshot): FieldTree<TFields>;
783
925
  }
784
926
 
927
+ /**
928
+ * A plain object representation of a DataStore's state, used for serialization.
929
+ *
930
+ * It captures both the detached 'flat' variables (used for stack frames/local scopes)
931
+ * and the hierarchical 'tree' structure (used for global/persistent data).
932
+ */
933
+ interface DataStoreSnapshot {
934
+ /**
935
+ * The type identifier for the store (e.g., 'dataStore').
936
+ * Used for type guards and polymorphic deserialization.
937
+ */
938
+ __type: string;
939
+ /**
940
+ * Snapshot of the independent, root-level variables (CoreFields).
941
+ * Present only if the store contained detached variables.
942
+ */
943
+ variables?: FieldsSnapshot;
944
+ /**
945
+ * Snapshot of the nested data hierarchy (CoreFieldTree).
946
+ * Present only if the store managed a complex tree structure.
947
+ */
948
+ tree?: FieldTreeSnapshot;
949
+ }
950
+
785
951
  interface StoreCreateFieldOptions {
786
952
  /** Allows to explicitly specify the field type, overriding the automatic type detection. */
787
953
  fieldType?: string;
@@ -792,6 +958,9 @@ interface StoreCreateFieldOptions {
792
958
  * both type-safe and dynamic methods for manipulating data.
793
959
  */
794
960
  interface Store extends DataStorage {
961
+ /**
962
+ */
963
+ readonly typeName: string;
795
964
  /**
796
965
  * Retrieves the raw value of a Field at a specific path.
797
966
  * @template T The expected type of the value.
@@ -912,6 +1081,16 @@ interface Store extends DataStorage {
912
1081
  * @param path The path to the node to remove.
913
1082
  */
914
1083
  remove(path: PathType): void;
1084
+ /**
1085
+ * Creates a new, independent instance of the Store.
1086
+ *
1087
+ * The returned store must operate on a completely separate data structure,
1088
+ * ensuring that operations on the new instance do not affect the current one (and vice versa).
1089
+ * Typically used for creating local execution scopes, stack frames, or sandbox environments.
1090
+ *
1091
+ * @returns {Store} A new, isolated Store instance.
1092
+ */
1093
+ createIsolated(): Store;
915
1094
  }
916
1095
 
917
1096
  interface DataStoreFieldResolver {
@@ -929,12 +1108,15 @@ interface DataStoreFieldResolver {
929
1108
  }
930
1109
 
931
1110
  declare class DataStore implements Store {
932
- private readonly tree;
1111
+ static readonly typeName = "dataStore";
1112
+ readonly typeName = "dataStore";
933
1113
  private readonly resolvers;
934
- private readonly rootFieldsName;
935
- private _rootFields;
936
- private get rootFields();
937
- constructor(tree: CoreFieldTree);
1114
+ private _variables;
1115
+ private _tree;
1116
+ private readonly _factory;
1117
+ private get variables();
1118
+ private get tree();
1119
+ constructor(treeOrFactory: CoreFieldTree | FieldTreeFactory<CoreFields>, variables?: CoreFields);
938
1120
  registerResolver(resolver: DataStoreFieldResolver): void;
939
1121
  clearResolvers(): void;
940
1122
  getValue<T>(path: PathType): T;
@@ -955,15 +1137,76 @@ declare class DataStore implements Store {
955
1137
  getFields(path: PathType): CoreFields;
956
1138
  getTree(path: PathType): CoreFieldTree;
957
1139
  remove(path: PathType): void;
958
- private isPathToRootFields;
959
- private getDestinationFields;
1140
+ /**
1141
+ * Creates a new, independent instance of the Store with a fresh, empty data state (FieldsTree).
1142
+ *
1143
+ * The new instance retains the same capabilities (e.g., factory configuration)
1144
+ * as the current one but is completely detached from the existing data hierarchy.
1145
+ * This is useful for creating local scopes, stack frames, or temporary data contexts.
1146
+ *
1147
+ * @returns {DataStore} A new, isolated DataStore instance.
1148
+ */
1149
+ createIsolated(): DataStore;
1150
+ /** code below -> implementation of the DataStore from utils */
960
1151
  has(path: PathType): boolean;
961
- /** implementation of the DataStore from utils */
962
1152
  get(path: PathType): unknown;
963
1153
  set(path: PathType, value: unknown): void;
964
1154
  create(path: PathType, value: unknown): void;
965
1155
  upset(path: PathType, value: unknown): void;
966
1156
  delete(path: PathType): void;
1157
+ /**
1158
+ * @internal Used for serialization
1159
+ */
1160
+ getInternalVariables(): CoreFields | undefined;
1161
+ /**
1162
+ * @internal Used for serialization
1163
+ */
1164
+ getInternalTree(): CoreFieldTree | undefined;
1165
+ /**
1166
+ * @private
1167
+ */
1168
+ private isPathToVariables;
1169
+ /**
1170
+ * @private
1171
+ */
1172
+ private getDestinationFields;
1173
+ }
1174
+
1175
+ /**
1176
+ * Handles the serialization and deserialization of DataStore instances.
1177
+ *
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
+ */
1182
+ declare class DataStoreSerializer {
1183
+ private readonly fieldTreeSerializer;
1184
+ /**
1185
+ * Creates an instance of DataStoreSerializer.
1186
+ * @param {FieldTreeSerializer} fieldTreeSerializer - The serializer used for the underlying tree and fields.
1187
+ */
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;
1199
+ /**
1200
+ * Reconstructs a DataStore instance from a snapshot.
1201
+ *
1202
+ * If the snapshot contains a tree, the store is initialized with it.
1203
+ * If not, the store is initialized with the factory (lazy mode), and the
1204
+ * detached variables are injected if present.
1205
+ *
1206
+ * @param {DataStoreSnapshot} snapshot - The snapshot to hydrate.
1207
+ * @returns {DataStore} A new, fully restored DataStore instance.
1208
+ */
1209
+ hydrate(snapshot: DataStoreSnapshot): DataStore;
967
1210
  }
968
1211
 
969
1212
  /**
@@ -983,18 +1226,15 @@ declare function isFields(value: unknown): value is Fields;
983
1226
  */
984
1227
  declare function isFieldTree(value: unknown): value is FieldTree<any>;
985
1228
  /**
986
- * Type guard that checks if an unknown value conforms to the `Store` interface.
987
- *
988
- * It performs a structural check (duck typing) by verifying the presence of methods
989
- * that are unique to the `Store` interface and are not part of the simpler `DataSource`
990
- * or `DataStorage` contracts, such as `createFields` and `createTree`.
1229
+ * Type guard that checks if a value is an instance of the `DataStore` class.
1230
+ * It verifies this by checking the static `typeName` property on the instance.
991
1231
  *
992
1232
  * @param value The `unknown` value to check.
993
- * @returns {boolean} `true` if the value is a `Store`-like object, `false` otherwise.
1233
+ * @returns {boolean} `true` if the value is a `DataStore` instance, otherwise `false`.
994
1234
  *
995
1235
  * @example
996
- * function processData(source: DataSource) {
997
- * if (isStore(source)) {
1236
+ * function processData(source: DataStore) {
1237
+ * if (isDataStore(source)) {
998
1238
  * // Inside this block, TypeScript now knows `source` is a full `Store`.
999
1239
  * // We can safely call Store-specific methods like `createFields`.
1000
1240
  * source.createFields('new.data.group');
@@ -1004,7 +1244,7 @@ declare function isFieldTree(value: unknown): value is FieldTree<any>;
1004
1244
  * }
1005
1245
  * }
1006
1246
  */
1007
- declare function isStore(value: unknown): value is Store;
1247
+ declare function isDataStore(value: unknown): value is DataStore;
1008
1248
 
1009
1249
  /**
1010
1250
  * Creates and configures a FieldRegistry with all the core field types.
@@ -1043,4 +1283,4 @@ declare function createCoreFieldSystem(config?: CoreFieldSystemConfig): {
1043
1283
  serializer: FieldTreeSerializer<CoreFields>;
1044
1284
  };
1045
1285
 
1046
- 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, 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, isFieldTree, isFields, isStore };
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 };