r-state-tree 0.6.2 → 0.7.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 +100 -8
- package/dist/configuration.d.ts +10 -0
- package/dist/model/Model.d.ts +1 -1
- package/dist/model/ModelAdministration.d.ts +3 -0
- package/dist/observables/object.d.ts +1 -1
- package/dist/r-state-tree.cjs +322 -128
- package/dist/r-state-tree.js +322 -128
- package/dist/store/Store.d.ts +1 -1
- package/dist/types.d.ts +8 -7
- package/package.json +58 -57
package/dist/r-state-tree.cjs
CHANGED
|
@@ -59,6 +59,140 @@ const modelType = {
|
|
|
59
59
|
type: "model"
|
|
60
60
|
/* model */
|
|
61
61
|
};
|
|
62
|
+
const computedType = {
|
|
63
|
+
type: "computed"
|
|
64
|
+
/* computed */
|
|
65
|
+
};
|
|
66
|
+
function makeDecorator(type) {
|
|
67
|
+
return function(value, context) {
|
|
68
|
+
context.metadata[context.name] = type;
|
|
69
|
+
return value;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function makeChildDecorator(typeObj) {
|
|
73
|
+
return function(valueOrChildType, context) {
|
|
74
|
+
if (context !== void 0) {
|
|
75
|
+
return makeDecorator(typeObj)(valueOrChildType, context);
|
|
76
|
+
}
|
|
77
|
+
const childCtor = valueOrChildType;
|
|
78
|
+
const typeWithCtor = typeObj(childCtor);
|
|
79
|
+
const decorator = function(value, context2) {
|
|
80
|
+
return makeDecorator(typeWithCtor)(value, context2);
|
|
81
|
+
};
|
|
82
|
+
decorator.type = typeWithCtor?.type;
|
|
83
|
+
decorator.childType = childCtor;
|
|
84
|
+
return decorator;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const child = makeChildDecorator(childType);
|
|
88
|
+
const modelRef = makeChildDecorator(modelRefType);
|
|
89
|
+
const model = makeDecorator(modelType);
|
|
90
|
+
const id = makeDecorator(idType);
|
|
91
|
+
const state = makeDecorator(stateType);
|
|
92
|
+
function hasConfigLike(value) {
|
|
93
|
+
if (!value) return false;
|
|
94
|
+
const t = typeof value;
|
|
95
|
+
if (t !== "object" && t !== "function") return false;
|
|
96
|
+
return "type" in value;
|
|
97
|
+
}
|
|
98
|
+
function normalizeEntry(entry) {
|
|
99
|
+
if (!entry) return void 0;
|
|
100
|
+
if (hasConfigLike(entry)) return entry;
|
|
101
|
+
if (typeof entry === "function") {
|
|
102
|
+
if (entry === id) return idType;
|
|
103
|
+
if (entry === state) return stateType;
|
|
104
|
+
if (entry === model) return modelType;
|
|
105
|
+
if (entry === child) return childType;
|
|
106
|
+
if (entry === modelRef) return modelRefType;
|
|
107
|
+
return void 0;
|
|
108
|
+
}
|
|
109
|
+
return void 0;
|
|
110
|
+
}
|
|
111
|
+
function getConfigType(entry) {
|
|
112
|
+
const normalized = normalizeEntry(entry);
|
|
113
|
+
return normalized && hasConfigLike(normalized) ? normalized.type : void 0;
|
|
114
|
+
}
|
|
115
|
+
function getConfigChildType(entry) {
|
|
116
|
+
const normalized = normalizeEntry(entry);
|
|
117
|
+
if (!normalized || !hasConfigLike(normalized)) return void 0;
|
|
118
|
+
if (typeof normalized === "function") {
|
|
119
|
+
return normalized.childType;
|
|
120
|
+
}
|
|
121
|
+
return normalized.childType;
|
|
122
|
+
}
|
|
123
|
+
function getParentConstructor(Ctor) {
|
|
124
|
+
return Ctor?.prototype ? Object.getPrototypeOf(Ctor.prototype)?.constructor : void 0;
|
|
125
|
+
}
|
|
126
|
+
function getCtorChain(ctor) {
|
|
127
|
+
const chain = [];
|
|
128
|
+
let node = ctor;
|
|
129
|
+
while (node) {
|
|
130
|
+
chain.push(node);
|
|
131
|
+
const parent = getParentConstructor(node);
|
|
132
|
+
if (!parent || parent === Object || parent === Function) {
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
node = parent;
|
|
136
|
+
}
|
|
137
|
+
chain.reverse();
|
|
138
|
+
return chain;
|
|
139
|
+
}
|
|
140
|
+
function getMetadataSymbol() {
|
|
141
|
+
const sym = Symbol.metadata;
|
|
142
|
+
return typeof sym === "symbol" ? sym : void 0;
|
|
143
|
+
}
|
|
144
|
+
function getOwnMetadata(ctor) {
|
|
145
|
+
const metadataSymbol = getMetadataSymbol();
|
|
146
|
+
if (!metadataSymbol) return void 0;
|
|
147
|
+
return Object.prototype.hasOwnProperty.call(ctor, metadataSymbol) ? ctor[metadataSymbol] : void 0;
|
|
148
|
+
}
|
|
149
|
+
function getOwnStaticTypes(ctor) {
|
|
150
|
+
return Object.prototype.hasOwnProperty.call(ctor, "types") ? ctor.types : void 0;
|
|
151
|
+
}
|
|
152
|
+
function mergeInto(target, source2) {
|
|
153
|
+
if (!source2 || typeof source2 !== "object") return;
|
|
154
|
+
for (const key of Reflect.ownKeys(source2)) {
|
|
155
|
+
const value = source2[key];
|
|
156
|
+
const normalized = normalizeEntry(value);
|
|
157
|
+
if (normalized) {
|
|
158
|
+
target[key] = normalized;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const configurationCache = /* @__PURE__ */ new WeakMap();
|
|
163
|
+
function refsEqual(a, b) {
|
|
164
|
+
if (a.length !== b.length) return false;
|
|
165
|
+
for (let i = 0; i < a.length; i++) {
|
|
166
|
+
if (a[i].ctor !== b[i].ctor) return false;
|
|
167
|
+
if (a[i].typesRef !== b[i].typesRef) return false;
|
|
168
|
+
if (a[i].metaRef !== b[i].metaRef) return false;
|
|
169
|
+
}
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
function getConfigurationForCtor(ctor) {
|
|
173
|
+
if (!ctor) return {};
|
|
174
|
+
const chain = getCtorChain(ctor);
|
|
175
|
+
const refs = chain.map((c) => ({
|
|
176
|
+
ctor: c,
|
|
177
|
+
typesRef: getOwnStaticTypes(c),
|
|
178
|
+
metaRef: getOwnMetadata(c)
|
|
179
|
+
}));
|
|
180
|
+
const cached = configurationCache.get(ctor);
|
|
181
|
+
if (cached && refsEqual(cached.refs, refs)) {
|
|
182
|
+
return cached.merged;
|
|
183
|
+
}
|
|
184
|
+
const merged = {};
|
|
185
|
+
for (let i = 0; i < refs.length; i++) {
|
|
186
|
+
const { typesRef, metaRef } = refs[i];
|
|
187
|
+
mergeInto(merged, metaRef);
|
|
188
|
+
mergeInto(merged, typesRef);
|
|
189
|
+
}
|
|
190
|
+
configurationCache.set(ctor, { refs, merged });
|
|
191
|
+
return merged;
|
|
192
|
+
}
|
|
193
|
+
function getConfigurationValue(ctor, key) {
|
|
194
|
+
return getConfigurationForCtor(ctor)[key];
|
|
195
|
+
}
|
|
62
196
|
function isNonPrimitive(val) {
|
|
63
197
|
return val != null && (typeof val === "object" || typeof val === "function");
|
|
64
198
|
}
|
|
@@ -66,19 +200,21 @@ function isPropertyKey(val) {
|
|
|
66
200
|
return typeof val === "string" || typeof val === "number" || typeof val === "symbol";
|
|
67
201
|
}
|
|
68
202
|
function getPropertyType(key, obj) {
|
|
69
|
-
const
|
|
70
|
-
const hasMetadata = ctor != null && ctor[Symbol.metadata] !== void 0;
|
|
203
|
+
const plain = isPlainObject(obj);
|
|
71
204
|
const descriptor = getPropertyDescriptor$1(obj, key);
|
|
72
205
|
if (descriptor?.value && typeof descriptor.value === "function") {
|
|
73
206
|
return "action";
|
|
74
207
|
}
|
|
75
|
-
|
|
208
|
+
const isAccessor = descriptor && (typeof descriptor.get === "function" || typeof descriptor.set === "function");
|
|
209
|
+
if (plain) {
|
|
76
210
|
return "observable";
|
|
77
211
|
}
|
|
78
|
-
const
|
|
79
|
-
|
|
212
|
+
const config = getConfigurationValue(
|
|
213
|
+
obj.constructor,
|
|
214
|
+
key
|
|
215
|
+
);
|
|
80
216
|
if (config) {
|
|
81
|
-
switch (config
|
|
217
|
+
switch (getConfigType(config)) {
|
|
82
218
|
case ObservableCfgTypes.computed:
|
|
83
219
|
return "computed";
|
|
84
220
|
case ModelCfgTypes.state:
|
|
@@ -89,6 +225,9 @@ function getPropertyType(key, obj) {
|
|
|
89
225
|
return "observable";
|
|
90
226
|
}
|
|
91
227
|
}
|
|
228
|
+
if (isAccessor) {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
92
231
|
return "observable";
|
|
93
232
|
}
|
|
94
233
|
function getPropertyDescriptor$1(obj, key) {
|
|
@@ -329,7 +468,7 @@ class ObjectAdministration extends Administration {
|
|
|
329
468
|
}
|
|
330
469
|
getType(key) {
|
|
331
470
|
let type = this.types.get(key);
|
|
332
|
-
if (type === void 0) {
|
|
471
|
+
if (type === void 0 && !this.types.has(key)) {
|
|
333
472
|
type = getPropertyType(key, this.source);
|
|
334
473
|
this.types.set(key, type);
|
|
335
474
|
}
|
|
@@ -347,6 +486,9 @@ class ObjectAdministration extends Administration {
|
|
|
347
486
|
if (type === "computed") {
|
|
348
487
|
return resolveNode(this.getComputed(key));
|
|
349
488
|
}
|
|
489
|
+
if (type === null) {
|
|
490
|
+
return void 0;
|
|
491
|
+
}
|
|
350
492
|
return resolveNode(this.valuesMap.getOrCreate(key, this.source[key]));
|
|
351
493
|
}
|
|
352
494
|
read(key, receiver = this.proxy) {
|
|
@@ -358,15 +500,16 @@ class ObjectAdministration extends Administration {
|
|
|
358
500
|
this.atom.reportObserved();
|
|
359
501
|
return Reflect.get(this.source, key, receiver);
|
|
360
502
|
}
|
|
361
|
-
if (
|
|
362
|
-
|
|
503
|
+
if (type === null) {
|
|
504
|
+
return Reflect.get(this.source, key, receiver);
|
|
363
505
|
}
|
|
364
506
|
this.atom.reportObserved();
|
|
365
507
|
if (this.atom.observing) {
|
|
366
508
|
this.hasMap.reportObserved(key);
|
|
367
509
|
}
|
|
510
|
+
const value = Reflect.get(this.source, key, receiver);
|
|
511
|
+
this.valuesMap.reportObserved(key, value);
|
|
368
512
|
if (type === "observable") {
|
|
369
|
-
const value = Reflect.get(this.source, key, receiver);
|
|
370
513
|
const shouldWrap = this.explicitObservables.has(key) || isObservable(value);
|
|
371
514
|
if (shouldWrap && value && typeof value === "object" && !Object.isFrozen(value)) {
|
|
372
515
|
const desc = getPropertyDescriptor$1(this.source, key);
|
|
@@ -380,13 +523,14 @@ class ObjectAdministration extends Administration {
|
|
|
380
523
|
}
|
|
381
524
|
return value;
|
|
382
525
|
}
|
|
383
|
-
return getAction(
|
|
384
|
-
Reflect.get(this.source, key, receiver)
|
|
385
|
-
);
|
|
526
|
+
return getAction(value);
|
|
386
527
|
}
|
|
387
528
|
explicitObservables = /* @__PURE__ */ new Set();
|
|
388
529
|
write(key, newValue) {
|
|
389
530
|
const type = this.getType(key);
|
|
531
|
+
if (type === null) {
|
|
532
|
+
return Reflect.set(this.source, key, newValue, this.proxy);
|
|
533
|
+
}
|
|
390
534
|
if (type === "computed") {
|
|
391
535
|
return signalsCore.batch(() => this.set(key, newValue));
|
|
392
536
|
}
|
|
@@ -609,11 +753,12 @@ function signal(value) {
|
|
|
609
753
|
}
|
|
610
754
|
function computed(value, context) {
|
|
611
755
|
if (context && typeof context === "object" && "kind" in context) {
|
|
612
|
-
context.metadata[context.name] =
|
|
756
|
+
context.metadata[context.name] = computedType;
|
|
613
757
|
return value;
|
|
614
758
|
}
|
|
615
759
|
return internalCreateComputed(value).c;
|
|
616
760
|
}
|
|
761
|
+
computed.type = computedType.type;
|
|
617
762
|
function source(obj) {
|
|
618
763
|
return getSource(obj);
|
|
619
764
|
}
|
|
@@ -1463,9 +1608,22 @@ function createFilterMethod(method) {
|
|
|
1463
1608
|
function(callback, thisArg) {
|
|
1464
1609
|
const adm = getAdministration(this);
|
|
1465
1610
|
adm.reportObserved();
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1611
|
+
const observedInput = [];
|
|
1612
|
+
observedInput.length = adm.source.length;
|
|
1613
|
+
const keys = Object.keys(adm.source);
|
|
1614
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1615
|
+
const key = keys[i];
|
|
1616
|
+
const idx = Number(key);
|
|
1617
|
+
if (!Number.isNaN(idx)) {
|
|
1618
|
+
observedInput[idx] = this[idx];
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
return Array.prototype[method].call(
|
|
1622
|
+
observedInput,
|
|
1623
|
+
(_element, index) => {
|
|
1624
|
+
return callback.call(thisArg, this[index], index, this);
|
|
1625
|
+
}
|
|
1626
|
+
);
|
|
1469
1627
|
}
|
|
1470
1628
|
);
|
|
1471
1629
|
}
|
|
@@ -1473,7 +1631,22 @@ function createReduceMethod(method) {
|
|
|
1473
1631
|
return createMethod(method, function() {
|
|
1474
1632
|
const adm = getAdministration(this);
|
|
1475
1633
|
adm.reportObserved();
|
|
1476
|
-
|
|
1634
|
+
const observedInput = [];
|
|
1635
|
+
observedInput.length = adm.source.length;
|
|
1636
|
+
const keys = Object.keys(adm.source);
|
|
1637
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1638
|
+
const key = keys[i];
|
|
1639
|
+
const idx = Number(key);
|
|
1640
|
+
if (!Number.isNaN(idx)) {
|
|
1641
|
+
observedInput[idx] = this[idx];
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
const args = Array.from(arguments);
|
|
1645
|
+
const callback = args[0];
|
|
1646
|
+
args[0] = (acc, _element, index) => {
|
|
1647
|
+
return callback(acc, this[index], index, this);
|
|
1648
|
+
};
|
|
1649
|
+
return Array.prototype[method].apply(observedInput, args);
|
|
1477
1650
|
});
|
|
1478
1651
|
}
|
|
1479
1652
|
class DateAdministration extends Administration {
|
|
@@ -1707,7 +1880,7 @@ function getDiff(o1, o2, getConfig) {
|
|
|
1707
1880
|
for (let i = 0; i < keys.length; i++) {
|
|
1708
1881
|
const key = keys[i];
|
|
1709
1882
|
if (obj1[key] !== obj2[key]) {
|
|
1710
|
-
if (config?.[key]
|
|
1883
|
+
if (getConfigType(config?.[key]) === CommonCfgTypes.child) {
|
|
1711
1884
|
const value = obj2[key];
|
|
1712
1885
|
if (Array.isArray(value)) {
|
|
1713
1886
|
diff[key] = value.map((model2, index) => {
|
|
@@ -1816,7 +1989,7 @@ class StoreAdministration extends PreactObjectAdministration {
|
|
|
1816
1989
|
return target.key;
|
|
1817
1990
|
}
|
|
1818
1991
|
const adm = getAdministration(target);
|
|
1819
|
-
switch (adm.configuration[name]
|
|
1992
|
+
switch (getConfigType(adm.configuration[name])) {
|
|
1820
1993
|
case CommonCfgTypes.child:
|
|
1821
1994
|
return adm.getStore(name);
|
|
1822
1995
|
case StoreCfgTypes.model:
|
|
@@ -1833,7 +2006,7 @@ class StoreAdministration extends PreactObjectAdministration {
|
|
|
1833
2006
|
if (name === "props") {
|
|
1834
2007
|
throw new Error(`r-state-tree: ${name} is read-only`);
|
|
1835
2008
|
}
|
|
1836
|
-
if (adm.configuration[name]
|
|
2009
|
+
if (getConfigType(adm.configuration[name]) === StoreCfgTypes.model) {
|
|
1837
2010
|
if (value !== void 0) {
|
|
1838
2011
|
throw new Error(`r-state-tree: model ${String(name)} is read-only`);
|
|
1839
2012
|
}
|
|
@@ -2110,9 +2283,6 @@ function updateStore(store, props) {
|
|
|
2110
2283
|
return store;
|
|
2111
2284
|
}
|
|
2112
2285
|
class Store {
|
|
2113
|
-
static get types() {
|
|
2114
|
-
return this[Symbol.metadata];
|
|
2115
|
-
}
|
|
2116
2286
|
props;
|
|
2117
2287
|
constructor(props) {
|
|
2118
2288
|
if (!initEnabled$1) {
|
|
@@ -2124,7 +2294,9 @@ class Store {
|
|
|
2124
2294
|
);
|
|
2125
2295
|
const adm = getStoreAdm(observable2);
|
|
2126
2296
|
adm.setConfiguration(
|
|
2127
|
-
() =>
|
|
2297
|
+
() => getConfigurationForCtor(
|
|
2298
|
+
this.constructor
|
|
2299
|
+
) ?? {}
|
|
2128
2300
|
);
|
|
2129
2301
|
adm.write("props", getObservable({}));
|
|
2130
2302
|
updateProps(observable2.props, props);
|
|
@@ -2360,8 +2532,9 @@ function getModelAdm(model2) {
|
|
|
2360
2532
|
}
|
|
2361
2533
|
function getIdKey(Ctor) {
|
|
2362
2534
|
if (!ctorIdKeyMap.has(Ctor)) {
|
|
2363
|
-
const
|
|
2364
|
-
|
|
2535
|
+
const config = getConfigurationForCtor(Ctor);
|
|
2536
|
+
const key = Object.keys(config).find(
|
|
2537
|
+
(prop) => config[prop]?.type === ModelCfgTypes.id
|
|
2365
2538
|
) ?? null;
|
|
2366
2539
|
ctorIdKeyMap.set(Ctor, key);
|
|
2367
2540
|
}
|
|
@@ -2410,6 +2583,12 @@ function validateModelChildValue(value, propertyName) {
|
|
|
2410
2583
|
);
|
|
2411
2584
|
}
|
|
2412
2585
|
class ModelAdministration extends PreactObjectAdministration {
|
|
2586
|
+
getCfgType(name) {
|
|
2587
|
+
return getConfigType(this.configuration[name]);
|
|
2588
|
+
}
|
|
2589
|
+
getCfgChildType(name) {
|
|
2590
|
+
return getConfigChildType(this.configuration[name]);
|
|
2591
|
+
}
|
|
2413
2592
|
static proxyTraps = Object.assign(
|
|
2414
2593
|
{},
|
|
2415
2594
|
PreactObjectAdministration.proxyTraps,
|
|
@@ -2419,7 +2598,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2419
2598
|
if (prop === "parent") {
|
|
2420
2599
|
return target.parent;
|
|
2421
2600
|
}
|
|
2422
|
-
switch (adm.
|
|
2601
|
+
switch (adm.getCfgType(prop)) {
|
|
2423
2602
|
case ModelCfgTypes.modelRef:
|
|
2424
2603
|
if (Array.isArray(adm.source[prop])) {
|
|
2425
2604
|
return adm.getModelRefs(prop);
|
|
@@ -2436,7 +2615,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2436
2615
|
const adm = getAdministration(target);
|
|
2437
2616
|
adm.writeInProgress.add(name);
|
|
2438
2617
|
try {
|
|
2439
|
-
switch (adm.
|
|
2618
|
+
switch (adm.getCfgType(name)) {
|
|
2440
2619
|
case ModelCfgTypes.modelRef: {
|
|
2441
2620
|
Array.isArray(value) ? adm.setModelRefs(name, value) : adm.setModelRef(name, value);
|
|
2442
2621
|
return true;
|
|
@@ -2471,7 +2650,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2471
2650
|
defineProperty(target, name, desc) {
|
|
2472
2651
|
const adm = getAdministration(target);
|
|
2473
2652
|
if (desc && "value" in desc && !adm.writeInProgress.has(name)) {
|
|
2474
|
-
switch (adm.
|
|
2653
|
+
switch (adm.getCfgType(name)) {
|
|
2475
2654
|
case ModelCfgTypes.modelRef:
|
|
2476
2655
|
case CommonCfgTypes.child:
|
|
2477
2656
|
case ModelCfgTypes.id: {
|
|
@@ -2535,6 +2714,39 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2535
2714
|
});
|
|
2536
2715
|
}
|
|
2537
2716
|
}
|
|
2717
|
+
hydrateStateValue(currentValue, snapshotValue) {
|
|
2718
|
+
if (currentValue instanceof signalsCore.Signal) {
|
|
2719
|
+
currentValue.value = this.hydrateStateValue(
|
|
2720
|
+
currentValue.value,
|
|
2721
|
+
snapshotValue
|
|
2722
|
+
);
|
|
2723
|
+
return currentValue;
|
|
2724
|
+
}
|
|
2725
|
+
if (Array.isArray(currentValue) && Array.isArray(snapshotValue)) {
|
|
2726
|
+
const nextItems = snapshotValue.map(
|
|
2727
|
+
(item, index) => this.hydrateStateValue(currentValue[index], item)
|
|
2728
|
+
);
|
|
2729
|
+
currentValue.splice(0, currentValue.length, ...nextItems);
|
|
2730
|
+
return currentValue;
|
|
2731
|
+
}
|
|
2732
|
+
if (isPlainObject(currentValue) && isPlainObject(snapshotValue)) {
|
|
2733
|
+
const currentRecord = currentValue;
|
|
2734
|
+
const snapshotRecord = snapshotValue;
|
|
2735
|
+
Object.keys(snapshotRecord).forEach((key) => {
|
|
2736
|
+
currentRecord[key] = this.hydrateStateValue(
|
|
2737
|
+
currentRecord[key],
|
|
2738
|
+
snapshotRecord[key]
|
|
2739
|
+
);
|
|
2740
|
+
});
|
|
2741
|
+
Object.keys(currentRecord).forEach((key) => {
|
|
2742
|
+
if (!(key in snapshotRecord)) {
|
|
2743
|
+
delete currentRecord[key];
|
|
2744
|
+
}
|
|
2745
|
+
});
|
|
2746
|
+
return currentValue;
|
|
2747
|
+
}
|
|
2748
|
+
return snapshotValue;
|
|
2749
|
+
}
|
|
2538
2750
|
setId(name, v) {
|
|
2539
2751
|
const id2 = getIdentifier(this.proxy);
|
|
2540
2752
|
if (id2 === v) {
|
|
@@ -2725,7 +2937,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2725
2937
|
}
|
|
2726
2938
|
toJSON() {
|
|
2727
2939
|
return Object.keys(this.configuration).reduce((json, key) => {
|
|
2728
|
-
switch (this.configuration[key]
|
|
2940
|
+
switch (getConfigType(this.configuration[key])) {
|
|
2729
2941
|
case ModelCfgTypes.state: {
|
|
2730
2942
|
json[key] = clone(this.proxy[key], key);
|
|
2731
2943
|
break;
|
|
@@ -2782,7 +2994,8 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2782
2994
|
}
|
|
2783
2995
|
loadSnapshot(snapshot) {
|
|
2784
2996
|
const ensureChildTypes = (key) => {
|
|
2785
|
-
|
|
2997
|
+
const childType2 = this.getCfgChildType(key);
|
|
2998
|
+
if (!childType2) {
|
|
2786
2999
|
throw new Error(
|
|
2787
3000
|
"r-state-tree: child constructor must be specified to load snapshots with child/children. eg: `@child(ChildCtor) MyChild`"
|
|
2788
3001
|
);
|
|
@@ -2790,80 +3003,85 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2790
3003
|
return true;
|
|
2791
3004
|
};
|
|
2792
3005
|
onSnapshotLoad(() => {
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
);
|
|
2808
|
-
this.referencedAtoms?.get(key)?.reportChanged();
|
|
2809
|
-
}
|
|
2810
|
-
break;
|
|
2811
|
-
} else if (value instanceof Model) {
|
|
2812
|
-
this.proxy[key] = value;
|
|
2813
|
-
} else {
|
|
2814
|
-
this.source[key] = getSnapshotRefId(value);
|
|
2815
|
-
this.referencedAtoms?.get(key)?.reportChanged();
|
|
2816
|
-
}
|
|
2817
|
-
break;
|
|
2818
|
-
case ModelCfgTypes.id:
|
|
2819
|
-
this.setId(key, value);
|
|
2820
|
-
break;
|
|
2821
|
-
case CommonCfgTypes.child:
|
|
2822
|
-
let model2;
|
|
2823
|
-
if (Array.isArray(value)) {
|
|
2824
|
-
const Ctor = childType2;
|
|
2825
|
-
this.proxy[key] = value?.map(
|
|
2826
|
-
(snapshot2, index) => {
|
|
2827
|
-
snapshot2 = snapshot2 ?? {};
|
|
2828
|
-
let model22;
|
|
2829
|
-
if (snapshot2 instanceof Model) {
|
|
2830
|
-
model22 = snapshot2;
|
|
3006
|
+
signalsCore.untracked(() => {
|
|
3007
|
+
signalsCore.batch(() => {
|
|
3008
|
+
Object.keys(snapshot).forEach((key) => {
|
|
3009
|
+
const type = this.getCfgType(key);
|
|
3010
|
+
const childType2 = this.getCfgChildType(key);
|
|
3011
|
+
const value = snapshot[key];
|
|
3012
|
+
switch (type) {
|
|
3013
|
+
case ModelCfgTypes.state:
|
|
3014
|
+
this.proxy[key] = this.hydrateStateValue(this.proxy[key], value);
|
|
3015
|
+
break;
|
|
3016
|
+
case ModelCfgTypes.modelRef:
|
|
3017
|
+
if (Array.isArray(value)) {
|
|
3018
|
+
if (value?.[0] instanceof Model) {
|
|
3019
|
+
this.proxy[key] = value;
|
|
2831
3020
|
} else {
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
3021
|
+
this.source[key] = value.map(
|
|
3022
|
+
(snapshot2) => getSnapshotRefId(snapshot2)
|
|
3023
|
+
);
|
|
3024
|
+
this.referencedAtoms?.get(key)?.reportChanged();
|
|
3025
|
+
}
|
|
3026
|
+
break;
|
|
3027
|
+
} else if (value instanceof Model) {
|
|
3028
|
+
this.proxy[key] = value;
|
|
3029
|
+
} else {
|
|
3030
|
+
this.source[key] = getSnapshotRefId(value);
|
|
3031
|
+
this.referencedAtoms?.get(key)?.reportChanged();
|
|
3032
|
+
}
|
|
3033
|
+
break;
|
|
3034
|
+
case ModelCfgTypes.id:
|
|
3035
|
+
this.setId(key, value);
|
|
3036
|
+
break;
|
|
3037
|
+
case CommonCfgTypes.child:
|
|
3038
|
+
let model2;
|
|
3039
|
+
if (Array.isArray(value)) {
|
|
3040
|
+
const Ctor = childType2;
|
|
3041
|
+
this.proxy[key] = value?.map(
|
|
3042
|
+
(snapshot2, index) => {
|
|
3043
|
+
snapshot2 = snapshot2 ?? {};
|
|
3044
|
+
let model22;
|
|
3045
|
+
if (snapshot2 instanceof Model) {
|
|
3046
|
+
model22 = snapshot2;
|
|
3047
|
+
} else {
|
|
3048
|
+
ensureChildTypes(key);
|
|
3049
|
+
const id2 = childType2 && getSnapshotId(snapshot2, Ctor);
|
|
3050
|
+
const foundModel = id2 != null ? getModelById(this.root.proxy, id2) : this.proxy[key][index];
|
|
3051
|
+
const adm = foundModel && getModelAdm(foundModel);
|
|
3052
|
+
if (adm && foundModel.parent === this.proxy && adm?.parentName === key) {
|
|
3053
|
+
adm.loadSnapshot(snapshot2);
|
|
3054
|
+
model22 = foundModel;
|
|
3055
|
+
} else {
|
|
3056
|
+
model22 = Ctor.create(snapshot2);
|
|
3057
|
+
}
|
|
3058
|
+
}
|
|
3059
|
+
return model22;
|
|
2841
3060
|
}
|
|
3061
|
+
);
|
|
3062
|
+
break;
|
|
3063
|
+
} else if (value instanceof Model) {
|
|
3064
|
+
model2 = value;
|
|
3065
|
+
} else {
|
|
3066
|
+
ensureChildTypes(key);
|
|
3067
|
+
const id2 = childType2 && getSnapshotId(value, childType2);
|
|
3068
|
+
if (id2 != null && this.proxy[key] && id2 === getIdentifier(this.proxy[key])) {
|
|
3069
|
+
const adm = getModelAdm(this.proxy[key]);
|
|
3070
|
+
adm.loadSnapshot(value);
|
|
3071
|
+
model2 = this.proxy[key];
|
|
3072
|
+
} else {
|
|
3073
|
+
model2 = childType2.create(value);
|
|
2842
3074
|
}
|
|
2843
|
-
return model22;
|
|
2844
3075
|
}
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
const id2 = childType2 && getSnapshotId(value, childType2);
|
|
2852
|
-
if (id2 != null && this.proxy[key] && id2 === getIdentifier(this.proxy[key])) {
|
|
2853
|
-
const adm = getModelAdm(this.proxy[key]);
|
|
2854
|
-
adm.loadSnapshot(value);
|
|
2855
|
-
model2 = this.proxy[key];
|
|
2856
|
-
} else {
|
|
2857
|
-
model2 = childType2.create(value);
|
|
2858
|
-
}
|
|
3076
|
+
this.proxy[key] = model2;
|
|
3077
|
+
break;
|
|
3078
|
+
default:
|
|
3079
|
+
console.warn(
|
|
3080
|
+
`r-state-tree: invalid key '${key}' found in snapshot, ignored.`
|
|
3081
|
+
);
|
|
2859
3082
|
}
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
default:
|
|
2863
|
-
console.warn(
|
|
2864
|
-
`r-state-tree: invalid key '${key}' found in snapshot, ignored.`
|
|
2865
|
-
);
|
|
2866
|
-
}
|
|
3083
|
+
});
|
|
3084
|
+
});
|
|
2867
3085
|
});
|
|
2868
3086
|
});
|
|
2869
3087
|
}
|
|
@@ -2880,9 +3098,6 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2880
3098
|
}
|
|
2881
3099
|
let initEnabled = false;
|
|
2882
3100
|
class Model {
|
|
2883
|
-
static get types() {
|
|
2884
|
-
return this[Symbol.metadata];
|
|
2885
|
-
}
|
|
2886
3101
|
static childTypes = {};
|
|
2887
3102
|
static create(snapshot, ...args) {
|
|
2888
3103
|
let instance;
|
|
@@ -2909,7 +3124,9 @@ class Model {
|
|
|
2909
3124
|
);
|
|
2910
3125
|
const adm = getModelAdm(observable2);
|
|
2911
3126
|
adm.setConfiguration(
|
|
2912
|
-
() =>
|
|
3127
|
+
() => getConfigurationForCtor(
|
|
3128
|
+
this.constructor
|
|
3129
|
+
) ?? {}
|
|
2913
3130
|
);
|
|
2914
3131
|
return observable2;
|
|
2915
3132
|
}
|
|
@@ -3055,29 +3272,6 @@ function toObservableTreeInternal(value, ancestorPath, path) {
|
|
|
3055
3272
|
}
|
|
3056
3273
|
return value;
|
|
3057
3274
|
}
|
|
3058
|
-
function makeDecorator(type) {
|
|
3059
|
-
return function(value, context) {
|
|
3060
|
-
context.metadata[context.name] = type;
|
|
3061
|
-
return value;
|
|
3062
|
-
};
|
|
3063
|
-
}
|
|
3064
|
-
function makeChildDecorator(typeObj) {
|
|
3065
|
-
return function(valueOrChildType, context) {
|
|
3066
|
-
if (context !== void 0) {
|
|
3067
|
-
return makeDecorator(typeObj)(valueOrChildType, context);
|
|
3068
|
-
}
|
|
3069
|
-
const childCtor = valueOrChildType;
|
|
3070
|
-
return function(value, context2) {
|
|
3071
|
-
const typeWithCtor = typeObj(childCtor);
|
|
3072
|
-
return makeDecorator(typeWithCtor)(value, context2);
|
|
3073
|
-
};
|
|
3074
|
-
};
|
|
3075
|
-
}
|
|
3076
|
-
const child = makeChildDecorator(childType);
|
|
3077
|
-
const modelRef = makeChildDecorator(modelRefType);
|
|
3078
|
-
const model = makeDecorator(modelType);
|
|
3079
|
-
const id = makeDecorator(idType);
|
|
3080
|
-
const state = makeDecorator(stateType);
|
|
3081
3275
|
Object.defineProperty(exports, "Signal", {
|
|
3082
3276
|
enumerable: true,
|
|
3083
3277
|
get: () => signalsCore.Signal
|