@esportsplus/reactivity 0.40.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/README.md +16 -10
- package/bench/compiler.bench.ts +122 -0
- package/bench/kairo.bench.ts +1 -1
- package/bench/lib/reactive-adapter.ts +1 -1
- package/bench/ownership.bench.ts +9 -5
- package/bench/reactive/array.bench.ts +7 -2
- package/bench/reactive/reactive-object.bench.ts +3 -2
- package/bench/system.bench.ts +9 -5
- package/build/compiler/array.js +28 -25
- package/build/compiler/bindings.d.ts +3 -1
- package/build/compiler/bindings.js +36 -18
- package/build/compiler/index.d.ts +3 -2
- package/build/compiler/index.js +21 -13
- package/build/compiler/object.js +30 -21
- package/build/compiler/plugins/vite.d.ts +29 -6
- package/build/compiler/primitives.js +9 -9
- package/build/constants.d.ts +3 -1
- package/build/constants.js +3 -1
- package/build/reactive/array.js +89 -17
- package/build/reactive/index.d.ts +1 -1
- package/build/reactive/object.d.ts +3 -3
- package/build/reactive/object.js +1 -1
- package/build/system.d.ts +4 -3
- package/build/system.js +48 -16
- package/oxlintrc.json +3 -0
- package/package.json +15 -7
- package/pnpm-workspace.yaml +4 -7
- package/src/compiler/array.ts +30 -27
- package/src/compiler/bindings.ts +52 -23
- package/src/compiler/index.ts +37 -20
- package/src/compiler/object.ts +43 -30
- package/src/compiler/primitives.ts +13 -10
- package/src/constants.ts +6 -1
- package/src/reactive/array.ts +133 -24
- package/src/reactive/index.ts +3 -3
- package/src/reactive/object.ts +4 -4
- package/src/system.ts +68 -23
- package/test/async-hardening.test.ts +2 -2
- package/test/async-iterable.test.ts +3 -3
- package/test/compiler/__snapshots__/output.test.ts.snap +157 -0
- package/test/compiler/compiler.test.ts +51 -9
- package/test/compiler/composition.test.ts +110 -0
- package/test/compiler/coverage.test.ts +22 -22
- package/test/compiler/origins.test.ts +156 -0
- package/test/compiler/output.test.ts +116 -0
- package/test/deep-graphs.test.ts +6 -6
- package/test/effects.test.ts +5 -5
- package/test/errors.test.ts +4 -4
- package/test/invalidate.test.ts +2 -2
- package/test/ownership.test.ts +30 -30
- package/test/pause.test.ts +267 -0
- package/test/reactive/array.test.ts +13 -12
- package/test/reactive/reactive.test.ts +6 -6
- package/test/signal-selector.test.ts +4 -4
- package/test/system.test.ts +13 -13
- package/test/untrack.test.ts +10 -10
- package/tsconfig.build.json +3 -0
- package/tsconfig.json +2 -2
- package/vitest.config.ts +2 -2
- package/bench/tsconfig.json +0 -16
- package/test/tsconfig.json +0 -16
package/src/constants.ts
CHANGED
|
@@ -35,6 +35,11 @@ const STATE_EFFECT = 1 << 6;
|
|
|
35
35
|
// Set by dispose(): a node that can never run or own anything again
|
|
36
36
|
const STATE_DISPOSED = 1 << 7;
|
|
37
37
|
|
|
38
|
+
// Set by pause(): runs that reach the effect are skipped and marked STATE_STALE, which resume() replays once
|
|
39
|
+
const STATE_PAUSED = 1 << 8;
|
|
40
|
+
|
|
41
|
+
const STATE_STALE = 1 << 9;
|
|
42
|
+
|
|
38
43
|
const STATE_NOTIFY_MASK = (STATE_CHECK | STATE_DIRTY);
|
|
39
44
|
|
|
40
45
|
|
|
@@ -44,5 +49,5 @@ export {
|
|
|
44
49
|
REACTIVE_ARRAY, REACTIVE_OBJECT,
|
|
45
50
|
SIGNAL,
|
|
46
51
|
STABILIZER_DEFERRED, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED,
|
|
47
|
-
STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_RECOMPUTING
|
|
52
|
+
STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_PAUSED, STATE_RECOMPUTING, STATE_STALE
|
|
48
53
|
};
|
package/src/reactive/array.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { isArray } from '@esportsplus/utilities';
|
|
2
|
-
import { PACKAGE_NAME, REACTIVE_ARRAY } from '
|
|
3
|
-
import { effect, read, root, signal, write } from '
|
|
4
|
-
import type { Signal } from '~/types';
|
|
2
|
+
import { PACKAGE_NAME, REACTIVE_ARRAY } from '../constants';
|
|
3
|
+
import { dispose as disposeNode, effect, read, root, signal, write } from '../system';
|
|
5
4
|
import { isReactiveObject } from './object';
|
|
5
|
+
import type { Computed, Signal } from '../types';
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
// One mutation as the batch event reports it: the matching event's payload, named by 'op'
|
|
@@ -69,15 +69,90 @@ function dispose(value: unknown) {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
// One element move of Array.prototype's algorithms: a hole moves as a hole
|
|
73
|
+
function move<T>(array: T[], from: number, to: number) {
|
|
74
|
+
if (from in array) {
|
|
75
|
+
array[to] = array[from];
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
// oxlint-disable-next-line typescript/no-array-delete -- a hole must move as a hole, as the builtin does
|
|
79
|
+
delete array[to];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Array.prototype.splice's element steps after the removal: `count` slots at `start` become `items`. Slots
|
|
84
|
+
// past the end are filled in ascending order, so a packed array stays packed.
|
|
85
|
+
function place<T>(array: T[], start: number, count: number, items: T[]) {
|
|
86
|
+
let length = array.length,
|
|
87
|
+
n = items.length,
|
|
88
|
+
total = length - count + n;
|
|
89
|
+
|
|
90
|
+
if (n < count) {
|
|
91
|
+
for (let i = start + count; i < length; i++) {
|
|
92
|
+
move(array, i, i - count + n);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else if (n > count) {
|
|
96
|
+
let shift = n - count;
|
|
97
|
+
|
|
98
|
+
for (let i = length; i < total; i++) {
|
|
99
|
+
if (i < start + n) {
|
|
100
|
+
array[i] = items[i - start];
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
move(array, i - shift, i);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
for (let i = length - 1; i >= start + n; i--) {
|
|
108
|
+
move(array, i - shift, i);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (let i = 0; i < n; i++) {
|
|
113
|
+
array[start + i] = items[i];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Trims what moved down, or restores a length a hole moved past the old end could not set
|
|
117
|
+
if (array.length !== total) {
|
|
118
|
+
array.length = total;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Array.prototype.sort's steps: present values sorted apart (a plain array sorts on the fast path), written
|
|
123
|
+
// back in order, holes left at the end
|
|
124
|
+
function rank<T>(array: T[], fn?: (a: T, b: T) => number) {
|
|
125
|
+
let n = array.length,
|
|
126
|
+
values: T[] = [];
|
|
127
|
+
|
|
128
|
+
for (let i = 0; i < n; i++) {
|
|
129
|
+
if (i in array) {
|
|
130
|
+
values.push(array[i]);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
values.sort(fn);
|
|
135
|
+
|
|
136
|
+
for (let i = 0, m = values.length; i < m; i++) {
|
|
137
|
+
array[i] = values[i];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
for (let i = values.length; i < n; i++) {
|
|
141
|
+
// oxlint-disable-next-line typescript/no-array-delete -- holes sort to the end, as the builtin leaves them
|
|
142
|
+
delete array[i];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
72
145
|
|
|
73
146
|
// Derived arrays (splice removals, map/filter/slice) must be plain Arrays: species-creating a
|
|
74
147
|
// ReactiveArray runs the reactive constructor per call and seeds _length from the length argument.
|
|
148
|
+
// V8 runs Array.prototype mutators on its generic path for an instance whose prototype is not
|
|
149
|
+
// Array.prototype, so mutations are spelled out as indexed reads and stores, which stay fast.
|
|
75
150
|
class ReactiveArray<T> extends Array<T> {
|
|
76
151
|
|
|
77
152
|
private _length: Signal<number>;
|
|
78
153
|
|
|
79
154
|
// The batch listeners' delivery: the mutations not yet delivered, and the effect that delivers them
|
|
80
|
-
private batched: { ops: ArrayOp<T>[] | null; stop:
|
|
155
|
+
private batched: { ops: ArrayOp<T>[] | null; stop: Computed<unknown>; tick: Signal<number> } | null = null;
|
|
81
156
|
|
|
82
157
|
private delivering = false;
|
|
83
158
|
|
|
@@ -210,7 +285,7 @@ class ReactiveArray<T> extends Array<T> {
|
|
|
210
285
|
|
|
211
286
|
if (batched !== null) {
|
|
212
287
|
this.batched = null;
|
|
213
|
-
batched.stop
|
|
288
|
+
disposeNode(batched.stop);
|
|
214
289
|
}
|
|
215
290
|
}
|
|
216
291
|
|
|
@@ -232,13 +307,15 @@ class ReactiveArray<T> extends Array<T> {
|
|
|
232
307
|
|
|
233
308
|
if (isArray(item)) {
|
|
234
309
|
for (let j = 0, o = item.length; j < o; j++) {
|
|
235
|
-
|
|
236
|
-
|
|
310
|
+
let value = item[j];
|
|
311
|
+
|
|
312
|
+
added.push(value);
|
|
313
|
+
this[this.length] = value;
|
|
237
314
|
}
|
|
238
315
|
}
|
|
239
316
|
else {
|
|
240
317
|
added.push(item as T);
|
|
241
|
-
|
|
318
|
+
this[this.length] = item as T;
|
|
242
319
|
}
|
|
243
320
|
}
|
|
244
321
|
|
|
@@ -392,7 +469,11 @@ class ReactiveArray<T> extends Array<T> {
|
|
|
392
469
|
return this.length;
|
|
393
470
|
}
|
|
394
471
|
|
|
395
|
-
let length =
|
|
472
|
+
let length = this.length;
|
|
473
|
+
|
|
474
|
+
for (let i = 0, n = items.length; i < n; i++) {
|
|
475
|
+
this[length++] = items[i];
|
|
476
|
+
}
|
|
396
477
|
|
|
397
478
|
write(this._length, length);
|
|
398
479
|
|
|
@@ -403,7 +484,21 @@ class ReactiveArray<T> extends Array<T> {
|
|
|
403
484
|
}
|
|
404
485
|
|
|
405
486
|
reverse() {
|
|
406
|
-
|
|
487
|
+
for (let lower = 0, upper = this.length - 1; lower < upper; lower++, upper--) {
|
|
488
|
+
let value = this[lower],
|
|
489
|
+
within = lower in this;
|
|
490
|
+
|
|
491
|
+
move(this, upper, lower);
|
|
492
|
+
|
|
493
|
+
if (within) {
|
|
494
|
+
this[upper] = value;
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
// oxlint-disable-next-line typescript/no-array-delete -- a hole must move as a hole, as the builtin does
|
|
498
|
+
delete this[upper];
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
407
502
|
this.dispatch('reverse');
|
|
408
503
|
this.record({ op: 'reverse' });
|
|
409
504
|
|
|
@@ -415,8 +510,9 @@ class ReactiveArray<T> extends Array<T> {
|
|
|
415
510
|
return undefined;
|
|
416
511
|
}
|
|
417
512
|
|
|
418
|
-
let item =
|
|
513
|
+
let item = this[0];
|
|
419
514
|
|
|
515
|
+
place(this, 0, 1, []);
|
|
420
516
|
dispose(item);
|
|
421
517
|
write(this._length, this.length);
|
|
422
518
|
this.dispatch('shift', { item });
|
|
@@ -427,19 +523,21 @@ class ReactiveArray<T> extends Array<T> {
|
|
|
427
523
|
|
|
428
524
|
sort(fn?: (a: T, b: T) => number) {
|
|
429
525
|
if (this.listeners.sort === undefined && this.batched === null) {
|
|
430
|
-
|
|
526
|
+
rank(this, fn);
|
|
431
527
|
}
|
|
432
528
|
else {
|
|
433
529
|
let n = this.length,
|
|
530
|
+
// oxlint-disable-next-line unicorn/no-new-array -- preallocated to n, every slot is written next
|
|
434
531
|
before = new Array(n) as T[];
|
|
435
532
|
|
|
436
533
|
for (let i = 0; i < n; i++) {
|
|
437
534
|
before[i] = this[i];
|
|
438
535
|
}
|
|
439
536
|
|
|
440
|
-
|
|
537
|
+
rank(this, fn);
|
|
441
538
|
|
|
442
539
|
let buckets = new Map<T, { indices: number[]; next: number }>(),
|
|
540
|
+
// oxlint-disable-next-line unicorn/no-new-array -- preallocated to n, every slot is written next
|
|
443
541
|
order = new Array(n);
|
|
444
542
|
|
|
445
543
|
for (let i = 0; i < n; i++) {
|
|
@@ -468,23 +566,32 @@ class ReactiveArray<T> extends Array<T> {
|
|
|
468
566
|
}
|
|
469
567
|
|
|
470
568
|
splice(start: number, deleteCount: number = this.length, ...items: T[]) {
|
|
569
|
+
// Arguments resolve as Array.prototype.splice resolves them (negative, fractional and out-of-range
|
|
570
|
+
// values clamped); listeners receive the resolved position and count, never the raw arguments
|
|
471
571
|
let length = this.length,
|
|
472
|
-
|
|
572
|
+
index = Math.trunc(start) || 0,
|
|
573
|
+
resolved = index < 0 ? Math.max(length + index, 0) : Math.min(index, length),
|
|
574
|
+
count = Math.min(Math.max(Math.trunc(deleteCount) || 0, 0), length - resolved),
|
|
575
|
+
removed: T[] = [];
|
|
576
|
+
|
|
577
|
+
for (let i = 0; i < count; i++) {
|
|
578
|
+
if (resolved + i in this) {
|
|
579
|
+
removed[i] = this[resolved + i];
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
removed.length = count;
|
|
473
584
|
|
|
474
|
-
if (items.length > 0 ||
|
|
585
|
+
if (items.length > 0 || count > 0) {
|
|
586
|
+
place(this, resolved, count, items);
|
|
475
587
|
write(this._length, this.length);
|
|
476
588
|
|
|
477
|
-
for (let i = 0
|
|
589
|
+
for (let i = 0; i < count; i++) {
|
|
478
590
|
dispose(removed[i]);
|
|
479
591
|
}
|
|
480
592
|
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
let index = Math.trunc(start) || 0,
|
|
484
|
-
resolved = index < 0 ? Math.max(length + index, 0) : Math.min(index, length);
|
|
485
|
-
|
|
486
|
-
this.dispatch('splice', { deleteCount: removed.length, items, start: resolved });
|
|
487
|
-
this.record({ deleteCount: removed.length, items, op: 'splice', start: resolved });
|
|
593
|
+
this.dispatch('splice', { deleteCount: count, items, start: resolved });
|
|
594
|
+
this.record({ deleteCount: count, items, op: 'splice', start: resolved });
|
|
488
595
|
}
|
|
489
596
|
|
|
490
597
|
return removed;
|
|
@@ -495,7 +602,9 @@ class ReactiveArray<T> extends Array<T> {
|
|
|
495
602
|
return this.length;
|
|
496
603
|
}
|
|
497
604
|
|
|
498
|
-
|
|
605
|
+
place(this, 0, 0, items);
|
|
606
|
+
|
|
607
|
+
let length = this.length;
|
|
499
608
|
|
|
500
609
|
write(this._length, length);
|
|
501
610
|
this.dispatch('unshift', { items });
|
package/src/reactive/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { isArray, isObject } from '@esportsplus/utilities';
|
|
2
|
-
import { PACKAGE_NAME } from '
|
|
3
|
-
import { onCleanup, root } from '
|
|
4
|
-
import type { Reactive } from '~/types';
|
|
2
|
+
import { PACKAGE_NAME } from '../constants';
|
|
3
|
+
import { onCleanup, root } from '../system';
|
|
5
4
|
import { ReactiveArray } from './array';
|
|
6
5
|
import { ReactiveObject } from './object';
|
|
6
|
+
import type { Reactive } from '../types';
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
type Guard<T> =
|
package/src/reactive/object.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { defineProperty, isArray } from '@esportsplus/utilities';
|
|
2
|
-
import { COMPUTED, REACTIVE_ARRAY, REACTIVE_OBJECT, SIGNAL } from '
|
|
3
|
-
import { computed, dispose, read, root, signal, write } from '
|
|
4
|
-
import { Computed } from '~/types';
|
|
2
|
+
import { COMPUTED, REACTIVE_ARRAY, REACTIVE_OBJECT, SIGNAL } from '../constants';
|
|
3
|
+
import { computed, dispose, read, root, signal, write } from '../system';
|
|
5
4
|
import { ReactiveArray } from './array';
|
|
5
|
+
import type { Computed } from '../types';
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
class ReactiveObject<T extends Record<PropertyKey, unknown>> {
|
|
@@ -87,7 +87,7 @@ class ReactiveObject<T extends Record<PropertyKey, unknown>> {
|
|
|
87
87
|
return;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
while (disposer = disposers.pop()) {
|
|
90
|
+
while ((disposer = disposers.pop())) {
|
|
91
91
|
disposer();
|
|
92
92
|
}
|
|
93
93
|
}
|
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';
|
|
@@ -21,6 +21,7 @@ let asyncFactory = new WeakMap<Computed<unknown>, Computed<unknown>>(),
|
|
|
21
21
|
depth = 0,
|
|
22
22
|
disposeHead: Walk | null = null,
|
|
23
23
|
draining = false,
|
|
24
|
+
// oxlint-disable-next-line unicorn/no-new-array -- preallocated heap; slots are filled by height
|
|
24
25
|
heap: (Computed<unknown> | undefined)[] = new Array(64),
|
|
25
26
|
heap_i = 0,
|
|
26
27
|
heap_n = 0,
|
|
@@ -276,6 +277,7 @@ function link<T>(dep: Signal<T> | Computed<T>, sub: Computed<T>) {
|
|
|
276
277
|
pooled.nextSub = null,
|
|
277
278
|
pooled.version = version,
|
|
278
279
|
pooled)
|
|
280
|
+
// oxlint-disable-next-line sort-keys -- creation order fixes the hidden class every link shares
|
|
279
281
|
: {
|
|
280
282
|
dep,
|
|
281
283
|
sub,
|
|
@@ -422,8 +424,16 @@ function propagate<T>(computed: Computed<T>) {
|
|
|
422
424
|
}
|
|
423
425
|
|
|
424
426
|
function recompute<T>(computed: Computed<T>) {
|
|
425
|
-
|
|
426
|
-
|
|
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
|
+
}
|
|
427
437
|
}
|
|
428
438
|
|
|
429
439
|
if (computed.cleanup !== null || computed.owned !== null) {
|
|
@@ -466,7 +476,7 @@ function recompute<T>(computed: Computed<T>) {
|
|
|
466
476
|
version++;
|
|
467
477
|
observer = o;
|
|
468
478
|
// fn may have re-queued this node (a tracked read drains pending writes), so heap membership survives
|
|
469
|
-
computed.state = STATE_COMPUTED | flags | (computed.state & STATE_IN_HEAP);
|
|
479
|
+
computed.state = STATE_COMPUTED | flags | (computed.state & (STATE_IN_HEAP | STATE_PAUSED));
|
|
470
480
|
// Entry snapshot, not current writes: a node whose fn wrote mid-run must stay gv < writes and validate normally
|
|
471
481
|
computed.gv = w;
|
|
472
482
|
|
|
@@ -474,16 +484,23 @@ function recompute<T>(computed: Computed<T>) {
|
|
|
474
484
|
remove = depsTail ? depsTail.nextDep : computed.deps;
|
|
475
485
|
|
|
476
486
|
if (remove) {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
if (depsTail) {
|
|
483
|
-
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;
|
|
484
491
|
}
|
|
485
492
|
else {
|
|
486
|
-
|
|
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
|
+
}
|
|
487
504
|
}
|
|
488
505
|
}
|
|
489
506
|
|
|
@@ -783,7 +800,7 @@ function makeAsyncComputed<T>(factory: Computed<Promise<T> | AsyncIterable<T> |
|
|
|
783
800
|
wrapper.pending = pending;
|
|
784
801
|
|
|
785
802
|
asyncFactory.set(wrapper as Computed<unknown>, factory as Computed<unknown>);
|
|
786
|
-
wrapper.disposal = stop;
|
|
803
|
+
wrapper.disposal = () => dispose(stop);
|
|
787
804
|
|
|
788
805
|
return wrapper;
|
|
789
806
|
}
|
|
@@ -1021,9 +1038,7 @@ const effect = <T>(fn: Computed<T>['fn'], apply?: (value: T, prev: T | undefined
|
|
|
1021
1038
|
});
|
|
1022
1039
|
}
|
|
1023
1040
|
|
|
1024
|
-
return
|
|
1025
|
-
dispose(c);
|
|
1026
|
-
};
|
|
1041
|
+
return c;
|
|
1027
1042
|
};
|
|
1028
1043
|
|
|
1029
1044
|
// A no-op while a pass is running: re-entering stabilize() would corrupt heap_i.
|
|
@@ -1033,10 +1048,6 @@ const flush = (): void => {
|
|
|
1033
1048
|
}
|
|
1034
1049
|
};
|
|
1035
1050
|
|
|
1036
|
-
// Whether an onCleanup() made now would land on an owner (a running computed/effect, also inside
|
|
1037
|
-
// untrack, or a root's scope)
|
|
1038
|
-
const hasOwner = (): boolean => observer !== null || scope !== null;
|
|
1039
|
-
|
|
1040
1051
|
const isComputed = (value: unknown): value is Computed<unknown> => {
|
|
1041
1052
|
return isObject(value) && !!((value as unknown as Computed<unknown>).state & STATE_COMPUTED);
|
|
1042
1053
|
};
|
|
@@ -1121,6 +1132,41 @@ const read = <T>(node: Signal<T> | Computed<T>): T => {
|
|
|
1121
1132
|
// .owner chain finds every enclosing owner.
|
|
1122
1133
|
const owner = (): Computed<unknown> | null => observer || scope;
|
|
1123
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
|
+
|
|
1124
1170
|
// Owned before fn runs (nothing can register on the owner meanwhile, so ownerSlot is unchanged): a
|
|
1125
1171
|
// root disposed inside its own fn then unlinks instead of being parked on the owner as a dead child.
|
|
1126
1172
|
//
|
|
@@ -1280,11 +1326,10 @@ export {
|
|
|
1280
1326
|
dispose,
|
|
1281
1327
|
effect,
|
|
1282
1328
|
flush,
|
|
1283
|
-
hasOwner,
|
|
1284
1329
|
isComputed, isSignal,
|
|
1285
1330
|
onCleanup, owner,
|
|
1286
|
-
peek,
|
|
1287
|
-
read, root,
|
|
1331
|
+
pause, peek,
|
|
1332
|
+
read, resume, root,
|
|
1288
1333
|
signal,
|
|
1289
1334
|
untrack,
|
|
1290
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();
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`compiled output > app.ts 1`] = `null`;
|
|
4
|
+
|
|
5
|
+
exports[`compiled output > consumer.ts 1`] = `
|
|
6
|
+
"import * as reactivity_u0 from '@esportsplus/reactivity';
|
|
7
|
+
import { count as renamed, later, make, total } from './app';
|
|
8
|
+
import * as app from './app';
|
|
9
|
+
export let a = reactivity_u0.signal(0);
|
|
10
|
+
export let b = () => reactivity_u0.read(renamed) + reactivity_u0.read(total) + reactivity_u0.read(later);
|
|
11
|
+
export let c = () => reactivity_u0.read(app.count) + app.list.$length;
|
|
12
|
+
export let d = () => reactivity_u0.read(app['count']);
|
|
13
|
+
export let e = () => { app.list.$set(0, 1); };"
|
|
14
|
+
`;
|
|
15
|
+
|
|
16
|
+
exports[`compiled output > forms.ts 1`] = `
|
|
17
|
+
"import * as reactivity_u0 from '@esportsplus/reactivity';
|
|
18
|
+
|
|
19
|
+
class ReactiveObject_1x8s4en0<T0 extends unknown[], T1 extends reactivity_u0.Computed<ReturnType<T1>>['fn'], T2 extends unknown[]> extends reactivity_u0.ReactiveObject<any> {
|
|
20
|
+
#a = this[reactivity_u0.SIGNAL](1);
|
|
21
|
+
#b = this[reactivity_u0.SIGNAL]('x');
|
|
22
|
+
#items;
|
|
23
|
+
#sum;
|
|
24
|
+
#neg = this[reactivity_u0.SIGNAL](-1);
|
|
25
|
+
#source;
|
|
26
|
+
#flag = this[reactivity_u0.SIGNAL](true);
|
|
27
|
+
|
|
28
|
+
constructor(_p0: T0, _p1: T1, _p2: T2) {
|
|
29
|
+
super(null);
|
|
30
|
+
this.#items = this[reactivity_u0.REACTIVE_ARRAY](_p0);
|
|
31
|
+
this.#sum = this[reactivity_u0.COMPUTED](_p1);
|
|
32
|
+
this.#source = this[reactivity_u0.REACTIVE_ARRAY](_p2);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
get a() {
|
|
37
|
+
return reactivity_u0.read(this.#a);
|
|
38
|
+
}
|
|
39
|
+
set a(_v0) {
|
|
40
|
+
reactivity_u0.write(this.#a, _v0);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
get b() {
|
|
45
|
+
return reactivity_u0.read(this.#b);
|
|
46
|
+
}
|
|
47
|
+
set b(_v1) {
|
|
48
|
+
reactivity_u0.write(this.#b, _v1);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
get items() {
|
|
53
|
+
return this.#items;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
get sum() {
|
|
58
|
+
return reactivity_u0.read(this.#sum);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
get neg() {
|
|
63
|
+
return reactivity_u0.read(this.#neg);
|
|
64
|
+
}
|
|
65
|
+
set neg(_v2) {
|
|
66
|
+
reactivity_u0.write(this.#neg, _v2);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
get source() {
|
|
71
|
+
return this.#source;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
get flag() {
|
|
76
|
+
return reactivity_u0.read(this.#flag);
|
|
77
|
+
}
|
|
78
|
+
set flag(_v3) {
|
|
79
|
+
reactivity_u0.write(this.#flag, _v3);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class ReactiveObject_1x8s4en1<T extends { 'a'?: unknown, 'items'?: unknown[] }> extends reactivity_u0.ReactiveObject<any> {
|
|
86
|
+
#a;
|
|
87
|
+
#items;
|
|
88
|
+
|
|
89
|
+
constructor(_p0: T['a'], _p1: T['items']) {
|
|
90
|
+
super(null);
|
|
91
|
+
this.#a = this[reactivity_u0.SIGNAL](_p0);
|
|
92
|
+
this.#items = this[reactivity_u0.REACTIVE_ARRAY](_p1);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
get a() {
|
|
97
|
+
return reactivity_u0.read(this.#a) as T['a'];
|
|
98
|
+
}
|
|
99
|
+
set a(_v0) {
|
|
100
|
+
reactivity_u0.write(this.#a, _v0);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
get items() {
|
|
105
|
+
return this.#items;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
declare function total(): number;
|
|
112
|
+
declare let list: number[];
|
|
113
|
+
declare let n: number;
|
|
114
|
+
let count = reactivity_u0.signal(0);
|
|
115
|
+
let doubled = reactivity_u0.computed(() => reactivity_u0.read(count) * 2);
|
|
116
|
+
let typed = reactivity_u0.signal(n);
|
|
117
|
+
let fn = reactivity_u0.computed(total);
|
|
118
|
+
let runtime = reactivity_u0.reactive(list);
|
|
119
|
+
let obj = new ReactiveObject_1x8s4en0([1, 2], () => 3, list);
|
|
120
|
+
let shorthand = reactivity_u0.reactive({ list });
|
|
121
|
+
let hinted = new ReactiveObject_1x8s4en1<{ a: number; items: number[] }>(1, []);
|
|
122
|
+
let spread = reactivity_u0.reactive({ ...{ a: 1 }, b: 2 });
|
|
123
|
+
let method = reactivity_u0.reactive({ m() { return 1; } });
|
|
124
|
+
let arr = new reactivity_u0.ReactiveArray([1, 2, 3]);
|
|
125
|
+
let empty = new reactivity_u0.ReactiveArray<string>();
|
|
126
|
+
let declared: number[] = new reactivity_u0.ReactiveArray<number>([4]);
|
|
127
|
+
reactivity_u0.write(count, 5);
|
|
128
|
+
reactivity_u0.write(count, count.value + 2);
|
|
129
|
+
reactivity_u0.write(count, count.value || 3);
|
|
130
|
+
reactivity_u0.write(count, count.value + 1);
|
|
131
|
+
reactivity_u0.write(count, count.value - 1);
|
|
132
|
+
let p = ((_t0) => (reactivity_u0.write(count, _t0 + 1), _t0))(count.value);
|
|
133
|
+
let q = (reactivity_u0.write(count, count.value + 1), count.value);
|
|
134
|
+
let o = { count: reactivity_u0.read(count) };
|
|
135
|
+
let t = typeof reactivity_u0.read(count);
|
|
136
|
+
arr.$set(0, 4);
|
|
137
|
+
((_a, _k) => _a.$set(_k, _a[_k] + (2)))(arr, 1);
|
|
138
|
+
((_a, _k, _v) => (_a.$set(_k, _v + 1), _v))(arr, 2, arr[2]);
|
|
139
|
+
let r = ((_a, _k) => (_a.$set(_k, _a[_k] + 1), _a[_k]))(arr, 0);
|
|
140
|
+
let size = arr.$length;
|
|
141
|
+
arr.$length = 1;
|
|
142
|
+
arr.$length = arr.length + 1;
|
|
143
|
+
arr.$length = arr.length + 1;
|
|
144
|
+
arr.splice(0, arr.length, ...[5, 6]);
|
|
145
|
+
arr.splice(0, arr.length);
|
|
146
|
+
export { count, doubled, typed, fn, runtime, obj, shorthand, hinted, spread, method, empty, declared, p, q, o, t, r, size };
|
|
147
|
+
export default reactivity_u0.signal(0);"
|
|
148
|
+
`;
|
|
149
|
+
|
|
150
|
+
exports[`compiled output > state.ts 1`] = `
|
|
151
|
+
"import * as reactivity_u0 from '@esportsplus/reactivity';
|
|
152
|
+
export let count = reactivity_u0.signal(0);
|
|
153
|
+
export let list = new reactivity_u0.ReactiveArray([1, 2]);
|
|
154
|
+
export let later: number;
|
|
155
|
+
later = reactivity_u0.signal(1);
|
|
156
|
+
export default reactivity_u0.computed(() => 2);"
|
|
157
|
+
`;
|