@axi-engine/fields 0.1.5 → 0.2.1
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/README.md +13 -7
- package/dist/index.d.mts +699 -214
- package/dist/index.d.ts +699 -214
- package/dist/index.js +782 -381
- package/dist/index.mjs +762 -372
- package/package.json +3 -6
package/dist/index.mjs
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
var
|
|
3
|
-
FieldsNodeType2["fieldTree"] = "FieldTree";
|
|
4
|
-
FieldsNodeType2["fields"] = "Fields";
|
|
5
|
-
return FieldsNodeType2;
|
|
6
|
-
})(FieldsNodeType || {});
|
|
7
|
-
|
|
8
|
-
// src/field-policies.ts
|
|
9
|
-
var _ClampPolicy = class _ClampPolicy {
|
|
1
|
+
// src/policies/clamp-policy.ts
|
|
2
|
+
var ClampPolicy = class _ClampPolicy {
|
|
10
3
|
constructor(min, max) {
|
|
11
4
|
this.min = min;
|
|
12
5
|
this.max = max;
|
|
13
|
-
this.id = _ClampPolicy.id;
|
|
14
6
|
}
|
|
7
|
+
static id = "clamp";
|
|
8
|
+
id = _ClampPolicy.id;
|
|
15
9
|
apply(val) {
|
|
16
10
|
return Math.max(this.min, Math.min(this.max, val));
|
|
17
11
|
}
|
|
@@ -20,27 +14,17 @@ var _ClampPolicy = class _ClampPolicy {
|
|
|
20
14
|
this.max = max;
|
|
21
15
|
}
|
|
22
16
|
};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
apply(val) {
|
|
31
|
-
return Math.max(this.min, val);
|
|
32
|
-
}
|
|
33
|
-
updateBounds(min) {
|
|
34
|
-
this.min = min;
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
_ClampMinPolicy.id = "clampMin";
|
|
38
|
-
var ClampMinPolicy = _ClampMinPolicy;
|
|
39
|
-
var _ClampMaxPolicy = class _ClampMaxPolicy {
|
|
17
|
+
function clampPolicy(min, max) {
|
|
18
|
+
return new ClampPolicy(min, max);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/policies/clamp-max-policy.ts
|
|
22
|
+
var ClampMaxPolicy = class _ClampMaxPolicy {
|
|
40
23
|
constructor(max) {
|
|
41
24
|
this.max = max;
|
|
42
|
-
this.id = _ClampMaxPolicy.id;
|
|
43
25
|
}
|
|
26
|
+
static id = "clampMax";
|
|
27
|
+
id = _ClampMaxPolicy.id;
|
|
44
28
|
apply(val) {
|
|
45
29
|
return Math.min(this.max, val);
|
|
46
30
|
}
|
|
@@ -48,58 +32,33 @@ var _ClampMaxPolicy = class _ClampMaxPolicy {
|
|
|
48
32
|
this.max = max;
|
|
49
33
|
}
|
|
50
34
|
};
|
|
51
|
-
_ClampMaxPolicy.id = "clampMax";
|
|
52
|
-
var ClampMaxPolicy = _ClampMaxPolicy;
|
|
53
|
-
function clampPolicy(min, max) {
|
|
54
|
-
return new ClampPolicy(min, max);
|
|
55
|
-
}
|
|
56
|
-
function clampMinPolicy(min) {
|
|
57
|
-
return new ClampMinPolicy(min);
|
|
58
|
-
}
|
|
59
35
|
function clampMaxPolicy(max) {
|
|
60
36
|
return new ClampMaxPolicy(max);
|
|
61
37
|
}
|
|
62
38
|
|
|
63
|
-
// src/
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
* Creates an instance of a Field.
|
|
68
|
-
* @param name A unique identifier for the field.
|
|
69
|
-
* @param initialVal The initial value of the field.
|
|
70
|
-
* @param options Optional configuration for the field.
|
|
71
|
-
* @param options.policies An array of policies to apply to the field's value on every `set` operation.
|
|
72
|
-
*/
|
|
73
|
-
constructor(name, initialVal, options) {
|
|
74
|
-
this.policies = /* @__PURE__ */ new Map();
|
|
75
|
-
this._val = signal(initialVal);
|
|
76
|
-
this.name = name;
|
|
77
|
-
options?.policies?.forEach((policy) => this.policies.set(policy.id, policy));
|
|
78
|
-
this.set(initialVal);
|
|
39
|
+
// src/policies/clamp-min-policy.ts
|
|
40
|
+
var ClampMinPolicy = class _ClampMinPolicy {
|
|
41
|
+
constructor(min) {
|
|
42
|
+
this.min = min;
|
|
79
43
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
get val() {
|
|
85
|
-
return this._val.value;
|
|
44
|
+
static id = "clampMin";
|
|
45
|
+
id = _ClampMinPolicy.id;
|
|
46
|
+
apply(val) {
|
|
47
|
+
return Math.max(this.min, val);
|
|
86
48
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
* Subscribe to this signal to react to value changes.
|
|
90
|
-
*/
|
|
91
|
-
get signal() {
|
|
92
|
-
return this._val;
|
|
49
|
+
updateBounds(min) {
|
|
50
|
+
this.min = min;
|
|
93
51
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
52
|
+
};
|
|
53
|
+
function clampMinPolicy(min) {
|
|
54
|
+
return new ClampMinPolicy(min);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/policies/policies.ts
|
|
58
|
+
var Policies = class {
|
|
59
|
+
policies = /* @__PURE__ */ new Map();
|
|
60
|
+
get items() {
|
|
61
|
+
return this.policies;
|
|
103
62
|
}
|
|
104
63
|
/**
|
|
105
64
|
* Retrieves a specific policy instance by its ID.
|
|
@@ -108,7 +67,7 @@ var Field = class {
|
|
|
108
67
|
* @param id The unique ID of the policy to retrieve.
|
|
109
68
|
* @returns The policy instance, or `undefined` if not found.
|
|
110
69
|
*/
|
|
111
|
-
|
|
70
|
+
get(id) {
|
|
112
71
|
return this.policies.get(id);
|
|
113
72
|
}
|
|
114
73
|
/**
|
|
@@ -117,17 +76,18 @@ var Field = class {
|
|
|
117
76
|
* If a policy with the same ID already exists, its `destroy` method will be called before it is replaced.
|
|
118
77
|
* @param policy The policy instance to add.
|
|
119
78
|
*/
|
|
120
|
-
|
|
79
|
+
add(policy) {
|
|
121
80
|
const existed = this.policies.get(policy.id);
|
|
122
81
|
existed?.destroy?.();
|
|
123
82
|
this.policies.set(policy.id, policy);
|
|
83
|
+
return this;
|
|
124
84
|
}
|
|
125
85
|
/**
|
|
126
86
|
* Removes a policy from the field by its ID and call `destroy` method.
|
|
127
87
|
* @param policyId The unique ID of the policy to remove.
|
|
128
88
|
* @returns `true` if the policy was found and removed, otherwise `false`.
|
|
129
89
|
*/
|
|
130
|
-
|
|
90
|
+
remove(policyId) {
|
|
131
91
|
const policyToRemove = this.policies.get(policyId);
|
|
132
92
|
if (!policyToRemove) {
|
|
133
93
|
return false;
|
|
@@ -135,11 +95,14 @@ var Field = class {
|
|
|
135
95
|
policyToRemove.destroy?.();
|
|
136
96
|
return this.policies.delete(policyId);
|
|
137
97
|
}
|
|
98
|
+
isEmpty() {
|
|
99
|
+
return this.policies.size === 0;
|
|
100
|
+
}
|
|
138
101
|
/**
|
|
139
102
|
* Removes all policies from the field.
|
|
140
103
|
* After this, `set()` will no longer apply any transformations to the value until new policies are added.
|
|
141
104
|
*/
|
|
142
|
-
|
|
105
|
+
clear() {
|
|
143
106
|
this.policies.forEach((policy) => policy.destroy?.());
|
|
144
107
|
this.policies.clear();
|
|
145
108
|
}
|
|
@@ -147,37 +110,133 @@ var Field = class {
|
|
|
147
110
|
* Forces the current value to be re-processed by all policies.
|
|
148
111
|
* Useful if a policy's logic has changed and you need to re-evaluate the current state.
|
|
149
112
|
*/
|
|
150
|
-
|
|
151
|
-
|
|
113
|
+
apply(val) {
|
|
114
|
+
let finalVal = val;
|
|
115
|
+
this.policies.forEach((policy) => finalVal = policy.apply(finalVal));
|
|
116
|
+
return finalVal;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// src/field-definitions/default-field.ts
|
|
121
|
+
import { Emitter } from "@axi-engine/utils";
|
|
122
|
+
import { dequal } from "dequal";
|
|
123
|
+
var DefaultField = class _DefaultField {
|
|
124
|
+
/** A type keyword of the field */
|
|
125
|
+
static typeName = "default";
|
|
126
|
+
typeName = _DefaultField.typeName;
|
|
127
|
+
/** A unique identifier for the field. */
|
|
128
|
+
_name;
|
|
129
|
+
_value;
|
|
130
|
+
_onChange = new Emitter();
|
|
131
|
+
onChange;
|
|
132
|
+
policies = new Policies();
|
|
133
|
+
get name() {
|
|
134
|
+
return this._name;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Gets the current raw value of the field.
|
|
138
|
+
* For reactive updates, it's recommended to use the `.signal` property instead.
|
|
139
|
+
*/
|
|
140
|
+
get value() {
|
|
141
|
+
return this._value;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Sets a new value for the field.
|
|
145
|
+
* The provided value will be processed by all registered policies before the underlying signal is updated.
|
|
146
|
+
* @param val The new value to set.
|
|
147
|
+
*/
|
|
148
|
+
set value(val) {
|
|
149
|
+
const oldVal = this._value;
|
|
150
|
+
const finalVal = this.policies.apply(val);
|
|
151
|
+
if (!dequal(this._value, finalVal)) {
|
|
152
|
+
this._value = finalVal;
|
|
153
|
+
this._onChange.emit(this._value, oldVal);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Creates an instance of a Field.
|
|
158
|
+
* @param name A unique identifier for the field.
|
|
159
|
+
* @param initialVal The initial value of the field.
|
|
160
|
+
* @param options Optional configuration for the field.
|
|
161
|
+
* @param options.policies An array of policies to apply to the field's value on every `set` operation.
|
|
162
|
+
* @param options.isEqual An function for compare old and new value, by default uses the strictEquals from `utils`
|
|
163
|
+
*
|
|
164
|
+
*/
|
|
165
|
+
constructor(name, initialVal, options) {
|
|
166
|
+
this.onChange = this._onChange;
|
|
167
|
+
this._name = name;
|
|
168
|
+
options?.policies?.forEach((policy) => this.policies.add(policy));
|
|
169
|
+
this.value = initialVal;
|
|
170
|
+
}
|
|
171
|
+
setValueSilently(val) {
|
|
172
|
+
this._value = this.policies.apply(val);
|
|
173
|
+
}
|
|
174
|
+
batchUpdate(updateFn) {
|
|
175
|
+
this.value = updateFn(this.value);
|
|
152
176
|
}
|
|
153
177
|
/**
|
|
154
178
|
* Cleans up resources used by the field and its policies.
|
|
155
179
|
* This should be called when the field is no longer needed to prevent memory leaks from reactive policies.
|
|
156
180
|
*/
|
|
157
181
|
destroy() {
|
|
158
|
-
this.
|
|
182
|
+
this.policies.clear();
|
|
183
|
+
this._onChange.clear();
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// src/field-definitions/default-boolean-field.ts
|
|
188
|
+
var DefaultBooleanField = class _DefaultBooleanField extends DefaultField {
|
|
189
|
+
static typeName = "boolean";
|
|
190
|
+
typeName = _DefaultBooleanField.typeName;
|
|
191
|
+
constructor(name, initialVal, options) {
|
|
192
|
+
super(name, initialVal, options);
|
|
193
|
+
}
|
|
194
|
+
toggle() {
|
|
195
|
+
this.value = !this.value;
|
|
196
|
+
return this.value;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
// src/field-definitions/default-string-field.ts
|
|
201
|
+
var DefaultStringField = class _DefaultStringField extends DefaultField {
|
|
202
|
+
static typeName = "string";
|
|
203
|
+
typeName = _DefaultStringField.typeName;
|
|
204
|
+
constructor(name, initialVal, options) {
|
|
205
|
+
super(name, initialVal, options);
|
|
206
|
+
}
|
|
207
|
+
append(str) {
|
|
208
|
+
this.value = this.value + str;
|
|
209
|
+
return this;
|
|
210
|
+
}
|
|
211
|
+
prepend(str) {
|
|
212
|
+
this.value = str + this.value;
|
|
213
|
+
return this;
|
|
214
|
+
}
|
|
215
|
+
trim() {
|
|
216
|
+
this.value = this.value.trim();
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
isEmpty() {
|
|
220
|
+
return this.value.length === 0;
|
|
221
|
+
}
|
|
222
|
+
clear() {
|
|
223
|
+
this.value = "";
|
|
159
224
|
}
|
|
160
225
|
};
|
|
161
226
|
|
|
162
|
-
// src/
|
|
227
|
+
// src/field-definitions/default-numeric-field.ts
|
|
163
228
|
import { isNullOrUndefined } from "@axi-engine/utils";
|
|
164
|
-
var
|
|
229
|
+
var DefaultNumericField = class _DefaultNumericField extends DefaultField {
|
|
230
|
+
static typeName = "numeric";
|
|
231
|
+
typeName = _DefaultNumericField.typeName;
|
|
165
232
|
get min() {
|
|
166
|
-
const policy = this.
|
|
233
|
+
const policy = this.policies.get(ClampPolicy.id) ?? this.policies.get(ClampMinPolicy.id);
|
|
167
234
|
return policy?.min;
|
|
168
235
|
}
|
|
169
236
|
get max() {
|
|
170
|
-
const policy = this.
|
|
237
|
+
const policy = this.policies.get(ClampPolicy.id) ?? this.policies.get(ClampMaxPolicy.id);
|
|
171
238
|
return policy?.max;
|
|
172
239
|
}
|
|
173
|
-
get isMin() {
|
|
174
|
-
const min = this.min;
|
|
175
|
-
return isNullOrUndefined(min) ? false : this.val <= min;
|
|
176
|
-
}
|
|
177
|
-
get isMax() {
|
|
178
|
-
const max = this.max;
|
|
179
|
-
return isNullOrUndefined(max) ? false : this.val >= max;
|
|
180
|
-
}
|
|
181
240
|
constructor(name, initialVal, options) {
|
|
182
241
|
const policies = options?.policies ?? [];
|
|
183
242
|
if (!isNullOrUndefined(options?.min) && !isNullOrUndefined(options?.max)) {
|
|
@@ -189,364 +248,711 @@ var NumberField = class extends Field {
|
|
|
189
248
|
}
|
|
190
249
|
super(name, initialVal, { policies });
|
|
191
250
|
}
|
|
251
|
+
isMin() {
|
|
252
|
+
const min = this.min;
|
|
253
|
+
return isNullOrUndefined(min) ? false : this.value <= min;
|
|
254
|
+
}
|
|
255
|
+
isMax() {
|
|
256
|
+
const max = this.max;
|
|
257
|
+
return isNullOrUndefined(max) ? false : this.value >= max;
|
|
258
|
+
}
|
|
192
259
|
inc(amount = 1) {
|
|
193
|
-
this.
|
|
260
|
+
this.value = this.value + amount;
|
|
194
261
|
}
|
|
195
262
|
dec(amount = 1) {
|
|
196
|
-
this.
|
|
263
|
+
this.value = this.value - amount;
|
|
197
264
|
}
|
|
198
265
|
};
|
|
199
266
|
|
|
200
|
-
// src/
|
|
201
|
-
import {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
267
|
+
// src/utils/constructor-registry.ts
|
|
268
|
+
import { throwIf, throwIfEmpty } from "@axi-engine/utils";
|
|
269
|
+
var ConstructorRegistry = class {
|
|
270
|
+
items = /* @__PURE__ */ new Map();
|
|
271
|
+
/**
|
|
272
|
+
* Registers a constructor with a unique string identifier.
|
|
273
|
+
*
|
|
274
|
+
* @param typeId - The unique identifier for the constructor (e.g., a static `typeName` property from a class).
|
|
275
|
+
* @param ctor - The class constructor to register.
|
|
276
|
+
* @returns The registry instance for chainable calls.
|
|
277
|
+
* @throws If a constructor with the same `typeId` is already registered.
|
|
278
|
+
*/
|
|
279
|
+
register(typeId, ctor) {
|
|
280
|
+
throwIf(this.items.has(typeId), `A constructor with typeId '${typeId}' is already registered.`);
|
|
281
|
+
this.items.set(typeId, ctor);
|
|
282
|
+
return this;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Retrieves a constructor by its identifier.
|
|
286
|
+
*
|
|
287
|
+
* @param typeId - The identifier of the constructor to retrieve.
|
|
288
|
+
* @returns The found class constructor.
|
|
289
|
+
* @throws If no constructor is found for the given `typeId`.
|
|
290
|
+
*/
|
|
291
|
+
get(typeId) {
|
|
292
|
+
const Ctor = this.items.get(typeId);
|
|
293
|
+
throwIfEmpty(Ctor, `No constructor found for typeId '${typeId}'`);
|
|
294
|
+
return Ctor;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Checks if a constructor for a given identifier is registered.
|
|
298
|
+
* @param typeId - The identifier to check.
|
|
299
|
+
* @returns `true` if a constructor is registered, otherwise `false`.
|
|
300
|
+
*/
|
|
301
|
+
has(typeId) {
|
|
302
|
+
return this.items.has(typeId);
|
|
208
303
|
}
|
|
209
304
|
/**
|
|
210
|
-
*
|
|
211
|
-
|
|
212
|
-
|
|
305
|
+
* Clears all registered constructors from the registry.
|
|
306
|
+
*/
|
|
307
|
+
clear() {
|
|
308
|
+
this.items.clear();
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
// src/field-registry.ts
|
|
313
|
+
var FieldRegistry = class extends ConstructorRegistry {
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
// src/fields.ts
|
|
317
|
+
import { Emitter as Emitter2, throwIf as throwIf2 } from "@axi-engine/utils";
|
|
318
|
+
var Fields = class _Fields {
|
|
319
|
+
static typeName = "fields";
|
|
320
|
+
typeName = _Fields.typeName;
|
|
321
|
+
_fields = /* @__PURE__ */ new Map();
|
|
322
|
+
_fieldRegistry;
|
|
323
|
+
/**
|
|
324
|
+
* An event emitter that fires when a new field is added to the collection.
|
|
325
|
+
* @event
|
|
326
|
+
* @param {object} event - The event payload.
|
|
327
|
+
* @param {string} event.name - The name of the added field.
|
|
328
|
+
* @param {Field<any>} event.field - The `Field` instance that was added.
|
|
329
|
+
*/
|
|
330
|
+
onAdd = new Emitter2();
|
|
331
|
+
/**
|
|
332
|
+
* An event emitter that fires after one or more fields have been removed.
|
|
333
|
+
* @event
|
|
334
|
+
* @param {object} event - The event payload.
|
|
335
|
+
* @param {string[]} event.names - An array of names of the fields that were successfully removed.
|
|
336
|
+
*/
|
|
337
|
+
onRemove = new Emitter2();
|
|
338
|
+
/**
|
|
339
|
+
* Gets the read-only map of all `Field` instances in this container.
|
|
340
|
+
* @returns {Map<string, Field<any>>} The collection of fields.
|
|
213
341
|
*/
|
|
214
342
|
get fields() {
|
|
215
343
|
return this._fields;
|
|
216
344
|
}
|
|
217
345
|
/**
|
|
218
|
-
*
|
|
219
|
-
* @param
|
|
220
|
-
* @returns `true` if the field exists, otherwise `false`.
|
|
346
|
+
* Creates an instance of Fields.
|
|
347
|
+
* @param {FieldRegistry} fieldRegistry - The registry used to create new `Field` instances.
|
|
221
348
|
*/
|
|
222
|
-
|
|
223
|
-
|
|
349
|
+
constructor(fieldRegistry) {
|
|
350
|
+
this._fieldRegistry = fieldRegistry;
|
|
224
351
|
}
|
|
225
352
|
/**
|
|
226
|
-
*
|
|
227
|
-
* @param name The
|
|
228
|
-
* @
|
|
229
|
-
* @returns The newly created `Field` instance.
|
|
353
|
+
* Checks if a field with the given name exists in the collection.
|
|
354
|
+
* @param {string} name The name of the field to check.
|
|
355
|
+
* @returns {boolean} `true` if the field exists, otherwise `false`.
|
|
230
356
|
*/
|
|
231
|
-
|
|
232
|
-
return this.
|
|
357
|
+
has(name) {
|
|
358
|
+
return this._fields.has(name);
|
|
233
359
|
}
|
|
234
360
|
/**
|
|
235
|
-
* Adds a pre-existing `Field` instance to the collection.
|
|
236
|
-
*
|
|
237
|
-
* @param field The `Field` instance to add.
|
|
238
|
-
* @returns The added `Field` instance
|
|
361
|
+
* Adds a pre-existing `Field` instance to the collection and fires the `onAdd` event.
|
|
362
|
+
* @template T - The specific `Field` type being added.
|
|
363
|
+
* @param {Field<any>} field - The `Field` instance to add.
|
|
364
|
+
* @returns {T} The added `Field` instance, cast to type `T`.
|
|
365
|
+
* @throws If a field with the same name already exists.
|
|
239
366
|
*/
|
|
240
367
|
add(field) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
this.events.emit("created", {
|
|
246
|
-
fieldName: field.name,
|
|
368
|
+
throwIf2(this.has(field.name), `Field with name '${field.name}' already exists`);
|
|
369
|
+
this._fields.set(field.name, field);
|
|
370
|
+
this.onAdd.emit({
|
|
371
|
+
name: field.name,
|
|
247
372
|
field
|
|
248
373
|
});
|
|
249
374
|
return field;
|
|
250
375
|
}
|
|
251
376
|
/**
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
* @
|
|
255
|
-
* @
|
|
377
|
+
* Creates a new `Field` instance of a specified type, adds it to the collection, and returns it.
|
|
378
|
+
* This is the primary factory method for creating fields within this container.
|
|
379
|
+
* @template T - The expected `Field` type to be returned.
|
|
380
|
+
* @param {string} typeName - The registered type name of the field to create (e.g., 'numeric', 'boolean').
|
|
381
|
+
* @param {string} name - The unique name for the new field.
|
|
382
|
+
* @param {*} initialValue - The initial value for the new field.
|
|
383
|
+
* @param {*} [options] - Optional configuration passed to the field's constructor.
|
|
384
|
+
* @returns {T} The newly created `Field` instance.
|
|
256
385
|
*/
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
386
|
+
create(typeName, name, initialValue, options) {
|
|
387
|
+
const Ctor = this._fieldRegistry.get(typeName);
|
|
388
|
+
const field = new Ctor(name, initialValue, options);
|
|
389
|
+
this.add(field);
|
|
390
|
+
return field;
|
|
260
391
|
}
|
|
261
392
|
/**
|
|
262
|
-
*
|
|
263
|
-
* @
|
|
264
|
-
* @param
|
|
265
|
-
* @
|
|
393
|
+
* Updates an existing field's value or creates a new one if it doesn't exist.
|
|
394
|
+
* @template T - The expected `Field` type.
|
|
395
|
+
* @param {string} typeName - The type name to use if a new field needs to be created.
|
|
396
|
+
* @param {string} name - The name of the field to update or create.
|
|
397
|
+
* @param {*} value - The new value to set.
|
|
398
|
+
* @param {*} [options] - Optional configuration, used only if a new field is created.
|
|
399
|
+
* @returns {T} The existing or newly created `Field` instance.
|
|
266
400
|
*/
|
|
267
|
-
upset(name, value) {
|
|
401
|
+
upset(typeName, name, value, options) {
|
|
268
402
|
if (this.has(name)) {
|
|
269
403
|
const field = this.get(name);
|
|
270
|
-
field.
|
|
404
|
+
field.value = value;
|
|
271
405
|
return field;
|
|
272
406
|
}
|
|
273
|
-
return this.create(name, value);
|
|
407
|
+
return this.create(typeName, name, value, options);
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Retrieves a field by its name.
|
|
411
|
+
* @template T - The expected `Field` type to be returned.
|
|
412
|
+
* @param {string} name - The name of the field to retrieve.
|
|
413
|
+
* @returns {T} The `Field` instance.
|
|
414
|
+
* @throws If the field does not exist.
|
|
415
|
+
*/
|
|
416
|
+
get(name) {
|
|
417
|
+
throwIf2(!this._fields.has(name), `Field with name '${name}' not exists`);
|
|
418
|
+
return this._fields.get(name);
|
|
274
419
|
}
|
|
275
420
|
/**
|
|
276
421
|
* Removes one or more fields from the collection.
|
|
277
422
|
* This method ensures that the `destroy` method of each removed field is called to clean up its resources.
|
|
278
|
-
* @param names A single name or an array of names to remove.
|
|
423
|
+
* @param {string| string[]} names A single name or an array of names to remove.
|
|
279
424
|
*/
|
|
280
425
|
remove(names) {
|
|
281
426
|
const namesToRemove = Array.isArray(names) ? names : [names];
|
|
282
|
-
const fieldsMap = new Map(this._fields.value);
|
|
283
427
|
const reallyRemoved = namesToRemove.filter((name) => {
|
|
284
|
-
const field =
|
|
428
|
+
const field = this._fields.get(name);
|
|
285
429
|
if (!field) {
|
|
286
430
|
return false;
|
|
287
431
|
}
|
|
288
432
|
field.destroy();
|
|
289
|
-
|
|
290
|
-
return true;
|
|
433
|
+
return this._fields.delete(name);
|
|
291
434
|
});
|
|
292
435
|
if (!reallyRemoved.length) {
|
|
293
436
|
return;
|
|
294
437
|
}
|
|
295
|
-
this.
|
|
296
|
-
this.events.emit("removed", { fieldNames: reallyRemoved });
|
|
438
|
+
this.onRemove.emit({ names: reallyRemoved });
|
|
297
439
|
}
|
|
298
440
|
/**
|
|
299
441
|
* Removes all fields from the collection, ensuring each is properly destroyed.
|
|
300
442
|
*/
|
|
301
443
|
clear() {
|
|
302
|
-
this.remove(Array.from(this._fields.
|
|
444
|
+
this.remove(Array.from(this._fields.keys()));
|
|
303
445
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
snapshot() {
|
|
309
|
-
const dump = {
|
|
310
|
-
__type: "Fields" /* fields */
|
|
311
|
-
};
|
|
312
|
-
this._fields.value.forEach((field, key) => dump[key] = field.val);
|
|
313
|
-
return dump;
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Restores the state of the fields from a snapshot.
|
|
317
|
-
* It uses the `upset` logic to create or update fields based on the snapshot data.
|
|
318
|
-
* @param snapshot The snapshot object to load.
|
|
319
|
-
*/
|
|
320
|
-
hydrate(snapshot) {
|
|
321
|
-
for (let key in snapshot) {
|
|
322
|
-
if (key === "__type") {
|
|
323
|
-
continue;
|
|
324
|
-
}
|
|
325
|
-
this.upset(key, snapshot[key]);
|
|
326
|
-
}
|
|
446
|
+
destroy() {
|
|
447
|
+
this.clear();
|
|
448
|
+
this.onAdd.clear();
|
|
449
|
+
this.onRemove.clear();
|
|
327
450
|
}
|
|
328
451
|
};
|
|
329
452
|
|
|
330
|
-
// src/fields.ts
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
}
|
|
336
|
-
upsetNumber(name, value, options) {
|
|
337
|
-
if (this.has(name)) {
|
|
338
|
-
const field = this.getNumber(name);
|
|
339
|
-
field.set(value);
|
|
340
|
-
return field;
|
|
453
|
+
// src/mixins/with-boolean-fields.mixin.ts
|
|
454
|
+
function WithBooleanFields(Base) {
|
|
455
|
+
return class FieldsWithBoolean extends Base {
|
|
456
|
+
createBoolean(name, initialValue, options) {
|
|
457
|
+
return this.create(DefaultBooleanField.typeName, name, initialValue, options);
|
|
341
458
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
getNumber(name) {
|
|
345
|
-
const field = this.get(name);
|
|
346
|
-
throwIf2(!(field instanceof NumberField), `wrong field type, field ${name} not a instance of NUmberField`);
|
|
347
|
-
return field;
|
|
348
|
-
}
|
|
349
|
-
create(name, initialValue) {
|
|
350
|
-
return this.add(new Field(name, initialValue));
|
|
351
|
-
}
|
|
352
|
-
upset(name, value) {
|
|
353
|
-
if (this.has(name)) {
|
|
354
|
-
const field = this.get(name);
|
|
355
|
-
field.set(value);
|
|
356
|
-
return field;
|
|
459
|
+
upsetBoolean(name, value, options) {
|
|
460
|
+
return this.upset(DefaultBooleanField.typeName, name, value, options);
|
|
357
461
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
}
|
|
364
|
-
};
|
|
462
|
+
getBoolean(name) {
|
|
463
|
+
return this.get(name);
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
}
|
|
365
467
|
|
|
366
|
-
// src/
|
|
367
|
-
|
|
468
|
+
// src/mixins/with-string-fields.mixin.ts
|
|
469
|
+
function WithStringFields(Base) {
|
|
470
|
+
return class FieldsWithString extends Base {
|
|
471
|
+
createString(name, initialValue, options) {
|
|
472
|
+
return this.create(DefaultStringField.typeName, name, initialValue, options);
|
|
473
|
+
}
|
|
474
|
+
upsetString(name, value, options) {
|
|
475
|
+
return this.upset(DefaultStringField.typeName, name, value, options);
|
|
476
|
+
}
|
|
477
|
+
getString(name) {
|
|
478
|
+
return this.get(name);
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// src/mixins/with-numeric-fields.mixin.ts
|
|
484
|
+
function WithNumericFields(Base) {
|
|
485
|
+
return class FieldsWithNumeric extends Base {
|
|
486
|
+
createNumeric(name, initialValue, options) {
|
|
487
|
+
return this.create(DefaultNumericField.typeName, name, initialValue, options);
|
|
488
|
+
}
|
|
489
|
+
upsetNumeric(name, value, options) {
|
|
490
|
+
return this.upset(DefaultNumericField.typeName, name, value, options);
|
|
491
|
+
}
|
|
492
|
+
getNumeric(name) {
|
|
493
|
+
return this.get(name);
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// src/mixins/with-default-fields.mixin.ts
|
|
499
|
+
function WithDefaultFields(Base) {
|
|
500
|
+
return class FieldsWithDefault extends Base {
|
|
501
|
+
createDefault(name, initialValue, options) {
|
|
502
|
+
return this.create(DefaultField.typeName, name, initialValue, options);
|
|
503
|
+
}
|
|
504
|
+
upsetDefault(name, value, options) {
|
|
505
|
+
return this.upset(DefaultField.typeName, name, value, options);
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// src/default-fields.ts
|
|
511
|
+
var DefaultFields = class extends WithBooleanFields(WithStringFields(WithNumericFields(WithDefaultFields(Fields)))) {
|
|
368
512
|
};
|
|
369
513
|
|
|
370
514
|
// src/field-tree.ts
|
|
371
|
-
import {
|
|
372
|
-
import { ensurePathArray, ensurePathString, throwIf as throwIf3, throwIfEmpty } from "@axi-engine/utils";
|
|
373
|
-
import { AxiEventEmitter as AxiEventEmitter2 } from "@axi-engine/events";
|
|
515
|
+
import { Emitter as Emitter3, ensurePathArray, ensurePathString, throwIf as throwIf3, throwIfEmpty as throwIfEmpty2 } from "@axi-engine/utils";
|
|
374
516
|
var FieldTree = class _FieldTree {
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
517
|
+
static typeName = "fieldTree";
|
|
518
|
+
typeName = _FieldTree.typeName;
|
|
519
|
+
/** @private The internal map storing child nodes (branches or leaves). */
|
|
520
|
+
_nodes = /* @__PURE__ */ new Map();
|
|
521
|
+
/** @private The factory used to create new child nodes. */
|
|
522
|
+
_factory;
|
|
523
|
+
/**
|
|
524
|
+
* An event emitter that fires immediately after a new node is added to this tree branch.
|
|
525
|
+
* @event
|
|
526
|
+
* @param {object} event - The event payload.
|
|
527
|
+
* @param {string} event.name - The name (key) of the added node.
|
|
528
|
+
* @param event.node - The node instance that was added.
|
|
529
|
+
* @example
|
|
530
|
+
* myTree.onAdd.subscribe(({ name, node }) => {
|
|
531
|
+
* console.log(`Node '${name}' was added.`, node);
|
|
532
|
+
* });
|
|
533
|
+
*/
|
|
534
|
+
onAdd = new Emitter3();
|
|
535
|
+
/**
|
|
536
|
+
* An event emitter that fires once after one or more nodes have been successfully removed.
|
|
537
|
+
* @event
|
|
538
|
+
* @param {object} event - The event payload.
|
|
539
|
+
* @param {string[]} event.names - An array of names of the nodes that were removed.
|
|
540
|
+
* @example
|
|
541
|
+
* myTree.onRemove.subscribe(({ names }) => {
|
|
542
|
+
* console.log(`Nodes removed: ${names.join(', ')}`);
|
|
543
|
+
* });
|
|
544
|
+
*/
|
|
545
|
+
onRemove = new Emitter3();
|
|
546
|
+
/**
|
|
547
|
+
* Gets the collection of direct child nodes of this tree branch.
|
|
548
|
+
*/
|
|
549
|
+
get nodes() {
|
|
550
|
+
return this._nodes;
|
|
378
551
|
}
|
|
379
552
|
/**
|
|
380
|
-
*
|
|
381
|
-
*
|
|
553
|
+
* Creates an instance of FieldTree.
|
|
554
|
+
* @param {TreeNodeFactory} factory - A factory responsible for creating new nodes within the tree.
|
|
382
555
|
*/
|
|
383
|
-
|
|
384
|
-
|
|
556
|
+
constructor(factory) {
|
|
557
|
+
this._factory = factory;
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Checks if a direct child node with the given name exists.
|
|
561
|
+
* @param {string} name - The name of the direct child node.
|
|
562
|
+
* @returns {boolean} `true` if the node exists, otherwise `false`.
|
|
563
|
+
*/
|
|
564
|
+
has(name) {
|
|
565
|
+
return this._nodes.has(name);
|
|
385
566
|
}
|
|
386
567
|
/**
|
|
387
|
-
* Checks if a
|
|
388
|
-
* @
|
|
568
|
+
* Checks if a node exists at a given path, traversing the tree.
|
|
569
|
+
* @param {PathType} path - The path to check (e.g., 'player/stats' or ['player', 'stats']).
|
|
570
|
+
* @returns {boolean} `true` if the entire path resolves to a node, otherwise `false`.
|
|
389
571
|
*/
|
|
390
572
|
hasPath(path) {
|
|
391
|
-
const
|
|
392
|
-
|
|
393
|
-
for (let i = 0; i < pathParts.length; i++) {
|
|
394
|
-
const part = pathParts[i];
|
|
395
|
-
const nextNode = currentNode._items.value.get(part);
|
|
396
|
-
if (!nextNode) {
|
|
397
|
-
return false;
|
|
398
|
-
}
|
|
399
|
-
if (nextNode instanceof BaseFields) {
|
|
400
|
-
if (i === pathParts.length - 1) {
|
|
401
|
-
return true;
|
|
402
|
-
}
|
|
403
|
-
throwIf3(
|
|
404
|
-
pathParts.length - i > 2,
|
|
405
|
-
`Path validation failed, full path: ${ensurePathString(path)}, has extra nodes after Fields placed at: ${ensurePathString(pathParts.slice(0, i + 1))}`
|
|
406
|
-
);
|
|
407
|
-
return nextNode.has(pathParts[i + 1]);
|
|
408
|
-
}
|
|
409
|
-
currentNode = nextNode;
|
|
410
|
-
}
|
|
411
|
-
return true;
|
|
573
|
+
const traversedPath = this.traversePath(path);
|
|
574
|
+
return traversedPath.branch.has(traversedPath.leafName);
|
|
412
575
|
}
|
|
413
576
|
/**
|
|
414
|
-
*
|
|
415
|
-
* @param name The name
|
|
416
|
-
* @
|
|
417
|
-
* @
|
|
577
|
+
* Adds a pre-existing node as a direct child of this tree branch.
|
|
578
|
+
* @param {string} name - The name to assign to the new child node.
|
|
579
|
+
* @param {TreeNode} node - The node instance to add.
|
|
580
|
+
* @returns {TreeNode} The added node.
|
|
581
|
+
* @throws If a node with the same name already exists.
|
|
418
582
|
*/
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
583
|
+
addNode(name, node) {
|
|
584
|
+
throwIf3(this.has(name), `Can't add node with name: '${name}', node already exists`);
|
|
585
|
+
this._nodes.set(name, node);
|
|
586
|
+
this.onAdd.emit({ name, node });
|
|
422
587
|
return node;
|
|
423
588
|
}
|
|
424
589
|
/**
|
|
425
|
-
* Retrieves a child node
|
|
426
|
-
* @param name The name of the child node.
|
|
427
|
-
* @returns The
|
|
428
|
-
* @throws If
|
|
590
|
+
* Retrieves a direct child node by its name.
|
|
591
|
+
* @param {string} name - The name of the child node.
|
|
592
|
+
* @returns {TreeNode} The retrieved node.
|
|
593
|
+
* @throws If a node with the given name cannot be found.
|
|
429
594
|
*/
|
|
430
|
-
|
|
431
|
-
const node = this.
|
|
432
|
-
|
|
595
|
+
getNode(name) {
|
|
596
|
+
const node = this._nodes.get(name);
|
|
597
|
+
throwIfEmpty2(node, `Can't find node with name '${name}'`);
|
|
433
598
|
return node;
|
|
434
599
|
}
|
|
435
600
|
/**
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
*
|
|
601
|
+
* Removes one or more nodes from this tree branch.
|
|
602
|
+
*
|
|
603
|
+
* This method first validates that all specified nodes exist. If validation passes,
|
|
604
|
+
* it recursively calls `destroy()` on each node to ensure proper cleanup of the entire subtree.
|
|
605
|
+
* Finally, it emits a single `onRemove` event with the names of all successfully removed nodes.
|
|
606
|
+
*
|
|
607
|
+
* @param {string | string[]} names - A single name or an array of names of the nodes to remove.
|
|
608
|
+
* @throws If any of the specified names do not correspond to an existing node.
|
|
609
|
+
*/
|
|
610
|
+
removeNode(names) {
|
|
611
|
+
const toRemoveNames = Array.isArray(names) ? names : [names];
|
|
612
|
+
toRemoveNames.forEach((name) => {
|
|
613
|
+
throwIf3(!this.has(name), `Can't remove node with name: '${name}', node doesn't exists`);
|
|
614
|
+
});
|
|
615
|
+
toRemoveNames.forEach((name) => {
|
|
616
|
+
this._nodes.get(name).destroy();
|
|
617
|
+
this._nodes.delete(name);
|
|
618
|
+
});
|
|
619
|
+
if (toRemoveNames.length) {
|
|
620
|
+
this.onRemove.emit({ names: toRemoveNames });
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Creates a new `FieldTree` (branch) node at the specified path.
|
|
625
|
+
* @param {PathType} path - The path where the new `FieldTree` should be created.
|
|
626
|
+
* @param {boolean} [createPath=false] - If `true`, any missing parent branches in the path will be created automatically.
|
|
627
|
+
* @returns {FieldTree} The newly created `FieldTree` instance.
|
|
628
|
+
* @throws If the path is invalid or a node already exists at the target location.
|
|
629
|
+
*/
|
|
630
|
+
createFieldTree(path, createPath) {
|
|
631
|
+
const traversedPath = this.traversePath(path, createPath);
|
|
632
|
+
return traversedPath.branch.addNode(traversedPath.leafName, this._factory.tree());
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Creates a new `Fields` (leaf) container at the specified path.
|
|
636
|
+
* @param {PathType} path - The path where the new `Fields` container should be created.
|
|
637
|
+
* @param {boolean} [createPath=false] - If `true`, any missing parent branches in the path will be created automatically.
|
|
638
|
+
* @returns {Fields} The newly created `Fields` instance.
|
|
639
|
+
* @throws If the path is invalid or a node already exists at the target location.
|
|
440
640
|
*/
|
|
441
|
-
|
|
442
|
-
const
|
|
443
|
-
|
|
641
|
+
createFields(path, createPath) {
|
|
642
|
+
const traversedPath = this.traversePath(path, createPath);
|
|
643
|
+
return traversedPath.branch.addNode(traversedPath.leafName, this._factory.fields());
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Retrieves a `FieldTree` (branch) node from a specified path.
|
|
647
|
+
* @param {PathType} path - The path to the `FieldTree` node.
|
|
648
|
+
* @returns {FieldTree} The `FieldTree` instance at the specified path.
|
|
649
|
+
* @throws If the path is invalid or the node at the path is not a `FieldTree`.
|
|
650
|
+
*/
|
|
651
|
+
getFieldTree(path) {
|
|
652
|
+
const traversedPath = this.traversePath(path);
|
|
653
|
+
const node = traversedPath.branch.getNode(traversedPath.leafName);
|
|
654
|
+
throwIf3(
|
|
655
|
+
!(node instanceof _FieldTree),
|
|
656
|
+
`Node with name: ${traversedPath.leafName} by path: '${ensurePathString(path)}' should be instance of FieldTree`
|
|
657
|
+
);
|
|
444
658
|
return node;
|
|
445
659
|
}
|
|
446
660
|
/**
|
|
447
|
-
* Retrieves a
|
|
448
|
-
* @param
|
|
449
|
-
* @returns The
|
|
450
|
-
* @throws If
|
|
661
|
+
* Retrieves a `Fields` (leaf) container from a specified path.
|
|
662
|
+
* @param {PathType} path - The path to the `Fields` container.
|
|
663
|
+
* @returns {Fields} The `Fields` instance at the specified path.
|
|
664
|
+
* @throws If the path is invalid or the node at the path is not a `Fields` container.
|
|
451
665
|
*/
|
|
452
|
-
|
|
453
|
-
const
|
|
454
|
-
|
|
666
|
+
getFields(path) {
|
|
667
|
+
const traversedPath = this.traversePath(path);
|
|
668
|
+
const node = traversedPath.branch.getNode(traversedPath.leafName);
|
|
669
|
+
throwIf3(
|
|
670
|
+
!(node instanceof Fields),
|
|
671
|
+
`Node with name: ${traversedPath.leafName} by path: '${ensurePathString(path)}' should be instance of Fields`
|
|
672
|
+
);
|
|
455
673
|
return node;
|
|
456
674
|
}
|
|
457
675
|
/**
|
|
458
|
-
*
|
|
459
|
-
* @param
|
|
460
|
-
* @returns The newly created `FieldTree` instance.
|
|
676
|
+
* Retrieves a `FieldTree` at the specified path. If it or any part of the path doesn't exist, it will be created.
|
|
677
|
+
* @param {PathType} path - The path to the `FieldTree` node.
|
|
678
|
+
* @returns {FieldTree} The existing or newly created `FieldTree` instance.
|
|
679
|
+
*/
|
|
680
|
+
getOrCreateFieldTree(path) {
|
|
681
|
+
const traversedPath = this.traversePath(path, true);
|
|
682
|
+
return traversedPath.branch.has(traversedPath.leafName) ? traversedPath.branch.getFieldTree(traversedPath.leafName) : traversedPath.branch.createFieldTree(traversedPath.leafName);
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Retrieves a `Fields` container at the specified path. If it or any part of the path doesn't exist, it will be created.
|
|
686
|
+
* @param {PathType} path - The path to the `Fields` container.
|
|
687
|
+
* @returns {Fields} The existing or newly created `Fields` instance.
|
|
688
|
+
*/
|
|
689
|
+
getOrCreateFields(path) {
|
|
690
|
+
const traversedPath = this.traversePath(path, true);
|
|
691
|
+
return traversedPath.branch.has(traversedPath.leafName) ? traversedPath.branch.getFields(traversedPath.leafName) : traversedPath.branch.createFields(traversedPath.leafName);
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Removes all child nodes from this tree branch.
|
|
695
|
+
* This method ensures that `destroy()` is called on each child node, allowing for
|
|
696
|
+
* a full, recursive cleanup of the entire subtree.
|
|
461
697
|
*/
|
|
462
|
-
|
|
463
|
-
|
|
698
|
+
clear() {
|
|
699
|
+
this.removeNode(Array.from(this._nodes.keys()));
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Performs a complete cleanup of this node and its entire subtree.
|
|
703
|
+
*
|
|
704
|
+
* It recursively destroys all child nodes by calling `clear()` and then
|
|
705
|
+
* unsubscribes all listeners from its own event emitters.
|
|
706
|
+
* This method should be called when a node is no longer needed.
|
|
707
|
+
*/
|
|
708
|
+
destroy() {
|
|
709
|
+
this.clear();
|
|
710
|
+
this.onAdd.clear();
|
|
711
|
+
this.onRemove.clear();
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* @private
|
|
715
|
+
* Navigates the tree to the parent of a target node.
|
|
716
|
+
* This is the core traversal logic for all path-based operations.
|
|
717
|
+
* @param {PathType} path - The full path to the target node.
|
|
718
|
+
* @param {boolean} [createPath=false] - If `true`, creates missing `FieldTree` branches along the path.
|
|
719
|
+
* @returns {{branch: FieldTree, leafName: string}} An object containing the final branch (parent node) and the name of the leaf (target node).
|
|
720
|
+
* @throws If the path is empty, invalid, or contains a `Fields` container as an intermediate segment.
|
|
721
|
+
*/
|
|
722
|
+
traversePath(path, createPath) {
|
|
723
|
+
const pathArr = ensurePathArray(path);
|
|
724
|
+
throwIfEmpty2(pathArr, "The path is empty");
|
|
725
|
+
const leafName = pathArr.pop();
|
|
726
|
+
let currentNode = this;
|
|
727
|
+
for (const pathPart of pathArr) {
|
|
728
|
+
let node;
|
|
729
|
+
if (currentNode.has(pathPart)) {
|
|
730
|
+
node = currentNode.getNode(pathPart);
|
|
731
|
+
} else {
|
|
732
|
+
if (createPath) {
|
|
733
|
+
node = currentNode.createFieldTree(pathPart);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
throwIfEmpty2(node, `Can't find node with name ${pathPart} by path parsing: ${ensurePathString(path)}`);
|
|
737
|
+
throwIf3(node instanceof Fields, `Node with name ${pathPart} should be instance of FieldTree`);
|
|
738
|
+
currentNode = node;
|
|
739
|
+
}
|
|
740
|
+
return { branch: currentNode, leafName };
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
// src/field-tree-node-factory.ts
|
|
745
|
+
var DefaultFieldsFactory = class {
|
|
746
|
+
constructor(fieldRegistry) {
|
|
747
|
+
this.fieldRegistry = fieldRegistry;
|
|
748
|
+
}
|
|
749
|
+
fields() {
|
|
750
|
+
return new DefaultFields(this.fieldRegistry);
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
var DefaultTreeNodeFactory = class extends DefaultFieldsFactory {
|
|
754
|
+
constructor(fieldRegistry) {
|
|
755
|
+
super(fieldRegistry);
|
|
756
|
+
}
|
|
757
|
+
tree() {
|
|
758
|
+
return new FieldTree(this);
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
|
|
762
|
+
// src/serializer/policies/clamp-policy-serializer-handler.ts
|
|
763
|
+
var ClampPolicySerializerHandler = class {
|
|
764
|
+
snapshot(policy) {
|
|
765
|
+
return { min: policy.min, max: policy.max };
|
|
766
|
+
}
|
|
767
|
+
hydrate(data) {
|
|
768
|
+
return new ClampPolicy(data.min, data.max);
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
|
|
772
|
+
// src/serializer/policies/clamp-max-policy-serializer-handler.ts
|
|
773
|
+
var ClampMaxPolicySerializerHandler = class {
|
|
774
|
+
snapshot(policy) {
|
|
775
|
+
return { max: policy.max };
|
|
776
|
+
}
|
|
777
|
+
hydrate(data) {
|
|
778
|
+
return new ClampMaxPolicy(data.max);
|
|
779
|
+
}
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
// src/serializer/policies/clamp-min-policy-serializer-handler.ts
|
|
783
|
+
var ClampMinPolicySerializerHandler = class {
|
|
784
|
+
snapshot(policy) {
|
|
785
|
+
return { min: policy.min };
|
|
786
|
+
}
|
|
787
|
+
hydrate(data) {
|
|
788
|
+
return new ClampMinPolicy(data.min);
|
|
789
|
+
}
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
// src/serializer/policy-serializer.ts
|
|
793
|
+
import { throwIf as throwIf4, throwIfEmpty as throwIfEmpty3 } from "@axi-engine/utils";
|
|
794
|
+
var PolicySerializer = class {
|
|
795
|
+
handlers = /* @__PURE__ */ new Map();
|
|
796
|
+
register(policyId, handler) {
|
|
797
|
+
throwIf4(this.handlers.has(policyId), `A handler for policy ID '${policyId}' is already registered.`);
|
|
798
|
+
this.handlers.set(policyId, handler);
|
|
799
|
+
return this;
|
|
800
|
+
}
|
|
801
|
+
clearHandlers() {
|
|
802
|
+
this.handlers.clear();
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Creates a serializable snapshot of a policy instance.
|
|
806
|
+
* The snapshot includes the policy's state and a `__type` identifier.
|
|
807
|
+
* @param policy The policy instance to snapshot.
|
|
808
|
+
* @returns A plain object ready for JSON serialization.
|
|
809
|
+
* @throws If no handler is registered for the policy's ID.
|
|
810
|
+
*/
|
|
811
|
+
snapshot(policy) {
|
|
812
|
+
const handler = this.handlers.get(policy.id);
|
|
813
|
+
throwIfEmpty3(handler, `No serializer handler registered for policy ID: '${policy.id}'`);
|
|
814
|
+
const data = handler.snapshot(policy);
|
|
815
|
+
return {
|
|
816
|
+
__type: policy.id,
|
|
817
|
+
...data
|
|
818
|
+
};
|
|
464
819
|
}
|
|
465
820
|
/**
|
|
466
|
-
*
|
|
467
|
-
* @param
|
|
468
|
-
* @returns
|
|
821
|
+
* Restores a policy instance from its snapshot representation.
|
|
822
|
+
* @param snapshot The plain object snapshot, which must contain a `__type` property.
|
|
823
|
+
* @returns A new, fully functional policy instance.
|
|
824
|
+
* @throws If the snapshot is invalid or no handler is registered for its `__type`.
|
|
469
825
|
*/
|
|
470
|
-
|
|
471
|
-
|
|
826
|
+
hydrate(snapshot) {
|
|
827
|
+
const typeId = snapshot?.__type;
|
|
828
|
+
throwIfEmpty3(typeId, 'Invalid policy snapshot: missing "__type" identifier.');
|
|
829
|
+
const handler = this.handlers.get(typeId);
|
|
830
|
+
throwIfEmpty3(handler, `No serializer handler registered for policy ID: '${typeId}'`);
|
|
831
|
+
const { __type, ...data } = snapshot;
|
|
832
|
+
return handler.hydrate(data);
|
|
472
833
|
}
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
// src/serializer/field-serializer.ts
|
|
837
|
+
import { isNullOrUndefined as isNullOrUndefined2, throwIfEmpty as throwIfEmpty4 } from "@axi-engine/utils";
|
|
838
|
+
var FieldSerializer = class {
|
|
473
839
|
/**
|
|
474
|
-
* Creates
|
|
475
|
-
* @param
|
|
476
|
-
* @
|
|
840
|
+
* Creates an instance of FieldSerializer.
|
|
841
|
+
* @param {FieldRegistry} fieldRegistry - A registry that maps string type names to Field constructors.
|
|
842
|
+
* @param {PolicySerializer} policySerializer - A serializer dedicated to handling Policy instances.
|
|
477
843
|
*/
|
|
478
|
-
|
|
479
|
-
|
|
844
|
+
constructor(fieldRegistry, policySerializer) {
|
|
845
|
+
this.fieldRegistry = fieldRegistry;
|
|
846
|
+
this.policySerializer = policySerializer;
|
|
480
847
|
}
|
|
481
848
|
/**
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
* @
|
|
485
|
-
* @
|
|
849
|
+
* Creates a serializable snapshot of a Field instance.
|
|
850
|
+
* The snapshot includes the field's type, name, current value, and the state of all its policies.
|
|
851
|
+
* @param {Field<any>} field - The Field instance to serialize.
|
|
852
|
+
* @returns {FieldSnapshot} A plain object ready for JSON serialization.
|
|
486
853
|
*/
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
854
|
+
snapshot(field) {
|
|
855
|
+
let snapshot = {
|
|
856
|
+
__type: field.typeName,
|
|
857
|
+
name: field.name,
|
|
858
|
+
value: field.value
|
|
859
|
+
};
|
|
860
|
+
if (!field.policies.isEmpty()) {
|
|
861
|
+
const serializedPolicies = [];
|
|
862
|
+
field.policies.items.forEach((policy) => serializedPolicies.push(this.policySerializer.snapshot(policy)));
|
|
863
|
+
snapshot.policies = serializedPolicies;
|
|
493
864
|
}
|
|
494
|
-
return
|
|
865
|
+
return snapshot;
|
|
495
866
|
}
|
|
496
867
|
/**
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
*
|
|
500
|
-
* @param
|
|
501
|
-
* @returns
|
|
868
|
+
* Restores a Field instance from its snapshot representation.
|
|
869
|
+
* It uses the `__type` property to find the correct constructor and hydrates
|
|
870
|
+
* the field with its value and all its policies.
|
|
871
|
+
* @param {FieldSnapshot} snapshot - The plain object snapshot to deserialize.
|
|
872
|
+
* @returns {Field<any>} A new, fully functional Field instance.
|
|
873
|
+
* @throws If the snapshot is invalid, missing a `__type`, or if the type is not registered.
|
|
502
874
|
*/
|
|
503
|
-
|
|
504
|
-
const
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
875
|
+
hydrate(snapshot) {
|
|
876
|
+
const fieldType = snapshot.__type;
|
|
877
|
+
throwIfEmpty4(fieldType, 'Invalid field snapshot: missing "__type" identifier.');
|
|
878
|
+
const Ctor = this.fieldRegistry.get(fieldType);
|
|
879
|
+
let policies;
|
|
880
|
+
if (!isNullOrUndefined2(snapshot.policies)) {
|
|
881
|
+
policies = [];
|
|
882
|
+
snapshot.policies.forEach((p) => policies.push(this.policySerializer.hydrate(p)));
|
|
883
|
+
}
|
|
884
|
+
return new Ctor(snapshot.name, snapshot.value, { policies });
|
|
508
885
|
}
|
|
886
|
+
};
|
|
887
|
+
|
|
888
|
+
// src/serializer/fields-serializer.ts
|
|
889
|
+
var FieldsSerializer = class {
|
|
509
890
|
/**
|
|
510
|
-
* Creates
|
|
511
|
-
* @param
|
|
512
|
-
* @param
|
|
513
|
-
* @returns The newly created `NumberField` instance.
|
|
891
|
+
* Creates an instance of FieldsSerializer.
|
|
892
|
+
* @param {FieldsFactory} fieldsFactory - A registry that maps string type names to Field constructors.
|
|
893
|
+
* @param {FieldSerializer} fieldSerializer - A serializer of field instances.
|
|
514
894
|
*/
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
return this.getFieldsByPath(fullPath).createNumber(fieldName, initialValue);
|
|
895
|
+
constructor(fieldsFactory, fieldSerializer) {
|
|
896
|
+
this.fieldsFactory = fieldsFactory;
|
|
897
|
+
this.fieldSerializer = fieldSerializer;
|
|
519
898
|
}
|
|
520
899
|
/**
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
900
|
+
* Creates a serializable snapshot of a `Fields` container.
|
|
901
|
+
*
|
|
902
|
+
* The snapshot includes a `__type` identifier (currently hardcoded) and an array of snapshots
|
|
903
|
+
* for each `Field` within the container.
|
|
904
|
+
* @param {Fields} fields - The `Fields` instance to serialize.
|
|
905
|
+
* @returns {FieldsSnapshot} A plain object ready for JSON serialization.
|
|
524
906
|
*/
|
|
525
|
-
|
|
526
|
-
const
|
|
527
|
-
|
|
528
|
-
|
|
907
|
+
snapshot(fields) {
|
|
908
|
+
const res = {
|
|
909
|
+
__type: fields.typeName
|
|
910
|
+
};
|
|
911
|
+
fields.fields.forEach((field) => res[field.name] = this.fieldSerializer.snapshot(field));
|
|
912
|
+
return res;
|
|
529
913
|
}
|
|
530
914
|
/**
|
|
531
|
-
*
|
|
532
|
-
*
|
|
533
|
-
*
|
|
915
|
+
* Restores a `Fields` container instance from its snapshot representation.
|
|
916
|
+
*
|
|
917
|
+
* It iterates through the field snapshots and hydrates them individually, adding them to the new container.
|
|
918
|
+
* @param {FieldsSnapshot} snapshot - The plain object snapshot to deserialize.
|
|
919
|
+
* @returns {Fields} A new `DefaultFields` instance populated with the restored fields.
|
|
534
920
|
*/
|
|
535
|
-
|
|
536
|
-
const
|
|
537
|
-
const
|
|
538
|
-
|
|
921
|
+
hydrate(snapshot) {
|
|
922
|
+
const { __type, ...fieldsData } = snapshot;
|
|
923
|
+
const fields = this.fieldsFactory.fields();
|
|
924
|
+
for (const fieldName in fieldsData) {
|
|
925
|
+
const fieldSnapshot = fieldsData[fieldName];
|
|
926
|
+
const restoredField = this.fieldSerializer.hydrate(fieldSnapshot);
|
|
927
|
+
fields.add(restoredField);
|
|
928
|
+
}
|
|
929
|
+
return fields;
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
// src/serializer/field-tree-serializer.ts
|
|
934
|
+
import { isString } from "@axi-engine/utils";
|
|
935
|
+
var FieldTreeSerializer = class {
|
|
936
|
+
constructor(fieldTreeNodeFactory, fieldsSerializer) {
|
|
937
|
+
this.fieldTreeNodeFactory = fieldTreeNodeFactory;
|
|
938
|
+
this.fieldsSerializer = fieldsSerializer;
|
|
539
939
|
}
|
|
540
940
|
/**
|
|
541
941
|
* Creates a serializable snapshot of the entire tree and its contained fields.
|
|
542
942
|
* @returns A plain JavaScript object representing the complete state managed by this tree.
|
|
543
943
|
*/
|
|
544
|
-
snapshot() {
|
|
545
|
-
const
|
|
546
|
-
__type:
|
|
944
|
+
snapshot(tree) {
|
|
945
|
+
const res = {
|
|
946
|
+
__type: tree.typeName
|
|
547
947
|
};
|
|
548
|
-
|
|
549
|
-
|
|
948
|
+
tree.nodes.forEach((node, key) => {
|
|
949
|
+
if (node.typeName === tree.typeName) {
|
|
950
|
+
res[key] = this.snapshot(node);
|
|
951
|
+
} else if (node.typeName === Fields.typeName) {
|
|
952
|
+
res[key] = this.fieldsSerializer.snapshot(node);
|
|
953
|
+
}
|
|
954
|
+
});
|
|
955
|
+
return res;
|
|
550
956
|
}
|
|
551
957
|
/**
|
|
552
958
|
* Restores the state of the tree from a snapshot.
|
|
@@ -554,60 +960,44 @@ var FieldTree = class _FieldTree {
|
|
|
554
960
|
* @param snapshot The snapshot object to load.
|
|
555
961
|
*/
|
|
556
962
|
hydrate(snapshot) {
|
|
557
|
-
|
|
558
|
-
|
|
963
|
+
const { __type, ...nodes } = snapshot;
|
|
964
|
+
const tree = this.fieldTreeNodeFactory.tree();
|
|
965
|
+
for (const key in nodes) {
|
|
966
|
+
const nodeData = nodes[key];
|
|
967
|
+
if (isString(nodeData)) {
|
|
559
968
|
continue;
|
|
560
969
|
}
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
if (type === "Fields" /* fields */) {
|
|
566
|
-
node = this.createFields(key);
|
|
567
|
-
} else if (type === "FieldTree" /* fieldTree */) {
|
|
568
|
-
node = this.createFieldTree(key);
|
|
569
|
-
} else {
|
|
570
|
-
console.warn(`Node '${key}' in snapshot has no __type metadata. Skipping.`);
|
|
571
|
-
}
|
|
970
|
+
if (nodeData.__type === FieldTree.typeName) {
|
|
971
|
+
tree.addNode(key, this.hydrate(nodeData));
|
|
972
|
+
} else if (nodeData.__type === Fields.typeName) {
|
|
973
|
+
tree.addNode(key, this.fieldsSerializer.hydrate(nodeData));
|
|
572
974
|
}
|
|
573
|
-
node?.hydrate(field);
|
|
574
975
|
}
|
|
575
|
-
|
|
576
|
-
/**
|
|
577
|
-
* @private
|
|
578
|
-
* Generic internal method for creating and adding a new node to the tree.
|
|
579
|
-
* @param name The name of the node to create.
|
|
580
|
-
* @param ctor The constructor for the node type (e.g., `FieldTree` or `Fields`).
|
|
581
|
-
* @returns The newly created node instance.
|
|
582
|
-
*/
|
|
583
|
-
createNode(name, ctor) {
|
|
584
|
-
const currentItems = this._items.value;
|
|
585
|
-
throwIf3(currentItems.has(name), `Can't create node with name: '${name}', node already exists`);
|
|
586
|
-
const res = new ctor();
|
|
587
|
-
const newItems = new Map(currentItems);
|
|
588
|
-
newItems.set(name, res);
|
|
589
|
-
this._items.value = newItems;
|
|
590
|
-
this.events.emit("created", {
|
|
591
|
-
type: "created",
|
|
592
|
-
name,
|
|
593
|
-
path: [],
|
|
594
|
-
// todo: need to decide how to pass full path
|
|
595
|
-
node: res
|
|
596
|
-
});
|
|
597
|
-
return res;
|
|
976
|
+
return tree;
|
|
598
977
|
}
|
|
599
978
|
};
|
|
600
979
|
export {
|
|
601
|
-
BaseFields,
|
|
602
980
|
ClampMaxPolicy,
|
|
981
|
+
ClampMaxPolicySerializerHandler,
|
|
603
982
|
ClampMinPolicy,
|
|
983
|
+
ClampMinPolicySerializerHandler,
|
|
604
984
|
ClampPolicy,
|
|
605
|
-
|
|
985
|
+
ClampPolicySerializerHandler,
|
|
986
|
+
DefaultBooleanField,
|
|
987
|
+
DefaultField,
|
|
988
|
+
DefaultFields,
|
|
989
|
+
DefaultFieldsFactory,
|
|
990
|
+
DefaultNumericField,
|
|
991
|
+
DefaultStringField,
|
|
992
|
+
DefaultTreeNodeFactory,
|
|
993
|
+
FieldRegistry,
|
|
994
|
+
FieldSerializer,
|
|
606
995
|
FieldTree,
|
|
996
|
+
FieldTreeSerializer,
|
|
607
997
|
Fields,
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
998
|
+
FieldsSerializer,
|
|
999
|
+
Policies,
|
|
1000
|
+
PolicySerializer,
|
|
611
1001
|
clampMaxPolicy,
|
|
612
1002
|
clampMinPolicy,
|
|
613
1003
|
clampPolicy
|