@esportsplus/reactivity 0.41.0 → 0.42.0

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/src/system.ts CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  PACKAGE_NAME,
3
3
  SIGNAL,
4
4
  STABILIZER_DEFERRED, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED,
5
- STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_RECOMPUTING
5
+ STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_PAUSED, STATE_RECOMPUTING, STATE_STALE
6
6
  } from './constants';
7
7
  import { Computed, ComputedResult, Link, SelectorSignal, Settled, Signal } from './types';
8
8
  import { isObject, isPromise } from '@esportsplus/utilities';
@@ -424,8 +424,16 @@ function propagate<T>(computed: Computed<T>) {
424
424
  }
425
425
 
426
426
  function recompute<T>(computed: Computed<T>) {
427
- if (computed.state & STATE_IN_HEAP) {
428
- deleteFromHeap(computed);
427
+ // One test on the common path: a node neither queued nor paused goes straight to its run
428
+ if (computed.state & (STATE_IN_HEAP | STATE_PAUSED)) {
429
+ if (computed.state & STATE_IN_HEAP) {
430
+ deleteFromHeap(computed);
431
+ }
432
+
433
+ if (computed.state & STATE_PAUSED) {
434
+ computed.state |= STATE_STALE;
435
+ return;
436
+ }
429
437
  }
430
438
 
431
439
  if (computed.cleanup !== null || computed.owned !== null) {
@@ -468,7 +476,7 @@ function recompute<T>(computed: Computed<T>) {
468
476
  version++;
469
477
  observer = o;
470
478
  // fn may have re-queued this node (a tracked read drains pending writes), so heap membership survives
471
- computed.state = STATE_COMPUTED | flags | (computed.state & STATE_IN_HEAP);
479
+ computed.state = STATE_COMPUTED | flags | (computed.state & (STATE_IN_HEAP | STATE_PAUSED));
472
480
  // Entry snapshot, not current writes: a node whose fn wrote mid-run must stay gv < writes and validate normally
473
481
  computed.gv = w;
474
482
 
@@ -476,16 +484,23 @@ function recompute<T>(computed: Computed<T>) {
476
484
  remove = depsTail ? depsTail.nextDep : computed.deps;
477
485
 
478
486
  if (remove) {
479
- do {
480
- remove = unlink(remove);
481
- }
482
- while (remove);
483
-
484
- if (depsTail) {
485
- depsTail.nextDep = null;
487
+ // A run that paused its own effect read only part of what it depends on: the rest stays linked and the run
488
+ // counts as skipped, so resume() replays it
489
+ if (computed.state & STATE_PAUSED) {
490
+ computed.state |= STATE_STALE;
486
491
  }
487
492
  else {
488
- computed.deps = null;
493
+ do {
494
+ remove = unlink(remove);
495
+ }
496
+ while (remove);
497
+
498
+ if (depsTail) {
499
+ depsTail.nextDep = null;
500
+ }
501
+ else {
502
+ computed.deps = null;
503
+ }
489
504
  }
490
505
  }
491
506
 
@@ -785,7 +800,7 @@ function makeAsyncComputed<T>(factory: Computed<Promise<T> | AsyncIterable<T> |
785
800
  wrapper.pending = pending;
786
801
 
787
802
  asyncFactory.set(wrapper as Computed<unknown>, factory as Computed<unknown>);
788
- wrapper.disposal = stop;
803
+ wrapper.disposal = () => dispose(stop);
789
804
 
790
805
  return wrapper;
791
806
  }
@@ -1023,9 +1038,7 @@ const effect = <T>(fn: Computed<T>['fn'], apply?: (value: T, prev: T | undefined
1023
1038
  });
1024
1039
  }
1025
1040
 
1026
- return () => {
1027
- dispose(c);
1028
- };
1041
+ return c;
1029
1042
  };
1030
1043
 
1031
1044
  // A no-op while a pass is running: re-entering stabilize() would corrupt heap_i.
@@ -1035,10 +1048,6 @@ const flush = (): void => {
1035
1048
  }
1036
1049
  };
1037
1050
 
1038
- // Whether an onCleanup() made now would land on an owner (a running computed/effect, also inside
1039
- // untrack, or a root's scope)
1040
- const hasOwner = (): boolean => observer !== null || scope !== null;
1041
-
1042
1051
  const isComputed = (value: unknown): value is Computed<unknown> => {
1043
1052
  return isObject(value) && !!((value as unknown as Computed<unknown>).state & STATE_COMPUTED);
1044
1053
  };
@@ -1123,6 +1132,41 @@ const read = <T>(node: Signal<T> | Computed<T>): T => {
1123
1132
  // .owner chain finds every enclosing owner.
1124
1133
  const owner = (): Computed<unknown> | null => observer || scope;
1125
1134
 
1135
+ // Effects only: a computed must stay derivable on read, which a skipped run would leave stale
1136
+ const pause = <T>(node: Computed<T>): void => {
1137
+ let state = node.state;
1138
+
1139
+ if (state & STATE_DISPOSED) {
1140
+ return;
1141
+ }
1142
+
1143
+ if (!(state & STATE_EFFECT)) {
1144
+ throw new Error(`${PACKAGE_NAME}: pause() expects an effect`);
1145
+ }
1146
+
1147
+ node.state = state | STATE_PAUSED;
1148
+ };
1149
+
1150
+ // A run skipped while paused is replayed once through the heap, so it lands in a normal stabilize pass
1151
+ const resume = <T>(node: Computed<T>): void => {
1152
+ let state = node.state;
1153
+
1154
+ if (state & STATE_DISPOSED) {
1155
+ return;
1156
+ }
1157
+
1158
+ if (!(state & STATE_EFFECT)) {
1159
+ throw new Error(`${PACKAGE_NAME}: resume() expects an effect`);
1160
+ }
1161
+
1162
+ node.state = state & ~(STATE_PAUSED | STATE_STALE);
1163
+
1164
+ if (state & STATE_STALE) {
1165
+ insertIntoHeap(node);
1166
+ schedule();
1167
+ }
1168
+ };
1169
+
1126
1170
  // Owned before fn runs (nothing can register on the owner meanwhile, so ownerSlot is unchanged): a
1127
1171
  // root disposed inside its own fn then unlinks instead of being parked on the owner as a dead child.
1128
1172
  //
@@ -1282,11 +1326,10 @@ export {
1282
1326
  dispose,
1283
1327
  effect,
1284
1328
  flush,
1285
- hasOwner,
1286
1329
  isComputed, isSignal,
1287
1330
  onCleanup, owner,
1288
- peek,
1289
- read, root,
1331
+ pause, peek,
1332
+ read, resume, root,
1290
1333
  signal,
1291
1334
  untrack,
1292
1335
  write
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, read, root, signal, write } from '~/system';
2
+ import { computed, dispose, effect, read, root, signal, write } from '~/system';
3
3
  import { tick, waitFor } from './lib/wait-for';
4
4
  import type { Computed } from '~/system';
5
5
 
@@ -61,7 +61,7 @@ describe('asyncComputed outside root', () => {
61
61
  expect(read(node)).toBe(1);
62
62
  expect(calls).toBe(1);
63
63
 
64
- stop();
64
+ dispose(stop);
65
65
 
66
66
  write(s, 2);
67
67
  await tick();
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, read, root, signal, write } from '~/system';
2
+ import { computed, dispose, effect, read, root, signal, write } from '~/system';
3
3
  import { tick } from './lib/wait-for';
4
4
  import type { Computed } from '~/system';
5
5
 
@@ -128,7 +128,7 @@ describe('asyncComputed AsyncIterable support', () => {
128
128
  expect(read(node)).toBe(1);
129
129
 
130
130
  // Mid-iteration: dispose while the generator is suspended awaiting its second gate.
131
- keeper();
131
+ dispose(keeper);
132
132
  stopRoot();
133
133
 
134
134
  allGates[0][1].resolve();
@@ -248,7 +248,7 @@ describe('asyncComputed AsyncIterable support', () => {
248
248
 
249
249
  await tick();
250
250
 
251
- keeper();
251
+ dispose(keeper);
252
252
  stopRoot();
253
253
 
254
254
  await tick();
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, flush, peek, read, signal, write } from '~/system';
2
+ import { computed, dispose, effect, flush, peek, read, signal, write } from '~/system';
3
3
 
4
4
 
5
5
  // Depth chosen well past the V8 call-stack frame limit (~10-15k): a recursive notify/update/dispose
@@ -44,7 +44,7 @@ describe('deep graphs', () => {
44
44
 
45
45
  expect(seen).toBe(DEPTH + 2);
46
46
 
47
- stop();
47
+ dispose(stop);
48
48
  }, 30000);
49
49
 
50
50
  it('a wide fan-out with deep branches settles (notify sibling traversal)', () => {
@@ -90,7 +90,7 @@ describe('deep graphs', () => {
90
90
 
91
91
  expect(observed).toBe(width * (2 + branchDepth));
92
92
 
93
- stop();
93
+ dispose(stop);
94
94
  });
95
95
 
96
96
  it('a diamond recomputes the sink once per source change (glitch-free, no redundant checks)', () => {
@@ -134,7 +134,7 @@ describe('deep graphs', () => {
134
134
  expect(cRuns).toBe(2);
135
135
  expect(peek(c)).toBe(7);
136
136
 
137
- stop();
137
+ dispose(stop);
138
138
  });
139
139
 
140
140
  it('a dynamic conditional dep does not over-recompute (DIRTY-break)', () => {
@@ -174,7 +174,7 @@ describe('deep graphs', () => {
174
174
 
175
175
  expect(cRuns).toBe(2);
176
176
 
177
- stop();
177
+ dispose(stop);
178
178
  });
179
179
 
180
180
  it('teardown of a 200k-deep chain completes without overflow; cleanup fires once per node', () => {
@@ -202,7 +202,7 @@ describe('deep graphs', () => {
202
202
  read(chain[DEPTH - 1]);
203
203
  });
204
204
 
205
- stop();
205
+ dispose(stop);
206
206
 
207
207
  expect(cleanups).toBe(DEPTH);
208
208
 
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, onCleanup, read, root, signal, write } from '~/system';
2
+ import { computed, dispose, effect, onCleanup, read, root, signal, write } from '~/system';
3
3
  import { ReactiveArray } from '~/reactive/array';
4
4
  import { ReactiveObject } from '~/reactive/object';
5
5
  import { waitFor } from './lib/wait-for';
@@ -14,7 +14,7 @@ describe('effect patterns', () => {
14
14
  let s = signal(0),
15
15
  runs = 0;
16
16
 
17
- let dispose = effect(() => {
17
+ let node = effect(() => {
18
18
  read(s);
19
19
  runs++;
20
20
  });
@@ -32,7 +32,7 @@ describe('effect patterns', () => {
32
32
 
33
33
  expect(runs).toBe(4);
34
34
 
35
- dispose();
35
+ dispose(node);
36
36
 
37
37
  write(s, 4);
38
38
  write(s, 5);
@@ -49,7 +49,7 @@ describe('effect patterns', () => {
49
49
  cleanupCalls = 0,
50
50
  effectValues: number[] = [];
51
51
 
52
- let dispose = effect(() => {
52
+ let node = effect(() => {
53
53
  let current = read(s);
54
54
 
55
55
  effectValues.push(current);
@@ -74,7 +74,7 @@ describe('effect patterns', () => {
74
74
  expect(effectValues).toEqual([0, 1, 2]);
75
75
  expect(cleanupCalls).toBe(2);
76
76
 
77
- dispose();
77
+ dispose(node);
78
78
 
79
79
  expect(cleanupCalls).toBe(3);
80
80
  });
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, read, signal, write } from '~/system';
2
+ import { computed, dispose, effect, read, signal, write } from '~/system';
3
3
  import { tick } from './lib/wait-for';
4
4
 
5
5
 
@@ -146,7 +146,7 @@ describe('effect error contract', () => {
146
146
  expect(captured.length).toBe(1);
147
147
  expect((captured[0] as Error).message).toBe('effect boom');
148
148
 
149
- stop();
149
+ dispose(stop);
150
150
  });
151
151
 
152
152
  it('effect rethrows on every failure, not just the first', async () => {
@@ -171,7 +171,7 @@ describe('effect error contract', () => {
171
171
  expect((captured[0] as Error).message).toBe('boom 1');
172
172
  expect((captured[1] as Error).message).toBe('boom 3');
173
173
 
174
- stop();
174
+ dispose(stop);
175
175
  });
176
176
 
177
177
  it('effect catching its own error internally rethrows nothing', async () => {
@@ -196,6 +196,6 @@ describe('effect error contract', () => {
196
196
  expect(errors.length).toBe(1);
197
197
  expect((errors[0] as Error).message).toBe('handled');
198
198
 
199
- stop();
199
+ dispose(stop);
200
200
  });
201
201
  });
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, flush, peek, read, signal, write } from '~/system';
2
+ import { computed, dispose, effect, flush, peek, read, signal, write } from '~/system';
3
3
  import { waitFor } from './lib/wait-for';
4
4
 
5
5
 
@@ -85,7 +85,7 @@ describe('invalidate()', () => {
85
85
  expect(fetches).toBe(2);
86
86
  expect(read(node)).toBe(2);
87
87
 
88
- stop();
88
+ dispose(stop);
89
89
  });
90
90
 
91
91
  it('forces a side-effecting computed (the public effect encoding) to re-run without propagating', () => {
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, owner as current, dispose, effect, flush, hasOwner, onCleanup, read, root, signal, untrack, write } from '~/system';
2
+ import { computed, owner as current, dispose, effect, flush, onCleanup, read, root, signal, untrack, write } from '~/system';
3
3
  import type { Computed } from '~/system';
4
4
  import { captureUncaught } from './lib/uncaught';
5
5
 
@@ -74,7 +74,7 @@ describe('ownership unlink', () => {
74
74
 
75
75
  it('child effect disposed early unlinks from its owner', () => {
76
76
  let runs = 0,
77
- stops: VoidFunction[] = [],
77
+ stops: Computed<unknown>[] = [],
78
78
  parent = owner(() => {
79
79
  for (let i = 0; i < 3; i++) {
80
80
  stops.push(effect(() => {
@@ -85,12 +85,12 @@ describe('ownership unlink', () => {
85
85
 
86
86
  expect(registrations(parent)).toBe(3);
87
87
 
88
- stops[0]();
88
+ dispose(stops[0]);
89
89
  expect(registrations(parent)).toBe(2);
90
90
  expect(parent.owned!.owner).toBe(parent);
91
91
 
92
- stops[2]();
93
- stops[1]();
92
+ dispose(stops[2]);
93
+ dispose(stops[1]);
94
94
  expect(registrations(parent)).toBe(0);
95
95
 
96
96
  dispose(parent);
@@ -155,9 +155,9 @@ describe('ownership churn', () => {
155
155
  it('10k child effects created + disposed leave no registrations behind', () => {
156
156
  let parent = owner(() => {
157
157
  for (let i = 0; i < 10000; i++) {
158
- effect(() => {
158
+ dispose(effect(() => {
159
159
  onCleanup(() => {});
160
- })();
160
+ }));
161
161
  }
162
162
  });
163
163
 
@@ -266,7 +266,7 @@ describe('ownership ordering', () => {
266
266
  it('an early-disposed child leaves the remaining order intact', () => {
267
267
  let log: string[] = [],
268
268
  s = signal(0),
269
- stop: VoidFunction | null = null;
269
+ stop: Computed<unknown> | null = null;
270
270
 
271
271
  effect(() => {
272
272
  if (read(s) === 0) {
@@ -285,7 +285,7 @@ describe('ownership ordering', () => {
285
285
  });
286
286
 
287
287
  flush();
288
- stop!();
288
+ dispose(stop!);
289
289
  expect(log).toEqual(['y']);
290
290
 
291
291
  write(s, 1);
@@ -322,7 +322,7 @@ describe('ownership ordering', () => {
322
322
  describe('ownership double dispose', () => {
323
323
  it('disposing a child twice is a no-op and leaves siblings registered', () => {
324
324
  let runs = 0,
325
- stop: VoidFunction | null = null,
325
+ stop: Computed<unknown> | null = null,
326
326
  parent = owner(() => {
327
327
  effect(() => {});
328
328
  stop = effect(() => {
@@ -331,8 +331,8 @@ describe('ownership double dispose', () => {
331
331
  effect(() => {});
332
332
  });
333
333
 
334
- stop!();
335
- stop!();
334
+ dispose(stop!);
335
+ dispose(stop!);
336
336
 
337
337
  expect(runs).toBe(1);
338
338
  expect(registrations(parent)).toBe(2);
@@ -364,7 +364,7 @@ describe('ownership double dispose', () => {
364
364
  describe('ownership dispose mid-drain', () => {
365
365
  it('a child cleanup disposing its parent and a sibling runs every cleanup once', () => {
366
366
  let counts = { a: 0, b: 0, c: 0, parent: 0 },
367
- stopB: VoidFunction | null = null,
367
+ stopB: Computed<unknown> | null = null,
368
368
  stopParent: VoidFunction | null = null;
369
369
 
370
370
  root((d) => {
@@ -373,7 +373,7 @@ describe('ownership dispose mid-drain', () => {
373
373
  effect(() => {
374
374
  onCleanup(() => {
375
375
  counts.a++;
376
- stopB!();
376
+ dispose(stopB!);
377
377
  stopParent!();
378
378
  });
379
379
  });
@@ -393,7 +393,7 @@ describe('ownership dispose mid-drain', () => {
393
393
  it('a child disposing its parent from its own cleanup unlinks first', () => {
394
394
  let counts = { child: 0, parent: 0, sibling: 0 },
395
395
  parent: Computed<unknown> | null = null,
396
- stop: VoidFunction | null = null;
396
+ stop: Computed<unknown> | null = null;
397
397
 
398
398
  parent = owner(() => {
399
399
  onCleanup(() => { counts.parent++; });
@@ -408,7 +408,7 @@ describe('ownership dispose mid-drain', () => {
408
408
  });
409
409
  });
410
410
 
411
- stop!();
411
+ dispose(stop!);
412
412
 
413
413
  expect(counts).toEqual({ child: 1, parent: 1, sibling: 1 });
414
414
  expect(registrations(parent)).toBe(0);
@@ -417,14 +417,14 @@ describe('ownership dispose mid-drain', () => {
417
417
  it('a re-run where one child disposes a later sibling skips it', () => {
418
418
  let log: string[] = [],
419
419
  s = signal(0),
420
- stopY: VoidFunction | null = null;
420
+ stopY: Computed<unknown> | null = null;
421
421
 
422
422
  effect(() => {
423
423
  if (read(s) === 0) {
424
424
  effect(() => {
425
425
  onCleanup(() => {
426
426
  log.push('x');
427
- stopY!();
427
+ dispose(stopY!);
428
428
  });
429
429
  });
430
430
  stopY = effect(() => {
@@ -550,20 +550,20 @@ describe('ownership throwing cleanup', () => {
550
550
  });
551
551
 
552
552
 
553
- describe('hasOwner', () => {
553
+ describe('owner() as an ownership probe', () => {
554
554
  it('is true inside a tracking root or a run (also untracked), false outside and in a detached root', () => {
555
555
  let seen: boolean[] = [],
556
556
  stop = effect(() => {
557
- seen.push(hasOwner());
558
- seen.push(untrack(() => hasOwner()));
559
- seen.push(untrack(() => root(() => hasOwner())));
557
+ seen.push(current() !== null);
558
+ seen.push(untrack(() => current() !== null));
559
+ seen.push(untrack(() => root(() => current() !== null)));
560
560
  });
561
561
 
562
- seen.push(hasOwner());
563
- seen.push(untrack(() => hasOwner()));
564
- root((d) => { seen.push(hasOwner()); d(); });
565
- root(() => { seen.push(hasOwner()); });
566
- stop();
562
+ seen.push(current() !== null);
563
+ seen.push(untrack(() => current() !== null));
564
+ root((d) => { seen.push(current() !== null); d(); });
565
+ root(() => { seen.push(current() !== null); });
566
+ dispose(stop);
567
567
 
568
568
  expect(seen).toEqual([true, true, false, false, false, true, false]);
569
569
  });
@@ -573,12 +573,12 @@ describe('hasOwner', () => {
573
573
  seen: boolean[] = [],
574
574
  stop = effect(() => {
575
575
  read(s);
576
- seen.push(untrack(() => hasOwner()));
576
+ seen.push(untrack(() => current() !== null));
577
577
  });
578
578
 
579
579
  write(s, 1);
580
580
  await Promise.resolve();
581
- stop();
581
+ dispose(stop);
582
582
 
583
583
  expect(seen).toEqual([true, true]);
584
584
  });
@@ -635,7 +635,7 @@ describe('root with an owner', () => {
635
635
  effect(() => { read(s); });
636
636
  }, node);
637
637
 
638
- expect(hasOwner()).toBe(false);
638
+ expect(current() !== null).toBe(false);
639
639
  expect(registrations(node!)).toBe(2);
640
640
 
641
641
  write(s, 1);