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.
@@ -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 ctor = obj.constructor;
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 (!hasMetadata) {
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
- return isAccessor ? null : "observable";
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
- 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
- }
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
- return getAction(
390
- Reflect.get(this.source, key, receiver)
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
- default:
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 this.set(key, newValue);
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 c = signalsCore.computed(context ? fn.bind(context) : fn);
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 signalsCore.computed(value);
623
+ return internalCreateComputed(value).c;
619
624
  }
620
625
  function source(obj) {
621
626
  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;
@@ -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 ctor = obj.constructor;
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 (!hasMetadata) {
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
- return isAccessor ? null : "observable";
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
- 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
- }
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
- return getAction(
389
- Reflect.get(this.source, key, receiver)
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
- default:
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 this.set(key, newValue);
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 c = computed$1(context ? fn.bind(context) : fn);
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 computed$1(value);
622
+ return internalCreateComputed(value).c;
618
623
  }
619
624
  function source(obj) {
620
625
  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.3",
4
4
  "description": "reactive state management library",
5
5
  "main": "dist/r-state-tree.cjs",
6
6
  "module": "dist/r-state-tree.js",