@slimlib/store 2.2.0 → 2.2.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.
- package/README.md +6 -0
- package/index.mjs +100 -62
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -101,6 +101,10 @@ store.items.push(4);
|
|
|
101
101
|
console.log(doubled()); // 20
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
+
Consecutive reads of the same signal or state property share a dependency entry when they observe the same value and no notification occurs for that source between reads. This reduces dependency-tracking overhead while preserving changes made between reads. Each computed read records a separate dependency entry; computed results remain cached until dependencies change.
|
|
105
|
+
|
|
106
|
+
Effects and computed values used by effects skip unnecessary re-runs when their computed dependencies remain equal, even if they also read signals or state properties directly. For example, an effect reading a user name and a computed item count will not re-run when the items change but the count stays the same, unless the user name also notifies a change. Skipped effects do not run their cleanup callbacks. Direct signal and state notifications still trigger updates, including changes reverted within a batch and state method calls that leave a property's value unchanged.
|
|
107
|
+
|
|
104
108
|
##### Reactive vs Imperative Usage
|
|
105
109
|
|
|
106
110
|
Computeds support two usage patterns:
|
|
@@ -343,6 +347,8 @@ flushEffects(); // runs = 2 (batched update executed immediately)
|
|
|
343
347
|
|
|
344
348
|
Sets a custom scheduler function for effect execution. By default, effects are scheduled using `queueMicrotask`. You can replace it with any function that accepts a callback.
|
|
345
349
|
|
|
350
|
+
With a synchronous scheduler, all affected dependencies are invalidated before effects run for a source write or a state method call, so effects observe consistent direct and computed values.
|
|
351
|
+
|
|
346
352
|
```js
|
|
347
353
|
import { setScheduler } from "@slimlib/store";
|
|
348
354
|
|
package/index.mjs
CHANGED
|
@@ -69,6 +69,22 @@ const unwrapValue = (value) => (value !== null &&
|
|
|
69
69
|
value;
|
|
70
70
|
|
|
71
71
|
const makeLive = (node) => {
|
|
72
|
+
|
|
73
|
+
if ((node.g & (1 | 2 | 4 )) === 0 && node.f !== globalVersion) {
|
|
74
|
+
const prevTracked = tracked;
|
|
75
|
+
tracked = false;
|
|
76
|
+
try {
|
|
77
|
+
computedRead(node);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
|
|
81
|
+
if (node.f !== globalVersion)
|
|
82
|
+
node.g |= 1 ;
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
tracked = prevTracked;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
72
88
|
node.g |= 64 ;
|
|
73
89
|
const nodeSources = node.h;
|
|
74
90
|
for (let i = 0, len = nodeSources.length; i < len; ++i) {
|
|
@@ -96,8 +112,12 @@ const makeNonLive = (node) => {
|
|
|
96
112
|
};
|
|
97
113
|
|
|
98
114
|
const clearSources = (node, fromIndex = 0) => {
|
|
99
|
-
const isLive = (node.g & (8 | 64 )) !== 0;
|
|
100
115
|
const nodeSources = node.h;
|
|
116
|
+
|
|
117
|
+
if ((node.g & (8 | 64 )) === 0) {
|
|
118
|
+
nodeSources.length = fromIndex;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
101
121
|
for (let i = fromIndex, len = nodeSources.length; i < len; ++i) {
|
|
102
122
|
const { c: c, d: sourceNode } = nodeSources[i];
|
|
103
123
|
|
|
@@ -109,8 +129,7 @@ const clearSources = (node, fromIndex = 0) => {
|
|
|
109
129
|
|
|
110
130
|
c.delete(node);
|
|
111
131
|
|
|
112
|
-
if (
|
|
113
|
-
sourceNode !== undefined &&
|
|
132
|
+
if (sourceNode !== undefined &&
|
|
114
133
|
(sourceNode.g & (8 | 64 )) === 64 &&
|
|
115
134
|
sourceNode.i.size === 0) {
|
|
116
135
|
makeNonLive(sourceNode);
|
|
@@ -134,7 +153,7 @@ const flushEffects = () => {
|
|
|
134
153
|
for (let i = 0, len = nodes.length; i < len; ++i) {
|
|
135
154
|
const node = nodes[i];
|
|
136
155
|
try {
|
|
137
|
-
node.j
|
|
156
|
+
node.j();
|
|
138
157
|
}
|
|
139
158
|
catch (e) {
|
|
140
159
|
console.error(e);
|
|
@@ -143,27 +162,33 @@ const flushEffects = () => {
|
|
|
143
162
|
};
|
|
144
163
|
|
|
145
164
|
const scheduleFlush = () => {
|
|
146
|
-
if (!flushScheduled) {
|
|
165
|
+
if (!flushScheduled && batched.length !== 0) {
|
|
147
166
|
flushScheduled = true;
|
|
148
167
|
scheduler(flushEffects);
|
|
149
168
|
}
|
|
150
169
|
};
|
|
151
170
|
|
|
152
|
-
const trackStateDependency = (deps,
|
|
171
|
+
const trackStateDependency = (deps, cachedValue) => {
|
|
153
172
|
|
|
154
173
|
const sourcesArray = currentComputing.h;
|
|
155
174
|
const skipIndex = currentComputing.k;
|
|
156
175
|
const existing = sourcesArray[skipIndex];
|
|
157
176
|
const noSource = existing === undefined;
|
|
158
177
|
if (noSource || existing.c !== deps) {
|
|
178
|
+
const previous = skipIndex === 0 ? undefined : sourcesArray[skipIndex - 1];
|
|
159
179
|
|
|
160
180
|
if (!noSource) {
|
|
161
181
|
clearSources(currentComputing, skipIndex);
|
|
162
182
|
}
|
|
163
183
|
|
|
164
|
-
|
|
184
|
+
if (previous !== undefined &&
|
|
185
|
+
previous.c === deps &&
|
|
186
|
+
previous.a === deps.a &&
|
|
187
|
+
Object.is(previous.e, cachedValue)) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
165
190
|
|
|
166
|
-
|
|
191
|
+
sourcesArray.push(createSourceEntry(deps, undefined, deps.a, deps.b, cachedValue));
|
|
167
192
|
|
|
168
193
|
if ((currentComputing.g & (8 | 64 )) !== 0) {
|
|
169
194
|
deps.add(currentComputing);
|
|
@@ -173,10 +198,7 @@ const trackStateDependency = (deps, valueGetter, cachedValue) => {
|
|
|
173
198
|
|
|
174
199
|
const entry = sourcesArray[skipIndex];
|
|
175
200
|
entry.a = deps.a;
|
|
176
|
-
entry.b = valueGetter;
|
|
177
201
|
entry.e = cachedValue;
|
|
178
|
-
|
|
179
|
-
currentComputing.g |= 128 ;
|
|
180
202
|
}
|
|
181
203
|
++currentComputing.k;
|
|
182
204
|
};
|
|
@@ -189,7 +211,6 @@ const markNeedsCheck = (node) => {
|
|
|
189
211
|
if ((flags & (4 | 8 | 1 )) === (4 | 8 )) {
|
|
190
212
|
node.g = flags | 1 ;
|
|
191
213
|
batchedAdd(node);
|
|
192
|
-
scheduleFlush();
|
|
193
214
|
}
|
|
194
215
|
return;
|
|
195
216
|
}
|
|
@@ -197,25 +218,27 @@ const markNeedsCheck = (node) => {
|
|
|
197
218
|
node.g = flags | 2 ;
|
|
198
219
|
if ((flags & 8 ) !== 0) {
|
|
199
220
|
batchedAdd(node);
|
|
200
|
-
scheduleFlush();
|
|
201
221
|
}
|
|
202
222
|
for (const dep of node.i) {
|
|
203
223
|
markNeedsCheck(dep);
|
|
204
224
|
}
|
|
205
225
|
};
|
|
206
226
|
|
|
207
|
-
const markDependents = (deps) => {
|
|
227
|
+
const markDependents = (deps, schedule = true) => {
|
|
208
228
|
++globalVersion;
|
|
209
229
|
|
|
210
230
|
++deps.a;
|
|
211
231
|
for (const dep of deps) {
|
|
212
232
|
markNeedsCheck(dep);
|
|
213
233
|
}
|
|
234
|
+
|
|
235
|
+
if (schedule)
|
|
236
|
+
scheduleFlush();
|
|
214
237
|
};
|
|
215
238
|
|
|
216
239
|
const runWithTracking = (node, getter) => {
|
|
217
240
|
|
|
218
|
-
node.g = (node.g & -
|
|
241
|
+
node.g = (node.g & -132) | 4 ;
|
|
219
242
|
node.k = 0;
|
|
220
243
|
const prev = currentComputing;
|
|
221
244
|
const prevTracked = tracked;
|
|
@@ -233,7 +256,7 @@ const runWithTracking = (node, getter) => {
|
|
|
233
256
|
const skipped = node.k;
|
|
234
257
|
const nodeSourcesLength = nodeSources.length;
|
|
235
258
|
|
|
236
|
-
if ((flags &
|
|
259
|
+
if ((flags & 128 ) !== 0) {
|
|
237
260
|
const updateLen = Math.min(skipped, nodeSourcesLength);
|
|
238
261
|
for (let i = 0; i < updateLen; ++i) {
|
|
239
262
|
const entry = nodeSources[i];
|
|
@@ -261,13 +284,21 @@ const untracked = (callback) => {
|
|
|
261
284
|
}
|
|
262
285
|
};
|
|
263
286
|
|
|
264
|
-
const
|
|
287
|
+
const checkSources = (sourcesArray) => {
|
|
288
|
+
const startVersion = globalVersion;
|
|
265
289
|
const prevTracked = tracked;
|
|
266
290
|
tracked = false;
|
|
267
291
|
const len = sourcesArray.length;
|
|
268
292
|
for (let i = 0; i < len; ++i) {
|
|
269
293
|
const sourceEntry = sourcesArray[i];
|
|
270
294
|
const sourceNode = sourceEntry.d;
|
|
295
|
+
if (sourceNode === undefined) {
|
|
296
|
+
if (sourceEntry.a !== sourceEntry.c.a) {
|
|
297
|
+
tracked = prevTracked;
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
271
302
|
|
|
272
303
|
try {
|
|
273
304
|
computedRead(sourceNode);
|
|
@@ -284,7 +315,8 @@ const checkComputedSources = (sourcesArray) => {
|
|
|
284
315
|
}
|
|
285
316
|
}
|
|
286
317
|
tracked = prevTracked;
|
|
287
|
-
|
|
318
|
+
|
|
319
|
+
return startVersion !== globalVersion;
|
|
288
320
|
};
|
|
289
321
|
|
|
290
322
|
const WARN_ON_WRITE_IN_COMPUTED = 1 << 0;
|
|
@@ -360,7 +392,6 @@ function computedRead(self) {
|
|
|
360
392
|
const existing = consumerSources[skipIndex];
|
|
361
393
|
const noSource = existing === undefined;
|
|
362
394
|
if (noSource || existing.c !== deps) {
|
|
363
|
-
|
|
364
395
|
if (!noSource) {
|
|
365
396
|
clearSources(currentComputing, skipIndex);
|
|
366
397
|
}
|
|
@@ -376,8 +407,8 @@ function computedRead(self) {
|
|
|
376
407
|
}
|
|
377
408
|
}
|
|
378
409
|
|
|
379
|
-
currentComputing.g |=
|
|
380
|
-
|
|
410
|
+
currentComputing.g |= 128 ;
|
|
411
|
+
currentComputing.k = skipIndex + 1;
|
|
381
412
|
}
|
|
382
413
|
let flags = self.g;
|
|
383
414
|
const sourcesArray = self.h;
|
|
@@ -403,46 +434,50 @@ function computedRead(self) {
|
|
|
403
434
|
const prevTracked = tracked;
|
|
404
435
|
|
|
405
436
|
setTracked(false);
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
437
|
+
try {
|
|
438
|
+
for (let i = 0, len = sourcesArray.length; i < len; ++i) {
|
|
439
|
+
const source = sourcesArray[i];
|
|
440
|
+
const sourceNode = source.d;
|
|
441
|
+
if (sourceNode === undefined) {
|
|
442
|
+
|
|
443
|
+
const currentDepsVersion = source.c.a;
|
|
444
|
+
if (source.a !== currentDepsVersion) {
|
|
445
|
+
|
|
446
|
+
const storedValue = source.e;
|
|
447
|
+
const storedType = typeof storedValue;
|
|
448
|
+
if (storedValue === null || (storedType !== 'object' && storedType !== 'function')) {
|
|
449
|
+
const currentValue = source.b();
|
|
450
|
+
if (Object.is(currentValue, storedValue)) {
|
|
451
|
+
|
|
452
|
+
source.a = currentDepsVersion;
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
422
455
|
}
|
|
423
|
-
}
|
|
424
456
|
|
|
425
|
-
|
|
426
|
-
|
|
457
|
+
sourceChanged = true;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
427
460
|
}
|
|
428
|
-
|
|
429
|
-
else {
|
|
461
|
+
else {
|
|
430
462
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
463
|
+
try {
|
|
464
|
+
computedRead(sourceNode);
|
|
465
|
+
}
|
|
466
|
+
catch {
|
|
435
467
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
468
|
+
sourceChanged = true;
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
if (source.a !== sourceNode.a) {
|
|
472
|
+
sourceChanged = true;
|
|
473
|
+
break;
|
|
474
|
+
}
|
|
442
475
|
}
|
|
443
476
|
}
|
|
444
477
|
}
|
|
445
|
-
|
|
478
|
+
finally {
|
|
479
|
+
setTracked(prevTracked);
|
|
480
|
+
}
|
|
446
481
|
if (sourceChanged) {
|
|
447
482
|
|
|
448
483
|
self.g = flags |= 1 ;
|
|
@@ -457,8 +492,8 @@ function computedRead(self) {
|
|
|
457
492
|
}
|
|
458
493
|
}
|
|
459
494
|
|
|
460
|
-
if ((flags & (1 | 2 | 128 )) === 2 && hasCached) {
|
|
461
|
-
if (
|
|
495
|
+
if ((flags & (1 | 2 | 128 )) === (2 | 128 ) && hasCached) {
|
|
496
|
+
if (checkSources(sourcesArray)) {
|
|
462
497
|
|
|
463
498
|
flags = (flags & -3 ) | 1 ;
|
|
464
499
|
}
|
|
@@ -559,9 +594,9 @@ const effect = (callback, eager = 0 ) => {
|
|
|
559
594
|
throw new Error(cycleMessage);
|
|
560
595
|
}
|
|
561
596
|
|
|
562
|
-
if ((flags & (1 | 2 | 128 )) === 2 ) {
|
|
597
|
+
if ((flags & (1 | 2 | 128 )) === (2 | 128 )) {
|
|
563
598
|
|
|
564
|
-
if (!
|
|
599
|
+
if (!checkSources(node.h)) {
|
|
565
600
|
node.g = flags & -3 ;
|
|
566
601
|
return;
|
|
567
602
|
}
|
|
@@ -696,7 +731,7 @@ function signal(initialValue) {
|
|
|
696
731
|
|
|
697
732
|
if (tracked && currentComputing !== undefined) {
|
|
698
733
|
|
|
699
|
-
trackStateDependency((deps ??= new DepsSet(read)),
|
|
734
|
+
trackStateDependency((deps ??= new DepsSet(read)), value);
|
|
700
735
|
}
|
|
701
736
|
return value;
|
|
702
737
|
|
|
@@ -730,8 +765,9 @@ function state(object = {}) {
|
|
|
730
765
|
}
|
|
731
766
|
};
|
|
732
767
|
const createProxy = (object) => {
|
|
733
|
-
|
|
734
|
-
|
|
768
|
+
const cachedProxy = proxiesCache.get(object);
|
|
769
|
+
if (cachedProxy !== undefined) {
|
|
770
|
+
return cachedProxy;
|
|
735
771
|
}
|
|
736
772
|
let methodCache;
|
|
737
773
|
const proxy = new Proxy(object, {
|
|
@@ -772,7 +808,7 @@ function state(object = {}) {
|
|
|
772
808
|
propsMap.set(p, (deps = new DepsSet(propertyGetter)));
|
|
773
809
|
}
|
|
774
810
|
|
|
775
|
-
trackStateDependency(deps,
|
|
811
|
+
trackStateDependency(deps, propValue);
|
|
776
812
|
}
|
|
777
813
|
|
|
778
814
|
const propertyType = typeof propValue;
|
|
@@ -802,8 +838,10 @@ function state(object = {}) {
|
|
|
802
838
|
return result;
|
|
803
839
|
for (const deps of propsMap.values()) {
|
|
804
840
|
|
|
805
|
-
markDependents(deps);
|
|
841
|
+
markDependents(deps, false);
|
|
806
842
|
}
|
|
843
|
+
|
|
844
|
+
scheduleFlush();
|
|
807
845
|
}
|
|
808
846
|
return result;
|
|
809
847
|
};
|