@slimlib/store 2.2.1 → 2.2.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.
- package/README.md +8 -0
- package/index.mjs +95 -57
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,6 +17,8 @@ It also works really well with imperative code because the state primitive is Pr
|
|
|
17
17
|
|
|
18
18
|
[Changelog](./CHANGELOG.md)
|
|
19
19
|
|
|
20
|
+
[Known effect GC and lifetime limitation](./TECH-DEBT.md#effect-garbage-collection-and-the-documented-lifetime-contract)
|
|
21
|
+
|
|
20
22
|
## Installation
|
|
21
23
|
|
|
22
24
|
```bash
|
|
@@ -101,6 +103,10 @@ store.items.push(4);
|
|
|
101
103
|
console.log(doubled()); // 20
|
|
102
104
|
```
|
|
103
105
|
|
|
106
|
+
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.
|
|
107
|
+
|
|
108
|
+
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.
|
|
109
|
+
|
|
104
110
|
##### Reactive vs Imperative Usage
|
|
105
111
|
|
|
106
112
|
Computeds support two usage patterns:
|
|
@@ -343,6 +349,8 @@ flushEffects(); // runs = 2 (batched update executed immediately)
|
|
|
343
349
|
|
|
344
350
|
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
351
|
|
|
352
|
+
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.
|
|
353
|
+
|
|
346
354
|
```js
|
|
347
355
|
import { setScheduler } from "@slimlib/store";
|
|
348
356
|
|
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) {
|
|
@@ -146,7 +162,7 @@ const flushEffects = () => {
|
|
|
146
162
|
};
|
|
147
163
|
|
|
148
164
|
const scheduleFlush = () => {
|
|
149
|
-
if (!flushScheduled) {
|
|
165
|
+
if (!flushScheduled && batched.length !== 0) {
|
|
150
166
|
flushScheduled = true;
|
|
151
167
|
scheduler(flushEffects);
|
|
152
168
|
}
|
|
@@ -159,14 +175,20 @@ const trackStateDependency = (deps, cachedValue) => {
|
|
|
159
175
|
const existing = sourcesArray[skipIndex];
|
|
160
176
|
const noSource = existing === undefined;
|
|
161
177
|
if (noSource || existing.c !== deps) {
|
|
178
|
+
const previous = skipIndex === 0 ? undefined : sourcesArray[skipIndex - 1];
|
|
162
179
|
|
|
163
180
|
if (!noSource) {
|
|
164
181
|
clearSources(currentComputing, skipIndex);
|
|
165
182
|
}
|
|
166
183
|
|
|
167
|
-
|
|
184
|
+
if (previous !== undefined &&
|
|
185
|
+
previous.c === deps &&
|
|
186
|
+
previous.a === deps.a &&
|
|
187
|
+
Object.is(previous.e, cachedValue)) {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
168
190
|
|
|
169
|
-
|
|
191
|
+
sourcesArray.push(createSourceEntry(deps, undefined, deps.a, deps.b, cachedValue));
|
|
170
192
|
|
|
171
193
|
if ((currentComputing.g & (8 | 64 )) !== 0) {
|
|
172
194
|
deps.add(currentComputing);
|
|
@@ -177,8 +199,6 @@ const trackStateDependency = (deps, cachedValue) => {
|
|
|
177
199
|
const entry = sourcesArray[skipIndex];
|
|
178
200
|
entry.a = deps.a;
|
|
179
201
|
entry.e = cachedValue;
|
|
180
|
-
|
|
181
|
-
currentComputing.g |= 128 ;
|
|
182
202
|
}
|
|
183
203
|
++currentComputing.k;
|
|
184
204
|
};
|
|
@@ -191,7 +211,6 @@ const markNeedsCheck = (node) => {
|
|
|
191
211
|
if ((flags & (4 | 8 | 1 )) === (4 | 8 )) {
|
|
192
212
|
node.g = flags | 1 ;
|
|
193
213
|
batchedAdd(node);
|
|
194
|
-
scheduleFlush();
|
|
195
214
|
}
|
|
196
215
|
return;
|
|
197
216
|
}
|
|
@@ -199,25 +218,27 @@ const markNeedsCheck = (node) => {
|
|
|
199
218
|
node.g = flags | 2 ;
|
|
200
219
|
if ((flags & 8 ) !== 0) {
|
|
201
220
|
batchedAdd(node);
|
|
202
|
-
scheduleFlush();
|
|
203
221
|
}
|
|
204
222
|
for (const dep of node.i) {
|
|
205
223
|
markNeedsCheck(dep);
|
|
206
224
|
}
|
|
207
225
|
};
|
|
208
226
|
|
|
209
|
-
const markDependents = (deps) => {
|
|
227
|
+
const markDependents = (deps, schedule = true) => {
|
|
210
228
|
++globalVersion;
|
|
211
229
|
|
|
212
230
|
++deps.a;
|
|
213
231
|
for (const dep of deps) {
|
|
214
232
|
markNeedsCheck(dep);
|
|
215
233
|
}
|
|
234
|
+
|
|
235
|
+
if (schedule)
|
|
236
|
+
scheduleFlush();
|
|
216
237
|
};
|
|
217
238
|
|
|
218
239
|
const runWithTracking = (node, getter) => {
|
|
219
240
|
|
|
220
|
-
node.g = (node.g & -
|
|
241
|
+
node.g = (node.g & -132) | 4 ;
|
|
221
242
|
node.k = 0;
|
|
222
243
|
const prev = currentComputing;
|
|
223
244
|
const prevTracked = tracked;
|
|
@@ -235,7 +256,7 @@ const runWithTracking = (node, getter) => {
|
|
|
235
256
|
const skipped = node.k;
|
|
236
257
|
const nodeSourcesLength = nodeSources.length;
|
|
237
258
|
|
|
238
|
-
if ((flags &
|
|
259
|
+
if ((flags & 128 ) !== 0) {
|
|
239
260
|
const updateLen = Math.min(skipped, nodeSourcesLength);
|
|
240
261
|
for (let i = 0; i < updateLen; ++i) {
|
|
241
262
|
const entry = nodeSources[i];
|
|
@@ -263,13 +284,21 @@ const untracked = (callback) => {
|
|
|
263
284
|
}
|
|
264
285
|
};
|
|
265
286
|
|
|
266
|
-
const
|
|
287
|
+
const checkSources = (sourcesArray) => {
|
|
288
|
+
const startVersion = globalVersion;
|
|
267
289
|
const prevTracked = tracked;
|
|
268
290
|
tracked = false;
|
|
269
291
|
const len = sourcesArray.length;
|
|
270
292
|
for (let i = 0; i < len; ++i) {
|
|
271
293
|
const sourceEntry = sourcesArray[i];
|
|
272
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
|
+
}
|
|
273
302
|
|
|
274
303
|
try {
|
|
275
304
|
computedRead(sourceNode);
|
|
@@ -286,7 +315,8 @@ const checkComputedSources = (sourcesArray) => {
|
|
|
286
315
|
}
|
|
287
316
|
}
|
|
288
317
|
tracked = prevTracked;
|
|
289
|
-
|
|
318
|
+
|
|
319
|
+
return startVersion !== globalVersion;
|
|
290
320
|
};
|
|
291
321
|
|
|
292
322
|
const WARN_ON_WRITE_IN_COMPUTED = 1 << 0;
|
|
@@ -362,7 +392,6 @@ function computedRead(self) {
|
|
|
362
392
|
const existing = consumerSources[skipIndex];
|
|
363
393
|
const noSource = existing === undefined;
|
|
364
394
|
if (noSource || existing.c !== deps) {
|
|
365
|
-
|
|
366
395
|
if (!noSource) {
|
|
367
396
|
clearSources(currentComputing, skipIndex);
|
|
368
397
|
}
|
|
@@ -378,8 +407,8 @@ function computedRead(self) {
|
|
|
378
407
|
}
|
|
379
408
|
}
|
|
380
409
|
|
|
381
|
-
currentComputing.g |=
|
|
382
|
-
|
|
410
|
+
currentComputing.g |= 128 ;
|
|
411
|
+
currentComputing.k = skipIndex + 1;
|
|
383
412
|
}
|
|
384
413
|
let flags = self.g;
|
|
385
414
|
const sourcesArray = self.h;
|
|
@@ -405,46 +434,50 @@ function computedRead(self) {
|
|
|
405
434
|
const prevTracked = tracked;
|
|
406
435
|
|
|
407
436
|
setTracked(false);
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
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
|
+
}
|
|
424
455
|
}
|
|
425
|
-
}
|
|
426
456
|
|
|
427
|
-
|
|
428
|
-
|
|
457
|
+
sourceChanged = true;
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
429
460
|
}
|
|
430
|
-
|
|
431
|
-
else {
|
|
461
|
+
else {
|
|
432
462
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
463
|
+
try {
|
|
464
|
+
computedRead(sourceNode);
|
|
465
|
+
}
|
|
466
|
+
catch {
|
|
437
467
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
468
|
+
sourceChanged = true;
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
if (source.a !== sourceNode.a) {
|
|
472
|
+
sourceChanged = true;
|
|
473
|
+
break;
|
|
474
|
+
}
|
|
444
475
|
}
|
|
445
476
|
}
|
|
446
477
|
}
|
|
447
|
-
|
|
478
|
+
finally {
|
|
479
|
+
setTracked(prevTracked);
|
|
480
|
+
}
|
|
448
481
|
if (sourceChanged) {
|
|
449
482
|
|
|
450
483
|
self.g = flags |= 1 ;
|
|
@@ -459,8 +492,8 @@ function computedRead(self) {
|
|
|
459
492
|
}
|
|
460
493
|
}
|
|
461
494
|
|
|
462
|
-
if ((flags & (1 | 2 | 128 )) === 2 && hasCached) {
|
|
463
|
-
if (
|
|
495
|
+
if ((flags & (1 | 2 | 128 )) === (2 | 128 ) && hasCached) {
|
|
496
|
+
if (checkSources(sourcesArray)) {
|
|
464
497
|
|
|
465
498
|
flags = (flags & -3 ) | 1 ;
|
|
466
499
|
}
|
|
@@ -561,9 +594,9 @@ const effect = (callback, eager = 0 ) => {
|
|
|
561
594
|
throw new Error(cycleMessage);
|
|
562
595
|
}
|
|
563
596
|
|
|
564
|
-
if ((flags & (1 | 2 | 128 )) === 2 ) {
|
|
597
|
+
if ((flags & (1 | 2 | 128 )) === (2 | 128 )) {
|
|
565
598
|
|
|
566
|
-
if (!
|
|
599
|
+
if (!checkSources(node.h)) {
|
|
567
600
|
node.g = flags & -3 ;
|
|
568
601
|
return;
|
|
569
602
|
}
|
|
@@ -600,14 +633,17 @@ const effect = (callback, eager = 0 ) => {
|
|
|
600
633
|
const effectId = node.f;
|
|
601
634
|
const dispose = () => {
|
|
602
635
|
|
|
603
|
-
disposed
|
|
636
|
+
if (!disposed) {
|
|
637
|
+
|
|
638
|
+
disposed = true;
|
|
604
639
|
|
|
605
|
-
|
|
640
|
+
unregisterEffect(gcToken);
|
|
606
641
|
|
|
607
|
-
|
|
608
|
-
|
|
642
|
+
if (typeof node.l === 'function') {
|
|
643
|
+
node.l();
|
|
644
|
+
}
|
|
645
|
+
clearSources(node);
|
|
609
646
|
}
|
|
610
|
-
clearSources(node);
|
|
611
647
|
};
|
|
612
648
|
|
|
613
649
|
if (effectScope) {
|
|
@@ -805,8 +841,10 @@ function state(object = {}) {
|
|
|
805
841
|
return result;
|
|
806
842
|
for (const deps of propsMap.values()) {
|
|
807
843
|
|
|
808
|
-
markDependents(deps);
|
|
844
|
+
markDependents(deps, false);
|
|
809
845
|
}
|
|
846
|
+
|
|
847
|
+
scheduleFlush();
|
|
810
848
|
}
|
|
811
849
|
return result;
|
|
812
850
|
};
|