totally-tubular 0.6.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.
@@ -30,6 +30,7 @@ export declare class Tubular<T extends object> {
30
30
  * disconnects an observer callback from totally tubular
31
31
  */
32
32
  unobserve<K extends AllObjectKeys<T>>(propPath: K, observeCallback: ObservationCallback<T, K>): void;
33
+ private notifyObserversForPath;
33
34
  /**
34
35
  * updates a portion of your state
35
36
  */
@@ -50,12 +50,28 @@ export class Tubular {
50
50
  }
51
51
  resetImpl(parentKey) {
52
52
  let sourceObj = this.initialStateClone;
53
+ let liveObj = this.state;
53
54
  if (parentKey) {
54
55
  for (const p of parentKey.split('.')) {
55
56
  sourceObj = sourceObj?.[p];
57
+ liveObj = liveObj?.[p];
56
58
  }
57
- if (isNullOrUndefined(sourceObj) || !isObject(sourceObj))
59
+ if (isNullOrUndefined(sourceObj) ||
60
+ !isObject(sourceObj) ||
61
+ isNullOrUndefined(liveObj) ||
62
+ !isObject(liveObj) ||
63
+ isArray(liveObj)) {
58
64
  return;
65
+ }
66
+ }
67
+ // keys created after construction are not part of the pristine state,
68
+ // so reset has to remove them, otherwise they leak past every reset
69
+ for (const key of Object.keys(liveObj)) {
70
+ if (!Object.hasOwn(sourceObj, key)) {
71
+ const removedVal = liveObj[key];
72
+ delete liveObj[key];
73
+ this.notifyObserversForPath((parentKey ? `${parentKey}.${key}` : key), null, removedVal);
74
+ }
59
75
  }
60
76
  for (const [key, val] of Object.entries(sourceObj)) {
61
77
  const childKey = parentKey ? `${parentKey}.${key}` : key;
@@ -63,7 +79,7 @@ export class Tubular {
63
79
  this.resetImpl(childKey);
64
80
  }
65
81
  else {
66
- this.update(childKey, () => val);
82
+ this.update(childKey, () => structuredClone(val));
67
83
  }
68
84
  }
69
85
  }
@@ -83,6 +99,11 @@ export class Tubular {
83
99
  return;
84
100
  this.observationCallbacks.set(propPath, observers.filter((ob) => ob !== observeCallback));
85
101
  }
102
+ notifyObserversForPath(propPath, newVal, oldVal) {
103
+ for (const cb of this.observationCallbacks.get(propPath) ?? []) {
104
+ cb(newVal, oldVal, propPath);
105
+ }
106
+ }
86
107
  /**
87
108
  * updates a portion of your state
88
109
  */
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "A completely framework-agnostic, JavaScript / TypeScript state management and update broadcasting library wit zero dependencies, solid performance and a micro file size.",
4
4
  "name": "totally-tubular",
5
5
  "type": "module",
6
- "version": "0.6.0",
6
+ "version": "0.6.1",
7
7
  "keywords": [
8
8
  "state",
9
9
  "management",