r-state-tree 0.6.3 → 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/r-state-tree.cjs +304 -118
- package/dist/r-state-tree.js +304 -118
- 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
|
}
|
|
@@ -75,10 +209,12 @@ function getPropertyType(key, obj) {
|
|
|
75
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:
|
|
@@ -617,11 +753,12 @@ function signal(value) {
|
|
|
617
753
|
}
|
|
618
754
|
function computed(value, context) {
|
|
619
755
|
if (context && typeof context === "object" && "kind" in context) {
|
|
620
|
-
context.metadata[context.name] =
|
|
756
|
+
context.metadata[context.name] = computedType;
|
|
621
757
|
return value;
|
|
622
758
|
}
|
|
623
759
|
return internalCreateComputed(value).c;
|
|
624
760
|
}
|
|
761
|
+
computed.type = computedType.type;
|
|
625
762
|
function source(obj) {
|
|
626
763
|
return getSource(obj);
|
|
627
764
|
}
|
|
@@ -1471,9 +1608,22 @@ function createFilterMethod(method) {
|
|
|
1471
1608
|
function(callback, thisArg) {
|
|
1472
1609
|
const adm = getAdministration(this);
|
|
1473
1610
|
adm.reportObserved();
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
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
|
+
);
|
|
1477
1627
|
}
|
|
1478
1628
|
);
|
|
1479
1629
|
}
|
|
@@ -1481,7 +1631,22 @@ function createReduceMethod(method) {
|
|
|
1481
1631
|
return createMethod(method, function() {
|
|
1482
1632
|
const adm = getAdministration(this);
|
|
1483
1633
|
adm.reportObserved();
|
|
1484
|
-
|
|
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);
|
|
1485
1650
|
});
|
|
1486
1651
|
}
|
|
1487
1652
|
class DateAdministration extends Administration {
|
|
@@ -1715,7 +1880,7 @@ function getDiff(o1, o2, getConfig) {
|
|
|
1715
1880
|
for (let i = 0; i < keys.length; i++) {
|
|
1716
1881
|
const key = keys[i];
|
|
1717
1882
|
if (obj1[key] !== obj2[key]) {
|
|
1718
|
-
if (config?.[key]
|
|
1883
|
+
if (getConfigType(config?.[key]) === CommonCfgTypes.child) {
|
|
1719
1884
|
const value = obj2[key];
|
|
1720
1885
|
if (Array.isArray(value)) {
|
|
1721
1886
|
diff[key] = value.map((model2, index) => {
|
|
@@ -1824,7 +1989,7 @@ class StoreAdministration extends PreactObjectAdministration {
|
|
|
1824
1989
|
return target.key;
|
|
1825
1990
|
}
|
|
1826
1991
|
const adm = getAdministration(target);
|
|
1827
|
-
switch (adm.configuration[name]
|
|
1992
|
+
switch (getConfigType(adm.configuration[name])) {
|
|
1828
1993
|
case CommonCfgTypes.child:
|
|
1829
1994
|
return adm.getStore(name);
|
|
1830
1995
|
case StoreCfgTypes.model:
|
|
@@ -1841,7 +2006,7 @@ class StoreAdministration extends PreactObjectAdministration {
|
|
|
1841
2006
|
if (name === "props") {
|
|
1842
2007
|
throw new Error(`r-state-tree: ${name} is read-only`);
|
|
1843
2008
|
}
|
|
1844
|
-
if (adm.configuration[name]
|
|
2009
|
+
if (getConfigType(adm.configuration[name]) === StoreCfgTypes.model) {
|
|
1845
2010
|
if (value !== void 0) {
|
|
1846
2011
|
throw new Error(`r-state-tree: model ${String(name)} is read-only`);
|
|
1847
2012
|
}
|
|
@@ -2118,9 +2283,6 @@ function updateStore(store, props) {
|
|
|
2118
2283
|
return store;
|
|
2119
2284
|
}
|
|
2120
2285
|
class Store {
|
|
2121
|
-
static get types() {
|
|
2122
|
-
return this[Symbol.metadata];
|
|
2123
|
-
}
|
|
2124
2286
|
props;
|
|
2125
2287
|
constructor(props) {
|
|
2126
2288
|
if (!initEnabled$1) {
|
|
@@ -2132,7 +2294,9 @@ class Store {
|
|
|
2132
2294
|
);
|
|
2133
2295
|
const adm = getStoreAdm(observable2);
|
|
2134
2296
|
adm.setConfiguration(
|
|
2135
|
-
() =>
|
|
2297
|
+
() => getConfigurationForCtor(
|
|
2298
|
+
this.constructor
|
|
2299
|
+
) ?? {}
|
|
2136
2300
|
);
|
|
2137
2301
|
adm.write("props", getObservable({}));
|
|
2138
2302
|
updateProps(observable2.props, props);
|
|
@@ -2368,8 +2532,9 @@ function getModelAdm(model2) {
|
|
|
2368
2532
|
}
|
|
2369
2533
|
function getIdKey(Ctor) {
|
|
2370
2534
|
if (!ctorIdKeyMap.has(Ctor)) {
|
|
2371
|
-
const
|
|
2372
|
-
|
|
2535
|
+
const config = getConfigurationForCtor(Ctor);
|
|
2536
|
+
const key = Object.keys(config).find(
|
|
2537
|
+
(prop) => config[prop]?.type === ModelCfgTypes.id
|
|
2373
2538
|
) ?? null;
|
|
2374
2539
|
ctorIdKeyMap.set(Ctor, key);
|
|
2375
2540
|
}
|
|
@@ -2418,6 +2583,12 @@ function validateModelChildValue(value, propertyName) {
|
|
|
2418
2583
|
);
|
|
2419
2584
|
}
|
|
2420
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
|
+
}
|
|
2421
2592
|
static proxyTraps = Object.assign(
|
|
2422
2593
|
{},
|
|
2423
2594
|
PreactObjectAdministration.proxyTraps,
|
|
@@ -2427,7 +2598,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2427
2598
|
if (prop === "parent") {
|
|
2428
2599
|
return target.parent;
|
|
2429
2600
|
}
|
|
2430
|
-
switch (adm.
|
|
2601
|
+
switch (adm.getCfgType(prop)) {
|
|
2431
2602
|
case ModelCfgTypes.modelRef:
|
|
2432
2603
|
if (Array.isArray(adm.source[prop])) {
|
|
2433
2604
|
return adm.getModelRefs(prop);
|
|
@@ -2444,7 +2615,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2444
2615
|
const adm = getAdministration(target);
|
|
2445
2616
|
adm.writeInProgress.add(name);
|
|
2446
2617
|
try {
|
|
2447
|
-
switch (adm.
|
|
2618
|
+
switch (adm.getCfgType(name)) {
|
|
2448
2619
|
case ModelCfgTypes.modelRef: {
|
|
2449
2620
|
Array.isArray(value) ? adm.setModelRefs(name, value) : adm.setModelRef(name, value);
|
|
2450
2621
|
return true;
|
|
@@ -2479,7 +2650,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2479
2650
|
defineProperty(target, name, desc) {
|
|
2480
2651
|
const adm = getAdministration(target);
|
|
2481
2652
|
if (desc && "value" in desc && !adm.writeInProgress.has(name)) {
|
|
2482
|
-
switch (adm.
|
|
2653
|
+
switch (adm.getCfgType(name)) {
|
|
2483
2654
|
case ModelCfgTypes.modelRef:
|
|
2484
2655
|
case CommonCfgTypes.child:
|
|
2485
2656
|
case ModelCfgTypes.id: {
|
|
@@ -2543,6 +2714,39 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2543
2714
|
});
|
|
2544
2715
|
}
|
|
2545
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
|
+
}
|
|
2546
2750
|
setId(name, v) {
|
|
2547
2751
|
const id2 = getIdentifier(this.proxy);
|
|
2548
2752
|
if (id2 === v) {
|
|
@@ -2733,7 +2937,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2733
2937
|
}
|
|
2734
2938
|
toJSON() {
|
|
2735
2939
|
return Object.keys(this.configuration).reduce((json, key) => {
|
|
2736
|
-
switch (this.configuration[key]
|
|
2940
|
+
switch (getConfigType(this.configuration[key])) {
|
|
2737
2941
|
case ModelCfgTypes.state: {
|
|
2738
2942
|
json[key] = clone(this.proxy[key], key);
|
|
2739
2943
|
break;
|
|
@@ -2790,7 +2994,8 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2790
2994
|
}
|
|
2791
2995
|
loadSnapshot(snapshot) {
|
|
2792
2996
|
const ensureChildTypes = (key) => {
|
|
2793
|
-
|
|
2997
|
+
const childType2 = this.getCfgChildType(key);
|
|
2998
|
+
if (!childType2) {
|
|
2794
2999
|
throw new Error(
|
|
2795
3000
|
"r-state-tree: child constructor must be specified to load snapshots with child/children. eg: `@child(ChildCtor) MyChild`"
|
|
2796
3001
|
);
|
|
@@ -2798,80 +3003,85 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2798
3003
|
return true;
|
|
2799
3004
|
};
|
|
2800
3005
|
onSnapshotLoad(() => {
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
);
|
|
2816
|
-
this.referencedAtoms?.get(key)?.reportChanged();
|
|
2817
|
-
}
|
|
2818
|
-
break;
|
|
2819
|
-
} else if (value instanceof Model) {
|
|
2820
|
-
this.proxy[key] = value;
|
|
2821
|
-
} else {
|
|
2822
|
-
this.source[key] = getSnapshotRefId(value);
|
|
2823
|
-
this.referencedAtoms?.get(key)?.reportChanged();
|
|
2824
|
-
}
|
|
2825
|
-
break;
|
|
2826
|
-
case ModelCfgTypes.id:
|
|
2827
|
-
this.setId(key, value);
|
|
2828
|
-
break;
|
|
2829
|
-
case CommonCfgTypes.child:
|
|
2830
|
-
let model2;
|
|
2831
|
-
if (Array.isArray(value)) {
|
|
2832
|
-
const Ctor = childType2;
|
|
2833
|
-
this.proxy[key] = value?.map(
|
|
2834
|
-
(snapshot2, index) => {
|
|
2835
|
-
snapshot2 = snapshot2 ?? {};
|
|
2836
|
-
let model22;
|
|
2837
|
-
if (snapshot2 instanceof Model) {
|
|
2838
|
-
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;
|
|
2839
3020
|
} else {
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
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;
|
|
2849
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);
|
|
2850
3074
|
}
|
|
2851
|
-
return model22;
|
|
2852
3075
|
}
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
const id2 = childType2 && getSnapshotId(value, childType2);
|
|
2860
|
-
if (id2 != null && this.proxy[key] && id2 === getIdentifier(this.proxy[key])) {
|
|
2861
|
-
const adm = getModelAdm(this.proxy[key]);
|
|
2862
|
-
adm.loadSnapshot(value);
|
|
2863
|
-
model2 = this.proxy[key];
|
|
2864
|
-
} else {
|
|
2865
|
-
model2 = childType2.create(value);
|
|
2866
|
-
}
|
|
3076
|
+
this.proxy[key] = model2;
|
|
3077
|
+
break;
|
|
3078
|
+
default:
|
|
3079
|
+
console.warn(
|
|
3080
|
+
`r-state-tree: invalid key '${key}' found in snapshot, ignored.`
|
|
3081
|
+
);
|
|
2867
3082
|
}
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
default:
|
|
2871
|
-
console.warn(
|
|
2872
|
-
`r-state-tree: invalid key '${key}' found in snapshot, ignored.`
|
|
2873
|
-
);
|
|
2874
|
-
}
|
|
3083
|
+
});
|
|
3084
|
+
});
|
|
2875
3085
|
});
|
|
2876
3086
|
});
|
|
2877
3087
|
}
|
|
@@ -2888,9 +3098,6 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2888
3098
|
}
|
|
2889
3099
|
let initEnabled = false;
|
|
2890
3100
|
class Model {
|
|
2891
|
-
static get types() {
|
|
2892
|
-
return this[Symbol.metadata];
|
|
2893
|
-
}
|
|
2894
3101
|
static childTypes = {};
|
|
2895
3102
|
static create(snapshot, ...args) {
|
|
2896
3103
|
let instance;
|
|
@@ -2917,7 +3124,9 @@ class Model {
|
|
|
2917
3124
|
);
|
|
2918
3125
|
const adm = getModelAdm(observable2);
|
|
2919
3126
|
adm.setConfiguration(
|
|
2920
|
-
() =>
|
|
3127
|
+
() => getConfigurationForCtor(
|
|
3128
|
+
this.constructor
|
|
3129
|
+
) ?? {}
|
|
2921
3130
|
);
|
|
2922
3131
|
return observable2;
|
|
2923
3132
|
}
|
|
@@ -3063,29 +3272,6 @@ function toObservableTreeInternal(value, ancestorPath, path) {
|
|
|
3063
3272
|
}
|
|
3064
3273
|
return value;
|
|
3065
3274
|
}
|
|
3066
|
-
function makeDecorator(type) {
|
|
3067
|
-
return function(value, context) {
|
|
3068
|
-
context.metadata[context.name] = type;
|
|
3069
|
-
return value;
|
|
3070
|
-
};
|
|
3071
|
-
}
|
|
3072
|
-
function makeChildDecorator(typeObj) {
|
|
3073
|
-
return function(valueOrChildType, context) {
|
|
3074
|
-
if (context !== void 0) {
|
|
3075
|
-
return makeDecorator(typeObj)(valueOrChildType, context);
|
|
3076
|
-
}
|
|
3077
|
-
const childCtor = valueOrChildType;
|
|
3078
|
-
return function(value, context2) {
|
|
3079
|
-
const typeWithCtor = typeObj(childCtor);
|
|
3080
|
-
return makeDecorator(typeWithCtor)(value, context2);
|
|
3081
|
-
};
|
|
3082
|
-
};
|
|
3083
|
-
}
|
|
3084
|
-
const child = makeChildDecorator(childType);
|
|
3085
|
-
const modelRef = makeChildDecorator(modelRefType);
|
|
3086
|
-
const model = makeDecorator(modelType);
|
|
3087
|
-
const id = makeDecorator(idType);
|
|
3088
|
-
const state = makeDecorator(stateType);
|
|
3089
3275
|
Object.defineProperty(exports, "Signal", {
|
|
3090
3276
|
enumerable: true,
|
|
3091
3277
|
get: () => signalsCore.Signal
|