@voidhash/mimic 0.0.1-alpha.5 → 0.0.1-alpha.7
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/.turbo/turbo-build.log +13 -13
- package/dist/index.cjs +140 -46
- package/dist/index.d.cts +229 -115
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +229 -115
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +140 -46
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/client/ClientDocument.ts +1 -1
- package/src/client/errors.ts +10 -10
- package/src/primitives/Array.ts +45 -21
- package/src/primitives/Boolean.ts +8 -6
- package/src/primitives/Either.ts +13 -11
- package/src/primitives/Literal.ts +7 -5
- package/src/primitives/Number.ts +13 -11
- package/src/primitives/String.ts +14 -12
- package/src/primitives/Struct.ts +66 -17
- package/src/primitives/Tree.ts +103 -32
- package/src/primitives/TreeNode.ts +49 -27
- package/src/primitives/Union.ts +44 -17
- package/src/primitives/shared.ts +106 -5
- package/src/server/errors.ts +6 -6
- package/tests/primitives/Struct.test.ts +250 -0
- package/tests/primitives/Tree.test.ts +122 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @voidhash/mimic@0.0.1-alpha.
|
|
2
|
+
> @voidhash/mimic@0.0.1-alpha.7 build /home/runner/work/mimic/mimic/packages/mimic
|
|
3
3
|
> tsdown
|
|
4
4
|
|
|
5
5
|
[34mℹ[39m tsdown [2mv0.18.2[22m powered by rolldown [2mv1.0.0-beta.55[22m
|
|
@@ -8,16 +8,16 @@
|
|
|
8
8
|
[34mℹ[39m target: [34mes2017[39m
|
|
9
9
|
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
10
10
|
[34mℹ[39m Build start
|
|
11
|
-
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mindex.cjs[22m [
|
|
12
|
-
[34mℹ[39m [33m[CJS][39m 1 files, total:
|
|
13
|
-
[34mℹ[39m [33m[CJS][39m [2mdist/[22mindex.d.cts.map [
|
|
14
|
-
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mindex.d.cts[22m[39m [
|
|
15
|
-
[34mℹ[39m [33m[CJS][39m 2 files, total:
|
|
16
|
-
[32m✔[39m Build complete in [
|
|
17
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mindex.mjs[22m [
|
|
18
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.mjs.map [
|
|
19
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.d.mts.map [2m
|
|
11
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mindex.cjs[22m [2m109.50 kB[22m [2m│ gzip: 18.89 kB[22m
|
|
12
|
+
[34mℹ[39m [33m[CJS][39m 1 files, total: 109.50 kB
|
|
13
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mindex.d.cts.map [2m21.02 kB[22m [2m│ gzip: 8.61 kB[22m
|
|
14
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mindex.d.cts[22m[39m [2m55.20 kB[22m [2m│ gzip: 11.08 kB[22m
|
|
15
|
+
[34mℹ[39m [33m[CJS][39m 2 files, total: 76.22 kB
|
|
16
|
+
[32m✔[39m Build complete in [32m5517ms[39m
|
|
17
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mindex.mjs[22m [2m106.90 kB[22m [2m│ gzip: 18.40 kB[22m
|
|
18
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.mjs.map [2m269.06 kB[22m [2m│ gzip: 48.01 kB[22m
|
|
19
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.d.mts.map [2m 26.03 kB[22m [2m│ gzip: 10.67 kB[22m
|
|
20
20
|
[34mℹ[39m [34m[ESM][39m [2mdist/[22mchunk-C6wwvPpM.mjs [2m 0.36 kB[22m [2m│ gzip: 0.25 kB[22m
|
|
21
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m
|
|
22
|
-
[34mℹ[39m [34m[ESM][39m 5 files, total:
|
|
23
|
-
[32m✔[39m Build complete in [
|
|
21
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 55.21 kB[22m [2m│ gzip: 11.07 kB[22m
|
|
22
|
+
[34mℹ[39m [34m[ESM][39m 5 files, total: 457.56 kB
|
|
23
|
+
[32m✔[39m Build complete in [32m5525ms[39m
|
package/dist/index.cjs
CHANGED
|
@@ -83,6 +83,30 @@ function _defineProperty(e, r, t) {
|
|
|
83
83
|
}) : e[r] = t, e;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region \0@oxc-project+runtime@0.103.0/helpers/objectSpread2.js
|
|
88
|
+
function ownKeys(e, r) {
|
|
89
|
+
var t = Object.keys(e);
|
|
90
|
+
if (Object.getOwnPropertySymbols) {
|
|
91
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
92
|
+
r && (o = o.filter(function(r$1) {
|
|
93
|
+
return Object.getOwnPropertyDescriptor(e, r$1).enumerable;
|
|
94
|
+
})), t.push.apply(t, o);
|
|
95
|
+
}
|
|
96
|
+
return t;
|
|
97
|
+
}
|
|
98
|
+
function _objectSpread2(e) {
|
|
99
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
100
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
101
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function(r$1) {
|
|
102
|
+
_defineProperty(e, r$1, t[r$1]);
|
|
103
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r$1) {
|
|
104
|
+
Object.defineProperty(e, r$1, Object.getOwnPropertyDescriptor(t, r$1));
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return e;
|
|
108
|
+
}
|
|
109
|
+
|
|
86
110
|
//#endregion
|
|
87
111
|
//#region src/primitives/shared.ts
|
|
88
112
|
var ValidationError = class extends Error {
|
|
@@ -107,6 +131,34 @@ function runValidators(value, validators) {
|
|
|
107
131
|
function isCompatibleOperation(operation, operationDefinitions) {
|
|
108
132
|
return Object.values(operationDefinitions).some((value) => value.kind === operation.kind);
|
|
109
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Applies default values to a partial input, recursively handling nested structs.
|
|
136
|
+
*
|
|
137
|
+
* Uses a two-layer approach:
|
|
138
|
+
* 1. First, get the struct's initial state (which includes struct-level defaults)
|
|
139
|
+
* 2. Then, layer the provided values on top
|
|
140
|
+
* 3. Finally, ensure nested structs are recursively processed
|
|
141
|
+
*
|
|
142
|
+
* @param primitive - The primitive definition containing field information
|
|
143
|
+
* @param value - The partial value provided by the user
|
|
144
|
+
* @returns The value with defaults applied for missing fields
|
|
145
|
+
*/
|
|
146
|
+
function applyDefaults(primitive, value) {
|
|
147
|
+
if (primitive._tag === "StructPrimitive") {
|
|
148
|
+
var _structPrimitive$_int;
|
|
149
|
+
const structPrimitive = primitive;
|
|
150
|
+
const result = _objectSpread2(_objectSpread2({}, (_structPrimitive$_int = structPrimitive._internal.getInitialState()) !== null && _structPrimitive$_int !== void 0 ? _structPrimitive$_int : {}), value);
|
|
151
|
+
for (const key in structPrimitive.fields) {
|
|
152
|
+
const fieldPrimitive = structPrimitive.fields[key];
|
|
153
|
+
if (result[key] === void 0) {
|
|
154
|
+
const fieldDefault = fieldPrimitive._internal.getInitialState();
|
|
155
|
+
if (fieldDefault !== void 0) result[key] = fieldDefault;
|
|
156
|
+
} else if (fieldPrimitive._tag === "StructPrimitive" && typeof result[key] === "object" && result[key] !== null) result[key] = applyDefaults(fieldPrimitive, result[key]);
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
return value;
|
|
161
|
+
}
|
|
110
162
|
|
|
111
163
|
//#endregion
|
|
112
164
|
//#region src/OperationDefinition.ts
|
|
@@ -294,30 +346,6 @@ const decode$1 = (encoded) => {
|
|
|
294
346
|
};
|
|
295
347
|
};
|
|
296
348
|
|
|
297
|
-
//#endregion
|
|
298
|
-
//#region \0@oxc-project+runtime@0.103.0/helpers/objectSpread2.js
|
|
299
|
-
function ownKeys(e, r) {
|
|
300
|
-
var t = Object.keys(e);
|
|
301
|
-
if (Object.getOwnPropertySymbols) {
|
|
302
|
-
var o = Object.getOwnPropertySymbols(e);
|
|
303
|
-
r && (o = o.filter(function(r$1) {
|
|
304
|
-
return Object.getOwnPropertyDescriptor(e, r$1).enumerable;
|
|
305
|
-
})), t.push.apply(t, o);
|
|
306
|
-
}
|
|
307
|
-
return t;
|
|
308
|
-
}
|
|
309
|
-
function _objectSpread2(e) {
|
|
310
|
-
for (var r = 1; r < arguments.length; r++) {
|
|
311
|
-
var t = null != arguments[r] ? arguments[r] : {};
|
|
312
|
-
r % 2 ? ownKeys(Object(t), !0).forEach(function(r$1) {
|
|
313
|
-
_defineProperty(e, r$1, t[r$1]);
|
|
314
|
-
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r$1) {
|
|
315
|
-
Object.defineProperty(e, r$1, Object.getOwnPropertyDescriptor(t, r$1));
|
|
316
|
-
});
|
|
317
|
-
}
|
|
318
|
-
return e;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
349
|
//#endregion
|
|
322
350
|
//#region src/primitives/String.ts
|
|
323
351
|
var StringPrimitive = class StringPrimitive {
|
|
@@ -325,6 +353,8 @@ var StringPrimitive = class StringPrimitive {
|
|
|
325
353
|
_defineProperty(this, "_tag", "StringPrimitive");
|
|
326
354
|
_defineProperty(this, "_State", void 0);
|
|
327
355
|
_defineProperty(this, "_Proxy", void 0);
|
|
356
|
+
_defineProperty(this, "_TDefined", void 0);
|
|
357
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
328
358
|
_defineProperty(this, "_schema", void 0);
|
|
329
359
|
_defineProperty(this, "_opDefinitions", { set: make$5({
|
|
330
360
|
kind: "string.set",
|
|
@@ -349,7 +379,7 @@ var StringPrimitive = class StringPrimitive {
|
|
|
349
379
|
}
|
|
350
380
|
};
|
|
351
381
|
},
|
|
352
|
-
applyOperation: (
|
|
382
|
+
applyOperation: (_state, operation) => {
|
|
353
383
|
if (!isCompatibleOperation(operation, this._opDefinitions)) throw new ValidationError(`StringPrimitive cannot apply operation of kind: ${operation.kind}`);
|
|
354
384
|
const payload = operation.payload;
|
|
355
385
|
if (typeof payload !== "string") throw new ValidationError(`StringPrimitive.set requires a string payload, got: ${typeof payload}`);
|
|
@@ -434,6 +464,8 @@ var StructPrimitive = class StructPrimitive {
|
|
|
434
464
|
_defineProperty(this, "_tag", "StructPrimitive");
|
|
435
465
|
_defineProperty(this, "_State", void 0);
|
|
436
466
|
_defineProperty(this, "_Proxy", void 0);
|
|
467
|
+
_defineProperty(this, "_TDefined", void 0);
|
|
468
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
437
469
|
_defineProperty(this, "_schema", void 0);
|
|
438
470
|
_defineProperty(this, "_opDefinitions", { set: make$5({
|
|
439
471
|
kind: "struct.set",
|
|
@@ -465,15 +497,29 @@ var StructPrimitive = class StructPrimitive {
|
|
|
465
497
|
return state !== null && state !== void 0 ? state : defaultValue;
|
|
466
498
|
},
|
|
467
499
|
set: (value) => {
|
|
468
|
-
|
|
500
|
+
const merged = applyDefaults(this, value);
|
|
501
|
+
env.addOperation(fromDefinition(operationPath, this._opDefinitions.set, merged));
|
|
502
|
+
},
|
|
503
|
+
update: (value) => {
|
|
504
|
+
for (const key in value) if (Object.prototype.hasOwnProperty.call(value, key)) {
|
|
505
|
+
const fieldValue = value[key];
|
|
506
|
+
if (fieldValue === void 0) continue;
|
|
507
|
+
const fieldPrimitive = fields[key];
|
|
508
|
+
if (!fieldPrimitive) continue;
|
|
509
|
+
const fieldPath = operationPath.append(key);
|
|
510
|
+
const fieldProxy = fieldPrimitive._internal.createProxy(env, fieldPath);
|
|
511
|
+
if (fieldPrimitive._tag === "StructPrimitive" && typeof fieldValue === "object" && fieldValue !== null && !Array.isArray(fieldValue)) fieldProxy.update(fieldValue);
|
|
512
|
+
else fieldProxy.set(fieldValue);
|
|
513
|
+
}
|
|
469
514
|
},
|
|
470
515
|
toSnapshot: () => {
|
|
471
516
|
return buildSnapshot();
|
|
472
517
|
}
|
|
473
518
|
}, {
|
|
474
|
-
get: (target, prop,
|
|
519
|
+
get: (target, prop, _receiver) => {
|
|
475
520
|
if (prop === "get") return target.get;
|
|
476
521
|
if (prop === "set") return target.set;
|
|
522
|
+
if (prop === "update") return target.update;
|
|
477
523
|
if (prop === "toSnapshot") return target.toSnapshot;
|
|
478
524
|
if (typeof prop === "symbol") return;
|
|
479
525
|
if (prop in fields) {
|
|
@@ -482,8 +528,8 @@ var StructPrimitive = class StructPrimitive {
|
|
|
482
528
|
return fieldPrimitive._internal.createProxy(env, fieldPath);
|
|
483
529
|
}
|
|
484
530
|
},
|
|
485
|
-
has: (
|
|
486
|
-
if (prop === "get" || prop === "set" || prop === "toSnapshot") return true;
|
|
531
|
+
has: (_target, prop) => {
|
|
532
|
+
if (prop === "get" || prop === "set" || prop === "update" || prop === "toSnapshot") return true;
|
|
487
533
|
if (typeof prop === "string" && prop in fields) return true;
|
|
488
534
|
return false;
|
|
489
535
|
}
|
|
@@ -581,7 +627,8 @@ var StructPrimitive = class StructPrimitive {
|
|
|
581
627
|
}
|
|
582
628
|
/** Set a default value for this struct */
|
|
583
629
|
default(defaultValue) {
|
|
584
|
-
|
|
630
|
+
const merged = applyDefaults(this, defaultValue);
|
|
631
|
+
return new StructPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { defaultValue: merged }));
|
|
585
632
|
}
|
|
586
633
|
/** Get the fields schema */
|
|
587
634
|
get fields() {
|
|
@@ -610,6 +657,8 @@ var BooleanPrimitive = class BooleanPrimitive {
|
|
|
610
657
|
_defineProperty(this, "_tag", "BooleanPrimitive");
|
|
611
658
|
_defineProperty(this, "_State", void 0);
|
|
612
659
|
_defineProperty(this, "_Proxy", void 0);
|
|
660
|
+
_defineProperty(this, "_TDefined", void 0);
|
|
661
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
613
662
|
_defineProperty(this, "_schema", void 0);
|
|
614
663
|
_defineProperty(this, "_opDefinitions", { set: make$5({
|
|
615
664
|
kind: "boolean.set",
|
|
@@ -634,7 +683,7 @@ var BooleanPrimitive = class BooleanPrimitive {
|
|
|
634
683
|
}
|
|
635
684
|
};
|
|
636
685
|
},
|
|
637
|
-
applyOperation: (
|
|
686
|
+
applyOperation: (_state, operation) => {
|
|
638
687
|
if (operation.kind !== "boolean.set") throw new ValidationError(`BooleanPrimitive cannot apply operation of kind: ${operation.kind}`);
|
|
639
688
|
const payload = operation.payload;
|
|
640
689
|
if (typeof payload !== "boolean") throw new ValidationError(`BooleanPrimitive.set requires a boolean payload, got: ${typeof payload}`);
|
|
@@ -687,6 +736,8 @@ var NumberPrimitive = class NumberPrimitive {
|
|
|
687
736
|
_defineProperty(this, "_tag", "NumberPrimitive");
|
|
688
737
|
_defineProperty(this, "_State", void 0);
|
|
689
738
|
_defineProperty(this, "_Proxy", void 0);
|
|
739
|
+
_defineProperty(this, "_TDefined", void 0);
|
|
740
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
690
741
|
_defineProperty(this, "_schema", void 0);
|
|
691
742
|
_defineProperty(this, "_opDefinitions", { set: make$5({
|
|
692
743
|
kind: "number.set",
|
|
@@ -711,7 +762,7 @@ var NumberPrimitive = class NumberPrimitive {
|
|
|
711
762
|
}
|
|
712
763
|
};
|
|
713
764
|
},
|
|
714
|
-
applyOperation: (
|
|
765
|
+
applyOperation: (_state, operation) => {
|
|
715
766
|
if (operation.kind !== "number.set") throw new ValidationError(`NumberPrimitive cannot apply operation of kind: ${operation.kind}`);
|
|
716
767
|
const payload = operation.payload;
|
|
717
768
|
if (typeof payload !== "number") throw new ValidationError(`NumberPrimitive.set requires a number payload, got: ${typeof payload}`);
|
|
@@ -784,6 +835,8 @@ var LiteralPrimitive = class LiteralPrimitive {
|
|
|
784
835
|
_defineProperty(this, "_tag", "LiteralPrimitive");
|
|
785
836
|
_defineProperty(this, "_State", void 0);
|
|
786
837
|
_defineProperty(this, "_Proxy", void 0);
|
|
838
|
+
_defineProperty(this, "_TDefined", void 0);
|
|
839
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
787
840
|
_defineProperty(this, "_schema", void 0);
|
|
788
841
|
_defineProperty(this, "_opDefinitions", { set: make$5({
|
|
789
842
|
kind: "literal.set",
|
|
@@ -808,7 +861,7 @@ var LiteralPrimitive = class LiteralPrimitive {
|
|
|
808
861
|
}
|
|
809
862
|
};
|
|
810
863
|
},
|
|
811
|
-
applyOperation: (
|
|
864
|
+
applyOperation: (_state, operation) => {
|
|
812
865
|
if (operation.kind !== "literal.set") throw new ValidationError(`LiteralPrimitive cannot apply operation of kind: ${operation.kind}`);
|
|
813
866
|
const payload = operation.payload;
|
|
814
867
|
if (payload !== this._schema.literal) throw new ValidationError(`LiteralPrimitive.set requires the exact literal value "${globalThis.String(this._schema.literal)}", got: "${globalThis.String(payload)}"`);
|
|
@@ -1228,6 +1281,8 @@ var ArrayPrimitive = class ArrayPrimitive {
|
|
|
1228
1281
|
_defineProperty(this, "_tag", "ArrayPrimitive");
|
|
1229
1282
|
_defineProperty(this, "_State", void 0);
|
|
1230
1283
|
_defineProperty(this, "_Proxy", void 0);
|
|
1284
|
+
_defineProperty(this, "_TDefined", void 0);
|
|
1285
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
1231
1286
|
_defineProperty(this, "_schema", void 0);
|
|
1232
1287
|
_defineProperty(this, "_opDefinitions", {
|
|
1233
1288
|
set: make$5({
|
|
@@ -1263,6 +1318,9 @@ var ArrayPrimitive = class ArrayPrimitive {
|
|
|
1263
1318
|
if (!state || !globalThis.Array.isArray(state)) return [];
|
|
1264
1319
|
return sortByPos(state);
|
|
1265
1320
|
};
|
|
1321
|
+
const applyElementDefaults = (value) => {
|
|
1322
|
+
return applyDefaults(elementPrimitive, value);
|
|
1323
|
+
};
|
|
1266
1324
|
return {
|
|
1267
1325
|
get: () => {
|
|
1268
1326
|
return getCurrentState();
|
|
@@ -1273,10 +1331,11 @@ var ArrayPrimitive = class ArrayPrimitive {
|
|
|
1273
1331
|
for (const value of values) {
|
|
1274
1332
|
const id = env.generateId();
|
|
1275
1333
|
const pos = generatePosBetween(prevPos, null);
|
|
1334
|
+
const mergedValue = applyElementDefaults(value);
|
|
1276
1335
|
entries.push({
|
|
1277
1336
|
id,
|
|
1278
1337
|
pos,
|
|
1279
|
-
value
|
|
1338
|
+
value: mergedValue
|
|
1280
1339
|
});
|
|
1281
1340
|
prevPos = pos;
|
|
1282
1341
|
}
|
|
@@ -1287,10 +1346,11 @@ var ArrayPrimitive = class ArrayPrimitive {
|
|
|
1287
1346
|
const lastPos = sorted.length > 0 ? sorted[sorted.length - 1].pos : null;
|
|
1288
1347
|
const id = env.generateId();
|
|
1289
1348
|
const pos = generatePosBetween(lastPos, null);
|
|
1349
|
+
const mergedValue = applyElementDefaults(value);
|
|
1290
1350
|
env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
1291
1351
|
id,
|
|
1292
1352
|
pos,
|
|
1293
|
-
value
|
|
1353
|
+
value: mergedValue
|
|
1294
1354
|
}));
|
|
1295
1355
|
},
|
|
1296
1356
|
insertAt: (index, value) => {
|
|
@@ -1299,10 +1359,11 @@ var ArrayPrimitive = class ArrayPrimitive {
|
|
|
1299
1359
|
const rightPos = index < sorted.length && sorted[index] ? sorted[index].pos : null;
|
|
1300
1360
|
const id = env.generateId();
|
|
1301
1361
|
const pos = generatePosBetween(leftPos, rightPos);
|
|
1362
|
+
const mergedValue = applyElementDefaults(value);
|
|
1302
1363
|
env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
1303
1364
|
id,
|
|
1304
1365
|
pos,
|
|
1305
|
-
value
|
|
1366
|
+
value: mergedValue
|
|
1306
1367
|
}));
|
|
1307
1368
|
},
|
|
1308
1369
|
remove: (id) => {
|
|
@@ -1534,6 +1595,8 @@ var UnionPrimitive = class UnionPrimitive {
|
|
|
1534
1595
|
_defineProperty(this, "_tag", "UnionPrimitive");
|
|
1535
1596
|
_defineProperty(this, "_State", void 0);
|
|
1536
1597
|
_defineProperty(this, "_Proxy", void 0);
|
|
1598
|
+
_defineProperty(this, "_TDefined", void 0);
|
|
1599
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
1537
1600
|
_defineProperty(this, "_schema", void 0);
|
|
1538
1601
|
_defineProperty(this, "_opDefinitions", { set: make$5({
|
|
1539
1602
|
kind: "union.set",
|
|
@@ -1551,7 +1614,8 @@ var UnionPrimitive = class UnionPrimitive {
|
|
|
1551
1614
|
return state !== null && state !== void 0 ? state : defaultValue;
|
|
1552
1615
|
},
|
|
1553
1616
|
set: (value) => {
|
|
1554
|
-
|
|
1617
|
+
const merged = this._applyVariantDefaults(value);
|
|
1618
|
+
env.addOperation(fromDefinition(operationPath, this._opDefinitions.set, merged));
|
|
1555
1619
|
},
|
|
1556
1620
|
as: (variant) => {
|
|
1557
1621
|
const variantPrimitive = variants[variant];
|
|
@@ -1640,7 +1704,8 @@ var UnionPrimitive = class UnionPrimitive {
|
|
|
1640
1704
|
}
|
|
1641
1705
|
/** Set a default value for this union */
|
|
1642
1706
|
default(defaultValue) {
|
|
1643
|
-
|
|
1707
|
+
const merged = this._applyVariantDefaults(defaultValue);
|
|
1708
|
+
return new UnionPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { defaultValue: merged }));
|
|
1644
1709
|
}
|
|
1645
1710
|
/** Get the discriminator field name */
|
|
1646
1711
|
get discriminator() {
|
|
@@ -1661,6 +1726,13 @@ var UnionPrimitive = class UnionPrimitive {
|
|
|
1661
1726
|
}
|
|
1662
1727
|
}
|
|
1663
1728
|
}
|
|
1729
|
+
/** Apply defaults to a variant value based on the discriminator */
|
|
1730
|
+
_applyVariantDefaults(value) {
|
|
1731
|
+
const variantKey = this._findVariantKey(value);
|
|
1732
|
+
if (!variantKey) return value;
|
|
1733
|
+
const variantPrimitive = this._schema.variants[variantKey];
|
|
1734
|
+
return applyDefaults(variantPrimitive, value);
|
|
1735
|
+
}
|
|
1664
1736
|
};
|
|
1665
1737
|
function Union(options) {
|
|
1666
1738
|
var _options$discriminato;
|
|
@@ -1679,6 +1751,8 @@ var EitherPrimitive = class EitherPrimitive {
|
|
|
1679
1751
|
_defineProperty(this, "_tag", "EitherPrimitive");
|
|
1680
1752
|
_defineProperty(this, "_State", void 0);
|
|
1681
1753
|
_defineProperty(this, "_Proxy", void 0);
|
|
1754
|
+
_defineProperty(this, "_TDefined", void 0);
|
|
1755
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
1682
1756
|
_defineProperty(this, "_schema", void 0);
|
|
1683
1757
|
_defineProperty(this, "_opDefinitions", { set: make$5({
|
|
1684
1758
|
kind: "either.set",
|
|
@@ -1868,7 +1942,7 @@ const TreeNodeSelfSymbol = Symbol.for("TreeNode.Self");
|
|
|
1868
1942
|
* ```typescript
|
|
1869
1943
|
* const FolderNode = TreeNode("folder", {
|
|
1870
1944
|
* data: Struct({ name: String() }),
|
|
1871
|
-
* children: [
|
|
1945
|
+
* children: [TreeNodeSelf], // Folder can contain other folders
|
|
1872
1946
|
* });
|
|
1873
1947
|
* ```
|
|
1874
1948
|
*/
|
|
@@ -1890,6 +1964,7 @@ var TreeNodePrimitive = class {
|
|
|
1890
1964
|
_defineProperty(this, "_tag", "TreeNodePrimitive");
|
|
1891
1965
|
_defineProperty(this, "_Type", void 0);
|
|
1892
1966
|
_defineProperty(this, "_Data", void 0);
|
|
1967
|
+
_defineProperty(this, "_Children", void 0);
|
|
1893
1968
|
_defineProperty(this, "_type", void 0);
|
|
1894
1969
|
_defineProperty(this, "_data", void 0);
|
|
1895
1970
|
_defineProperty(this, "_children", void 0);
|
|
@@ -1959,6 +2034,8 @@ var TreePrimitive = class TreePrimitive {
|
|
|
1959
2034
|
_defineProperty(this, "_tag", "TreePrimitive");
|
|
1960
2035
|
_defineProperty(this, "_State", void 0);
|
|
1961
2036
|
_defineProperty(this, "_Proxy", void 0);
|
|
2037
|
+
_defineProperty(this, "_TDefined", void 0);
|
|
2038
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
1962
2039
|
_defineProperty(this, "_schema", void 0);
|
|
1963
2040
|
_defineProperty(this, "_nodeTypeRegistry", void 0);
|
|
1964
2041
|
_defineProperty(this, "_opDefinitions", {
|
|
@@ -2009,11 +2086,15 @@ var TreePrimitive = class TreePrimitive {
|
|
|
2009
2086
|
as: (nodeType) => {
|
|
2010
2087
|
if (nodeState.type !== nodeType.type) throw new ValidationError(`Node is of type "${nodeState.type}", not "${nodeType.type}"`);
|
|
2011
2088
|
const nodePath = operationPath.append(nodeState.id);
|
|
2089
|
+
const dataProxy = nodeType.data._internal.createProxy(env, nodePath);
|
|
2012
2090
|
return {
|
|
2013
2091
|
id: nodeState.id,
|
|
2014
2092
|
type: nodeType.type,
|
|
2015
|
-
data:
|
|
2016
|
-
get: () => nodeState
|
|
2093
|
+
data: dataProxy,
|
|
2094
|
+
get: () => nodeState,
|
|
2095
|
+
update: (value) => {
|
|
2096
|
+
dataProxy.update(value);
|
|
2097
|
+
}
|
|
2017
2098
|
};
|
|
2018
2099
|
},
|
|
2019
2100
|
get: () => nodeState
|
|
@@ -2060,12 +2141,13 @@ var TreePrimitive = class TreePrimitive {
|
|
|
2060
2141
|
const parentType = getParentType(parentId);
|
|
2061
2142
|
this._validateChildType(parentType, nodeType.type);
|
|
2062
2143
|
if (parentId === null && state.some((n) => n.parentId === null)) throw new ValidationError("Tree already has a root node");
|
|
2144
|
+
const mergedData = applyDefaults(nodeType.data, data);
|
|
2063
2145
|
env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
2064
2146
|
id,
|
|
2065
2147
|
type: nodeType.type,
|
|
2066
2148
|
parentId,
|
|
2067
2149
|
pos,
|
|
2068
|
-
data
|
|
2150
|
+
data: mergedData
|
|
2069
2151
|
}));
|
|
2070
2152
|
return id;
|
|
2071
2153
|
},
|
|
@@ -2078,12 +2160,13 @@ var TreePrimitive = class TreePrimitive {
|
|
|
2078
2160
|
const parentType = getParentType(parentId);
|
|
2079
2161
|
this._validateChildType(parentType, nodeType.type);
|
|
2080
2162
|
if (parentId === null && state.some((n) => n.parentId === null)) throw new ValidationError("Tree already has a root node");
|
|
2163
|
+
const mergedData = applyDefaults(nodeType.data, data);
|
|
2081
2164
|
env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
2082
2165
|
id,
|
|
2083
2166
|
type: nodeType.type,
|
|
2084
2167
|
parentId,
|
|
2085
2168
|
pos,
|
|
2086
|
-
data
|
|
2169
|
+
data: mergedData
|
|
2087
2170
|
}));
|
|
2088
2171
|
return id;
|
|
2089
2172
|
},
|
|
@@ -2097,12 +2180,13 @@ var TreePrimitive = class TreePrimitive {
|
|
|
2097
2180
|
const parentType = getParentType(parentId);
|
|
2098
2181
|
this._validateChildType(parentType, nodeType.type);
|
|
2099
2182
|
if (parentId === null && state.some((n) => n.parentId === null)) throw new ValidationError("Tree already has a root node");
|
|
2183
|
+
const mergedData = applyDefaults(nodeType.data, data);
|
|
2100
2184
|
env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
2101
2185
|
id,
|
|
2102
2186
|
type: nodeType.type,
|
|
2103
2187
|
parentId,
|
|
2104
2188
|
pos,
|
|
2105
|
-
data
|
|
2189
|
+
data: mergedData
|
|
2106
2190
|
}));
|
|
2107
2191
|
return id;
|
|
2108
2192
|
},
|
|
@@ -2118,12 +2202,13 @@ var TreePrimitive = class TreePrimitive {
|
|
|
2118
2202
|
const id = env.generateId();
|
|
2119
2203
|
const parentType = getParentType(parentId);
|
|
2120
2204
|
this._validateChildType(parentType, nodeType.type);
|
|
2205
|
+
const mergedData = applyDefaults(nodeType.data, data);
|
|
2121
2206
|
env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
2122
2207
|
id,
|
|
2123
2208
|
type: nodeType.type,
|
|
2124
2209
|
parentId,
|
|
2125
2210
|
pos,
|
|
2126
|
-
data
|
|
2211
|
+
data: mergedData
|
|
2127
2212
|
}));
|
|
2128
2213
|
return id;
|
|
2129
2214
|
},
|
|
@@ -2139,12 +2224,13 @@ var TreePrimitive = class TreePrimitive {
|
|
|
2139
2224
|
const id = env.generateId();
|
|
2140
2225
|
const parentType = getParentType(parentId);
|
|
2141
2226
|
this._validateChildType(parentType, nodeType.type);
|
|
2227
|
+
const mergedData = applyDefaults(nodeType.data, data);
|
|
2142
2228
|
env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
2143
2229
|
id,
|
|
2144
2230
|
type: nodeType.type,
|
|
2145
2231
|
parentId,
|
|
2146
2232
|
pos,
|
|
2147
|
-
data
|
|
2233
|
+
data: mergedData
|
|
2148
2234
|
}));
|
|
2149
2235
|
return id;
|
|
2150
2236
|
},
|
|
@@ -2255,6 +2341,13 @@ var TreePrimitive = class TreePrimitive {
|
|
|
2255
2341
|
const nodePath = operationPath.append(id);
|
|
2256
2342
|
return nodeType.data._internal.createProxy(env, nodePath);
|
|
2257
2343
|
},
|
|
2344
|
+
updateAt: (id, nodeType, value) => {
|
|
2345
|
+
const node = getCurrentState().find((n) => n.id === id);
|
|
2346
|
+
if (!node) throw new ValidationError(`Node not found: ${id}`);
|
|
2347
|
+
if (node.type !== nodeType.type) throw new ValidationError(`Node is of type "${node.type}", not "${nodeType.type}"`);
|
|
2348
|
+
const nodePath = operationPath.append(id);
|
|
2349
|
+
nodeType.data._internal.createProxy(env, nodePath).update(value);
|
|
2350
|
+
},
|
|
2258
2351
|
toSnapshot: () => {
|
|
2259
2352
|
const state = getCurrentState();
|
|
2260
2353
|
const rootNode = state.find((n) => n.parentId === null);
|
|
@@ -2489,6 +2582,7 @@ var Primitive_exports = /* @__PURE__ */ __export({
|
|
|
2489
2582
|
Union: () => Union,
|
|
2490
2583
|
UnionPrimitive: () => UnionPrimitive,
|
|
2491
2584
|
ValidationError: () => ValidationError,
|
|
2585
|
+
applyDefaults: () => applyDefaults,
|
|
2492
2586
|
isCompatibleOperation: () => isCompatibleOperation,
|
|
2493
2587
|
runValidators: () => runValidators
|
|
2494
2588
|
});
|