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