@slimlib/store 2.1.0 → 2.2.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.
Files changed (3) hide show
  1. package/index.d.ts +8 -1
  2. package/index.mjs +19 -14
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -116,7 +116,8 @@ declare module '@slimlib/store' {
116
116
  * Creates a reactive scope for tracking effects
117
117
  * Effects created within a scope callback are automatically tracked and disposed together
118
118
  */
119
- export const scope: (callback?: ScopeCallback, parent?: Scope | undefined | null) => Scope;
119
+ export const scope: (callback?: ScopeCallback, parent?: Scope | undefined) => Scope;
120
+ export const getParentScope: (scope: ScopeParent) => ScopeParent | undefined;
120
121
  /**
121
122
  * Create a simple signal without an initial value
122
123
  */
@@ -160,6 +161,12 @@ declare module '@slimlib/store' {
160
161
  export type Scope = ScopeFunction & {
161
162
  [key: symbol]: unknown;
162
163
  };
164
+ /**
165
+ * A non-disposable reference to a parent scope
166
+ */
167
+ export type ScopeParent = {
168
+ [key: symbol]: unknown;
169
+ };
163
170
  /**
164
171
  * Signal type - a callable that returns the current value with a set method
165
172
  */
package/index.mjs CHANGED
@@ -12,9 +12,9 @@ const setScheduler = (newScheduler) => {
12
12
  scheduler = newScheduler;
13
13
  };
14
14
 
15
- const [unwrap, propertyDepsSymbol, trackSymbol, childrenSymbol,
15
+ const [unwrap, propertyDepsSymbol, trackSymbol, childrenSymbol, parentSymbol,
16
16
 
17
- ] = Array.from([, , , ,], Symbol);
17
+ ] = Array.from([, , , , ,], Symbol);
18
18
 
19
19
  const noopGetter = () => undefined;
20
20
 
@@ -96,8 +96,12 @@ const makeNonLive = (node) => {
96
96
  };
97
97
 
98
98
  const clearSources = (node, fromIndex = 0) => {
99
- const isLive = (node.g & (8 | 64 )) !== 0;
100
99
  const nodeSources = node.h;
100
+
101
+ if ((node.g & (8 | 64 )) === 0) {
102
+ nodeSources.length = fromIndex;
103
+ return;
104
+ }
101
105
  for (let i = fromIndex, len = nodeSources.length; i < len; ++i) {
102
106
  const { c: c, d: sourceNode } = nodeSources[i];
103
107
 
@@ -109,8 +113,7 @@ const clearSources = (node, fromIndex = 0) => {
109
113
 
110
114
  c.delete(node);
111
115
 
112
- if (isLive &&
113
- sourceNode !== undefined &&
116
+ if (sourceNode !== undefined &&
114
117
  (sourceNode.g & (8 | 64 )) === 64 &&
115
118
  sourceNode.i.size === 0) {
116
119
  makeNonLive(sourceNode);
@@ -134,7 +137,7 @@ const flushEffects = () => {
134
137
  for (let i = 0, len = nodes.length; i < len; ++i) {
135
138
  const node = nodes[i];
136
139
  try {
137
- node.j?.();
140
+ node.j();
138
141
  }
139
142
  catch (e) {
140
143
  console.error(e);
@@ -149,7 +152,7 @@ const scheduleFlush = () => {
149
152
  }
150
153
  };
151
154
 
152
- const trackStateDependency = (deps, valueGetter, cachedValue) => {
155
+ const trackStateDependency = (deps, cachedValue) => {
153
156
 
154
157
  const sourcesArray = currentComputing.h;
155
158
  const skipIndex = currentComputing.k;
@@ -161,7 +164,7 @@ const trackStateDependency = (deps, valueGetter, cachedValue) => {
161
164
  clearSources(currentComputing, skipIndex);
162
165
  }
163
166
 
164
- sourcesArray.push(createSourceEntry(deps, undefined, deps.a, valueGetter, cachedValue));
167
+ sourcesArray.push(createSourceEntry(deps, undefined, deps.a, deps.b, cachedValue));
165
168
 
166
169
  currentComputing.g |= 128 ;
167
170
 
@@ -173,7 +176,6 @@ const trackStateDependency = (deps, valueGetter, cachedValue) => {
173
176
 
174
177
  const entry = sourcesArray[skipIndex];
175
178
  entry.a = deps.a;
176
- entry.b = valueGetter;
177
179
  entry.e = cachedValue;
178
180
 
179
181
  currentComputing.g |= 128 ;
@@ -675,6 +677,7 @@ const scope = (callback, parent = activeScope) => {
675
677
 
676
678
  ctx[trackSymbol] = (dispose) => effects.push(dispose);
677
679
  ctx[childrenSymbol] = children;
680
+ ctx[parentSymbol] = parent;
678
681
 
679
682
  if (parent) {
680
683
  myIndex = parent[childrenSymbol].push(ctx) - 1;
@@ -685,6 +688,7 @@ const scope = (callback, parent = activeScope) => {
685
688
  }
686
689
  return ctx;
687
690
  };
691
+ const getParentScope = (scope) => scope[parentSymbol];
688
692
 
689
693
  function signal(initialValue) {
690
694
  let value = initialValue;
@@ -694,7 +698,7 @@ function signal(initialValue) {
694
698
 
695
699
  if (tracked && currentComputing !== undefined) {
696
700
 
697
- trackStateDependency((deps ??= new DepsSet(read)), read, value);
701
+ trackStateDependency((deps ??= new DepsSet(read)), value);
698
702
  }
699
703
  return value;
700
704
 
@@ -728,8 +732,9 @@ function state(object = {}) {
728
732
  }
729
733
  };
730
734
  const createProxy = (object) => {
731
- if (proxiesCache.has(object)) {
732
- return proxiesCache.get(object);
735
+ const cachedProxy = proxiesCache.get(object);
736
+ if (cachedProxy !== undefined) {
737
+ return cachedProxy;
733
738
  }
734
739
  let methodCache;
735
740
  const proxy = new Proxy(object, {
@@ -770,7 +775,7 @@ function state(object = {}) {
770
775
  propsMap.set(p, (deps = new DepsSet(propertyGetter)));
771
776
  }
772
777
 
773
- trackStateDependency(deps, deps.b, propValue);
778
+ trackStateDependency(deps, propValue);
774
779
  }
775
780
 
776
781
  const propertyType = typeof propValue;
@@ -841,4 +846,4 @@ function state(object = {}) {
841
846
  return createProxy(object);
842
847
  }
843
848
 
844
- export { EffectOptions, SUPPRESS_EFFECT_GC_WARNING, WARN_ON_UNTRACKED_EFFECT, WARN_ON_WRITE_IN_COMPUTED, activeScope, computed, debugConfig, effect, flushEffects, scope, setActiveScope, setScheduler, signal, state, untracked, unwrapValue };
849
+ export { EffectOptions, SUPPRESS_EFFECT_GC_WARNING, WARN_ON_UNTRACKED_EFFECT, WARN_ON_WRITE_IN_COMPUTED, activeScope, computed, debugConfig, effect, flushEffects, getParentScope, scope, setActiveScope, setScheduler, signal, state, untracked, unwrapValue };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "2.1.0",
3
+ "version": "2.2.1",
4
4
  "name": "@slimlib/store",
5
5
  "description": "Simple Proxy-based store for SPA",
6
6
  "license": "MIT",