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.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
|
}
|
|
@@ -74,10 +208,12 @@ function getPropertyType(key, obj) {
|
|
|
74
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:
|
|
@@ -616,11 +752,12 @@ function signal(value) {
|
|
|
616
752
|
}
|
|
617
753
|
function computed(value, context) {
|
|
618
754
|
if (context && typeof context === "object" && "kind" in context) {
|
|
619
|
-
context.metadata[context.name] =
|
|
755
|
+
context.metadata[context.name] = computedType;
|
|
620
756
|
return value;
|
|
621
757
|
}
|
|
622
758
|
return internalCreateComputed(value).c;
|
|
623
759
|
}
|
|
760
|
+
computed.type = computedType.type;
|
|
624
761
|
function source(obj) {
|
|
625
762
|
return getSource(obj);
|
|
626
763
|
}
|
|
@@ -1470,9 +1607,22 @@ function createFilterMethod(method) {
|
|
|
1470
1607
|
function(callback, thisArg) {
|
|
1471
1608
|
const adm = getAdministration(this);
|
|
1472
1609
|
adm.reportObserved();
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
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
|
+
);
|
|
1476
1626
|
}
|
|
1477
1627
|
);
|
|
1478
1628
|
}
|
|
@@ -1480,7 +1630,22 @@ function createReduceMethod(method) {
|
|
|
1480
1630
|
return createMethod(method, function() {
|
|
1481
1631
|
const adm = getAdministration(this);
|
|
1482
1632
|
adm.reportObserved();
|
|
1483
|
-
|
|
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);
|
|
1484
1649
|
});
|
|
1485
1650
|
}
|
|
1486
1651
|
class DateAdministration extends Administration {
|
|
@@ -1714,7 +1879,7 @@ function getDiff(o1, o2, getConfig) {
|
|
|
1714
1879
|
for (let i = 0; i < keys.length; i++) {
|
|
1715
1880
|
const key = keys[i];
|
|
1716
1881
|
if (obj1[key] !== obj2[key]) {
|
|
1717
|
-
if (config?.[key]
|
|
1882
|
+
if (getConfigType(config?.[key]) === CommonCfgTypes.child) {
|
|
1718
1883
|
const value = obj2[key];
|
|
1719
1884
|
if (Array.isArray(value)) {
|
|
1720
1885
|
diff[key] = value.map((model2, index) => {
|
|
@@ -1823,7 +1988,7 @@ class StoreAdministration extends PreactObjectAdministration {
|
|
|
1823
1988
|
return target.key;
|
|
1824
1989
|
}
|
|
1825
1990
|
const adm = getAdministration(target);
|
|
1826
|
-
switch (adm.configuration[name]
|
|
1991
|
+
switch (getConfigType(adm.configuration[name])) {
|
|
1827
1992
|
case CommonCfgTypes.child:
|
|
1828
1993
|
return adm.getStore(name);
|
|
1829
1994
|
case StoreCfgTypes.model:
|
|
@@ -1840,7 +2005,7 @@ class StoreAdministration extends PreactObjectAdministration {
|
|
|
1840
2005
|
if (name === "props") {
|
|
1841
2006
|
throw new Error(`r-state-tree: ${name} is read-only`);
|
|
1842
2007
|
}
|
|
1843
|
-
if (adm.configuration[name]
|
|
2008
|
+
if (getConfigType(adm.configuration[name]) === StoreCfgTypes.model) {
|
|
1844
2009
|
if (value !== void 0) {
|
|
1845
2010
|
throw new Error(`r-state-tree: model ${String(name)} is read-only`);
|
|
1846
2011
|
}
|
|
@@ -2117,9 +2282,6 @@ function updateStore(store, props) {
|
|
|
2117
2282
|
return store;
|
|
2118
2283
|
}
|
|
2119
2284
|
class Store {
|
|
2120
|
-
static get types() {
|
|
2121
|
-
return this[Symbol.metadata];
|
|
2122
|
-
}
|
|
2123
2285
|
props;
|
|
2124
2286
|
constructor(props) {
|
|
2125
2287
|
if (!initEnabled$1) {
|
|
@@ -2131,7 +2293,9 @@ class Store {
|
|
|
2131
2293
|
);
|
|
2132
2294
|
const adm = getStoreAdm(observable2);
|
|
2133
2295
|
adm.setConfiguration(
|
|
2134
|
-
() =>
|
|
2296
|
+
() => getConfigurationForCtor(
|
|
2297
|
+
this.constructor
|
|
2298
|
+
) ?? {}
|
|
2135
2299
|
);
|
|
2136
2300
|
adm.write("props", getObservable({}));
|
|
2137
2301
|
updateProps(observable2.props, props);
|
|
@@ -2367,8 +2531,9 @@ function getModelAdm(model2) {
|
|
|
2367
2531
|
}
|
|
2368
2532
|
function getIdKey(Ctor) {
|
|
2369
2533
|
if (!ctorIdKeyMap.has(Ctor)) {
|
|
2370
|
-
const
|
|
2371
|
-
|
|
2534
|
+
const config = getConfigurationForCtor(Ctor);
|
|
2535
|
+
const key = Object.keys(config).find(
|
|
2536
|
+
(prop) => config[prop]?.type === ModelCfgTypes.id
|
|
2372
2537
|
) ?? null;
|
|
2373
2538
|
ctorIdKeyMap.set(Ctor, key);
|
|
2374
2539
|
}
|
|
@@ -2417,6 +2582,12 @@ function validateModelChildValue(value, propertyName) {
|
|
|
2417
2582
|
);
|
|
2418
2583
|
}
|
|
2419
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
|
+
}
|
|
2420
2591
|
static proxyTraps = Object.assign(
|
|
2421
2592
|
{},
|
|
2422
2593
|
PreactObjectAdministration.proxyTraps,
|
|
@@ -2426,7 +2597,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2426
2597
|
if (prop === "parent") {
|
|
2427
2598
|
return target.parent;
|
|
2428
2599
|
}
|
|
2429
|
-
switch (adm.
|
|
2600
|
+
switch (adm.getCfgType(prop)) {
|
|
2430
2601
|
case ModelCfgTypes.modelRef:
|
|
2431
2602
|
if (Array.isArray(adm.source[prop])) {
|
|
2432
2603
|
return adm.getModelRefs(prop);
|
|
@@ -2443,7 +2614,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2443
2614
|
const adm = getAdministration(target);
|
|
2444
2615
|
adm.writeInProgress.add(name);
|
|
2445
2616
|
try {
|
|
2446
|
-
switch (adm.
|
|
2617
|
+
switch (adm.getCfgType(name)) {
|
|
2447
2618
|
case ModelCfgTypes.modelRef: {
|
|
2448
2619
|
Array.isArray(value) ? adm.setModelRefs(name, value) : adm.setModelRef(name, value);
|
|
2449
2620
|
return true;
|
|
@@ -2478,7 +2649,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2478
2649
|
defineProperty(target, name, desc) {
|
|
2479
2650
|
const adm = getAdministration(target);
|
|
2480
2651
|
if (desc && "value" in desc && !adm.writeInProgress.has(name)) {
|
|
2481
|
-
switch (adm.
|
|
2652
|
+
switch (adm.getCfgType(name)) {
|
|
2482
2653
|
case ModelCfgTypes.modelRef:
|
|
2483
2654
|
case CommonCfgTypes.child:
|
|
2484
2655
|
case ModelCfgTypes.id: {
|
|
@@ -2542,6 +2713,39 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2542
2713
|
});
|
|
2543
2714
|
}
|
|
2544
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
|
+
}
|
|
2545
2749
|
setId(name, v) {
|
|
2546
2750
|
const id2 = getIdentifier(this.proxy);
|
|
2547
2751
|
if (id2 === v) {
|
|
@@ -2732,7 +2936,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2732
2936
|
}
|
|
2733
2937
|
toJSON() {
|
|
2734
2938
|
return Object.keys(this.configuration).reduce((json, key) => {
|
|
2735
|
-
switch (this.configuration[key]
|
|
2939
|
+
switch (getConfigType(this.configuration[key])) {
|
|
2736
2940
|
case ModelCfgTypes.state: {
|
|
2737
2941
|
json[key] = clone(this.proxy[key], key);
|
|
2738
2942
|
break;
|
|
@@ -2789,7 +2993,8 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2789
2993
|
}
|
|
2790
2994
|
loadSnapshot(snapshot) {
|
|
2791
2995
|
const ensureChildTypes = (key) => {
|
|
2792
|
-
|
|
2996
|
+
const childType2 = this.getCfgChildType(key);
|
|
2997
|
+
if (!childType2) {
|
|
2793
2998
|
throw new Error(
|
|
2794
2999
|
"r-state-tree: child constructor must be specified to load snapshots with child/children. eg: `@child(ChildCtor) MyChild`"
|
|
2795
3000
|
);
|
|
@@ -2797,80 +3002,85 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2797
3002
|
return true;
|
|
2798
3003
|
};
|
|
2799
3004
|
onSnapshotLoad(() => {
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
);
|
|
2815
|
-
this.referencedAtoms?.get(key)?.reportChanged();
|
|
2816
|
-
}
|
|
2817
|
-
break;
|
|
2818
|
-
} else if (value instanceof Model) {
|
|
2819
|
-
this.proxy[key] = value;
|
|
2820
|
-
} else {
|
|
2821
|
-
this.source[key] = getSnapshotRefId(value);
|
|
2822
|
-
this.referencedAtoms?.get(key)?.reportChanged();
|
|
2823
|
-
}
|
|
2824
|
-
break;
|
|
2825
|
-
case ModelCfgTypes.id:
|
|
2826
|
-
this.setId(key, value);
|
|
2827
|
-
break;
|
|
2828
|
-
case CommonCfgTypes.child:
|
|
2829
|
-
let model2;
|
|
2830
|
-
if (Array.isArray(value)) {
|
|
2831
|
-
const Ctor = childType2;
|
|
2832
|
-
this.proxy[key] = value?.map(
|
|
2833
|
-
(snapshot2, index) => {
|
|
2834
|
-
snapshot2 = snapshot2 ?? {};
|
|
2835
|
-
let model22;
|
|
2836
|
-
if (snapshot2 instanceof Model) {
|
|
2837
|
-
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;
|
|
2838
3019
|
} else {
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
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;
|
|
2848
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);
|
|
2849
3073
|
}
|
|
2850
|
-
return model22;
|
|
2851
3074
|
}
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
const id2 = childType2 && getSnapshotId(value, childType2);
|
|
2859
|
-
if (id2 != null && this.proxy[key] && id2 === getIdentifier(this.proxy[key])) {
|
|
2860
|
-
const adm = getModelAdm(this.proxy[key]);
|
|
2861
|
-
adm.loadSnapshot(value);
|
|
2862
|
-
model2 = this.proxy[key];
|
|
2863
|
-
} else {
|
|
2864
|
-
model2 = childType2.create(value);
|
|
2865
|
-
}
|
|
3075
|
+
this.proxy[key] = model2;
|
|
3076
|
+
break;
|
|
3077
|
+
default:
|
|
3078
|
+
console.warn(
|
|
3079
|
+
`r-state-tree: invalid key '${key}' found in snapshot, ignored.`
|
|
3080
|
+
);
|
|
2866
3081
|
}
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
default:
|
|
2870
|
-
console.warn(
|
|
2871
|
-
`r-state-tree: invalid key '${key}' found in snapshot, ignored.`
|
|
2872
|
-
);
|
|
2873
|
-
}
|
|
3082
|
+
});
|
|
3083
|
+
});
|
|
2874
3084
|
});
|
|
2875
3085
|
});
|
|
2876
3086
|
}
|
|
@@ -2887,9 +3097,6 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2887
3097
|
}
|
|
2888
3098
|
let initEnabled = false;
|
|
2889
3099
|
class Model {
|
|
2890
|
-
static get types() {
|
|
2891
|
-
return this[Symbol.metadata];
|
|
2892
|
-
}
|
|
2893
3100
|
static childTypes = {};
|
|
2894
3101
|
static create(snapshot, ...args) {
|
|
2895
3102
|
let instance;
|
|
@@ -2916,7 +3123,9 @@ class Model {
|
|
|
2916
3123
|
);
|
|
2917
3124
|
const adm = getModelAdm(observable2);
|
|
2918
3125
|
adm.setConfiguration(
|
|
2919
|
-
() =>
|
|
3126
|
+
() => getConfigurationForCtor(
|
|
3127
|
+
this.constructor
|
|
3128
|
+
) ?? {}
|
|
2920
3129
|
);
|
|
2921
3130
|
return observable2;
|
|
2922
3131
|
}
|
|
@@ -3062,29 +3271,6 @@ function toObservableTreeInternal(value, ancestorPath, path) {
|
|
|
3062
3271
|
}
|
|
3063
3272
|
return value;
|
|
3064
3273
|
}
|
|
3065
|
-
function makeDecorator(type) {
|
|
3066
|
-
return function(value, context) {
|
|
3067
|
-
context.metadata[context.name] = type;
|
|
3068
|
-
return value;
|
|
3069
|
-
};
|
|
3070
|
-
}
|
|
3071
|
-
function makeChildDecorator(typeObj) {
|
|
3072
|
-
return function(valueOrChildType, context) {
|
|
3073
|
-
if (context !== void 0) {
|
|
3074
|
-
return makeDecorator(typeObj)(valueOrChildType, context);
|
|
3075
|
-
}
|
|
3076
|
-
const childCtor = valueOrChildType;
|
|
3077
|
-
return function(value, context2) {
|
|
3078
|
-
const typeWithCtor = typeObj(childCtor);
|
|
3079
|
-
return makeDecorator(typeWithCtor)(value, context2);
|
|
3080
|
-
};
|
|
3081
|
-
};
|
|
3082
|
-
}
|
|
3083
|
-
const child = makeChildDecorator(childType);
|
|
3084
|
-
const modelRef = makeChildDecorator(modelRefType);
|
|
3085
|
-
const model = makeDecorator(modelType);
|
|
3086
|
-
const id = makeDecorator(idType);
|
|
3087
|
-
const state = makeDecorator(stateType);
|
|
3088
3274
|
export {
|
|
3089
3275
|
Model,
|
|
3090
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;
|
package/dist/types.d.ts
CHANGED
|
@@ -45,11 +45,12 @@ export declare enum ObservableCfgTypes {
|
|
|
45
45
|
computed = "computed"
|
|
46
46
|
}
|
|
47
47
|
export type ConfigurationTypes = CommonCfgTypes | ModelCfgTypes | StoreCfgTypes | ObservableCfgTypes;
|
|
48
|
-
export type
|
|
48
|
+
export type ConfigurationValue = {
|
|
49
49
|
type: ConfigurationTypes;
|
|
50
50
|
childType?: Function;
|
|
51
51
|
[key: string]: unknown;
|
|
52
52
|
};
|
|
53
|
+
export type ConfigurationType = ConfigurationValue | Function;
|
|
53
54
|
export type ModelConfiguration<T> = Record<PropertyKey, ConfigurationType>;
|
|
54
55
|
export type StoreConfiguration<T> = Record<PropertyKey, ConfigurationType>;
|
|
55
56
|
export type Configuration<T> = ModelConfiguration<T> | StoreConfiguration<T>;
|
|
@@ -69,14 +70,14 @@ export type RefSnapshot = {
|
|
|
69
70
|
[key: string]: IdType;
|
|
70
71
|
[key: number]: IdType;
|
|
71
72
|
};
|
|
72
|
-
export declare const childType: ((childType: Function) =>
|
|
73
|
+
export declare const childType: ((childType: Function) => ConfigurationValue) & {
|
|
73
74
|
type: CommonCfgTypes;
|
|
74
75
|
};
|
|
75
|
-
export declare const stateType:
|
|
76
|
-
export declare const modelRefType: ((childType: Function) =>
|
|
76
|
+
export declare const stateType: ConfigurationValue;
|
|
77
|
+
export declare const modelRefType: ((childType: Function) => ConfigurationValue) & {
|
|
77
78
|
type: ModelCfgTypes;
|
|
78
79
|
};
|
|
79
|
-
export declare const idType:
|
|
80
|
-
export declare const modelType:
|
|
81
|
-
export declare const computedType:
|
|
80
|
+
export declare const idType: ConfigurationValue;
|
|
81
|
+
export declare const modelType: ConfigurationValue;
|
|
82
|
+
export declare const computedType: ConfigurationValue;
|
|
82
83
|
export {};
|