r-state-tree 0.6.1 → 0.6.2

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.
@@ -7,7 +7,7 @@ export declare class ObjectAdministration<T extends object> extends Administrati
7
7
  hasMap: AtomMap<PropertyKey>;
8
8
  valuesMap: SignalMap<PropertyKey>;
9
9
  computedMap: Map<PropertyKey, ComputedNode<T[keyof T]>>;
10
- types: Map<PropertyKey, PropertyType | null>;
10
+ types: Map<PropertyKey, PropertyType>;
11
11
  isWriting: boolean;
12
12
  static proxyTraps: ProxyHandler<object>;
13
13
  constructor(source?: T);
@@ -72,14 +72,7 @@ function getPropertyType(key, obj) {
72
72
  if (descriptor?.value && typeof descriptor.value === "function") {
73
73
  return "action";
74
74
  }
75
- const isAccessor = descriptor && (typeof descriptor.get === "function" || typeof descriptor.set === "function");
76
75
  if (!hasMetadata) {
77
- if (descriptor?.get) {
78
- return "computed";
79
- }
80
- if (isAccessor) {
81
- return null;
82
- }
83
76
  return "observable";
84
77
  }
85
78
  const metadata = obj.constructor[Symbol.metadata];
@@ -96,7 +89,7 @@ function getPropertyType(key, obj) {
96
89
  return "observable";
97
90
  }
98
91
  }
99
- return isAccessor ? null : "observable";
92
+ return "observable";
100
93
  }
101
94
  function getPropertyDescriptor$1(obj, key) {
102
95
  let node = obj;
@@ -358,55 +351,42 @@ class ObjectAdministration extends Administration {
358
351
  }
359
352
  read(key, receiver = this.proxy) {
360
353
  const type = this.getType(key);
361
- if (type === null) {
354
+ if (type === "computed") {
355
+ if (receiver === this.proxy) {
356
+ return this.callComputed(key);
357
+ }
358
+ this.atom.reportObserved();
362
359
  return Reflect.get(this.source, key, receiver);
363
360
  }
364
- switch (type) {
365
- case "observable":
366
- case "action": {
367
- if (key in this.source) {
368
- this.valuesMap.reportObserved(key, this.source[key]);
369
- }
370
- this.atom.reportObserved();
371
- if (this.atom.observing) {
372
- this.hasMap.reportObserved(key);
373
- }
374
- if (type === "observable") {
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
- }
361
+ if (key in this.source) {
362
+ this.valuesMap.reportObserved(key, this.source[key]);
363
+ }
364
+ this.atom.reportObserved();
365
+ if (this.atom.observing) {
366
+ this.hasMap.reportObserved(key);
367
+ }
368
+ if (type === "observable") {
369
+ const value = Reflect.get(this.source, key, receiver);
370
+ const shouldWrap = this.explicitObservables.has(key) || isObservable(value);
371
+ if (shouldWrap && value && typeof value === "object" && !Object.isFrozen(value)) {
372
+ const desc = getPropertyDescriptor$1(this.source, key);
373
+ if (desc && !desc.configurable && !desc.writable) {
387
374
  return value;
388
375
  }
389
- return getAction(
390
- Reflect.get(this.source, key, receiver)
391
- );
392
- }
393
- case "computed": {
394
- if (receiver === this.proxy) {
395
- return this.callComputed(key);
376
+ const existingAdm = getAdministration(value);
377
+ if (existingAdm) {
378
+ return existingAdm.proxy;
396
379
  }
397
- this.atom.reportObserved();
398
- return Reflect.get(this.source, key, receiver);
399
380
  }
400
- default:
401
- throw new Error(`unknown type passed to configure`);
381
+ return value;
402
382
  }
383
+ return getAction(
384
+ Reflect.get(this.source, key, receiver)
385
+ );
403
386
  }
404
387
  explicitObservables = /* @__PURE__ */ new Set();
405
388
  write(key, newValue) {
406
389
  const type = this.getType(key);
407
- if (type === null) {
408
- return this.set(key, newValue);
409
- }
410
390
  if (type === "computed") {
411
391
  return signalsCore.batch(() => this.set(key, newValue));
412
392
  }
@@ -590,8 +570,24 @@ function createListener(callback) {
590
570
  };
591
571
  return listener;
592
572
  }
573
+ function internalCreateComputed(fn) {
574
+ const version = signalsCore.signal(0);
575
+ const c = signalsCore.computed(
576
+ () => {
577
+ version.value;
578
+ return fn();
579
+ },
580
+ {
581
+ unwatched() {
582
+ version.value++;
583
+ }
584
+ }
585
+ );
586
+ return { c, version };
587
+ }
593
588
  function createComputed(fn, context = null) {
594
- const c = signalsCore.computed(context ? fn.bind(context) : fn);
589
+ const bound = context ? fn.bind(context) : fn;
590
+ const { c } = internalCreateComputed(bound);
595
591
  return {
596
592
  node: c,
597
593
  get() {
@@ -600,6 +596,7 @@ function createComputed(fn, context = null) {
600
596
  clear: () => {
601
597
  signalsCore.untracked(() => {
602
598
  c._value = void 0;
599
+ c._globalVersion = -1;
603
600
  });
604
601
  }
605
602
  };
@@ -615,7 +612,7 @@ function computed(value, context) {
615
612
  context.metadata[context.name] = { type: "computed" };
616
613
  return value;
617
614
  }
618
- return signalsCore.computed(value);
615
+ return internalCreateComputed(value).c;
619
616
  }
620
617
  function source(obj) {
621
618
  return getSource(obj);
@@ -1,4 +1,4 @@
1
- import { batch, signal as signal$1, Signal, computed as computed$1, untracked, effect } from "@preact/signals-core";
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;
@@ -71,14 +71,7 @@ function getPropertyType(key, obj) {
71
71
  if (descriptor?.value && typeof descriptor.value === "function") {
72
72
  return "action";
73
73
  }
74
- const isAccessor = descriptor && (typeof descriptor.get === "function" || typeof descriptor.set === "function");
75
74
  if (!hasMetadata) {
76
- if (descriptor?.get) {
77
- return "computed";
78
- }
79
- if (isAccessor) {
80
- return null;
81
- }
82
75
  return "observable";
83
76
  }
84
77
  const metadata = obj.constructor[Symbol.metadata];
@@ -95,7 +88,7 @@ function getPropertyType(key, obj) {
95
88
  return "observable";
96
89
  }
97
90
  }
98
- return isAccessor ? null : "observable";
91
+ return "observable";
99
92
  }
100
93
  function getPropertyDescriptor$1(obj, key) {
101
94
  let node = obj;
@@ -357,55 +350,42 @@ class ObjectAdministration extends Administration {
357
350
  }
358
351
  read(key, receiver = this.proxy) {
359
352
  const type = this.getType(key);
360
- if (type === null) {
353
+ if (type === "computed") {
354
+ if (receiver === this.proxy) {
355
+ return this.callComputed(key);
356
+ }
357
+ this.atom.reportObserved();
361
358
  return Reflect.get(this.source, key, receiver);
362
359
  }
363
- switch (type) {
364
- case "observable":
365
- case "action": {
366
- if (key in this.source) {
367
- this.valuesMap.reportObserved(key, this.source[key]);
368
- }
369
- this.atom.reportObserved();
370
- if (this.atom.observing) {
371
- this.hasMap.reportObserved(key);
372
- }
373
- if (type === "observable") {
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
- }
360
+ if (key in this.source) {
361
+ this.valuesMap.reportObserved(key, this.source[key]);
362
+ }
363
+ this.atom.reportObserved();
364
+ if (this.atom.observing) {
365
+ this.hasMap.reportObserved(key);
366
+ }
367
+ if (type === "observable") {
368
+ const value = Reflect.get(this.source, key, receiver);
369
+ const shouldWrap = this.explicitObservables.has(key) || isObservable(value);
370
+ if (shouldWrap && value && typeof value === "object" && !Object.isFrozen(value)) {
371
+ const desc = getPropertyDescriptor$1(this.source, key);
372
+ if (desc && !desc.configurable && !desc.writable) {
386
373
  return value;
387
374
  }
388
- return getAction(
389
- Reflect.get(this.source, key, receiver)
390
- );
391
- }
392
- case "computed": {
393
- if (receiver === this.proxy) {
394
- return this.callComputed(key);
375
+ const existingAdm = getAdministration(value);
376
+ if (existingAdm) {
377
+ return existingAdm.proxy;
395
378
  }
396
- this.atom.reportObserved();
397
- return Reflect.get(this.source, key, receiver);
398
379
  }
399
- default:
400
- throw new Error(`unknown type passed to configure`);
380
+ return value;
401
381
  }
382
+ return getAction(
383
+ Reflect.get(this.source, key, receiver)
384
+ );
402
385
  }
403
386
  explicitObservables = /* @__PURE__ */ new Set();
404
387
  write(key, newValue) {
405
388
  const type = this.getType(key);
406
- if (type === null) {
407
- return this.set(key, newValue);
408
- }
409
389
  if (type === "computed") {
410
390
  return batch(() => this.set(key, newValue));
411
391
  }
@@ -589,8 +569,24 @@ function createListener(callback) {
589
569
  };
590
570
  return listener;
591
571
  }
572
+ function internalCreateComputed(fn) {
573
+ const version = signal$1(0);
574
+ const c = computed$1(
575
+ () => {
576
+ version.value;
577
+ return fn();
578
+ },
579
+ {
580
+ unwatched() {
581
+ version.value++;
582
+ }
583
+ }
584
+ );
585
+ return { c, version };
586
+ }
592
587
  function createComputed(fn, context = null) {
593
- const c = computed$1(context ? fn.bind(context) : fn);
588
+ const bound = context ? fn.bind(context) : fn;
589
+ const { c } = internalCreateComputed(bound);
594
590
  return {
595
591
  node: c,
596
592
  get() {
@@ -599,6 +595,7 @@ function createComputed(fn, context = null) {
599
595
  clear: () => {
600
596
  untracked(() => {
601
597
  c._value = void 0;
598
+ c._globalVersion = -1;
602
599
  });
603
600
  }
604
601
  };
@@ -614,7 +611,7 @@ function computed(value, context) {
614
611
  context.metadata[context.name] = { type: "computed" };
615
612
  return value;
616
613
  }
617
- return computed$1(value);
614
+ return internalCreateComputed(value).c;
618
615
  }
619
616
  function source(obj) {
620
617
  return getSource(obj);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "r-state-tree",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "reactive state management library",
5
5
  "main": "dist/r-state-tree.cjs",
6
6
  "module": "dist/r-state-tree.js",