r-state-tree 0.5.0 → 0.6.1
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 +291 -69
- package/dist/index.d.ts +4 -3
- package/dist/model/ChildModelsAdministration.d.ts +1 -1
- package/dist/observables/array.d.ts +6 -3
- package/dist/observables/collection.d.ts +10 -14
- package/dist/observables/index.d.ts +2 -2
- package/dist/observables/internal/Administration.d.ts +2 -3
- package/dist/observables/internal/NodeMap.d.ts +1 -0
- package/dist/observables/internal/lookup.d.ts +3 -2
- package/dist/observables/internal/utils.d.ts +1 -1
- package/dist/observables/object.d.ts +5 -4
- package/dist/observables/preact.d.ts +6 -22
- package/dist/r-state-tree.cjs +776 -384
- package/dist/r-state-tree.js +790 -395
- package/dist/toObservableTree.d.ts +1 -0
- package/dist/types.d.ts +5 -11
- package/dist/utils.d.ts +1 -1
- package/package.json +1 -1
- package/dist/computedProxy.d.ts +0 -2
package/dist/r-state-tree.cjs
CHANGED
|
@@ -17,8 +17,6 @@ var CommonCfgTypes = /* @__PURE__ */ ((CommonCfgTypes2) => {
|
|
|
17
17
|
})(CommonCfgTypes || {});
|
|
18
18
|
var ModelCfgTypes = /* @__PURE__ */ ((ModelCfgTypes2) => {
|
|
19
19
|
ModelCfgTypes2["state"] = "state";
|
|
20
|
-
ModelCfgTypes2["stateShallow"] = "stateShallow";
|
|
21
|
-
ModelCfgTypes2["stateSignal"] = "stateSignal";
|
|
22
20
|
ModelCfgTypes2["id"] = "id";
|
|
23
21
|
ModelCfgTypes2["modelRef"] = "modelRef";
|
|
24
22
|
return ModelCfgTypes2;
|
|
@@ -28,9 +26,6 @@ var StoreCfgTypes = /* @__PURE__ */ ((StoreCfgTypes2) => {
|
|
|
28
26
|
return StoreCfgTypes2;
|
|
29
27
|
})(StoreCfgTypes || {});
|
|
30
28
|
var ObservableCfgTypes = /* @__PURE__ */ ((ObservableCfgTypes2) => {
|
|
31
|
-
ObservableCfgTypes2["observable"] = "observable";
|
|
32
|
-
ObservableCfgTypes2["observableShallow"] = "observableShallow";
|
|
33
|
-
ObservableCfgTypes2["observableSignal"] = "observableSignal";
|
|
34
29
|
ObservableCfgTypes2["computed"] = "computed";
|
|
35
30
|
return ObservableCfgTypes2;
|
|
36
31
|
})(ObservableCfgTypes || {});
|
|
@@ -47,14 +42,6 @@ const stateType = {
|
|
|
47
42
|
type: "state"
|
|
48
43
|
/* state */
|
|
49
44
|
};
|
|
50
|
-
const stateShallowType = {
|
|
51
|
-
type: "stateShallow"
|
|
52
|
-
/* stateShallow */
|
|
53
|
-
};
|
|
54
|
-
const stateSignalType = {
|
|
55
|
-
type: "stateSignal"
|
|
56
|
-
/* stateSignal */
|
|
57
|
-
};
|
|
58
45
|
const modelRefType = Object.assign(
|
|
59
46
|
function(childType2) {
|
|
60
47
|
return { type: "modelRef", childType: childType2 };
|
|
@@ -79,32 +66,29 @@ function isPropertyKey(val) {
|
|
|
79
66
|
return typeof val === "string" || typeof val === "number" || typeof val === "symbol";
|
|
80
67
|
}
|
|
81
68
|
function getPropertyType(key, obj) {
|
|
82
|
-
const
|
|
69
|
+
const ctor = obj.constructor;
|
|
70
|
+
const hasMetadata = ctor != null && ctor[Symbol.metadata] !== void 0;
|
|
83
71
|
const descriptor = getPropertyDescriptor$1(obj, key);
|
|
84
72
|
if (descriptor?.value && typeof descriptor.value === "function") {
|
|
85
73
|
return "action";
|
|
86
74
|
}
|
|
87
|
-
const
|
|
75
|
+
const isAccessor = descriptor && (typeof descriptor.get === "function" || typeof descriptor.set === "function");
|
|
88
76
|
if (!hasMetadata) {
|
|
89
|
-
if (
|
|
77
|
+
if (descriptor?.get) {
|
|
90
78
|
return "computed";
|
|
91
79
|
}
|
|
80
|
+
if (isAccessor) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
92
83
|
return "observable";
|
|
93
84
|
}
|
|
94
85
|
const metadata = obj.constructor[Symbol.metadata];
|
|
95
|
-
|
|
96
|
-
|
|
86
|
+
const config = metadata?.[key];
|
|
87
|
+
if (config) {
|
|
97
88
|
switch (config.type) {
|
|
98
89
|
case ObservableCfgTypes.computed:
|
|
99
90
|
return "computed";
|
|
100
|
-
case ObservableCfgTypes.observableShallow:
|
|
101
|
-
case ModelCfgTypes.stateShallow:
|
|
102
|
-
return "observableShallow";
|
|
103
|
-
case ObservableCfgTypes.observableSignal:
|
|
104
|
-
case ModelCfgTypes.stateSignal:
|
|
105
|
-
return "observableSignal";
|
|
106
91
|
case ModelCfgTypes.state:
|
|
107
|
-
case ObservableCfgTypes.observable:
|
|
108
92
|
case ModelCfgTypes.id:
|
|
109
93
|
case ModelCfgTypes.modelRef:
|
|
110
94
|
case CommonCfgTypes.child:
|
|
@@ -112,7 +96,7 @@ function getPropertyType(key, obj) {
|
|
|
112
96
|
return "observable";
|
|
113
97
|
}
|
|
114
98
|
}
|
|
115
|
-
return null;
|
|
99
|
+
return isAccessor ? null : "observable";
|
|
116
100
|
}
|
|
117
101
|
function getPropertyDescriptor$1(obj, key) {
|
|
118
102
|
let node = obj;
|
|
@@ -141,7 +125,7 @@ class Administration {
|
|
|
141
125
|
atom;
|
|
142
126
|
valuesMap;
|
|
143
127
|
isObserved = false;
|
|
144
|
-
|
|
128
|
+
forceObservedAtom;
|
|
145
129
|
constructor(source2) {
|
|
146
130
|
this.atom = createObservedAtom();
|
|
147
131
|
this.source = source2;
|
|
@@ -151,13 +135,9 @@ class Administration {
|
|
|
151
135
|
);
|
|
152
136
|
}
|
|
153
137
|
flushChange() {
|
|
154
|
-
if (this.
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
this.forceObservedAtoms[i].reportChanged();
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
this.forceObservedAtoms = void 0;
|
|
138
|
+
if (this.forceObservedAtom) {
|
|
139
|
+
this.forceObservedAtom.reportChanged();
|
|
140
|
+
this.forceObservedAtom = void 0;
|
|
161
141
|
}
|
|
162
142
|
}
|
|
163
143
|
getNode() {
|
|
@@ -166,9 +146,7 @@ class Administration {
|
|
|
166
146
|
reportChanged() {
|
|
167
147
|
this.atom.reportChanged();
|
|
168
148
|
}
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
reportObserved(deep = false) {
|
|
149
|
+
reportObserved() {
|
|
172
150
|
const entry = circularRefSet == null;
|
|
173
151
|
if (entry) {
|
|
174
152
|
circularRefSet = /* @__PURE__ */ new WeakSet();
|
|
@@ -176,15 +154,10 @@ class Administration {
|
|
|
176
154
|
return;
|
|
177
155
|
}
|
|
178
156
|
circularRefSet.add(this);
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
this.forceObservedAtoms = [];
|
|
182
|
-
}
|
|
183
|
-
this.forceObservedAtoms.push(atom);
|
|
184
|
-
atom.reportObserved();
|
|
185
|
-
if (deep) {
|
|
186
|
-
this.reportObserveDeep();
|
|
157
|
+
if (!this.forceObservedAtom) {
|
|
158
|
+
this.forceObservedAtom = createAtom();
|
|
187
159
|
}
|
|
160
|
+
this.forceObservedAtom.reportObserved();
|
|
188
161
|
if (entry) {
|
|
189
162
|
circularRefSet = null;
|
|
190
163
|
}
|
|
@@ -247,6 +220,9 @@ class NodeMap {
|
|
|
247
220
|
reportChanged(key, value) {
|
|
248
221
|
return this.get(key)?.reportChanged(value);
|
|
249
222
|
}
|
|
223
|
+
keys() {
|
|
224
|
+
return this.map?.keys() ?? [];
|
|
225
|
+
}
|
|
250
226
|
}
|
|
251
227
|
class AtomMap extends NodeMap {
|
|
252
228
|
createNode() {
|
|
@@ -264,31 +240,34 @@ class ObjectAdministration extends Administration {
|
|
|
264
240
|
valuesMap;
|
|
265
241
|
computedMap;
|
|
266
242
|
types;
|
|
243
|
+
isWriting = false;
|
|
267
244
|
static proxyTraps = {
|
|
268
245
|
has(target, name) {
|
|
269
246
|
const adm = getAdministration(target);
|
|
270
|
-
if (!(name in Object.prototype)
|
|
247
|
+
if (isPropertyKey(name) && (!(name in Object.prototype) || Object.prototype.hasOwnProperty.call(adm.source, name)))
|
|
271
248
|
return adm.has(name);
|
|
272
249
|
return Reflect.has(adm.source, name);
|
|
273
250
|
},
|
|
274
251
|
get(target, name) {
|
|
275
252
|
const adm = getAdministration(target);
|
|
276
|
-
if (!(name in Object.prototype)
|
|
277
|
-
return adm.read(name);
|
|
253
|
+
if (isPropertyKey(name) && (!(name in Object.prototype) || Object.prototype.hasOwnProperty.call(adm.source, name)) && (typeof adm.source !== "function" || name !== "prototype")) {
|
|
254
|
+
return adm.read(name, arguments[2]);
|
|
278
255
|
}
|
|
279
|
-
return Reflect.get(adm.source, name,
|
|
256
|
+
return Reflect.get(adm.source, name, arguments[2]);
|
|
280
257
|
},
|
|
281
258
|
set(target, name, value) {
|
|
282
259
|
if (!isPropertyKey(name)) return false;
|
|
283
260
|
const adm = getAdministration(target);
|
|
284
|
-
|
|
285
|
-
|
|
261
|
+
const receiver = arguments[3];
|
|
262
|
+
if (receiver === adm.proxy) {
|
|
263
|
+
return adm.write(name, value);
|
|
264
|
+
}
|
|
265
|
+
return Reflect.set(adm.source, name, value, receiver);
|
|
286
266
|
},
|
|
287
267
|
deleteProperty(target, name) {
|
|
288
268
|
if (!isPropertyKey(name)) return false;
|
|
289
269
|
const adm = getAdministration(target);
|
|
290
|
-
adm.remove(name);
|
|
291
|
-
return true;
|
|
270
|
+
return adm.remove(name);
|
|
292
271
|
},
|
|
293
272
|
ownKeys(target) {
|
|
294
273
|
const adm = getAdministration(target);
|
|
@@ -297,22 +276,46 @@ class ObjectAdministration extends Administration {
|
|
|
297
276
|
adm.atom.reportObserved();
|
|
298
277
|
});
|
|
299
278
|
return Reflect.ownKeys(adm.source);
|
|
279
|
+
},
|
|
280
|
+
defineProperty(target, name, descriptor) {
|
|
281
|
+
const adm = getAdministration(target);
|
|
282
|
+
if (adm.isWriting) {
|
|
283
|
+
return Reflect.defineProperty(adm.source, name, descriptor);
|
|
284
|
+
}
|
|
285
|
+
const result = Reflect.defineProperty(adm.source, name, descriptor);
|
|
286
|
+
if (result) {
|
|
287
|
+
signalsCore.batch(() => {
|
|
288
|
+
adm.types.delete(name);
|
|
289
|
+
adm.flushChange();
|
|
290
|
+
adm.keysAtom.reportChanged();
|
|
291
|
+
adm.hasMap.reportChanged(name);
|
|
292
|
+
if ("value" in descriptor) {
|
|
293
|
+
if (isObservable(descriptor.value)) {
|
|
294
|
+
adm.explicitObservables.add(name);
|
|
295
|
+
} else {
|
|
296
|
+
adm.explicitObservables.delete(name);
|
|
297
|
+
}
|
|
298
|
+
adm.valuesMap.reportChanged(name, descriptor.value);
|
|
299
|
+
} else {
|
|
300
|
+
adm.valuesMap.reportChanged(name, void 0);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
return result;
|
|
300
305
|
}
|
|
301
306
|
};
|
|
302
307
|
constructor(source2 = {}) {
|
|
303
308
|
super(source2);
|
|
304
309
|
this.keysAtom = createAtom();
|
|
305
310
|
this.hasMap = new AtomMap(this.atom);
|
|
306
|
-
this.valuesMap = new SignalMap();
|
|
311
|
+
this.valuesMap = new SignalMap(this.atom);
|
|
307
312
|
this.types = /* @__PURE__ */ new Map();
|
|
308
313
|
}
|
|
309
314
|
get(key) {
|
|
310
315
|
return Reflect.get(this.source, key, this.proxy);
|
|
311
316
|
}
|
|
312
317
|
set(key, value) {
|
|
313
|
-
signalsCore.batch(() =>
|
|
314
|
-
Reflect.set(this.source, key, value, this.proxy);
|
|
315
|
-
});
|
|
318
|
+
return signalsCore.batch(() => Reflect.set(this.source, key, value, this.proxy));
|
|
316
319
|
}
|
|
317
320
|
getComputed(key) {
|
|
318
321
|
if (!this.computedMap) this.computedMap = /* @__PURE__ */ new Map();
|
|
@@ -339,17 +342,6 @@ class ObjectAdministration extends Administration {
|
|
|
339
342
|
}
|
|
340
343
|
return type;
|
|
341
344
|
}
|
|
342
|
-
reportObserveDeep() {
|
|
343
|
-
Object.getOwnPropertyNames(this.source).forEach((name) => {
|
|
344
|
-
const type = this.getType(name);
|
|
345
|
-
if (type === "observable") {
|
|
346
|
-
const value = this.source[name];
|
|
347
|
-
if (value && typeof value === "object") {
|
|
348
|
-
getAdministration(getObservable(value))?.reportObserved();
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
});
|
|
352
|
-
}
|
|
353
345
|
reportChanged() {
|
|
354
346
|
this.types.clear();
|
|
355
347
|
super.reportChanged();
|
|
@@ -364,14 +356,13 @@ class ObjectAdministration extends Administration {
|
|
|
364
356
|
}
|
|
365
357
|
return resolveNode(this.valuesMap.getOrCreate(key, this.source[key]));
|
|
366
358
|
}
|
|
367
|
-
read(key) {
|
|
359
|
+
read(key, receiver = this.proxy) {
|
|
368
360
|
const type = this.getType(key);
|
|
369
361
|
if (type === null) {
|
|
370
|
-
return
|
|
362
|
+
return Reflect.get(this.source, key, receiver);
|
|
371
363
|
}
|
|
372
364
|
switch (type) {
|
|
373
365
|
case "observable":
|
|
374
|
-
case "observableShallow":
|
|
375
366
|
case "action": {
|
|
376
367
|
if (key in this.source) {
|
|
377
368
|
this.valuesMap.reportObserved(key, this.source[key]);
|
|
@@ -381,48 +372,66 @@ class ObjectAdministration extends Administration {
|
|
|
381
372
|
this.hasMap.reportObserved(key);
|
|
382
373
|
}
|
|
383
374
|
if (type === "observable") {
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
375
|
+
const value = Reflect.get(this.source, key, receiver);
|
|
376
|
+
const shouldWrap = this.explicitObservables.has(key) || isObservable(value);
|
|
377
|
+
if (shouldWrap && value && typeof value === "object" && !Object.isFrozen(value)) {
|
|
378
|
+
const desc = getPropertyDescriptor$1(this.source, key);
|
|
379
|
+
if (desc && !desc.configurable && !desc.writable) {
|
|
380
|
+
return value;
|
|
381
|
+
}
|
|
382
|
+
const existingAdm = getAdministration(value);
|
|
383
|
+
if (existingAdm) {
|
|
384
|
+
return existingAdm.proxy;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return value;
|
|
388
388
|
}
|
|
389
|
-
return getAction(
|
|
389
|
+
return getAction(
|
|
390
|
+
Reflect.get(this.source, key, receiver)
|
|
391
|
+
);
|
|
390
392
|
}
|
|
391
|
-
case "
|
|
392
|
-
if (
|
|
393
|
-
this.
|
|
393
|
+
case "computed": {
|
|
394
|
+
if (receiver === this.proxy) {
|
|
395
|
+
return this.callComputed(key);
|
|
394
396
|
}
|
|
395
397
|
this.atom.reportObserved();
|
|
396
|
-
|
|
397
|
-
this.hasMap.reportObserved(key);
|
|
398
|
-
}
|
|
399
|
-
return this.get(key);
|
|
400
|
-
}
|
|
401
|
-
case "computed": {
|
|
402
|
-
return this.callComputed(key);
|
|
398
|
+
return Reflect.get(this.source, key, receiver);
|
|
403
399
|
}
|
|
404
400
|
default:
|
|
405
401
|
throw new Error(`unknown type passed to configure`);
|
|
406
402
|
}
|
|
407
403
|
}
|
|
404
|
+
explicitObservables = /* @__PURE__ */ new Set();
|
|
408
405
|
write(key, newValue) {
|
|
409
406
|
const type = this.getType(key);
|
|
410
407
|
if (type === null) {
|
|
411
|
-
this.set(key, newValue);
|
|
412
|
-
return;
|
|
408
|
+
return this.set(key, newValue);
|
|
413
409
|
}
|
|
414
410
|
if (type === "computed") {
|
|
415
|
-
signalsCore.batch(() => this.set(key, newValue));
|
|
416
|
-
return;
|
|
411
|
+
return signalsCore.batch(() => this.set(key, newValue));
|
|
417
412
|
}
|
|
418
413
|
const had = key in this.source;
|
|
419
414
|
const oldValue = this.get(key);
|
|
420
415
|
const targetValue = getSource(newValue);
|
|
416
|
+
const oldExplicit = this.explicitObservables.has(key);
|
|
417
|
+
const newExplicit = isObservable(newValue);
|
|
418
|
+
if (newExplicit) {
|
|
419
|
+
this.explicitObservables.add(key);
|
|
420
|
+
} else {
|
|
421
|
+
this.explicitObservables.delete(key);
|
|
422
|
+
}
|
|
421
423
|
if (type === "action" && typeof newValue !== "function" || type === "observable" && typeof newValue === "function") {
|
|
422
424
|
this.types.delete(key);
|
|
423
425
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
+
const changed = !had || !isObservable(oldValue) && oldExplicit !== newExplicit || (isObservable(oldValue) ? oldValue !== newValue : oldValue !== targetValue);
|
|
427
|
+
if (changed) {
|
|
428
|
+
this.isWriting = true;
|
|
429
|
+
try {
|
|
430
|
+
const result = this.set(key, targetValue);
|
|
431
|
+
if (!result) return false;
|
|
432
|
+
} finally {
|
|
433
|
+
this.isWriting = false;
|
|
434
|
+
}
|
|
426
435
|
signalsCore.batch(() => {
|
|
427
436
|
this.flushChange();
|
|
428
437
|
if (!had) {
|
|
@@ -431,7 +440,9 @@ class ObjectAdministration extends Administration {
|
|
|
431
440
|
}
|
|
432
441
|
this.valuesMap.reportChanged(key, newValue);
|
|
433
442
|
});
|
|
443
|
+
return true;
|
|
434
444
|
}
|
|
445
|
+
return true;
|
|
435
446
|
}
|
|
436
447
|
has(key) {
|
|
437
448
|
this.atom.reportObserved();
|
|
@@ -441,33 +452,21 @@ class ObjectAdministration extends Administration {
|
|
|
441
452
|
return key in this.source;
|
|
442
453
|
}
|
|
443
454
|
remove(key) {
|
|
444
|
-
if (!(key in this.source)) return;
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
455
|
+
if (!(key in this.source)) return true;
|
|
456
|
+
const result = Reflect.deleteProperty(this.source, key);
|
|
457
|
+
if (result) {
|
|
458
|
+
signalsCore.batch(() => {
|
|
459
|
+
this.flushChange();
|
|
460
|
+
this.valuesMap.reportChanged(key, void 0);
|
|
461
|
+
this.keysAtom.reportChanged();
|
|
462
|
+
this.hasMap.reportChanged(key);
|
|
463
|
+
this.valuesMap.delete(key);
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
return result;
|
|
453
467
|
}
|
|
454
468
|
}
|
|
455
469
|
class PreactObjectAdministration extends ObjectAdministration {
|
|
456
|
-
static proxyTraps = Object.assign(
|
|
457
|
-
{},
|
|
458
|
-
ObjectAdministration.proxyTraps,
|
|
459
|
-
{
|
|
460
|
-
get(target, prop, proxy) {
|
|
461
|
-
if (!(prop in target) && (typeof prop === "string" || typeof prop === "number") && String(prop)[0] === "$") {
|
|
462
|
-
return getSignal(proxy, prop.substring(1));
|
|
463
|
-
}
|
|
464
|
-
return ObjectAdministration.proxyTraps.get?.apply(
|
|
465
|
-
null,
|
|
466
|
-
arguments
|
|
467
|
-
);
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
);
|
|
471
470
|
}
|
|
472
471
|
function createObservedAtom() {
|
|
473
472
|
let value = 0;
|
|
@@ -605,31 +604,12 @@ function createComputed(fn, context = null) {
|
|
|
605
604
|
}
|
|
606
605
|
};
|
|
607
606
|
}
|
|
608
|
-
function
|
|
609
|
-
|
|
610
|
-
context.metadata[context.name] = { type: "observable" };
|
|
611
|
-
return value;
|
|
612
|
-
}
|
|
613
|
-
return getObservable(value);
|
|
607
|
+
function observable(obj) {
|
|
608
|
+
return getObservable(obj);
|
|
614
609
|
}
|
|
615
|
-
function
|
|
616
|
-
|
|
617
|
-
context.metadata[context.name] = { type: "observableShallow" };
|
|
618
|
-
return value;
|
|
619
|
-
}
|
|
620
|
-
throw new Error("observable.shallow can only be used as a decorator");
|
|
621
|
-
}
|
|
622
|
-
function signalDecorator(value, context) {
|
|
623
|
-
if (context && typeof context === "object" && "kind" in context) {
|
|
624
|
-
context.metadata[context.name] = { type: "observableSignal" };
|
|
625
|
-
return value;
|
|
626
|
-
}
|
|
627
|
-
throw new Error("observable.signal can only be used as a decorator");
|
|
610
|
+
function signal(value) {
|
|
611
|
+
return signalsCore.signal(value);
|
|
628
612
|
}
|
|
629
|
-
const observable = Object.assign(observableImpl, {
|
|
630
|
-
shallow: shallowDecorator,
|
|
631
|
-
signal: signalDecorator
|
|
632
|
-
});
|
|
633
613
|
function computed(value, context) {
|
|
634
614
|
if (context && typeof context === "object" && "kind" in context) {
|
|
635
615
|
context.metadata[context.name] = { type: "computed" };
|
|
@@ -640,19 +620,14 @@ function computed(value, context) {
|
|
|
640
620
|
function source(obj) {
|
|
641
621
|
return getSource(obj);
|
|
642
622
|
}
|
|
643
|
-
class Observable {
|
|
644
|
-
constructor() {
|
|
645
|
-
return getObservableClassInstance(this);
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
623
|
function reportChanged(obj) {
|
|
649
624
|
const adm = getAdministration(obj);
|
|
650
625
|
adm.reportChanged();
|
|
651
626
|
return obj;
|
|
652
627
|
}
|
|
653
|
-
function reportObserved(obj
|
|
628
|
+
function reportObserved(obj) {
|
|
654
629
|
const adm = getAdministration(obj);
|
|
655
|
-
adm.reportObserved(
|
|
630
|
+
adm.reportObserved();
|
|
656
631
|
return obj;
|
|
657
632
|
}
|
|
658
633
|
const signalMap = /* @__PURE__ */ new WeakMap();
|
|
@@ -688,17 +663,26 @@ class CollectionAdministration extends Administration {
|
|
|
688
663
|
hasMap;
|
|
689
664
|
valuesMap;
|
|
690
665
|
keysAtom;
|
|
666
|
+
isWeak;
|
|
667
|
+
strongTracking = null;
|
|
668
|
+
weakTracking = null;
|
|
691
669
|
static proxyTraps = {
|
|
692
670
|
get(target, name) {
|
|
693
671
|
const adm = getAdministration(target);
|
|
694
|
-
if (name === "size" && "size" in adm.source) {
|
|
672
|
+
if (name === "size" && !adm.isWeak && "size" in adm.source) {
|
|
695
673
|
return adm.size;
|
|
696
674
|
}
|
|
697
675
|
const val = adm.source[name];
|
|
698
676
|
const collectionMethods = adm.constructor.methods;
|
|
699
|
-
if (collectionMethods.hasOwnProperty(name)
|
|
677
|
+
if (collectionMethods.hasOwnProperty(name)) {
|
|
678
|
+
if (adm.isWeak && !isValidWeakMethod(name)) {
|
|
679
|
+
return val;
|
|
680
|
+
}
|
|
700
681
|
return collectionMethods[name];
|
|
701
682
|
}
|
|
683
|
+
if (typeof val === "function") {
|
|
684
|
+
return val;
|
|
685
|
+
}
|
|
702
686
|
return val;
|
|
703
687
|
}
|
|
704
688
|
};
|
|
@@ -713,17 +697,71 @@ class CollectionAdministration extends Administration {
|
|
|
713
697
|
entries: createMethod$1("entries"),
|
|
714
698
|
keys: createMethod$1("keys"),
|
|
715
699
|
values: createMethod$1("values"),
|
|
716
|
-
[Symbol.iterator]: createMethod$1(Symbol.iterator)
|
|
700
|
+
[Symbol.iterator]: createMethod$1(Symbol.iterator),
|
|
701
|
+
// New ES methods
|
|
702
|
+
union: createGenericMethod("union"),
|
|
703
|
+
intersection: createGenericMethod("intersection"),
|
|
704
|
+
difference: createGenericMethod("difference"),
|
|
705
|
+
symmetricDifference: createGenericMethod("symmetricDifference"),
|
|
706
|
+
isSubsetOf: createGenericMethod("isSubsetOf"),
|
|
707
|
+
isSupersetOf: createGenericMethod("isSupersetOf"),
|
|
708
|
+
isDisjointFrom: createGenericMethod("isDisjointFrom")
|
|
717
709
|
};
|
|
718
710
|
constructor(source2) {
|
|
719
711
|
super(source2);
|
|
720
712
|
this.hasMap = new AtomMap(this.atom);
|
|
721
|
-
this.valuesMap = new SignalMap();
|
|
713
|
+
this.valuesMap = new SignalMap(this.atom);
|
|
722
714
|
this.keysAtom = createAtom();
|
|
723
715
|
this.isMap = typeof source2.set === "function" && typeof source2.get === "function";
|
|
716
|
+
this.isWeak = source2 instanceof WeakMap || source2 instanceof WeakSet;
|
|
717
|
+
if (this.isWeak) {
|
|
718
|
+
this.weakTracking = /* @__PURE__ */ new WeakSet();
|
|
719
|
+
} else {
|
|
720
|
+
this.strongTracking = /* @__PURE__ */ new Set();
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
trackExplicitObservable(key) {
|
|
724
|
+
if (this.isWeak) {
|
|
725
|
+
if (isNonPrimitive(key)) {
|
|
726
|
+
this.weakTracking.add(key);
|
|
727
|
+
}
|
|
728
|
+
} else {
|
|
729
|
+
this.strongTracking.add(key);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
untrackExplicitObservable(key) {
|
|
733
|
+
if (this.isWeak) {
|
|
734
|
+
if (isNonPrimitive(key)) {
|
|
735
|
+
this.weakTracking.delete(key);
|
|
736
|
+
}
|
|
737
|
+
} else {
|
|
738
|
+
this.strongTracking.delete(key);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
hasExplicitObservable(key) {
|
|
742
|
+
if (this.isWeak) {
|
|
743
|
+
return isNonPrimitive(key) && this.weakTracking.has(key);
|
|
744
|
+
}
|
|
745
|
+
return this.strongTracking.has(key);
|
|
746
|
+
}
|
|
747
|
+
getProxyVariant(key) {
|
|
748
|
+
if (!isNonPrimitive(key)) return void 0;
|
|
749
|
+
const adm = getAdministration(key);
|
|
750
|
+
if (adm && adm.proxy && adm.proxy !== key) {
|
|
751
|
+
return adm.proxy;
|
|
752
|
+
}
|
|
753
|
+
return void 0;
|
|
754
|
+
}
|
|
755
|
+
getExistingKey(key) {
|
|
756
|
+
const target = getSource(key);
|
|
757
|
+
if (this.source.has(target)) return target;
|
|
758
|
+
if (this.source.has(key)) return key;
|
|
759
|
+
const proxy = this.getProxyVariant(key);
|
|
760
|
+
if (proxy !== void 0 && this.source.has(proxy)) return proxy;
|
|
761
|
+
return void 0;
|
|
724
762
|
}
|
|
725
763
|
hasEntry(key) {
|
|
726
|
-
return this.
|
|
764
|
+
return this.getExistingKey(key) !== void 0;
|
|
727
765
|
}
|
|
728
766
|
onCollectionChange(key) {
|
|
729
767
|
signalsCore.batch(() => {
|
|
@@ -732,13 +770,6 @@ class CollectionAdministration extends Administration {
|
|
|
732
770
|
this.flushChange();
|
|
733
771
|
});
|
|
734
772
|
}
|
|
735
|
-
reportObserveDeep() {
|
|
736
|
-
this.source.forEach?.((value) => {
|
|
737
|
-
if (value && typeof value === "object") {
|
|
738
|
-
getAdministration(getObservable(value))?.reportObserved();
|
|
739
|
-
}
|
|
740
|
-
});
|
|
741
|
-
}
|
|
742
773
|
getNode(key) {
|
|
743
774
|
if (key == null) {
|
|
744
775
|
return resolveNode(this.atom);
|
|
@@ -759,13 +790,12 @@ class CollectionAdministration extends Administration {
|
|
|
759
790
|
this.keysAtom.reportObserved();
|
|
760
791
|
this.atom.reportObserved();
|
|
761
792
|
this.source.forEach((value, key) => {
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
);
|
|
793
|
+
if (this.isMap) {
|
|
794
|
+
callbackFn.call(thisArg, this.get(key), key, this.proxy);
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
const wrapped = this.wrapSetValue(key);
|
|
798
|
+
callbackFn.call(thisArg, wrapped, wrapped, this.proxy);
|
|
769
799
|
});
|
|
770
800
|
}
|
|
771
801
|
get size() {
|
|
@@ -774,43 +804,65 @@ class CollectionAdministration extends Administration {
|
|
|
774
804
|
return this.source.size;
|
|
775
805
|
}
|
|
776
806
|
add(value) {
|
|
807
|
+
const target = getSource(value);
|
|
808
|
+
if (isObservable(value)) {
|
|
809
|
+
this.trackExplicitObservable(target);
|
|
810
|
+
}
|
|
777
811
|
if (!this.hasEntry(value)) {
|
|
778
|
-
const target = getSource(value);
|
|
779
812
|
this.source.add(target);
|
|
780
813
|
this.onCollectionChange(target);
|
|
781
814
|
}
|
|
782
815
|
return this;
|
|
783
816
|
}
|
|
784
817
|
delete(value) {
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
818
|
+
const existingKey = this.getExistingKey(value);
|
|
819
|
+
if (existingKey === void 0) return false;
|
|
820
|
+
const target = getSource(value);
|
|
821
|
+
this.untrackExplicitObservable(target);
|
|
822
|
+
this.untrackExplicitObservable(value);
|
|
823
|
+
const proxy = this.getProxyVariant(value);
|
|
824
|
+
if (proxy !== void 0) this.untrackExplicitObservable(proxy);
|
|
825
|
+
this.source.delete(existingKey);
|
|
826
|
+
this.source.delete(target);
|
|
827
|
+
this.source.delete(value);
|
|
828
|
+
if (proxy !== void 0) this.source.delete(proxy);
|
|
829
|
+
this.onCollectionChange(target);
|
|
830
|
+
return true;
|
|
831
|
+
}
|
|
832
|
+
wrapSetValue(value) {
|
|
833
|
+
if (this.hasExplicitObservable(value)) {
|
|
834
|
+
if (value && typeof value === "object" && !Object.isFrozen(value)) {
|
|
835
|
+
const existingAdm = getAdministration(value);
|
|
836
|
+
if (existingAdm) return existingAdm.proxy;
|
|
837
|
+
}
|
|
791
838
|
}
|
|
792
|
-
return
|
|
839
|
+
return value;
|
|
793
840
|
}
|
|
794
841
|
has(value) {
|
|
795
842
|
this.atom.reportObserved();
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
this.hasMap.reportObserved(target);
|
|
799
|
-
}
|
|
843
|
+
const target = getSource(value);
|
|
844
|
+
this.hasMap.reportObserved(target);
|
|
800
845
|
return this.hasEntry(value);
|
|
801
846
|
}
|
|
802
847
|
entries() {
|
|
848
|
+
this.keysAtom.reportObserved();
|
|
849
|
+
this.atom.reportObserved();
|
|
803
850
|
const self = this;
|
|
804
|
-
const
|
|
851
|
+
const iterator = this.source.entries();
|
|
805
852
|
return {
|
|
806
853
|
[Symbol.iterator]: function() {
|
|
807
854
|
return this;
|
|
808
855
|
},
|
|
809
856
|
next() {
|
|
810
|
-
const { done, value } =
|
|
857
|
+
const { done, value } = iterator.next();
|
|
858
|
+
if (done) return { done: true, value: void 0 };
|
|
859
|
+
const [key, val] = value;
|
|
811
860
|
return {
|
|
812
|
-
done,
|
|
813
|
-
value:
|
|
861
|
+
done: false,
|
|
862
|
+
value: [
|
|
863
|
+
self.isMap ? key : self.wrapSetValue(key),
|
|
864
|
+
self.isMap ? self.get(key) : self.wrapSetValue(val)
|
|
865
|
+
]
|
|
814
866
|
};
|
|
815
867
|
}
|
|
816
868
|
};
|
|
@@ -818,19 +870,19 @@ class CollectionAdministration extends Administration {
|
|
|
818
870
|
keys() {
|
|
819
871
|
this.keysAtom.reportObserved();
|
|
820
872
|
this.atom.reportObserved();
|
|
821
|
-
|
|
822
|
-
const
|
|
823
|
-
(o) => getObservable(o)
|
|
824
|
-
);
|
|
873
|
+
const self = this;
|
|
874
|
+
const iterator = this.source.keys();
|
|
825
875
|
return {
|
|
826
876
|
[Symbol.iterator]: function() {
|
|
827
877
|
return this;
|
|
828
878
|
},
|
|
829
879
|
next() {
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
880
|
+
const { done, value } = iterator.next();
|
|
881
|
+
if (done) return { done: true, value: void 0 };
|
|
882
|
+
return {
|
|
883
|
+
done: false,
|
|
884
|
+
value: self.isMap ? value : self.wrapSetValue(value)
|
|
885
|
+
};
|
|
834
886
|
}
|
|
835
887
|
};
|
|
836
888
|
}
|
|
@@ -838,31 +890,54 @@ class CollectionAdministration extends Administration {
|
|
|
838
890
|
const targetKey = getSource(key);
|
|
839
891
|
const sourceMap = this.source;
|
|
840
892
|
const has = this.has(key);
|
|
841
|
-
|
|
842
|
-
|
|
893
|
+
if (!has) return void 0;
|
|
894
|
+
const existingKey = this.getExistingKey(key);
|
|
895
|
+
const value = sourceMap.get(existingKey);
|
|
896
|
+
this.valuesMap.reportObserved(targetKey, value);
|
|
897
|
+
if (key !== targetKey) {
|
|
843
898
|
this.valuesMap.reportObserved(key, value);
|
|
844
|
-
|
|
845
|
-
|
|
899
|
+
}
|
|
900
|
+
const proxyKey = this.getProxyVariant(key);
|
|
901
|
+
if (proxyKey !== void 0 && proxyKey !== key && proxyKey !== targetKey) {
|
|
902
|
+
this.valuesMap.reportObserved(proxyKey, value);
|
|
903
|
+
}
|
|
904
|
+
if (this.hasExplicitObservable(targetKey) || this.hasExplicitObservable(key) || proxyKey !== void 0 && this.hasExplicitObservable(proxyKey)) {
|
|
905
|
+
if (value && typeof value === "object" && !Object.isFrozen(value)) {
|
|
906
|
+
const existingAdm = getAdministration(value);
|
|
907
|
+
if (existingAdm) return existingAdm.proxy;
|
|
846
908
|
}
|
|
847
|
-
return getObservable(value);
|
|
848
909
|
}
|
|
849
|
-
return
|
|
910
|
+
return value;
|
|
850
911
|
}
|
|
851
912
|
set(key, value) {
|
|
852
913
|
const targetKey = getSource(key);
|
|
853
914
|
const targetValue = getSource(value);
|
|
854
915
|
const sourceMap = this.source;
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
916
|
+
if (isObservable(value)) {
|
|
917
|
+
this.trackExplicitObservable(targetKey);
|
|
918
|
+
} else {
|
|
919
|
+
this.untrackExplicitObservable(targetKey);
|
|
920
|
+
this.untrackExplicitObservable(key);
|
|
921
|
+
}
|
|
922
|
+
const existingKey = this.getExistingKey(key);
|
|
923
|
+
const hasKey = existingKey !== void 0;
|
|
924
|
+
const oldValue = hasKey ? sourceMap.get(existingKey) : void 0;
|
|
925
|
+
if (!hasKey || (isObservable(oldValue) ? oldValue !== value : oldValue !== targetValue)) {
|
|
858
926
|
signalsCore.batch(() => {
|
|
859
927
|
this.flushChange();
|
|
860
|
-
if (
|
|
861
|
-
sourceMap.set(
|
|
928
|
+
if (existingKey !== void 0) {
|
|
929
|
+
sourceMap.set(existingKey, targetValue);
|
|
862
930
|
} else {
|
|
863
931
|
sourceMap.set(targetKey, targetValue);
|
|
864
932
|
}
|
|
865
|
-
this.valuesMap.reportChanged(
|
|
933
|
+
this.valuesMap.reportChanged(targetKey, value);
|
|
934
|
+
if (key !== targetKey) {
|
|
935
|
+
this.valuesMap.reportChanged(key, value);
|
|
936
|
+
}
|
|
937
|
+
const proxyKey = this.getProxyVariant(key);
|
|
938
|
+
if (proxyKey !== void 0 && proxyKey !== key && proxyKey !== targetKey) {
|
|
939
|
+
this.valuesMap.reportChanged(proxyKey, value);
|
|
940
|
+
}
|
|
866
941
|
if (!hasKey) {
|
|
867
942
|
this.hasMap.reportChanged(targetKey);
|
|
868
943
|
this.keysAtom.reportChanged();
|
|
@@ -872,20 +947,22 @@ class CollectionAdministration extends Administration {
|
|
|
872
947
|
return this;
|
|
873
948
|
}
|
|
874
949
|
values() {
|
|
875
|
-
|
|
876
|
-
|
|
950
|
+
this.keysAtom.reportObserved();
|
|
951
|
+
this.atom.reportObserved();
|
|
877
952
|
if (!this.isMap) {
|
|
878
|
-
return keys;
|
|
953
|
+
return this.keys();
|
|
879
954
|
}
|
|
955
|
+
const entries = this.entries();
|
|
880
956
|
return {
|
|
881
957
|
[Symbol.iterator]: function() {
|
|
882
958
|
return this;
|
|
883
959
|
},
|
|
884
960
|
next() {
|
|
885
|
-
const { done, value } =
|
|
961
|
+
const { done, value } = entries.next();
|
|
962
|
+
if (done) return { done: true, value: void 0 };
|
|
886
963
|
return {
|
|
887
|
-
done,
|
|
888
|
-
value:
|
|
964
|
+
done: false,
|
|
965
|
+
value: value[1]
|
|
889
966
|
};
|
|
890
967
|
}
|
|
891
968
|
};
|
|
@@ -895,53 +972,197 @@ class CollectionAdministration extends Administration {
|
|
|
895
972
|
}
|
|
896
973
|
[Symbol.toStringTag] = "Set";
|
|
897
974
|
}
|
|
975
|
+
function isValidWeakMethod(name) {
|
|
976
|
+
const n = name;
|
|
977
|
+
return n === "get" || n === "set" || n === "add" || n === "has" || n === "delete";
|
|
978
|
+
}
|
|
979
|
+
function createGenericMethod(name) {
|
|
980
|
+
return function() {
|
|
981
|
+
const adm = getAdministration(this);
|
|
982
|
+
const method = adm.source[name];
|
|
983
|
+
if (typeof method !== "function") return method;
|
|
984
|
+
if (["union", "intersection", "difference", "symmetricDifference"].includes(
|
|
985
|
+
name
|
|
986
|
+
)) {
|
|
987
|
+
const other = arguments[0];
|
|
988
|
+
const result2 = /* @__PURE__ */ new Set();
|
|
989
|
+
if (name === "union") {
|
|
990
|
+
for (const item of this) result2.add(item);
|
|
991
|
+
for (const item of other) result2.add(item);
|
|
992
|
+
} else if (name === "intersection") {
|
|
993
|
+
for (const item of this) {
|
|
994
|
+
if (other.has(item)) result2.add(item);
|
|
995
|
+
}
|
|
996
|
+
} else if (name === "difference") {
|
|
997
|
+
for (const item of this) {
|
|
998
|
+
if (!other.has(item)) result2.add(item);
|
|
999
|
+
}
|
|
1000
|
+
} else if (name === "symmetricDifference") {
|
|
1001
|
+
for (const item of this) {
|
|
1002
|
+
if (!other.has(item)) result2.add(item);
|
|
1003
|
+
}
|
|
1004
|
+
for (const item of other) {
|
|
1005
|
+
if (!this.has(item)) result2.add(item);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
return result2;
|
|
1009
|
+
}
|
|
1010
|
+
adm.keysAtom.reportObserved();
|
|
1011
|
+
adm.atom.reportObserved();
|
|
1012
|
+
const args = new Array(arguments.length);
|
|
1013
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
1014
|
+
const arg = arguments[i];
|
|
1015
|
+
args[i] = getSource(arg);
|
|
1016
|
+
if (isObservable(arg)) {
|
|
1017
|
+
reportObserved(arg);
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
const result = method.apply(adm.source, args);
|
|
1021
|
+
if (result === adm.source) return adm.proxy;
|
|
1022
|
+
return result;
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
898
1025
|
function createMethod$1(method) {
|
|
899
1026
|
return function() {
|
|
900
1027
|
const adm = getAdministration(this);
|
|
901
|
-
|
|
1028
|
+
const result = adm[method].apply(adm, arguments);
|
|
1029
|
+
if (method === "add" || method === "set") {
|
|
1030
|
+
return this;
|
|
1031
|
+
}
|
|
1032
|
+
return result;
|
|
902
1033
|
};
|
|
903
1034
|
}
|
|
1035
|
+
function isArrayIndexKey(key) {
|
|
1036
|
+
if (typeof key === "symbol") return false;
|
|
1037
|
+
const keyStr = String(key);
|
|
1038
|
+
const num = Number(keyStr);
|
|
1039
|
+
return Number.isInteger(num) && num >= 0 && num < 4294967295 && // 2^32 - 1
|
|
1040
|
+
String(num) === keyStr;
|
|
1041
|
+
}
|
|
1042
|
+
function reportConflict() {
|
|
1043
|
+
if (process.env.NODE_ENV !== "production") {
|
|
1044
|
+
throw new Error(
|
|
1045
|
+
"r-state-tree: Cannot assign the same object as both observable and raw source in the same array. The value will be treated as observable."
|
|
1046
|
+
);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
904
1049
|
class ArrayAdministration extends Administration {
|
|
905
1050
|
valuesMap;
|
|
906
1051
|
keysAtom;
|
|
1052
|
+
/**
|
|
1053
|
+
* Parent-based ownership tracking: sources assigned as observable.
|
|
1054
|
+
* Unlike index-based tracking, this doesn't require updates on reorder ops.
|
|
1055
|
+
*/
|
|
1056
|
+
observableSources = /* @__PURE__ */ new WeakSet();
|
|
1057
|
+
rawSources = /* @__PURE__ */ new WeakSet();
|
|
1058
|
+
/**
|
|
1059
|
+
* Track a value being assigned to this array.
|
|
1060
|
+
* Detects conflicts if same source is assigned as both observable and raw.
|
|
1061
|
+
*/
|
|
1062
|
+
trackValue(value) {
|
|
1063
|
+
if (value && typeof value === "object") {
|
|
1064
|
+
const source2 = getSource(value);
|
|
1065
|
+
if (isObservable(value)) {
|
|
1066
|
+
if (this.rawSources.has(source2)) {
|
|
1067
|
+
reportConflict();
|
|
1068
|
+
}
|
|
1069
|
+
this.observableSources.add(source2);
|
|
1070
|
+
} else {
|
|
1071
|
+
if (this.observableSources.has(source2)) {
|
|
1072
|
+
reportConflict();
|
|
1073
|
+
} else {
|
|
1074
|
+
this.rawSources.add(source2);
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
907
1079
|
static proxyTraps = {
|
|
908
|
-
get(target, name) {
|
|
1080
|
+
get(target, name, receiver) {
|
|
909
1081
|
const adm = getAdministration(target);
|
|
910
1082
|
if (name === "length") {
|
|
911
1083
|
return adm.getArrayLength();
|
|
912
1084
|
}
|
|
913
|
-
if (
|
|
914
|
-
return adm.get(name);
|
|
915
|
-
}
|
|
916
|
-
if (typeof name === "string" && String(parseInt(name)) === name) {
|
|
917
|
-
return adm.get(parseInt(name));
|
|
1085
|
+
if (isArrayIndexKey(name)) {
|
|
1086
|
+
return adm.get(Number(name));
|
|
918
1087
|
}
|
|
919
1088
|
const arrayMethods = adm.constructor.methods;
|
|
920
1089
|
if (arrayMethods.hasOwnProperty(name)) {
|
|
921
1090
|
return arrayMethods[name];
|
|
922
1091
|
}
|
|
923
|
-
return adm.source
|
|
1092
|
+
return Reflect.get(adm.source, name, receiver);
|
|
924
1093
|
},
|
|
925
1094
|
set(target, name, value) {
|
|
926
1095
|
const adm = getAdministration(target);
|
|
927
1096
|
if (name === "length") {
|
|
928
|
-
adm.setArrayLength(value);
|
|
929
|
-
} else if (
|
|
930
|
-
adm.set(name, value);
|
|
931
|
-
} else if (typeof name === "string" && String(parseInt(name)) === name) {
|
|
932
|
-
adm.set(parseInt(name), value);
|
|
1097
|
+
return adm.setArrayLength(value);
|
|
1098
|
+
} else if (isArrayIndexKey(name)) {
|
|
1099
|
+
return adm.set(Number(name), value);
|
|
933
1100
|
} else {
|
|
934
|
-
adm.source
|
|
1101
|
+
return Reflect.set(adm.source, name, value, arguments[3]);
|
|
935
1102
|
}
|
|
936
|
-
|
|
1103
|
+
},
|
|
1104
|
+
defineProperty(target, name, descriptor) {
|
|
1105
|
+
const adm = getAdministration(target);
|
|
1106
|
+
const result = Reflect.defineProperty(adm.source, name, descriptor);
|
|
1107
|
+
if (result) {
|
|
1108
|
+
signalsCore.batch(() => {
|
|
1109
|
+
adm.flushChange();
|
|
1110
|
+
if (name === "length") {
|
|
1111
|
+
adm.keysAtom.reportChanged();
|
|
1112
|
+
adm.atom.reportChanged();
|
|
1113
|
+
} else if (isArrayIndexKey(name)) {
|
|
1114
|
+
const index = Number(name);
|
|
1115
|
+
adm.keysAtom.reportChanged();
|
|
1116
|
+
adm.onArrayChanged(false, index, 1);
|
|
1117
|
+
} else {
|
|
1118
|
+
adm.atom.reportChanged();
|
|
1119
|
+
}
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
return result;
|
|
1123
|
+
},
|
|
1124
|
+
deleteProperty(target, name) {
|
|
1125
|
+
const adm = getAdministration(target);
|
|
1126
|
+
const isIndex = isArrayIndexKey(name);
|
|
1127
|
+
const had = name in adm.source;
|
|
1128
|
+
const result = Reflect.deleteProperty(adm.source, name);
|
|
1129
|
+
if (result && had && isIndex) {
|
|
1130
|
+
const index = Number(name);
|
|
1131
|
+
adm.onArrayChanged(true, index, 1);
|
|
1132
|
+
}
|
|
1133
|
+
return result;
|
|
1134
|
+
},
|
|
1135
|
+
ownKeys(target) {
|
|
1136
|
+
const adm = getAdministration(target);
|
|
1137
|
+
signalsCore.batch(() => {
|
|
1138
|
+
adm.keysAtom.reportObserved();
|
|
1139
|
+
adm.atom.reportObserved();
|
|
1140
|
+
});
|
|
1141
|
+
return Reflect.ownKeys(adm.source);
|
|
1142
|
+
},
|
|
1143
|
+
has(target, name) {
|
|
1144
|
+
const adm = getAdministration(target);
|
|
1145
|
+
if (isArrayIndexKey(name)) {
|
|
1146
|
+
const index = Number(name);
|
|
1147
|
+
adm.atom.reportObserved();
|
|
1148
|
+
adm.valuesMap.reportObserved(index, adm.source[index]);
|
|
1149
|
+
return index in adm.source;
|
|
1150
|
+
}
|
|
1151
|
+
return Reflect.has(adm.source, name);
|
|
937
1152
|
}
|
|
938
1153
|
};
|
|
939
1154
|
static methods = {
|
|
940
1155
|
fill(value, start, end) {
|
|
941
1156
|
const adm = getAdministration(this);
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
adm.
|
|
1157
|
+
adm.trackValue(value);
|
|
1158
|
+
const targetValue = getSource(value);
|
|
1159
|
+
adm.source.fill(targetValue, start, end);
|
|
1160
|
+
const length = adm.source.length;
|
|
1161
|
+
const from = start == null ? 0 : start < 0 ? Math.max(length + start, 0) : start;
|
|
1162
|
+
const to = end == null ? length : end < 0 ? Math.max(length + end, 0) : Math.min(end, length);
|
|
1163
|
+
if (from < to) {
|
|
1164
|
+
adm.onArrayChanged(false, from, to - from);
|
|
1165
|
+
}
|
|
945
1166
|
return this;
|
|
946
1167
|
},
|
|
947
1168
|
splice(index, deleteCount, ...newItems) {
|
|
@@ -983,10 +1204,20 @@ class ArrayAdministration extends Administration {
|
|
|
983
1204
|
},
|
|
984
1205
|
sort(compareFn) {
|
|
985
1206
|
const adm = getAdministration(this);
|
|
986
|
-
adm.
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
);
|
|
1207
|
+
const pairs = adm.source.map((stored, i) => ({
|
|
1208
|
+
value: this[i],
|
|
1209
|
+
stored
|
|
1210
|
+
}));
|
|
1211
|
+
const comparator = compareFn ?? ((a, b) => {
|
|
1212
|
+
const as = String(a);
|
|
1213
|
+
const bs = String(b);
|
|
1214
|
+
return as < bs ? -1 : as > bs ? 1 : 0;
|
|
1215
|
+
});
|
|
1216
|
+
pairs.sort((a, b) => comparator(a.value, b.value));
|
|
1217
|
+
for (let i = 0; i < pairs.length; i++) {
|
|
1218
|
+
adm.source[i] = pairs[i].stored;
|
|
1219
|
+
}
|
|
1220
|
+
adm.onArrayChanged(false, 0, adm.source.length);
|
|
990
1221
|
return this;
|
|
991
1222
|
},
|
|
992
1223
|
join: createStringMethod("join"),
|
|
@@ -998,7 +1229,12 @@ class ArrayAdministration extends Administration {
|
|
|
998
1229
|
slice: createCopyMethod("slice"),
|
|
999
1230
|
concat: createCopyMethod("concat"),
|
|
1000
1231
|
flat: createCopyMethod("flat"),
|
|
1001
|
-
copyWithin
|
|
1232
|
+
copyWithin(target, start, end) {
|
|
1233
|
+
const adm = getAdministration(this);
|
|
1234
|
+
adm.source.copyWithin(target, start, end);
|
|
1235
|
+
adm.onArrayChanged(false, 0, adm.source.length);
|
|
1236
|
+
return this;
|
|
1237
|
+
},
|
|
1002
1238
|
every: createMapMethod("every"),
|
|
1003
1239
|
forEach: createMapMethod("forEach"),
|
|
1004
1240
|
map: createMapMethod("map"),
|
|
@@ -1012,17 +1248,9 @@ class ArrayAdministration extends Administration {
|
|
|
1012
1248
|
};
|
|
1013
1249
|
constructor(source2 = []) {
|
|
1014
1250
|
super(source2);
|
|
1015
|
-
this.valuesMap = new SignalMap();
|
|
1251
|
+
this.valuesMap = new SignalMap(this.atom);
|
|
1016
1252
|
this.keysAtom = createAtom();
|
|
1017
1253
|
}
|
|
1018
|
-
reportObserveDeep() {
|
|
1019
|
-
for (let i = 0; i < this.source.length; i++) {
|
|
1020
|
-
const value = this.source[i];
|
|
1021
|
-
if (value && typeof value === "object") {
|
|
1022
|
-
getAdministration(getObservable(value))?.reportObserved();
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
1254
|
getNode(key) {
|
|
1027
1255
|
if (key == null) {
|
|
1028
1256
|
return this.atom;
|
|
@@ -1032,45 +1260,69 @@ class ArrayAdministration extends Administration {
|
|
|
1032
1260
|
get(index) {
|
|
1033
1261
|
this.atom.reportObserved();
|
|
1034
1262
|
this.valuesMap.reportObserved(index, this.source[index]);
|
|
1035
|
-
|
|
1036
|
-
|
|
1263
|
+
return this._getEffectiveValue(index);
|
|
1264
|
+
}
|
|
1265
|
+
_getEffectiveValue(index) {
|
|
1266
|
+
const value = this.source[index];
|
|
1267
|
+
if (value && typeof value === "object" && !Object.isFrozen(value)) {
|
|
1268
|
+
if (this.observableSources.has(value)) {
|
|
1269
|
+
const adm = getAdministration(value);
|
|
1270
|
+
if (adm.proxy !== value) {
|
|
1271
|
+
const desc = Object.getOwnPropertyDescriptor(this.source, index);
|
|
1272
|
+
if (desc && !desc.configurable && !desc.writable) {
|
|
1273
|
+
if (process.env.NODE_ENV !== "production") {
|
|
1274
|
+
console.warn(
|
|
1275
|
+
`r-state-tree: cannot return an observable proxy for arr[${index}] because it is a non-configurable, non-writable data property; returning the raw value to uphold Proxy invariants.`
|
|
1276
|
+
);
|
|
1277
|
+
}
|
|
1278
|
+
return value;
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
return adm.proxy;
|
|
1282
|
+
}
|
|
1037
1283
|
}
|
|
1038
|
-
return
|
|
1284
|
+
return value;
|
|
1039
1285
|
}
|
|
1040
1286
|
set(index, newValue) {
|
|
1041
1287
|
const values = this.source;
|
|
1042
1288
|
const targetValue = getSource(newValue);
|
|
1043
|
-
|
|
1289
|
+
const oldLength = values.length;
|
|
1290
|
+
this.trackValue(newValue);
|
|
1291
|
+
let changed = true;
|
|
1292
|
+
if (index < oldLength) {
|
|
1044
1293
|
const oldValue = values[index];
|
|
1045
|
-
|
|
1046
|
-
if (changed) {
|
|
1047
|
-
values[index] = targetValue;
|
|
1048
|
-
this.onArrayChanged(false, index, 1);
|
|
1049
|
-
}
|
|
1050
|
-
} else if (index === values.length) {
|
|
1051
|
-
this.spliceWithArray(index, 0, [newValue]);
|
|
1052
|
-
} else {
|
|
1053
|
-
throw new Error(
|
|
1054
|
-
`Index out of bounds, ${index} is larger than ${values.length}`
|
|
1055
|
-
);
|
|
1294
|
+
changed = targetValue !== oldValue;
|
|
1056
1295
|
}
|
|
1296
|
+
if (!changed) return true;
|
|
1297
|
+
const result = Reflect.set(values, index, targetValue);
|
|
1298
|
+
if (!result) return false;
|
|
1299
|
+
const newLength = values.length;
|
|
1300
|
+
const lengthChanged = newLength !== oldLength;
|
|
1301
|
+
this.onArrayChanged(lengthChanged, index, 1);
|
|
1302
|
+
return true;
|
|
1057
1303
|
}
|
|
1058
1304
|
getArrayLength() {
|
|
1059
1305
|
this.atom.reportObserved();
|
|
1060
1306
|
this.keysAtom.reportObserved();
|
|
1061
1307
|
return this.source.length;
|
|
1062
1308
|
}
|
|
1063
|
-
setArrayLength(
|
|
1064
|
-
|
|
1065
|
-
|
|
1309
|
+
setArrayLength(input) {
|
|
1310
|
+
const num = Number(input);
|
|
1311
|
+
const coerced = num >>> 0;
|
|
1312
|
+
if (coerced !== num) {
|
|
1313
|
+
throw new RangeError("Invalid array length");
|
|
1314
|
+
}
|
|
1315
|
+
const newLength = coerced;
|
|
1066
1316
|
const currentLength = this.source.length;
|
|
1067
|
-
if (newLength === currentLength) return;
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1317
|
+
if (newLength === currentLength) return true;
|
|
1318
|
+
const result = Reflect.set(this.source, "length", newLength);
|
|
1319
|
+
if (!result) return false;
|
|
1320
|
+
if (newLength < currentLength) {
|
|
1321
|
+
this.onArrayChanged(true, newLength, currentLength - newLength);
|
|
1322
|
+
} else {
|
|
1323
|
+
this.onArrayChanged(true, currentLength, newLength - currentLength);
|
|
1324
|
+
}
|
|
1325
|
+
return true;
|
|
1074
1326
|
}
|
|
1075
1327
|
spliceWithArray(index, deleteCount, newItems) {
|
|
1076
1328
|
const length = this.source.length;
|
|
@@ -1078,6 +1330,7 @@ class ArrayAdministration extends Administration {
|
|
|
1078
1330
|
if (newItems) {
|
|
1079
1331
|
for (let i = 0; i < newItems.length; i++) {
|
|
1080
1332
|
newTargetItems[i] = getSource(newItems[i]);
|
|
1333
|
+
this.trackValue(newItems[i]);
|
|
1081
1334
|
}
|
|
1082
1335
|
}
|
|
1083
1336
|
if (index === void 0) index = 0;
|
|
@@ -1086,15 +1339,18 @@ class ArrayAdministration extends Administration {
|
|
|
1086
1339
|
if (arguments.length === 1) deleteCount = length - index;
|
|
1087
1340
|
else if (deleteCount === void 0 || deleteCount === null) deleteCount = 0;
|
|
1088
1341
|
else deleteCount = Math.max(0, Math.min(deleteCount, length - index));
|
|
1089
|
-
const
|
|
1342
|
+
const removedItems = [];
|
|
1343
|
+
for (let i = index; i < index + deleteCount; i++) {
|
|
1344
|
+
removedItems.push(this._getEffectiveValue(i));
|
|
1345
|
+
}
|
|
1346
|
+
this.spliceItemsIntoValues(index, deleteCount, newTargetItems);
|
|
1090
1347
|
if (deleteCount !== 0 || newTargetItems.length !== 0) {
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
);
|
|
1348
|
+
const shift = (newItems?.length ?? 0) - deleteCount;
|
|
1349
|
+
const reindexing = shift !== 0 && index + deleteCount < length;
|
|
1350
|
+
const count = reindexing ? Number.POSITIVE_INFINITY : Math.max(deleteCount ?? 0, newItems?.length ?? 0);
|
|
1351
|
+
this.onArrayChanged(length !== this.source.length, index, count);
|
|
1096
1352
|
}
|
|
1097
|
-
return
|
|
1353
|
+
return removedItems;
|
|
1098
1354
|
}
|
|
1099
1355
|
spliceItemsIntoValues(index, deleteCount, newItems) {
|
|
1100
1356
|
return this.source.splice.apply(
|
|
@@ -1109,9 +1365,16 @@ class ArrayAdministration extends Administration {
|
|
|
1109
1365
|
}
|
|
1110
1366
|
if (index == null) {
|
|
1111
1367
|
this.atom.reportChanged();
|
|
1112
|
-
} else {
|
|
1113
|
-
|
|
1114
|
-
|
|
1368
|
+
} else if (count !== void 0 && count > 0) {
|
|
1369
|
+
const observedKeys = this.valuesMap.keys();
|
|
1370
|
+
const end = index + count;
|
|
1371
|
+
for (const key of observedKeys) {
|
|
1372
|
+
if (typeof key === "number" && key >= index && key < end) {
|
|
1373
|
+
const node = this.valuesMap.get(key);
|
|
1374
|
+
if (node) {
|
|
1375
|
+
node.reportChanged(this._getEffectiveValue(key));
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1115
1378
|
}
|
|
1116
1379
|
}
|
|
1117
1380
|
this.flushChange();
|
|
@@ -1127,7 +1390,7 @@ function createMethod(method, func) {
|
|
|
1127
1390
|
function createStringMethod(method) {
|
|
1128
1391
|
return createMethod(method, function() {
|
|
1129
1392
|
const adm = getAdministration(this);
|
|
1130
|
-
adm.reportObserved(
|
|
1393
|
+
adm.reportObserved();
|
|
1131
1394
|
const sourceArr = getSource(this);
|
|
1132
1395
|
return sourceArr[method].apply(sourceArr, arguments);
|
|
1133
1396
|
});
|
|
@@ -1135,7 +1398,7 @@ function createStringMethod(method) {
|
|
|
1135
1398
|
function createSearchMethod(method) {
|
|
1136
1399
|
return createMethod(method, function() {
|
|
1137
1400
|
const adm = getAdministration(this);
|
|
1138
|
-
adm.reportObserved(
|
|
1401
|
+
adm.reportObserved();
|
|
1139
1402
|
const target = arguments[0];
|
|
1140
1403
|
const source2 = getSource(target);
|
|
1141
1404
|
const sourceArr = getSource(this);
|
|
@@ -1151,10 +1414,38 @@ function createSearchMethod(method) {
|
|
|
1151
1414
|
function createCopyMethod(method) {
|
|
1152
1415
|
return createMethod(method, function() {
|
|
1153
1416
|
const adm = getAdministration(this);
|
|
1154
|
-
adm.reportObserved(
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
);
|
|
1417
|
+
adm.reportObserved();
|
|
1418
|
+
const observedInput = [];
|
|
1419
|
+
observedInput.length = adm.source.length;
|
|
1420
|
+
const keys = Object.keys(adm.source);
|
|
1421
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1422
|
+
const key = keys[i];
|
|
1423
|
+
const idx = Number(key);
|
|
1424
|
+
if (!Number.isNaN(idx)) {
|
|
1425
|
+
observedInput[idx] = this[idx];
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
const args = Array.from(arguments);
|
|
1429
|
+
if (method === "concat") {
|
|
1430
|
+
for (let i = 0; i < args.length; i++) {
|
|
1431
|
+
const arg = args[i];
|
|
1432
|
+
if (Array.isArray(arg) && isObservable(arg)) {
|
|
1433
|
+
const argAdm = getAdministration(arg);
|
|
1434
|
+
const argObserved = [];
|
|
1435
|
+
argObserved.length = argAdm.source.length;
|
|
1436
|
+
const argKeys = Object.keys(argAdm.source);
|
|
1437
|
+
for (let j = 0; j < argKeys.length; j++) {
|
|
1438
|
+
const argKey = argKeys[j];
|
|
1439
|
+
const argIdx = Number(argKey);
|
|
1440
|
+
if (!Number.isNaN(argIdx)) {
|
|
1441
|
+
argObserved[argIdx] = arg[argIdx];
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
args[i] = argObserved;
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
return Array.prototype[method].apply(observedInput, args);
|
|
1158
1449
|
});
|
|
1159
1450
|
}
|
|
1160
1451
|
function createMapMethod(method) {
|
|
@@ -1162,14 +1453,9 @@ function createMapMethod(method) {
|
|
|
1162
1453
|
method,
|
|
1163
1454
|
function(callback, thisArg) {
|
|
1164
1455
|
const adm = getAdministration(this);
|
|
1165
|
-
adm.reportObserved(
|
|
1166
|
-
return adm.source[method]((
|
|
1167
|
-
return callback.call(
|
|
1168
|
-
thisArg,
|
|
1169
|
-
element && typeof element === "object" ? getObservable(element) : element,
|
|
1170
|
-
index,
|
|
1171
|
-
this
|
|
1172
|
-
);
|
|
1456
|
+
adm.reportObserved();
|
|
1457
|
+
return adm.source[method]((_element, index) => {
|
|
1458
|
+
return callback.call(thisArg, this[index], index, this);
|
|
1173
1459
|
});
|
|
1174
1460
|
}
|
|
1175
1461
|
);
|
|
@@ -1179,28 +1465,17 @@ function createFilterMethod(method) {
|
|
|
1179
1465
|
method,
|
|
1180
1466
|
function(callback, thisArg) {
|
|
1181
1467
|
const adm = getAdministration(this);
|
|
1182
|
-
adm.reportObserved(
|
|
1183
|
-
return
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
thisArg,
|
|
1187
|
-
element && typeof element === "object" ? getObservable(element) : element,
|
|
1188
|
-
index,
|
|
1189
|
-
this
|
|
1190
|
-
);
|
|
1191
|
-
})
|
|
1192
|
-
);
|
|
1468
|
+
adm.reportObserved();
|
|
1469
|
+
return adm.source[method]((_element, index) => {
|
|
1470
|
+
return callback.call(thisArg, this[index], index, this);
|
|
1471
|
+
});
|
|
1193
1472
|
}
|
|
1194
1473
|
);
|
|
1195
1474
|
}
|
|
1196
1475
|
function createReduceMethod(method) {
|
|
1197
1476
|
return createMethod(method, function() {
|
|
1198
1477
|
const adm = getAdministration(this);
|
|
1199
|
-
adm.reportObserved(
|
|
1200
|
-
const callback = arguments[0];
|
|
1201
|
-
arguments[0] = (accumulator, currentValue, index) => {
|
|
1202
|
-
return callback(accumulator, getObservable(currentValue), index, this);
|
|
1203
|
-
};
|
|
1478
|
+
adm.reportObserved();
|
|
1204
1479
|
return adm.source[method].apply(adm.source, arguments);
|
|
1205
1480
|
});
|
|
1206
1481
|
}
|
|
@@ -1227,6 +1502,7 @@ function addDateSetMethod(method) {
|
|
|
1227
1502
|
const adm = getAdministration(this);
|
|
1228
1503
|
const res = adm.source[method].apply(adm.source, arguments);
|
|
1229
1504
|
adm.atom.reportChanged();
|
|
1505
|
+
adm.flushChange();
|
|
1230
1506
|
return res;
|
|
1231
1507
|
};
|
|
1232
1508
|
}
|
|
@@ -1262,9 +1538,15 @@ function getAction(fn) {
|
|
|
1262
1538
|
}
|
|
1263
1539
|
function getObservableClassInstance(value) {
|
|
1264
1540
|
const adm = new PreactObjectAdministration(value);
|
|
1541
|
+
administrationMap.set(adm.proxy, adm);
|
|
1265
1542
|
administrationMap.set(adm.source, adm);
|
|
1266
1543
|
return adm.proxy;
|
|
1267
1544
|
}
|
|
1545
|
+
class Observable {
|
|
1546
|
+
constructor() {
|
|
1547
|
+
return getObservableClassInstance(this);
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1268
1550
|
function createObservableWithCustomAdministration(value, Adm) {
|
|
1269
1551
|
const adm = new Adm(value);
|
|
1270
1552
|
administrationMap.set(adm.proxy, adm);
|
|
@@ -1272,40 +1554,6 @@ function createObservableWithCustomAdministration(value, Adm) {
|
|
|
1272
1554
|
return adm.proxy;
|
|
1273
1555
|
}
|
|
1274
1556
|
function getObservable(value) {
|
|
1275
|
-
if (!value) {
|
|
1276
|
-
return value;
|
|
1277
|
-
}
|
|
1278
|
-
const adm = getAdministration(value);
|
|
1279
|
-
if (adm) {
|
|
1280
|
-
return adm.proxy;
|
|
1281
|
-
}
|
|
1282
|
-
if ((typeof value === "object" || typeof value === "function") && !Object.isFrozen(value)) {
|
|
1283
|
-
const obj = value;
|
|
1284
|
-
let Adm = PreactObjectAdministration;
|
|
1285
|
-
if (Array.isArray(obj)) {
|
|
1286
|
-
Adm = ArrayAdministration;
|
|
1287
|
-
} else if (obj instanceof Map || obj instanceof WeakMap) {
|
|
1288
|
-
Adm = CollectionAdministration;
|
|
1289
|
-
} else if (obj instanceof Set || obj instanceof WeakSet) {
|
|
1290
|
-
Adm = CollectionAdministration;
|
|
1291
|
-
} else if (obj instanceof Date) {
|
|
1292
|
-
Adm = DateAdministration;
|
|
1293
|
-
} else if (!isPlainObject(value)) {
|
|
1294
|
-
return value;
|
|
1295
|
-
}
|
|
1296
|
-
const adm2 = new Adm(obj);
|
|
1297
|
-
administrationMap.set(adm2.proxy, adm2);
|
|
1298
|
-
administrationMap.set(adm2.source, adm2);
|
|
1299
|
-
return adm2.proxy;
|
|
1300
|
-
}
|
|
1301
|
-
return value;
|
|
1302
|
-
}
|
|
1303
|
-
const shallowObservables = /* @__PURE__ */ new WeakSet();
|
|
1304
|
-
function isShallowObservable(obj) {
|
|
1305
|
-
if (!obj || typeof obj !== "object") return false;
|
|
1306
|
-
return shallowObservables.has(obj);
|
|
1307
|
-
}
|
|
1308
|
-
function getShallowObservable(value) {
|
|
1309
1557
|
if (!value) {
|
|
1310
1558
|
return value;
|
|
1311
1559
|
}
|
|
@@ -1313,23 +1561,43 @@ function getShallowObservable(value) {
|
|
|
1313
1561
|
if (existingAdm) {
|
|
1314
1562
|
return existingAdm.proxy;
|
|
1315
1563
|
}
|
|
1316
|
-
if (
|
|
1564
|
+
if (typeof value === "function") {
|
|
1565
|
+
if (process.env.NODE_ENV !== "production") {
|
|
1566
|
+
console.warn(
|
|
1567
|
+
`r-state-tree: functions are not observable containers. The function will be returned unchanged. Note: functions read from observable objects are still automatically batched as actions.`
|
|
1568
|
+
);
|
|
1569
|
+
}
|
|
1570
|
+
return value;
|
|
1571
|
+
}
|
|
1572
|
+
if (typeof value === "object") {
|
|
1317
1573
|
const obj = value;
|
|
1318
1574
|
let Adm = null;
|
|
1319
|
-
if (
|
|
1320
|
-
Adm = ArrayAdministration;
|
|
1321
|
-
} else if (obj instanceof Map || obj instanceof WeakMap) {
|
|
1575
|
+
if (obj instanceof Map || obj instanceof WeakMap) {
|
|
1322
1576
|
Adm = CollectionAdministration;
|
|
1323
1577
|
} else if (obj instanceof Set || obj instanceof WeakSet) {
|
|
1324
1578
|
Adm = CollectionAdministration;
|
|
1579
|
+
} else if (obj instanceof Date) {
|
|
1580
|
+
Adm = DateAdministration;
|
|
1581
|
+
} else if (!Object.isFrozen(obj)) {
|
|
1582
|
+
if (Array.isArray(obj)) {
|
|
1583
|
+
Adm = ArrayAdministration;
|
|
1584
|
+
} else if (isPlainObject(obj)) {
|
|
1585
|
+
Adm = PreactObjectAdministration;
|
|
1586
|
+
}
|
|
1325
1587
|
}
|
|
1326
1588
|
if (Adm) {
|
|
1327
1589
|
const adm = new Adm(obj);
|
|
1328
1590
|
administrationMap.set(adm.proxy, adm);
|
|
1329
1591
|
administrationMap.set(adm.source, adm);
|
|
1330
|
-
shallowObservables.add(adm.proxy);
|
|
1331
1592
|
return adm.proxy;
|
|
1332
1593
|
}
|
|
1594
|
+
if (process.env.NODE_ENV !== "production" && !Object.isFrozen(obj)) {
|
|
1595
|
+
const proto = Object.getPrototypeOf(obj);
|
|
1596
|
+
const typeName = proto?.constructor?.name && proto.constructor.name !== "Object" ? `instance of ${proto.constructor.name}` : "non-plain object";
|
|
1597
|
+
console.warn(
|
|
1598
|
+
`r-state-tree: observable() was called with a ${typeName}. This object will NOT be made observable because proxying arbitrary class instances can break #private fields and built-in brand checks. To make a class observable, use 'class MyClass extends Observable'.`
|
|
1599
|
+
);
|
|
1600
|
+
}
|
|
1333
1601
|
}
|
|
1334
1602
|
return value;
|
|
1335
1603
|
}
|
|
@@ -1358,19 +1626,77 @@ function getPropertyDescriptor(obj, key) {
|
|
|
1358
1626
|
}
|
|
1359
1627
|
return void 0;
|
|
1360
1628
|
}
|
|
1361
|
-
function clone(val) {
|
|
1362
|
-
if (
|
|
1363
|
-
return val
|
|
1364
|
-
}
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1629
|
+
function clone(val, path = "") {
|
|
1630
|
+
if (val === null || val === void 0) {
|
|
1631
|
+
return val;
|
|
1632
|
+
}
|
|
1633
|
+
if (typeof val !== "object") {
|
|
1634
|
+
if (typeof val === "string" || typeof val === "number" || typeof val === "boolean") {
|
|
1635
|
+
return val;
|
|
1636
|
+
}
|
|
1637
|
+
const atPath = path ? ` at path "${path}"` : "";
|
|
1638
|
+
if (typeof val === "bigint") {
|
|
1639
|
+
throw new Error(
|
|
1640
|
+
`r-state-tree: snapshots do not support bigint${atPath}. Snapshots are JSON-only (primitives, arrays, plain objects, Dates as ISO strings).`
|
|
1641
|
+
);
|
|
1642
|
+
}
|
|
1643
|
+
if (typeof val === "symbol") {
|
|
1644
|
+
throw new Error(
|
|
1645
|
+
`r-state-tree: snapshots do not support symbol${atPath}. Snapshots are JSON-only (primitives, arrays, plain objects, Dates as ISO strings).`
|
|
1646
|
+
);
|
|
1647
|
+
}
|
|
1648
|
+
if (typeof val === "function") {
|
|
1649
|
+
throw new Error(
|
|
1650
|
+
`r-state-tree: snapshots do not support function${atPath}. Snapshots are JSON-only (primitives, arrays, plain objects, Dates as ISO strings).`
|
|
1651
|
+
);
|
|
1370
1652
|
}
|
|
1371
|
-
|
|
1653
|
+
throw new Error(
|
|
1654
|
+
`r-state-tree: snapshots do not support ${typeof val}${atPath}. Snapshots are JSON-only (primitives, arrays, plain objects, Dates as ISO strings).`
|
|
1655
|
+
);
|
|
1656
|
+
}
|
|
1657
|
+
if (val instanceof signalsCore.Signal) {
|
|
1658
|
+
return clone(
|
|
1659
|
+
val.value,
|
|
1660
|
+
path
|
|
1661
|
+
);
|
|
1662
|
+
}
|
|
1663
|
+
if (val instanceof Date) {
|
|
1664
|
+
return val.toISOString();
|
|
1665
|
+
}
|
|
1666
|
+
if (Array.isArray(val)) {
|
|
1667
|
+
return val.map(
|
|
1668
|
+
(v, i) => clone(v, path ? `${path}[${i}]` : `[${i}]`)
|
|
1669
|
+
);
|
|
1670
|
+
}
|
|
1671
|
+
if (!isPlainObject(val)) {
|
|
1672
|
+
const typeName = getTypeName(val);
|
|
1673
|
+
const atPath = path ? ` at path "${path}"` : "";
|
|
1674
|
+
throw new Error(
|
|
1675
|
+
`r-state-tree: snapshots do not support ${typeName}${atPath}. Snapshots are JSON-only (primitives, arrays, plain objects, Dates as ISO strings).`
|
|
1676
|
+
);
|
|
1677
|
+
}
|
|
1678
|
+
const keys = Object.keys(val);
|
|
1679
|
+
const cloned = {};
|
|
1680
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1681
|
+
const key = keys[i];
|
|
1682
|
+
const keyPath = path ? `${path}.${key}` : key;
|
|
1683
|
+
cloned[key] = clone(val[key], keyPath);
|
|
1684
|
+
}
|
|
1685
|
+
return cloned;
|
|
1686
|
+
}
|
|
1687
|
+
function getTypeName(val) {
|
|
1688
|
+
if (val instanceof Map) return "Map";
|
|
1689
|
+
if (val instanceof Set) return "Set";
|
|
1690
|
+
if (val instanceof WeakMap) return "WeakMap";
|
|
1691
|
+
if (val instanceof WeakSet) return "WeakSet";
|
|
1692
|
+
if (val instanceof RegExp) return "RegExp";
|
|
1693
|
+
if (val instanceof Error) return "Error";
|
|
1694
|
+
if (val instanceof Promise) return "Promise";
|
|
1695
|
+
const proto = Object.getPrototypeOf(val);
|
|
1696
|
+
if (proto?.constructor?.name && proto.constructor.name !== "Object") {
|
|
1697
|
+
return `class instance (${proto.constructor.name})`;
|
|
1372
1698
|
}
|
|
1373
|
-
return
|
|
1699
|
+
return "non-plain object";
|
|
1374
1700
|
}
|
|
1375
1701
|
function getDiff(o1, o2, getConfig) {
|
|
1376
1702
|
const config = getConfig(o2);
|
|
@@ -1443,7 +1769,9 @@ function updateProps(props, newProps) {
|
|
|
1443
1769
|
}
|
|
1444
1770
|
});
|
|
1445
1771
|
if (newProps.models) {
|
|
1446
|
-
if (!props.models
|
|
1772
|
+
if (!props.models || !isObservable(props.models)) {
|
|
1773
|
+
props.models = getObservable(props.models || {});
|
|
1774
|
+
}
|
|
1447
1775
|
Object.assign(props.models, newProps.models);
|
|
1448
1776
|
}
|
|
1449
1777
|
});
|
|
@@ -1667,7 +1995,9 @@ class StoreAdministration extends PreactObjectAdministration {
|
|
|
1667
1995
|
}
|
|
1668
1996
|
}
|
|
1669
1997
|
getModelRef(name) {
|
|
1670
|
-
|
|
1998
|
+
const models = this.proxy.props.models;
|
|
1999
|
+
const ref = models?.[name] ?? null;
|
|
2000
|
+
return ref;
|
|
1671
2001
|
}
|
|
1672
2002
|
isRoot() {
|
|
1673
2003
|
return !this.parent;
|
|
@@ -2005,11 +2335,12 @@ class ObservableListener {
|
|
|
2005
2335
|
class ChildModelsAdministration extends ArrayAdministration {
|
|
2006
2336
|
set(index, newValue) {
|
|
2007
2337
|
return signalsCore.batch(() => {
|
|
2008
|
-
super.set(index, newValue);
|
|
2338
|
+
const result = super.set(index, newValue);
|
|
2009
2339
|
const sourceValue = getSource(newValue);
|
|
2010
2340
|
if (this.source[index] !== sourceValue) {
|
|
2011
2341
|
notifyArrayUpdate(this.proxy, index, this.source[index], sourceValue);
|
|
2012
2342
|
}
|
|
2343
|
+
return result;
|
|
2013
2344
|
});
|
|
2014
2345
|
}
|
|
2015
2346
|
spliceWithArray(index, deleteCount, newItems) {
|
|
@@ -2127,9 +2458,7 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2127
2458
|
adm.setId(name, value);
|
|
2128
2459
|
break;
|
|
2129
2460
|
}
|
|
2130
|
-
case ModelCfgTypes.state:
|
|
2131
|
-
case ModelCfgTypes.stateShallow:
|
|
2132
|
-
case ModelCfgTypes.stateSignal: {
|
|
2461
|
+
case ModelCfgTypes.state: {
|
|
2133
2462
|
adm.setState(name, value);
|
|
2134
2463
|
break;
|
|
2135
2464
|
}
|
|
@@ -2199,12 +2528,14 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2199
2528
|
setState(name, value) {
|
|
2200
2529
|
const oldValue = this.source[name];
|
|
2201
2530
|
if (value !== oldValue) {
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2531
|
+
signalsCore.batch(() => {
|
|
2532
|
+
PreactObjectAdministration.proxyTraps.set(
|
|
2533
|
+
this.source,
|
|
2534
|
+
name,
|
|
2535
|
+
value,
|
|
2536
|
+
this.proxy
|
|
2537
|
+
);
|
|
2538
|
+
});
|
|
2208
2539
|
}
|
|
2209
2540
|
}
|
|
2210
2541
|
setId(name, v) {
|
|
@@ -2258,9 +2589,9 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2258
2589
|
getModelAdm(currentValue).detach();
|
|
2259
2590
|
} else if (Array.isArray(currentValue)) {
|
|
2260
2591
|
const oldModels = currentValue;
|
|
2261
|
-
const newModelSet = new Set(newModels);
|
|
2592
|
+
const newModelSet = new Set(newModels.map((m) => getSource(m)));
|
|
2262
2593
|
oldModels.forEach(
|
|
2263
|
-
(child2) => newModelSet.has(child2) || getModelAdm(child2).detach()
|
|
2594
|
+
(child2) => newModelSet.has(getSource(child2)) || getModelAdm(child2).detach()
|
|
2264
2595
|
);
|
|
2265
2596
|
this.modelsTraceUnsub.get(name)?.();
|
|
2266
2597
|
}
|
|
@@ -2283,8 +2614,8 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2283
2614
|
})
|
|
2284
2615
|
);
|
|
2285
2616
|
newModels.forEach((child2) => {
|
|
2286
|
-
const oldModelSet = Array.isArray(currentValue) ? new Set(currentValue) : /* @__PURE__ */ new Set();
|
|
2287
|
-
if (!oldModelSet.has(child2)) {
|
|
2617
|
+
const oldModelSet = Array.isArray(currentValue) ? new Set(currentValue.map((m) => getSource(m))) : /* @__PURE__ */ new Set();
|
|
2618
|
+
if (!oldModelSet.has(getSource(child2))) {
|
|
2288
2619
|
const internalModel = getModelAdm(child2);
|
|
2289
2620
|
internalModel.attach(this, name);
|
|
2290
2621
|
}
|
|
@@ -2398,11 +2729,12 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2398
2729
|
toJSON() {
|
|
2399
2730
|
return Object.keys(this.configuration).reduce((json, key) => {
|
|
2400
2731
|
switch (this.configuration[key].type) {
|
|
2401
|
-
case ModelCfgTypes.state:
|
|
2402
|
-
|
|
2403
|
-
|
|
2732
|
+
case ModelCfgTypes.state: {
|
|
2733
|
+
json[key] = clone(this.proxy[key], key);
|
|
2734
|
+
break;
|
|
2735
|
+
}
|
|
2404
2736
|
case ModelCfgTypes.id:
|
|
2405
|
-
json[key] = clone(getSource(this.proxy[key]));
|
|
2737
|
+
json[key] = clone(getSource(this.proxy[key]), key);
|
|
2406
2738
|
break;
|
|
2407
2739
|
case ModelCfgTypes.modelRef:
|
|
2408
2740
|
const model2 = this.proxy[key];
|
|
@@ -2466,8 +2798,6 @@ class ModelAdministration extends PreactObjectAdministration {
|
|
|
2466
2798
|
const value = snapshot[key];
|
|
2467
2799
|
switch (type) {
|
|
2468
2800
|
case ModelCfgTypes.state:
|
|
2469
|
-
case ModelCfgTypes.stateShallow:
|
|
2470
|
-
case ModelCfgTypes.stateSignal:
|
|
2471
2801
|
this.proxy[key] = value;
|
|
2472
2802
|
break;
|
|
2473
2803
|
case ModelCfgTypes.modelRef:
|
|
@@ -2661,6 +2991,73 @@ function createContext(defaultValue) {
|
|
|
2661
2991
|
}
|
|
2662
2992
|
};
|
|
2663
2993
|
}
|
|
2994
|
+
function toObservableTree(value) {
|
|
2995
|
+
return toObservableTreeInternal(value, /* @__PURE__ */ new Set(), "");
|
|
2996
|
+
}
|
|
2997
|
+
function buildPath(currentPath, key) {
|
|
2998
|
+
if (typeof key === "number") {
|
|
2999
|
+
return `${currentPath}[${key}]`;
|
|
3000
|
+
}
|
|
3001
|
+
return currentPath ? `${currentPath}.${key}` : key;
|
|
3002
|
+
}
|
|
3003
|
+
function toObservableTreeInternal(value, ancestorPath, path) {
|
|
3004
|
+
if (value === null || typeof value !== "object") {
|
|
3005
|
+
return value;
|
|
3006
|
+
}
|
|
3007
|
+
if (Array.isArray(value)) {
|
|
3008
|
+
if (ancestorPath.has(value)) {
|
|
3009
|
+
throw new Error(
|
|
3010
|
+
`r-state-tree: toObservableTree does not support circular references (cycle detected at path "${path}")`
|
|
3011
|
+
);
|
|
3012
|
+
}
|
|
3013
|
+
ancestorPath.add(value);
|
|
3014
|
+
const toWrap = [];
|
|
3015
|
+
for (let i = 0; i < value.length; i++) {
|
|
3016
|
+
const element = value[i];
|
|
3017
|
+
if (Array.isArray(element) || isPlainObject(element)) {
|
|
3018
|
+
toWrap.push({ index: i, element });
|
|
3019
|
+
}
|
|
3020
|
+
}
|
|
3021
|
+
const proxy = observable(value);
|
|
3022
|
+
for (const { index, element } of toWrap) {
|
|
3023
|
+
proxy[index] = toObservableTreeInternal(
|
|
3024
|
+
element,
|
|
3025
|
+
ancestorPath,
|
|
3026
|
+
buildPath(path, index)
|
|
3027
|
+
);
|
|
3028
|
+
}
|
|
3029
|
+
ancestorPath.delete(value);
|
|
3030
|
+
return proxy;
|
|
3031
|
+
}
|
|
3032
|
+
if (isPlainObject(value)) {
|
|
3033
|
+
if (ancestorPath.has(value)) {
|
|
3034
|
+
throw new Error(
|
|
3035
|
+
`r-state-tree: toObservableTree does not support circular references (cycle detected at path "${path}")`
|
|
3036
|
+
);
|
|
3037
|
+
}
|
|
3038
|
+
ancestorPath.add(value);
|
|
3039
|
+
const toWrap = [];
|
|
3040
|
+
const keys = Object.keys(value);
|
|
3041
|
+
for (let i = 0; i < keys.length; i++) {
|
|
3042
|
+
const key = keys[i];
|
|
3043
|
+
const propValue = value[key];
|
|
3044
|
+
if (Array.isArray(propValue) || isPlainObject(propValue)) {
|
|
3045
|
+
toWrap.push({ key, propValue });
|
|
3046
|
+
}
|
|
3047
|
+
}
|
|
3048
|
+
const proxy = observable(value);
|
|
3049
|
+
for (const { key, propValue } of toWrap) {
|
|
3050
|
+
proxy[key] = toObservableTreeInternal(
|
|
3051
|
+
propValue,
|
|
3052
|
+
ancestorPath,
|
|
3053
|
+
buildPath(path, key)
|
|
3054
|
+
);
|
|
3055
|
+
}
|
|
3056
|
+
ancestorPath.delete(value);
|
|
3057
|
+
return proxy;
|
|
3058
|
+
}
|
|
3059
|
+
return value;
|
|
3060
|
+
}
|
|
2664
3061
|
function makeDecorator(type) {
|
|
2665
3062
|
return function(value, context) {
|
|
2666
3063
|
context.metadata[context.name] = type;
|
|
@@ -2683,10 +3080,7 @@ const child = makeChildDecorator(childType);
|
|
|
2683
3080
|
const modelRef = makeChildDecorator(modelRefType);
|
|
2684
3081
|
const model = makeDecorator(modelType);
|
|
2685
3082
|
const id = makeDecorator(idType);
|
|
2686
|
-
const state =
|
|
2687
|
-
shallow: makeDecorator(stateShallowType),
|
|
2688
|
-
signal: makeDecorator(stateSignalType)
|
|
2689
|
-
});
|
|
3083
|
+
const state = makeDecorator(stateType);
|
|
2690
3084
|
Object.defineProperty(exports, "Signal", {
|
|
2691
3085
|
enumerable: true,
|
|
2692
3086
|
get: () => signalsCore.Signal
|
|
@@ -2699,10 +3093,6 @@ Object.defineProperty(exports, "effect", {
|
|
|
2699
3093
|
enumerable: true,
|
|
2700
3094
|
get: () => signalsCore.effect
|
|
2701
3095
|
});
|
|
2702
|
-
Object.defineProperty(exports, "signal", {
|
|
2703
|
-
enumerable: true,
|
|
2704
|
-
get: () => signalsCore.signal
|
|
2705
|
-
});
|
|
2706
3096
|
Object.defineProperty(exports, "untracked", {
|
|
2707
3097
|
enumerable: true,
|
|
2708
3098
|
get: () => signalsCore.untracked
|
|
@@ -2727,8 +3117,10 @@ exports.onSnapshotDiff = onSnapshotDiff;
|
|
|
2727
3117
|
exports.reaction = reaction;
|
|
2728
3118
|
exports.reportChanged = reportChanged;
|
|
2729
3119
|
exports.reportObserved = reportObserved;
|
|
3120
|
+
exports.signal = signal;
|
|
2730
3121
|
exports.source = source;
|
|
2731
3122
|
exports.state = state;
|
|
3123
|
+
exports.toObservableTree = toObservableTree;
|
|
2732
3124
|
exports.toSnapshot = toSnapshot;
|
|
2733
3125
|
exports.unmount = unmount;
|
|
2734
3126
|
exports.updateStore = updateStore;
|