r-state-tree 0.6.1 → 0.6.3
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/dist/r-state-tree.cjs +53 -48
- package/dist/r-state-tree.js +54 -49
- package/package.json +1 -1
package/dist/r-state-tree.cjs
CHANGED
|
@@ -66,20 +66,13 @@ function isPropertyKey(val) {
|
|
|
66
66
|
return typeof val === "string" || typeof val === "number" || typeof val === "symbol";
|
|
67
67
|
}
|
|
68
68
|
function getPropertyType(key, obj) {
|
|
69
|
-
const
|
|
70
|
-
const hasMetadata = ctor != null && ctor[Symbol.metadata] !== void 0;
|
|
69
|
+
const plain = isPlainObject(obj);
|
|
71
70
|
const descriptor = getPropertyDescriptor$1(obj, key);
|
|
72
71
|
if (descriptor?.value && typeof descriptor.value === "function") {
|
|
73
72
|
return "action";
|
|
74
73
|
}
|
|
75
74
|
const isAccessor = descriptor && (typeof descriptor.get === "function" || typeof descriptor.set === "function");
|
|
76
|
-
if (
|
|
77
|
-
if (descriptor?.get) {
|
|
78
|
-
return "computed";
|
|
79
|
-
}
|
|
80
|
-
if (isAccessor) {
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
75
|
+
if (plain) {
|
|
83
76
|
return "observable";
|
|
84
77
|
}
|
|
85
78
|
const metadata = obj.constructor[Symbol.metadata];
|
|
@@ -96,7 +89,10 @@ function getPropertyType(key, obj) {
|
|
|
96
89
|
return "observable";
|
|
97
90
|
}
|
|
98
91
|
}
|
|
99
|
-
|
|
92
|
+
if (isAccessor) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
return "observable";
|
|
100
96
|
}
|
|
101
97
|
function getPropertyDescriptor$1(obj, key) {
|
|
102
98
|
let node = obj;
|
|
@@ -336,7 +332,7 @@ class ObjectAdministration extends Administration {
|
|
|
336
332
|
}
|
|
337
333
|
getType(key) {
|
|
338
334
|
let type = this.types.get(key);
|
|
339
|
-
if (type === void 0) {
|
|
335
|
+
if (type === void 0 && !this.types.has(key)) {
|
|
340
336
|
type = getPropertyType(key, this.source);
|
|
341
337
|
this.types.set(key, type);
|
|
342
338
|
}
|
|
@@ -354,58 +350,50 @@ class ObjectAdministration extends Administration {
|
|
|
354
350
|
if (type === "computed") {
|
|
355
351
|
return resolveNode(this.getComputed(key));
|
|
356
352
|
}
|
|
353
|
+
if (type === null) {
|
|
354
|
+
return void 0;
|
|
355
|
+
}
|
|
357
356
|
return resolveNode(this.valuesMap.getOrCreate(key, this.source[key]));
|
|
358
357
|
}
|
|
359
358
|
read(key, receiver = this.proxy) {
|
|
360
359
|
const type = this.getType(key);
|
|
360
|
+
if (type === "computed") {
|
|
361
|
+
if (receiver === this.proxy) {
|
|
362
|
+
return this.callComputed(key);
|
|
363
|
+
}
|
|
364
|
+
this.atom.reportObserved();
|
|
365
|
+
return Reflect.get(this.source, key, receiver);
|
|
366
|
+
}
|
|
361
367
|
if (type === null) {
|
|
362
368
|
return Reflect.get(this.source, key, receiver);
|
|
363
369
|
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
if (
|
|
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
|
-
}
|
|
370
|
+
this.atom.reportObserved();
|
|
371
|
+
if (this.atom.observing) {
|
|
372
|
+
this.hasMap.reportObserved(key);
|
|
373
|
+
}
|
|
374
|
+
const value = Reflect.get(this.source, key, receiver);
|
|
375
|
+
this.valuesMap.reportObserved(key, value);
|
|
376
|
+
if (type === "observable") {
|
|
377
|
+
const shouldWrap = this.explicitObservables.has(key) || isObservable(value);
|
|
378
|
+
if (shouldWrap && value && typeof value === "object" && !Object.isFrozen(value)) {
|
|
379
|
+
const desc = getPropertyDescriptor$1(this.source, key);
|
|
380
|
+
if (desc && !desc.configurable && !desc.writable) {
|
|
387
381
|
return value;
|
|
388
382
|
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
}
|
|
393
|
-
case "computed": {
|
|
394
|
-
if (receiver === this.proxy) {
|
|
395
|
-
return this.callComputed(key);
|
|
383
|
+
const existingAdm = getAdministration(value);
|
|
384
|
+
if (existingAdm) {
|
|
385
|
+
return existingAdm.proxy;
|
|
396
386
|
}
|
|
397
|
-
this.atom.reportObserved();
|
|
398
|
-
return Reflect.get(this.source, key, receiver);
|
|
399
387
|
}
|
|
400
|
-
|
|
401
|
-
throw new Error(`unknown type passed to configure`);
|
|
388
|
+
return value;
|
|
402
389
|
}
|
|
390
|
+
return getAction(value);
|
|
403
391
|
}
|
|
404
392
|
explicitObservables = /* @__PURE__ */ new Set();
|
|
405
393
|
write(key, newValue) {
|
|
406
394
|
const type = this.getType(key);
|
|
407
395
|
if (type === null) {
|
|
408
|
-
return
|
|
396
|
+
return Reflect.set(this.source, key, newValue, this.proxy);
|
|
409
397
|
}
|
|
410
398
|
if (type === "computed") {
|
|
411
399
|
return signalsCore.batch(() => this.set(key, newValue));
|
|
@@ -590,8 +578,24 @@ function createListener(callback) {
|
|
|
590
578
|
};
|
|
591
579
|
return listener;
|
|
592
580
|
}
|
|
581
|
+
function internalCreateComputed(fn) {
|
|
582
|
+
const version = signalsCore.signal(0);
|
|
583
|
+
const c = signalsCore.computed(
|
|
584
|
+
() => {
|
|
585
|
+
version.value;
|
|
586
|
+
return fn();
|
|
587
|
+
},
|
|
588
|
+
{
|
|
589
|
+
unwatched() {
|
|
590
|
+
version.value++;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
);
|
|
594
|
+
return { c, version };
|
|
595
|
+
}
|
|
593
596
|
function createComputed(fn, context = null) {
|
|
594
|
-
const
|
|
597
|
+
const bound = context ? fn.bind(context) : fn;
|
|
598
|
+
const { c } = internalCreateComputed(bound);
|
|
595
599
|
return {
|
|
596
600
|
node: c,
|
|
597
601
|
get() {
|
|
@@ -600,6 +604,7 @@ function createComputed(fn, context = null) {
|
|
|
600
604
|
clear: () => {
|
|
601
605
|
signalsCore.untracked(() => {
|
|
602
606
|
c._value = void 0;
|
|
607
|
+
c._globalVersion = -1;
|
|
603
608
|
});
|
|
604
609
|
}
|
|
605
610
|
};
|
|
@@ -615,7 +620,7 @@ function computed(value, context) {
|
|
|
615
620
|
context.metadata[context.name] = { type: "computed" };
|
|
616
621
|
return value;
|
|
617
622
|
}
|
|
618
|
-
return
|
|
623
|
+
return internalCreateComputed(value).c;
|
|
619
624
|
}
|
|
620
625
|
function source(obj) {
|
|
621
626
|
return getSource(obj);
|
package/dist/r-state-tree.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { batch, signal as signal$1, Signal, computed as computed$1
|
|
1
|
+
import { batch, signal as signal$1, Signal, untracked, effect, computed as computed$1 } from "@preact/signals-core";
|
|
2
2
|
import { Signal as Signal2, batch as batch2, effect as effect2, untracked as untracked2 } from "@preact/signals-core";
|
|
3
3
|
var lib = {};
|
|
4
4
|
var hasRequiredLib;
|
|
@@ -65,20 +65,13 @@ function isPropertyKey(val) {
|
|
|
65
65
|
return typeof val === "string" || typeof val === "number" || typeof val === "symbol";
|
|
66
66
|
}
|
|
67
67
|
function getPropertyType(key, obj) {
|
|
68
|
-
const
|
|
69
|
-
const hasMetadata = ctor != null && ctor[Symbol.metadata] !== void 0;
|
|
68
|
+
const plain = isPlainObject(obj);
|
|
70
69
|
const descriptor = getPropertyDescriptor$1(obj, key);
|
|
71
70
|
if (descriptor?.value && typeof descriptor.value === "function") {
|
|
72
71
|
return "action";
|
|
73
72
|
}
|
|
74
73
|
const isAccessor = descriptor && (typeof descriptor.get === "function" || typeof descriptor.set === "function");
|
|
75
|
-
if (
|
|
76
|
-
if (descriptor?.get) {
|
|
77
|
-
return "computed";
|
|
78
|
-
}
|
|
79
|
-
if (isAccessor) {
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
74
|
+
if (plain) {
|
|
82
75
|
return "observable";
|
|
83
76
|
}
|
|
84
77
|
const metadata = obj.constructor[Symbol.metadata];
|
|
@@ -95,7 +88,10 @@ function getPropertyType(key, obj) {
|
|
|
95
88
|
return "observable";
|
|
96
89
|
}
|
|
97
90
|
}
|
|
98
|
-
|
|
91
|
+
if (isAccessor) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
return "observable";
|
|
99
95
|
}
|
|
100
96
|
function getPropertyDescriptor$1(obj, key) {
|
|
101
97
|
let node = obj;
|
|
@@ -335,7 +331,7 @@ class ObjectAdministration extends Administration {
|
|
|
335
331
|
}
|
|
336
332
|
getType(key) {
|
|
337
333
|
let type = this.types.get(key);
|
|
338
|
-
if (type === void 0) {
|
|
334
|
+
if (type === void 0 && !this.types.has(key)) {
|
|
339
335
|
type = getPropertyType(key, this.source);
|
|
340
336
|
this.types.set(key, type);
|
|
341
337
|
}
|
|
@@ -353,58 +349,50 @@ class ObjectAdministration extends Administration {
|
|
|
353
349
|
if (type === "computed") {
|
|
354
350
|
return resolveNode(this.getComputed(key));
|
|
355
351
|
}
|
|
352
|
+
if (type === null) {
|
|
353
|
+
return void 0;
|
|
354
|
+
}
|
|
356
355
|
return resolveNode(this.valuesMap.getOrCreate(key, this.source[key]));
|
|
357
356
|
}
|
|
358
357
|
read(key, receiver = this.proxy) {
|
|
359
358
|
const type = this.getType(key);
|
|
359
|
+
if (type === "computed") {
|
|
360
|
+
if (receiver === this.proxy) {
|
|
361
|
+
return this.callComputed(key);
|
|
362
|
+
}
|
|
363
|
+
this.atom.reportObserved();
|
|
364
|
+
return Reflect.get(this.source, key, receiver);
|
|
365
|
+
}
|
|
360
366
|
if (type === null) {
|
|
361
367
|
return Reflect.get(this.source, key, receiver);
|
|
362
368
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
if (
|
|
374
|
-
const value = Reflect.get(this.source, key, receiver);
|
|
375
|
-
const shouldWrap = this.explicitObservables.has(key) || isObservable(value);
|
|
376
|
-
if (shouldWrap && value && typeof value === "object" && !Object.isFrozen(value)) {
|
|
377
|
-
const desc = getPropertyDescriptor$1(this.source, key);
|
|
378
|
-
if (desc && !desc.configurable && !desc.writable) {
|
|
379
|
-
return value;
|
|
380
|
-
}
|
|
381
|
-
const existingAdm = getAdministration(value);
|
|
382
|
-
if (existingAdm) {
|
|
383
|
-
return existingAdm.proxy;
|
|
384
|
-
}
|
|
385
|
-
}
|
|
369
|
+
this.atom.reportObserved();
|
|
370
|
+
if (this.atom.observing) {
|
|
371
|
+
this.hasMap.reportObserved(key);
|
|
372
|
+
}
|
|
373
|
+
const value = Reflect.get(this.source, key, receiver);
|
|
374
|
+
this.valuesMap.reportObserved(key, value);
|
|
375
|
+
if (type === "observable") {
|
|
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) {
|
|
386
380
|
return value;
|
|
387
381
|
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
}
|
|
392
|
-
case "computed": {
|
|
393
|
-
if (receiver === this.proxy) {
|
|
394
|
-
return this.callComputed(key);
|
|
382
|
+
const existingAdm = getAdministration(value);
|
|
383
|
+
if (existingAdm) {
|
|
384
|
+
return existingAdm.proxy;
|
|
395
385
|
}
|
|
396
|
-
this.atom.reportObserved();
|
|
397
|
-
return Reflect.get(this.source, key, receiver);
|
|
398
386
|
}
|
|
399
|
-
|
|
400
|
-
throw new Error(`unknown type passed to configure`);
|
|
387
|
+
return value;
|
|
401
388
|
}
|
|
389
|
+
return getAction(value);
|
|
402
390
|
}
|
|
403
391
|
explicitObservables = /* @__PURE__ */ new Set();
|
|
404
392
|
write(key, newValue) {
|
|
405
393
|
const type = this.getType(key);
|
|
406
394
|
if (type === null) {
|
|
407
|
-
return
|
|
395
|
+
return Reflect.set(this.source, key, newValue, this.proxy);
|
|
408
396
|
}
|
|
409
397
|
if (type === "computed") {
|
|
410
398
|
return batch(() => this.set(key, newValue));
|
|
@@ -589,8 +577,24 @@ function createListener(callback) {
|
|
|
589
577
|
};
|
|
590
578
|
return listener;
|
|
591
579
|
}
|
|
580
|
+
function internalCreateComputed(fn) {
|
|
581
|
+
const version = signal$1(0);
|
|
582
|
+
const c = computed$1(
|
|
583
|
+
() => {
|
|
584
|
+
version.value;
|
|
585
|
+
return fn();
|
|
586
|
+
},
|
|
587
|
+
{
|
|
588
|
+
unwatched() {
|
|
589
|
+
version.value++;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
);
|
|
593
|
+
return { c, version };
|
|
594
|
+
}
|
|
592
595
|
function createComputed(fn, context = null) {
|
|
593
|
-
const
|
|
596
|
+
const bound = context ? fn.bind(context) : fn;
|
|
597
|
+
const { c } = internalCreateComputed(bound);
|
|
594
598
|
return {
|
|
595
599
|
node: c,
|
|
596
600
|
get() {
|
|
@@ -599,6 +603,7 @@ function createComputed(fn, context = null) {
|
|
|
599
603
|
clear: () => {
|
|
600
604
|
untracked(() => {
|
|
601
605
|
c._value = void 0;
|
|
606
|
+
c._globalVersion = -1;
|
|
602
607
|
});
|
|
603
608
|
}
|
|
604
609
|
};
|
|
@@ -614,7 +619,7 @@ function computed(value, context) {
|
|
|
614
619
|
context.metadata[context.name] = { type: "computed" };
|
|
615
620
|
return value;
|
|
616
621
|
}
|
|
617
|
-
return
|
|
622
|
+
return internalCreateComputed(value).c;
|
|
618
623
|
}
|
|
619
624
|
function source(obj) {
|
|
620
625
|
return getSource(obj);
|