@slimlib/store 2.2.1 → 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.
Files changed (3) hide show
  1. package/README.md +6 -0
  2. package/index.mjs +87 -52
  3. 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) {
@@ -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
- sourcesArray.push(createSourceEntry(deps, undefined, deps.a, deps.b, cachedValue));
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
- currentComputing.g |= 128 ;
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 & -388) | 4 ;
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 & 256 ) !== 0) {
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 checkComputedSources = (sourcesArray) => {
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
- return false;
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 |= 256 ;
382
- ++currentComputing.k;
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
- for (let i = 0, len = sourcesArray.length; i < len; ++i) {
409
- const source = sourcesArray[i];
410
- const sourceNode = source.d;
411
- if (sourceNode === undefined) {
412
-
413
- const currentDepsVersion = source.c.a;
414
- if (source.a !== currentDepsVersion) {
415
-
416
- const storedValue = source.e;
417
- const storedType = typeof storedValue;
418
- if (storedValue === null || (storedType !== 'object' && storedType !== 'function')) {
419
- const currentValue = source.b();
420
- if (Object.is(currentValue, storedValue)) {
421
-
422
- source.a = currentDepsVersion;
423
- continue;
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
- sourceChanged = true;
428
- break;
457
+ sourceChanged = true;
458
+ break;
459
+ }
429
460
  }
430
- }
431
- else {
461
+ else {
432
462
 
433
- try {
434
- computedRead(sourceNode);
435
- }
436
- catch {
463
+ try {
464
+ computedRead(sourceNode);
465
+ }
466
+ catch {
437
467
 
438
- sourceChanged = true;
439
- break;
440
- }
441
- if (source.a !== sourceNode.a) {
442
- sourceChanged = true;
443
- break;
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
- setTracked(prevTracked);
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 (checkComputedSources(sourcesArray)) {
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 (!checkComputedSources(node.h)) {
599
+ if (!checkSources(node.h)) {
567
600
  node.g = flags & -3 ;
568
601
  return;
569
602
  }
@@ -805,8 +838,10 @@ function state(object = {}) {
805
838
  return result;
806
839
  for (const deps of propsMap.values()) {
807
840
 
808
- markDependents(deps);
841
+ markDependents(deps, false);
809
842
  }
843
+
844
+ scheduleFlush();
810
845
  }
811
846
  return result;
812
847
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "type": "module",
3
- "version": "2.2.1",
3
+ "version": "2.2.2",
4
4
  "name": "@slimlib/store",
5
5
  "description": "Simple Proxy-based store for SPA",
6
6
  "license": "MIT",