@axi-engine/fields 0.1.4 → 0.2.0
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 +644 -217
- package/dist/index.d.ts +644 -217
- package/dist/index.js +705 -397
- package/dist/index.mjs +686 -378
- package/package.json +3 -9
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,371 +248,637 @@ 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
|
-
|
|
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);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Clears all registered constructors from the registry.
|
|
306
|
+
*/
|
|
307
|
+
clear() {
|
|
308
|
+
this.items.clear();
|
|
309
|
+
}
|
|
310
|
+
};
|
|
203
311
|
|
|
204
|
-
//
|
|
205
|
-
|
|
206
|
-
var AxiEventEmitter = class extends EventEmitter3 {
|
|
207
|
-
// Currently, we don't need to add any custom logic.
|
|
208
|
-
// The main purpose of this class is to create an abstraction layer.
|
|
312
|
+
// src/field-registry.ts
|
|
313
|
+
var FieldRegistry = class extends ConstructorRegistry {
|
|
209
314
|
};
|
|
210
315
|
|
|
211
|
-
// src/
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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();
|
|
217
338
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
* Avoid to change any data in the map manually.
|
|
339
|
+
* Gets the read-only map of all `Field` instances in this container.
|
|
340
|
+
* @returns {Map<string, Field<any>>} The collection of fields.
|
|
221
341
|
*/
|
|
222
342
|
get fields() {
|
|
223
343
|
return this._fields;
|
|
224
344
|
}
|
|
225
345
|
/**
|
|
226
|
-
*
|
|
227
|
-
* @param
|
|
228
|
-
* @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.
|
|
229
348
|
*/
|
|
230
|
-
|
|
231
|
-
|
|
349
|
+
constructor(fieldRegistry) {
|
|
350
|
+
this._fieldRegistry = fieldRegistry;
|
|
232
351
|
}
|
|
233
352
|
/**
|
|
234
|
-
*
|
|
235
|
-
* @param name The
|
|
236
|
-
* @
|
|
237
|
-
* @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`.
|
|
238
356
|
*/
|
|
239
|
-
|
|
240
|
-
return this.
|
|
357
|
+
has(name) {
|
|
358
|
+
return this._fields.has(name);
|
|
241
359
|
}
|
|
242
360
|
/**
|
|
243
|
-
* Adds a pre-existing `Field` instance to the collection.
|
|
244
|
-
*
|
|
245
|
-
* @param field The `Field` instance to add.
|
|
246
|
-
* @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.
|
|
247
366
|
*/
|
|
248
367
|
add(field) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
this.events.emit("created", {
|
|
254
|
-
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,
|
|
255
372
|
field
|
|
256
373
|
});
|
|
257
374
|
return field;
|
|
258
375
|
}
|
|
259
376
|
/**
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
* @
|
|
263
|
-
* @
|
|
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.
|
|
264
385
|
*/
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
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;
|
|
268
391
|
}
|
|
269
392
|
/**
|
|
270
|
-
*
|
|
271
|
-
* @
|
|
272
|
-
* @param
|
|
273
|
-
* @
|
|
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.
|
|
274
400
|
*/
|
|
275
|
-
upset(name, value) {
|
|
401
|
+
upset(typeName, name, value, options) {
|
|
276
402
|
if (this.has(name)) {
|
|
277
403
|
const field = this.get(name);
|
|
278
|
-
field.
|
|
404
|
+
field.value = value;
|
|
279
405
|
return field;
|
|
280
406
|
}
|
|
281
|
-
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);
|
|
282
419
|
}
|
|
283
420
|
/**
|
|
284
421
|
* Removes one or more fields from the collection.
|
|
285
422
|
* This method ensures that the `destroy` method of each removed field is called to clean up its resources.
|
|
286
|
-
* @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.
|
|
287
424
|
*/
|
|
288
425
|
remove(names) {
|
|
289
426
|
const namesToRemove = Array.isArray(names) ? names : [names];
|
|
290
|
-
const fieldsMap = new Map(this._fields.value);
|
|
291
427
|
const reallyRemoved = namesToRemove.filter((name) => {
|
|
292
|
-
const field =
|
|
428
|
+
const field = this._fields.get(name);
|
|
293
429
|
if (!field) {
|
|
294
430
|
return false;
|
|
295
431
|
}
|
|
296
432
|
field.destroy();
|
|
297
|
-
|
|
298
|
-
return true;
|
|
433
|
+
return this._fields.delete(name);
|
|
299
434
|
});
|
|
300
435
|
if (!reallyRemoved.length) {
|
|
301
436
|
return;
|
|
302
437
|
}
|
|
303
|
-
this.
|
|
304
|
-
this.events.emit("removed", { fieldNames: reallyRemoved });
|
|
438
|
+
this.onRemove.emit({ names: reallyRemoved });
|
|
305
439
|
}
|
|
306
440
|
/**
|
|
307
441
|
* Removes all fields from the collection, ensuring each is properly destroyed.
|
|
308
442
|
*/
|
|
309
443
|
clear() {
|
|
310
|
-
this.remove(Array.from(this._fields.
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Creates a serializable snapshot of the current state of all fields.
|
|
314
|
-
* @returns A plain JavaScript object representing the values of all fields.
|
|
315
|
-
*/
|
|
316
|
-
snapshot() {
|
|
317
|
-
const dump = {
|
|
318
|
-
__type: "Fields" /* fields */
|
|
319
|
-
};
|
|
320
|
-
this._fields.value.forEach((field, key) => dump[key] = field.val);
|
|
321
|
-
return dump;
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Restores the state of the fields from a snapshot.
|
|
325
|
-
* It uses the `upset` logic to create or update fields based on the snapshot data.
|
|
326
|
-
* @param snapshot The snapshot object to load.
|
|
327
|
-
*/
|
|
328
|
-
hydrate(snapshot) {
|
|
329
|
-
for (let key in snapshot) {
|
|
330
|
-
if (key === "__type") {
|
|
331
|
-
continue;
|
|
332
|
-
}
|
|
333
|
-
this.upset(key, snapshot[key]);
|
|
334
|
-
}
|
|
444
|
+
this.remove(Array.from(this._fields.keys()));
|
|
335
445
|
}
|
|
336
446
|
};
|
|
337
447
|
|
|
338
|
-
// src/fields.ts
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
}
|
|
344
|
-
upsetNumber(name, value, options) {
|
|
345
|
-
if (this.has(name)) {
|
|
346
|
-
const field = this.getNumber(name);
|
|
347
|
-
field.set(value);
|
|
348
|
-
return field;
|
|
448
|
+
// src/mixins/with-boolean-fields.mixin.ts
|
|
449
|
+
function WithBooleanFields(Base) {
|
|
450
|
+
return class FieldsWithBoolean extends Base {
|
|
451
|
+
createBoolean(name, initialValue, options) {
|
|
452
|
+
return this.create(DefaultBooleanField.typeName, name, initialValue, options);
|
|
349
453
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
getNumber(name) {
|
|
353
|
-
const field = this.get(name);
|
|
354
|
-
throwIf2(!(field instanceof NumberField), `wrong field type, field ${name} not a instance of NUmberField`);
|
|
355
|
-
return field;
|
|
356
|
-
}
|
|
357
|
-
create(name, initialValue) {
|
|
358
|
-
return this.add(new Field(name, initialValue));
|
|
359
|
-
}
|
|
360
|
-
upset(name, value) {
|
|
361
|
-
if (this.has(name)) {
|
|
362
|
-
const field = this.get(name);
|
|
363
|
-
field.set(value);
|
|
364
|
-
return field;
|
|
454
|
+
upsetBoolean(name, value, options) {
|
|
455
|
+
return this.upset(DefaultBooleanField.typeName, name, value, options);
|
|
365
456
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
};
|
|
457
|
+
getBoolean(name) {
|
|
458
|
+
return this.get(name);
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
373
462
|
|
|
374
|
-
// src/
|
|
375
|
-
|
|
463
|
+
// src/mixins/with-string-fields.mixin.ts
|
|
464
|
+
function WithStringFields(Base) {
|
|
465
|
+
return class FieldsWithString extends Base {
|
|
466
|
+
createString(name, initialValue, options) {
|
|
467
|
+
return this.create(DefaultStringField.typeName, name, initialValue, options);
|
|
468
|
+
}
|
|
469
|
+
upsetString(name, value, options) {
|
|
470
|
+
return this.upset(DefaultStringField.typeName, name, value, options);
|
|
471
|
+
}
|
|
472
|
+
getString(name) {
|
|
473
|
+
return this.get(name);
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// src/mixins/with-numeric-fields.mixin.ts
|
|
479
|
+
function WithNumericFields(Base) {
|
|
480
|
+
return class FieldsWithNumeric extends Base {
|
|
481
|
+
createNumeric(name, initialValue, options) {
|
|
482
|
+
return this.create(DefaultNumericField.typeName, name, initialValue, options);
|
|
483
|
+
}
|
|
484
|
+
upsetNumeric(name, value, options) {
|
|
485
|
+
return this.upset(DefaultNumericField.typeName, name, value, options);
|
|
486
|
+
}
|
|
487
|
+
getNumeric(name) {
|
|
488
|
+
return this.get(name);
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// src/mixins/with-default-fields.mixin.ts
|
|
494
|
+
function WithDefaultFields(Base) {
|
|
495
|
+
return class FieldsWithDefault extends Base {
|
|
496
|
+
createDefault(name, initialValue, options) {
|
|
497
|
+
return this.create(DefaultField.typeName, name, initialValue, options);
|
|
498
|
+
}
|
|
499
|
+
upsetDefault(name, value, options) {
|
|
500
|
+
return this.upset(DefaultField.typeName, name, value, options);
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// src/default-fields.ts
|
|
506
|
+
var DefaultFields = class extends WithBooleanFields(WithStringFields(WithNumericFields(WithDefaultFields(Fields)))) {
|
|
376
507
|
};
|
|
377
508
|
|
|
378
509
|
// src/field-tree.ts
|
|
379
|
-
import {
|
|
380
|
-
import { ensurePathArray, ensurePathString, throwIf as throwIf3, throwIfEmpty } from "@axi-engine/utils";
|
|
510
|
+
import { ensurePathArray, ensurePathString, throwIf as throwIf3, throwIfEmpty as throwIfEmpty2 } from "@axi-engine/utils";
|
|
381
511
|
var FieldTree = class _FieldTree {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
512
|
+
static typeName = "fieldTree";
|
|
513
|
+
typeName = _FieldTree.typeName;
|
|
514
|
+
/** @private The internal map storing child nodes (branches or leaves). */
|
|
515
|
+
_nodes = /* @__PURE__ */ new Map();
|
|
516
|
+
/** @private The factory used to create new child nodes. */
|
|
517
|
+
_factory;
|
|
518
|
+
/**
|
|
519
|
+
* Gets the collection of direct child nodes of this tree branch.
|
|
520
|
+
*/
|
|
521
|
+
get nodes() {
|
|
522
|
+
return this._nodes;
|
|
385
523
|
}
|
|
386
524
|
/**
|
|
387
|
-
*
|
|
388
|
-
*
|
|
525
|
+
* Creates an instance of FieldTree.
|
|
526
|
+
* @param {TreeNodeFactory} factory - A factory responsible for creating new nodes within the tree.
|
|
389
527
|
*/
|
|
390
|
-
|
|
391
|
-
|
|
528
|
+
constructor(factory) {
|
|
529
|
+
this._factory = factory;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Checks if a direct child node with the given name exists.
|
|
533
|
+
* @param {string} name - The name of the direct child node.
|
|
534
|
+
* @returns {boolean} `true` if the node exists, otherwise `false`.
|
|
535
|
+
*/
|
|
536
|
+
has(name) {
|
|
537
|
+
return this._nodes.has(name);
|
|
392
538
|
}
|
|
393
539
|
/**
|
|
394
|
-
* Checks if a
|
|
395
|
-
* @
|
|
540
|
+
* Checks if a node exists at a given path, traversing the tree.
|
|
541
|
+
* @param {PathType} path - The path to check (e.g., 'player/stats' or ['player', 'stats']).
|
|
542
|
+
* @returns {boolean} `true` if the entire path resolves to a node, otherwise `false`.
|
|
396
543
|
*/
|
|
397
544
|
hasPath(path) {
|
|
398
|
-
const
|
|
399
|
-
|
|
400
|
-
for (let i = 0; i < pathParts.length; i++) {
|
|
401
|
-
const part = pathParts[i];
|
|
402
|
-
const nextNode = currentNode._items.value.get(part);
|
|
403
|
-
if (!nextNode) {
|
|
404
|
-
return false;
|
|
405
|
-
}
|
|
406
|
-
if (nextNode instanceof BaseFields) {
|
|
407
|
-
if (i === pathParts.length - 1) {
|
|
408
|
-
return true;
|
|
409
|
-
}
|
|
410
|
-
throwIf3(
|
|
411
|
-
pathParts.length - i > 2,
|
|
412
|
-
`Path validation failed, full path: ${ensurePathString(path)}, has extra nodes after Fields placed at: ${ensurePathString(pathParts.slice(0, i + 1))}`
|
|
413
|
-
);
|
|
414
|
-
return nextNode.has(pathParts[i + 1]);
|
|
415
|
-
}
|
|
416
|
-
currentNode = nextNode;
|
|
417
|
-
}
|
|
418
|
-
return true;
|
|
545
|
+
const traversedPath = this.traversePath(path);
|
|
546
|
+
return traversedPath.branch.has(traversedPath.leafName);
|
|
419
547
|
}
|
|
420
548
|
/**
|
|
421
|
-
*
|
|
422
|
-
* @param name The name
|
|
423
|
-
* @
|
|
424
|
-
* @
|
|
549
|
+
* Adds a pre-existing node as a direct child of this tree branch.
|
|
550
|
+
* @param {string} name - The name to assign to the new child node.
|
|
551
|
+
* @param {TreeOrFieldsNode} node - The node instance to add.
|
|
552
|
+
* @returns {TreeOrFieldsNode} The added node.
|
|
553
|
+
* @throws If a node with the same name already exists.
|
|
425
554
|
*/
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
555
|
+
addNode(name, node) {
|
|
556
|
+
throwIf3(this.has(name), `Can't add node with name: '${name}', node already exists`);
|
|
557
|
+
this._nodes.set(name, node);
|
|
429
558
|
return node;
|
|
430
559
|
}
|
|
431
560
|
/**
|
|
432
|
-
* Retrieves a child node
|
|
433
|
-
* @param name The name of the child node.
|
|
434
|
-
* @returns The
|
|
435
|
-
* @throws If
|
|
561
|
+
* Retrieves a direct child node by its name.
|
|
562
|
+
* @param {string} name - The name of the child node.
|
|
563
|
+
* @returns {TreeOrFieldsNode} The retrieved node.
|
|
564
|
+
* @throws If a node with the given name cannot be found.
|
|
436
565
|
*/
|
|
437
|
-
|
|
438
|
-
const node = this.
|
|
439
|
-
|
|
566
|
+
getNode(name) {
|
|
567
|
+
const node = this._nodes.get(name);
|
|
568
|
+
throwIfEmpty2(node, `Can't find node with name '${name}'`);
|
|
440
569
|
return node;
|
|
441
570
|
}
|
|
442
571
|
/**
|
|
443
|
-
*
|
|
444
|
-
* @param
|
|
445
|
-
* @
|
|
446
|
-
* @
|
|
572
|
+
* Creates a new `FieldTree` (branch) node at the specified path.
|
|
573
|
+
* @param {PathType} path - The path where the new `FieldTree` should be created.
|
|
574
|
+
* @param {boolean} [createPath=false] - If `true`, any missing parent branches in the path will be created automatically.
|
|
575
|
+
* @returns {FieldTree} The newly created `FieldTree` instance.
|
|
576
|
+
* @throws If the path is invalid or a node already exists at the target location.
|
|
447
577
|
*/
|
|
448
|
-
|
|
449
|
-
const
|
|
450
|
-
|
|
578
|
+
createFieldTree(path, createPath) {
|
|
579
|
+
const traversedPath = this.traversePath(path, createPath);
|
|
580
|
+
return traversedPath.branch.addNode(traversedPath.leafName, this._factory.tree());
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Creates a new `Fields` (leaf) container at the specified path.
|
|
584
|
+
* @param {PathType} path - The path where the new `Fields` container should be created.
|
|
585
|
+
* @param {boolean} [createPath=false] - If `true`, any missing parent branches in the path will be created automatically.
|
|
586
|
+
* @returns {Fields} The newly created `Fields` instance.
|
|
587
|
+
* @throws If the path is invalid or a node already exists at the target location.
|
|
588
|
+
*/
|
|
589
|
+
createFields(path, createPath) {
|
|
590
|
+
const traversedPath = this.traversePath(path, createPath);
|
|
591
|
+
return traversedPath.branch.addNode(traversedPath.leafName, this._factory.fields());
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Retrieves a `FieldTree` (branch) node from a specified path.
|
|
595
|
+
* @param {PathType} path - The path to the `FieldTree` node.
|
|
596
|
+
* @returns {FieldTree} The `FieldTree` instance at the specified path.
|
|
597
|
+
* @throws If the path is invalid or the node at the path is not a `FieldTree`.
|
|
598
|
+
*/
|
|
599
|
+
getFieldTree(path) {
|
|
600
|
+
const traversedPath = this.traversePath(path);
|
|
601
|
+
const node = traversedPath.branch.getNode(traversedPath.leafName);
|
|
602
|
+
throwIf3(
|
|
603
|
+
!(node instanceof _FieldTree),
|
|
604
|
+
`Node with name: ${traversedPath.leafName} by path: '${ensurePathString(path)}' should be instance of FieldTree`
|
|
605
|
+
);
|
|
451
606
|
return node;
|
|
452
607
|
}
|
|
453
608
|
/**
|
|
454
|
-
* Retrieves a
|
|
455
|
-
* @param
|
|
456
|
-
* @returns The
|
|
457
|
-
* @throws If
|
|
609
|
+
* Retrieves a `Fields` (leaf) container from a specified path.
|
|
610
|
+
* @param {PathType} path - The path to the `Fields` container.
|
|
611
|
+
* @returns {Fields} The `Fields` instance at the specified path.
|
|
612
|
+
* @throws If the path is invalid or the node at the path is not a `Fields` container.
|
|
458
613
|
*/
|
|
459
|
-
|
|
460
|
-
const
|
|
461
|
-
|
|
614
|
+
getFields(path) {
|
|
615
|
+
const traversedPath = this.traversePath(path);
|
|
616
|
+
const node = traversedPath.branch.getNode(traversedPath.leafName);
|
|
617
|
+
throwIf3(
|
|
618
|
+
!(node instanceof Fields),
|
|
619
|
+
`Node with name: ${traversedPath.leafName} by path: '${ensurePathString(path)}' should be instance of Fields`
|
|
620
|
+
);
|
|
462
621
|
return node;
|
|
463
622
|
}
|
|
464
623
|
/**
|
|
465
|
-
*
|
|
466
|
-
* @param
|
|
467
|
-
* @returns The newly created `FieldTree` instance.
|
|
624
|
+
* Retrieves a `FieldTree` at the specified path. If it or any part of the path doesn't exist, it will be created.
|
|
625
|
+
* @param {PathType} path - The path to the `FieldTree` node.
|
|
626
|
+
* @returns {FieldTree} The existing or newly created `FieldTree` instance.
|
|
468
627
|
*/
|
|
469
|
-
|
|
470
|
-
|
|
628
|
+
getOrCreateFieldTree(path) {
|
|
629
|
+
const traversedPath = this.traversePath(path, true);
|
|
630
|
+
return traversedPath.branch.has(traversedPath.leafName) ? traversedPath.branch.getFieldTree(traversedPath.leafName) : traversedPath.branch.createFieldTree(traversedPath.leafName);
|
|
471
631
|
}
|
|
472
632
|
/**
|
|
473
|
-
*
|
|
474
|
-
* @param
|
|
475
|
-
* @returns The newly created `Fields` instance.
|
|
633
|
+
* Retrieves a `Fields` container at the specified path. If it or any part of the path doesn't exist, it will be created.
|
|
634
|
+
* @param {PathType} path - The path to the `Fields` container.
|
|
635
|
+
* @returns {Fields} The existing or newly created `Fields` instance.
|
|
476
636
|
*/
|
|
477
|
-
|
|
478
|
-
|
|
637
|
+
getOrCreateFields(path) {
|
|
638
|
+
const traversedPath = this.traversePath(path, true);
|
|
639
|
+
return traversedPath.branch.has(traversedPath.leafName) ? traversedPath.branch.getFields(traversedPath.leafName) : traversedPath.branch.createFields(traversedPath.leafName);
|
|
479
640
|
}
|
|
480
641
|
/**
|
|
481
|
-
*
|
|
482
|
-
*
|
|
483
|
-
*
|
|
642
|
+
* @private
|
|
643
|
+
* Navigates the tree to the parent of a target node.
|
|
644
|
+
* This is the core traversal logic for all path-based operations.
|
|
645
|
+
* @param {PathType} path - The full path to the target node.
|
|
646
|
+
* @param {boolean} [createPath=false] - If `true`, creates missing `FieldTree` branches along the path.
|
|
647
|
+
* @returns {{branch: FieldTree, leafName: string}} An object containing the final branch (parent node) and the name of the leaf (target node).
|
|
648
|
+
* @throws If the path is empty, invalid, or contains a `Fields` container as an intermediate segment.
|
|
649
|
+
*/
|
|
650
|
+
traversePath(path, createPath) {
|
|
651
|
+
const pathArr = ensurePathArray(path);
|
|
652
|
+
throwIfEmpty2(pathArr, "The path is empty");
|
|
653
|
+
const leafName = pathArr.pop();
|
|
654
|
+
let currentNode = this;
|
|
655
|
+
for (const pathPart of pathArr) {
|
|
656
|
+
let node;
|
|
657
|
+
if (currentNode.has(pathPart)) {
|
|
658
|
+
node = currentNode.getNode(pathPart);
|
|
659
|
+
} else {
|
|
660
|
+
if (createPath) {
|
|
661
|
+
node = currentNode.createFieldTree(pathPart);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
throwIfEmpty2(node, `Can't find node with name ${pathPart} by path parsing: ${ensurePathString(path)}`);
|
|
665
|
+
throwIf3(node instanceof Fields, `Node with name ${pathPart} should be instance of FieldTree`);
|
|
666
|
+
currentNode = node;
|
|
667
|
+
}
|
|
668
|
+
return { branch: currentNode, leafName };
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
|
|
672
|
+
// src/field-tree-node-factory.ts
|
|
673
|
+
var DefaultTreeNodeFactory = class {
|
|
674
|
+
constructor(fieldRegistry) {
|
|
675
|
+
this.fieldRegistry = fieldRegistry;
|
|
676
|
+
}
|
|
677
|
+
fields = () => new DefaultFields(this.fieldRegistry);
|
|
678
|
+
tree = () => new FieldTree(this);
|
|
679
|
+
};
|
|
680
|
+
|
|
681
|
+
// src/serializer/policies/clamp-policy-serializer-handler.ts
|
|
682
|
+
var ClampPolicySerializerHandler = class {
|
|
683
|
+
snapshot(policy) {
|
|
684
|
+
return { min: policy.min, max: policy.max };
|
|
685
|
+
}
|
|
686
|
+
hydrate(data) {
|
|
687
|
+
return new ClampPolicy(data.min, data.max);
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
// src/serializer/policies/clamp-max-policy-serializer-handler.ts
|
|
692
|
+
var ClampMaxPolicySerializerHandler = class {
|
|
693
|
+
snapshot(policy) {
|
|
694
|
+
return { max: policy.max };
|
|
695
|
+
}
|
|
696
|
+
hydrate(data) {
|
|
697
|
+
return new ClampMaxPolicy(data.max);
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
// src/serializer/policies/clamp-min-policy-serializer-handler.ts
|
|
702
|
+
var ClampMinPolicySerializerHandler = class {
|
|
703
|
+
snapshot(policy) {
|
|
704
|
+
return { min: policy.min };
|
|
705
|
+
}
|
|
706
|
+
hydrate(data) {
|
|
707
|
+
return new ClampMinPolicy(data.min);
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
|
|
711
|
+
// src/serializer/policy-serializer.ts
|
|
712
|
+
import { throwIf as throwIf4, throwIfEmpty as throwIfEmpty3 } from "@axi-engine/utils";
|
|
713
|
+
var PolicySerializer = class {
|
|
714
|
+
handlers = /* @__PURE__ */ new Map();
|
|
715
|
+
register(policyId, handler) {
|
|
716
|
+
throwIf4(this.handlers.has(policyId), `A handler for policy ID '${policyId}' is already registered.`);
|
|
717
|
+
this.handlers.set(policyId, handler);
|
|
718
|
+
return this;
|
|
719
|
+
}
|
|
720
|
+
clearHandlers() {
|
|
721
|
+
this.handlers.clear();
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Creates a serializable snapshot of a policy instance.
|
|
725
|
+
* The snapshot includes the policy's state and a `__type` identifier.
|
|
726
|
+
* @param policy The policy instance to snapshot.
|
|
727
|
+
* @returns A plain object ready for JSON serialization.
|
|
728
|
+
* @throws If no handler is registered for the policy's ID.
|
|
729
|
+
*/
|
|
730
|
+
snapshot(policy) {
|
|
731
|
+
const handler = this.handlers.get(policy.id);
|
|
732
|
+
throwIfEmpty3(handler, `No serializer handler registered for policy ID: '${policy.id}'`);
|
|
733
|
+
const data = handler.snapshot(policy);
|
|
734
|
+
return {
|
|
735
|
+
__type: policy.id,
|
|
736
|
+
...data
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* Restores a policy instance from its snapshot representation.
|
|
741
|
+
* @param snapshot The plain object snapshot, which must contain a `__type` property.
|
|
742
|
+
* @returns A new, fully functional policy instance.
|
|
743
|
+
* @throws If the snapshot is invalid or no handler is registered for its `__type`.
|
|
744
|
+
*/
|
|
745
|
+
hydrate(snapshot) {
|
|
746
|
+
const typeId = snapshot?.__type;
|
|
747
|
+
throwIfEmpty3(typeId, 'Invalid policy snapshot: missing "__type" identifier.');
|
|
748
|
+
const handler = this.handlers.get(typeId);
|
|
749
|
+
throwIfEmpty3(handler, `No serializer handler registered for policy ID: '${typeId}'`);
|
|
750
|
+
const { __type, ...data } = snapshot;
|
|
751
|
+
return handler.hydrate(data);
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
// src/serializer/field-serializer.ts
|
|
756
|
+
import { isNullOrUndefined as isNullOrUndefined2, throwIfEmpty as throwIfEmpty4 } from "@axi-engine/utils";
|
|
757
|
+
var FieldSerializer = class {
|
|
758
|
+
/**
|
|
759
|
+
* Creates an instance of FieldSerializer.
|
|
760
|
+
* @param {FieldRegistry} fieldRegistry - A registry that maps string type names to Field constructors.
|
|
761
|
+
* @param {PolicySerializer} policySerializer - A serializer dedicated to handling Policy instances.
|
|
484
762
|
*/
|
|
485
|
-
|
|
486
|
-
|
|
763
|
+
constructor(fieldRegistry, policySerializer) {
|
|
764
|
+
this.fieldRegistry = fieldRegistry;
|
|
765
|
+
this.policySerializer = policySerializer;
|
|
487
766
|
}
|
|
488
767
|
/**
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
* @
|
|
492
|
-
* @
|
|
768
|
+
* Creates a serializable snapshot of a Field instance.
|
|
769
|
+
* The snapshot includes the field's type, name, current value, and the state of all its policies.
|
|
770
|
+
* @param {Field<any>} field - The Field instance to serialize.
|
|
771
|
+
* @returns {FieldSnapshot} A plain object ready for JSON serialization.
|
|
493
772
|
*/
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
773
|
+
snapshot(field) {
|
|
774
|
+
let snapshot = {
|
|
775
|
+
__type: field.typeName,
|
|
776
|
+
name: field.name,
|
|
777
|
+
value: field.value
|
|
778
|
+
};
|
|
779
|
+
if (!field.policies.isEmpty()) {
|
|
780
|
+
const serializedPolicies = [];
|
|
781
|
+
field.policies.items.forEach((policy) => serializedPolicies.push(this.policySerializer.snapshot(policy)));
|
|
782
|
+
snapshot.policies = serializedPolicies;
|
|
500
783
|
}
|
|
501
|
-
return
|
|
784
|
+
return snapshot;
|
|
502
785
|
}
|
|
503
786
|
/**
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
* @param
|
|
508
|
-
* @returns
|
|
787
|
+
* Restores a Field instance from its snapshot representation.
|
|
788
|
+
* It uses the `__type` property to find the correct constructor and hydrates
|
|
789
|
+
* the field with its value and all its policies.
|
|
790
|
+
* @param {FieldSnapshot} snapshot - The plain object snapshot to deserialize.
|
|
791
|
+
* @returns {Field<any>} A new, fully functional Field instance.
|
|
792
|
+
* @throws If the snapshot is invalid, missing a `__type`, or if the type is not registered.
|
|
509
793
|
*/
|
|
510
|
-
|
|
511
|
-
const
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
794
|
+
hydrate(snapshot) {
|
|
795
|
+
const fieldType = snapshot.__type;
|
|
796
|
+
throwIfEmpty4(fieldType, 'Invalid field snapshot: missing "__type" identifier.');
|
|
797
|
+
const Ctor = this.fieldRegistry.get(fieldType);
|
|
798
|
+
let policies;
|
|
799
|
+
if (!isNullOrUndefined2(snapshot.policies)) {
|
|
800
|
+
policies = [];
|
|
801
|
+
snapshot.policies.forEach((p) => policies.push(this.policySerializer.hydrate(p)));
|
|
802
|
+
}
|
|
803
|
+
return new Ctor(snapshot.name, snapshot.value, { policies });
|
|
515
804
|
}
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
// src/serializer/fields-serializer.ts
|
|
808
|
+
var FieldsSerializer = class {
|
|
516
809
|
/**
|
|
517
|
-
* Creates
|
|
518
|
-
* @param
|
|
519
|
-
* @param
|
|
520
|
-
* @returns The newly created `NumberField` instance.
|
|
810
|
+
* Creates an instance of FieldsSerializer.
|
|
811
|
+
* @param {FieldRegistry} fieldRegistry - A registry that maps string type names to Field constructors.
|
|
812
|
+
* @param {PolicySerializer} policySerializer - A serializer dedicated to handling Policy instances.
|
|
521
813
|
*/
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
814
|
+
constructor(fieldRegistry, policySerializer) {
|
|
815
|
+
this.fieldRegistry = fieldRegistry;
|
|
816
|
+
this.policySerializer = policySerializer;
|
|
817
|
+
this.fieldSerializer = new FieldSerializer(this.fieldRegistry, this.policySerializer);
|
|
526
818
|
}
|
|
527
819
|
/**
|
|
528
|
-
*
|
|
529
|
-
* @
|
|
530
|
-
|
|
820
|
+
* An internal instance of FieldSerializer to handle individual fields.
|
|
821
|
+
* @private
|
|
822
|
+
*/
|
|
823
|
+
fieldSerializer;
|
|
824
|
+
/**
|
|
825
|
+
* Creates a serializable snapshot of a `Fields` container.
|
|
826
|
+
*
|
|
827
|
+
* The snapshot includes a `__type` identifier (currently hardcoded) and an array of snapshots
|
|
828
|
+
* for each `Field` within the container.
|
|
829
|
+
* @param {Fields} fields - The `Fields` instance to serialize.
|
|
830
|
+
* @returns {FieldsSnapshot} A plain object ready for JSON serialization.
|
|
531
831
|
*/
|
|
532
|
-
|
|
533
|
-
const
|
|
534
|
-
|
|
535
|
-
|
|
832
|
+
snapshot(fields) {
|
|
833
|
+
const res = {
|
|
834
|
+
__type: fields.typeName
|
|
835
|
+
};
|
|
836
|
+
fields.fields.forEach((field) => res[field.name] = this.fieldSerializer.snapshot(field));
|
|
837
|
+
return res;
|
|
536
838
|
}
|
|
537
839
|
/**
|
|
538
|
-
*
|
|
539
|
-
*
|
|
540
|
-
*
|
|
840
|
+
* Restores a `Fields` container instance from its snapshot representation.
|
|
841
|
+
*
|
|
842
|
+
* **Limitation:** This method is currently hardcoded to always create an instance of `DefaultFields`.
|
|
843
|
+
* It iterates through the field snapshots and hydrates them individually, adding them to the new container.
|
|
844
|
+
* @param {FieldsSnapshot} snapshot - The plain object snapshot to deserialize.
|
|
845
|
+
* @returns {DefaultFields} A new `DefaultFields` instance populated with the restored fields.
|
|
541
846
|
*/
|
|
542
|
-
|
|
543
|
-
const
|
|
544
|
-
const
|
|
545
|
-
|
|
847
|
+
hydrate(snapshot) {
|
|
848
|
+
const { __type, ...fieldsData } = snapshot;
|
|
849
|
+
const fields = new DefaultFields(this.fieldRegistry);
|
|
850
|
+
for (const fieldName in fieldsData) {
|
|
851
|
+
const fieldSnapshot = fieldsData[fieldName];
|
|
852
|
+
const restoredField = this.fieldSerializer.hydrate(fieldSnapshot);
|
|
853
|
+
fields.add(restoredField);
|
|
854
|
+
}
|
|
855
|
+
return fields;
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
// src/serializer/field-tree-serializer.ts
|
|
860
|
+
import { isString } from "@axi-engine/utils";
|
|
861
|
+
var FieldTreeSerializer = class {
|
|
862
|
+
constructor(fieldTreeNodeFactory, fieldsSerializer) {
|
|
863
|
+
this.fieldTreeNodeFactory = fieldTreeNodeFactory;
|
|
864
|
+
this.fieldsSerializer = fieldsSerializer;
|
|
546
865
|
}
|
|
547
866
|
/**
|
|
548
867
|
* Creates a serializable snapshot of the entire tree and its contained fields.
|
|
549
868
|
* @returns A plain JavaScript object representing the complete state managed by this tree.
|
|
550
869
|
*/
|
|
551
|
-
snapshot() {
|
|
552
|
-
const
|
|
553
|
-
__type:
|
|
870
|
+
snapshot(tree) {
|
|
871
|
+
const res = {
|
|
872
|
+
__type: tree.typeName
|
|
554
873
|
};
|
|
555
|
-
|
|
556
|
-
|
|
874
|
+
tree.nodes.forEach((node, key) => {
|
|
875
|
+
if (node.typeName === tree.typeName) {
|
|
876
|
+
res[key] = this.snapshot(node);
|
|
877
|
+
} else if (node.typeName === Fields.typeName) {
|
|
878
|
+
res[key] = this.fieldsSerializer.snapshot(node);
|
|
879
|
+
}
|
|
880
|
+
});
|
|
881
|
+
return res;
|
|
557
882
|
}
|
|
558
883
|
/**
|
|
559
884
|
* Restores the state of the tree from a snapshot.
|
|
@@ -561,60 +886,43 @@ var FieldTree = class _FieldTree {
|
|
|
561
886
|
* @param snapshot The snapshot object to load.
|
|
562
887
|
*/
|
|
563
888
|
hydrate(snapshot) {
|
|
564
|
-
|
|
565
|
-
|
|
889
|
+
const { __type, ...nodes } = snapshot;
|
|
890
|
+
const tree = this.fieldTreeNodeFactory.tree();
|
|
891
|
+
for (const key in nodes) {
|
|
892
|
+
const nodeData = nodes[key];
|
|
893
|
+
if (isString(nodeData)) {
|
|
566
894
|
continue;
|
|
567
895
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
if (type === "Fields" /* fields */) {
|
|
573
|
-
node = this.createFields(key);
|
|
574
|
-
} else if (type === "FieldTree" /* fieldTree */) {
|
|
575
|
-
node = this.createFieldTree(key);
|
|
576
|
-
} else {
|
|
577
|
-
console.warn(`Node '${key}' in snapshot has no __type metadata. Skipping.`);
|
|
578
|
-
}
|
|
896
|
+
if (nodeData.__type === FieldTree.typeName) {
|
|
897
|
+
tree.addNode(key, this.hydrate(nodeData));
|
|
898
|
+
} else {
|
|
899
|
+
tree.addNode(key, this.fieldsSerializer.hydrate(nodeData));
|
|
579
900
|
}
|
|
580
|
-
node?.hydrate(field);
|
|
581
901
|
}
|
|
582
|
-
|
|
583
|
-
/**
|
|
584
|
-
* @private
|
|
585
|
-
* Generic internal method for creating and adding a new node to the tree.
|
|
586
|
-
* @param name The name of the node to create.
|
|
587
|
-
* @param ctor The constructor for the node type (e.g., `FieldTree` or `Fields`).
|
|
588
|
-
* @returns The newly created node instance.
|
|
589
|
-
*/
|
|
590
|
-
createNode(name, ctor) {
|
|
591
|
-
const currentItems = this._items.value;
|
|
592
|
-
throwIf3(currentItems.has(name), `Can't create node with name: '${name}', node already exists`);
|
|
593
|
-
const res = new ctor();
|
|
594
|
-
const newItems = new Map(currentItems);
|
|
595
|
-
newItems.set(name, res);
|
|
596
|
-
this._items.value = newItems;
|
|
597
|
-
this.events.emit("created", {
|
|
598
|
-
type: "created",
|
|
599
|
-
name,
|
|
600
|
-
path: [],
|
|
601
|
-
// todo: need to decide how to pass full path
|
|
602
|
-
node: res
|
|
603
|
-
});
|
|
604
|
-
return res;
|
|
902
|
+
return tree;
|
|
605
903
|
}
|
|
606
904
|
};
|
|
607
905
|
export {
|
|
608
|
-
BaseFields,
|
|
609
906
|
ClampMaxPolicy,
|
|
907
|
+
ClampMaxPolicySerializerHandler,
|
|
610
908
|
ClampMinPolicy,
|
|
909
|
+
ClampMinPolicySerializerHandler,
|
|
611
910
|
ClampPolicy,
|
|
612
|
-
|
|
911
|
+
ClampPolicySerializerHandler,
|
|
912
|
+
DefaultBooleanField,
|
|
913
|
+
DefaultField,
|
|
914
|
+
DefaultFields,
|
|
915
|
+
DefaultNumericField,
|
|
916
|
+
DefaultStringField,
|
|
917
|
+
DefaultTreeNodeFactory,
|
|
918
|
+
FieldRegistry,
|
|
919
|
+
FieldSerializer,
|
|
613
920
|
FieldTree,
|
|
921
|
+
FieldTreeSerializer,
|
|
614
922
|
Fields,
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
923
|
+
FieldsSerializer,
|
|
924
|
+
Policies,
|
|
925
|
+
PolicySerializer,
|
|
618
926
|
clampMaxPolicy,
|
|
619
927
|
clampMinPolicy,
|
|
620
928
|
clampPolicy
|