@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/README.md +7 -5
- package/bench/compiler.bench.ts +19 -12
- package/bench/lib/reactive-adapter.ts +1 -1
- package/bench/ownership.bench.ts +9 -5
- package/bench/reactive/array.bench.ts +6 -2
- package/bench/system.bench.ts +9 -5
- package/build/compiler/bindings.js +4 -9
- package/build/compiler/object.js +19 -9
- package/build/compiler/plugins/vite.d.ts +17 -3
- package/build/constants.d.ts +3 -1
- package/build/constants.js +3 -1
- package/build/reactive/array.js +89 -17
- package/build/system.d.ts +4 -3
- package/build/system.js +48 -16
- package/package.json +2 -2
- package/src/compiler/bindings.ts +6 -16
- package/src/compiler/object.ts +31 -16
- package/src/constants.ts +6 -1
- package/src/reactive/array.ts +130 -23
- package/src/system.ts +66 -23
- package/test/async-hardening.test.ts +2 -2
- package/test/async-iterable.test.ts +3 -3
- 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 +5 -5
- package/test/reactive/reactive.test.ts +3 -3
- package/test/signal-selector.test.ts +4 -4
- package/test/system.test.ts +10 -9
- package/test/untrack.test.ts +9 -9
package/build/system.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PACKAGE_NAME, SIGNAL, STABILIZER_DEFERRED, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_RECOMPUTING } from './constants.js';
|
|
1
|
+
import { PACKAGE_NAME, SIGNAL, STABILIZER_DEFERRED, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED, STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_PAUSED, STATE_RECOMPUTING, STATE_STALE } from './constants.js';
|
|
2
2
|
import { isObject, isPromise } from '@esportsplus/utilities';
|
|
3
3
|
let asyncFactory = new WeakMap(), depth = 0, disposeHead = null, draining = false, heap = new Array(64), heap_i = 0, heap_n = 0, linkPoolHead = null, microtask = queueMicrotask, notified = false, observer = null, pendingHead = null, pulled = null, puller = null, scope = null, stabilizer = STABILIZER_IDLE, version = 0, walkPoolHead = null, writes = 0;
|
|
4
4
|
function noop() {
|
|
@@ -267,8 +267,14 @@ function propagate(computed) {
|
|
|
267
267
|
schedule();
|
|
268
268
|
}
|
|
269
269
|
function recompute(computed) {
|
|
270
|
-
if (computed.state & STATE_IN_HEAP) {
|
|
271
|
-
|
|
270
|
+
if (computed.state & (STATE_IN_HEAP | STATE_PAUSED)) {
|
|
271
|
+
if (computed.state & STATE_IN_HEAP) {
|
|
272
|
+
deleteFromHeap(computed);
|
|
273
|
+
}
|
|
274
|
+
if (computed.state & STATE_PAUSED) {
|
|
275
|
+
computed.state |= STATE_STALE;
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
272
278
|
}
|
|
273
279
|
if (computed.cleanup !== null || computed.owned !== null) {
|
|
274
280
|
try {
|
|
@@ -296,18 +302,23 @@ function recompute(computed) {
|
|
|
296
302
|
depth--;
|
|
297
303
|
version++;
|
|
298
304
|
observer = o;
|
|
299
|
-
computed.state = STATE_COMPUTED | flags | (computed.state & STATE_IN_HEAP);
|
|
305
|
+
computed.state = STATE_COMPUTED | flags | (computed.state & (STATE_IN_HEAP | STATE_PAUSED));
|
|
300
306
|
computed.gv = w;
|
|
301
307
|
let depsTail = computed.depsTail, remove = depsTail ? depsTail.nextDep : computed.deps;
|
|
302
308
|
if (remove) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
} while (remove);
|
|
306
|
-
if (depsTail) {
|
|
307
|
-
depsTail.nextDep = null;
|
|
309
|
+
if (computed.state & STATE_PAUSED) {
|
|
310
|
+
computed.state |= STATE_STALE;
|
|
308
311
|
}
|
|
309
312
|
else {
|
|
310
|
-
|
|
313
|
+
do {
|
|
314
|
+
remove = unlink(remove);
|
|
315
|
+
} while (remove);
|
|
316
|
+
if (depsTail) {
|
|
317
|
+
depsTail.nextDep = null;
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
computed.deps = null;
|
|
321
|
+
}
|
|
311
322
|
}
|
|
312
323
|
}
|
|
313
324
|
if (ok) {
|
|
@@ -519,7 +530,7 @@ function makeAsyncComputed(factory) {
|
|
|
519
530
|
});
|
|
520
531
|
wrapper.pending = pending;
|
|
521
532
|
asyncFactory.set(wrapper, factory);
|
|
522
|
-
wrapper.disposal = stop;
|
|
533
|
+
wrapper.disposal = () => dispose(stop);
|
|
523
534
|
return wrapper;
|
|
524
535
|
}
|
|
525
536
|
function makeComputed(fn, eager = false) {
|
|
@@ -681,16 +692,13 @@ const effect = (fn, apply) => {
|
|
|
681
692
|
throw err;
|
|
682
693
|
});
|
|
683
694
|
}
|
|
684
|
-
return
|
|
685
|
-
dispose(c);
|
|
686
|
-
};
|
|
695
|
+
return c;
|
|
687
696
|
};
|
|
688
697
|
const flush = () => {
|
|
689
698
|
if (stabilizer === STABILIZER_SCHEDULED) {
|
|
690
699
|
stabilize();
|
|
691
700
|
}
|
|
692
701
|
};
|
|
693
|
-
const hasOwner = () => observer !== null || scope !== null;
|
|
694
702
|
const isComputed = (value) => {
|
|
695
703
|
return isObject(value) && !!(value.state & STATE_COMPUTED);
|
|
696
704
|
};
|
|
@@ -753,6 +761,30 @@ const read = (node) => {
|
|
|
753
761
|
return node.value;
|
|
754
762
|
};
|
|
755
763
|
const owner = () => observer || scope;
|
|
764
|
+
const pause = (node) => {
|
|
765
|
+
let state = node.state;
|
|
766
|
+
if (state & STATE_DISPOSED) {
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
if (!(state & STATE_EFFECT)) {
|
|
770
|
+
throw new Error(`${PACKAGE_NAME}: pause() expects an effect`);
|
|
771
|
+
}
|
|
772
|
+
node.state = state | STATE_PAUSED;
|
|
773
|
+
};
|
|
774
|
+
const resume = (node) => {
|
|
775
|
+
let state = node.state;
|
|
776
|
+
if (state & STATE_DISPOSED) {
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
if (!(state & STATE_EFFECT)) {
|
|
780
|
+
throw new Error(`${PACKAGE_NAME}: resume() expects an effect`);
|
|
781
|
+
}
|
|
782
|
+
node.state = state & ~(STATE_PAUSED | STATE_STALE);
|
|
783
|
+
if (state & STATE_STALE) {
|
|
784
|
+
insertIntoHeap(node);
|
|
785
|
+
schedule();
|
|
786
|
+
}
|
|
787
|
+
};
|
|
756
788
|
const root = (fn, parent) => {
|
|
757
789
|
if (parent && parent.state & STATE_DISPOSED) {
|
|
758
790
|
throw new Error(`${PACKAGE_NAME}: root() was given a disposed owner`);
|
|
@@ -859,4 +891,4 @@ const write = (signal, value) => {
|
|
|
859
891
|
}
|
|
860
892
|
schedule();
|
|
861
893
|
};
|
|
862
|
-
export { batch, computed, dispose, effect, flush,
|
|
894
|
+
export { batch, computed, dispose, effect, flush, isComputed, isSignal, onCleanup, owner, pause, peek, read, resume, root, signal, untrack, write };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "ICJR",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@esportsplus/typescript": "^0.
|
|
4
|
+
"@esportsplus/typescript": "^0.41.0",
|
|
5
5
|
"@esportsplus/utilities": "^0.28.6"
|
|
6
6
|
},
|
|
7
7
|
"devDependencies": {
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"type": "module",
|
|
43
43
|
"types": "build/index.d.ts",
|
|
44
|
-
"version": "0.
|
|
44
|
+
"version": "0.42.0",
|
|
45
45
|
"scripts": {
|
|
46
46
|
"agent:bench": "vitest bench --run",
|
|
47
47
|
"agent:test": "pnpm typecheck && vitest run",
|
package/src/compiler/bindings.ts
CHANGED
|
@@ -7,11 +7,9 @@ import type { Bindings } from './types';
|
|
|
7
7
|
|
|
8
8
|
type Assignment = { call: ts.CallExpression; target: ts.Identifier };
|
|
9
9
|
|
|
10
|
-
// Per program snapshot
|
|
11
|
-
//
|
|
12
|
-
// getNonNullableType, isArrayType), so dropping them costs a round-trip per array candidate or read
|
|
10
|
+
// Per program snapshot. `kinds` saves no checker round-trip (the shared memo answers every query it
|
|
11
|
+
// makes), but kind() runs on every read of a binding and re-deriving it per read dominates a warm transform
|
|
13
12
|
type Caches = {
|
|
14
|
-
arrays: Map<number, boolean>;
|
|
15
13
|
assignments: Map<string, Map<string, Assignment[]>>;
|
|
16
14
|
kinds: Map<string, TYPES | null>;
|
|
17
15
|
};
|
|
@@ -69,7 +67,7 @@ function cachesOf(program: ts.Program): Caches {
|
|
|
69
67
|
let found = caches.get(program);
|
|
70
68
|
|
|
71
69
|
if (!found) {
|
|
72
|
-
found = {
|
|
70
|
+
found = { assignments: new Map(), kinds: new Map() };
|
|
73
71
|
caches.set(program, found);
|
|
74
72
|
}
|
|
75
73
|
|
|
@@ -151,8 +149,7 @@ const arrays = (bindings: Bindings, nodes: ts.Node[]): Set<ts.Node> => {
|
|
|
151
149
|
return result;
|
|
152
150
|
}
|
|
153
151
|
|
|
154
|
-
let
|
|
155
|
-
types = bindings.checker.getTypeAtLocation(nodes);
|
|
152
|
+
let types = bindings.checker.getTypeAtLocation(nodes);
|
|
156
153
|
|
|
157
154
|
for (let i = 0, n = nodes.length; i < n; i++) {
|
|
158
155
|
let type = types[i];
|
|
@@ -161,16 +158,9 @@ const arrays = (bindings: Bindings, nodes: ts.Node[]): Set<ts.Node> => {
|
|
|
161
158
|
continue;
|
|
162
159
|
}
|
|
163
160
|
|
|
164
|
-
let
|
|
161
|
+
let property = bindings.checker.getPropertyOfType(bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND);
|
|
165
162
|
|
|
166
|
-
if (
|
|
167
|
-
let property = bindings.checker.getPropertyOfType(bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND);
|
|
168
|
-
|
|
169
|
-
branded = property?.declarations?.some(declaration => files.has(declaration.path)) === true;
|
|
170
|
-
cache.set(type.id, branded);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (branded) {
|
|
163
|
+
if (property?.declarations?.some(declaration => files.has(declaration.path))) {
|
|
174
164
|
result.add(nodes[i]);
|
|
175
165
|
}
|
|
176
166
|
}
|
package/src/compiler/object.ts
CHANGED
|
@@ -26,14 +26,16 @@ interface ReactiveObjectCall {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
interface VisitContext {
|
|
29
|
-
bindings: Bindings;
|
|
30
29
|
calls: ReactiveObjectCall[];
|
|
31
30
|
isReactiveCall: IsReactiveCall;
|
|
32
31
|
sourceFile: ts.SourceFile;
|
|
32
|
+
typed: AnalyzedProperty[];
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
// Syntax decides every property except a non-literal value, recorded in `typed` as a signal until
|
|
37
|
+
// its type says whether it is an array
|
|
38
|
+
function analyzeProperty(prop: ts.ObjectLiteralElementLike, typed: AnalyzedProperty[]): AnalyzedProperty | null {
|
|
37
39
|
if (!ts.isPropertyAssignment(prop)) {
|
|
38
40
|
return null;
|
|
39
41
|
}
|
|
@@ -86,20 +88,18 @@ function analyzeProperty(prop: ts.ObjectLiteralElementLike, bindings: Bindings):
|
|
|
86
88
|
return { isStatic, key, type: TYPES.Array, value };
|
|
87
89
|
}
|
|
88
90
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
91
|
+
let property: AnalyzedProperty = {
|
|
92
|
+
isStatic: isStaticValue(value),
|
|
93
|
+
key,
|
|
94
|
+
type: TYPES.Signal,
|
|
95
|
+
value
|
|
96
|
+
};
|
|
92
97
|
|
|
93
|
-
if (
|
|
94
|
-
|
|
98
|
+
if (!isStaticValue(unwrapped)) {
|
|
99
|
+
typed.push(property);
|
|
95
100
|
}
|
|
96
101
|
|
|
97
|
-
return
|
|
98
|
-
isStatic: isStaticValue(value),
|
|
99
|
-
key,
|
|
100
|
-
type: TYPES.Signal,
|
|
101
|
-
value
|
|
102
|
-
};
|
|
102
|
+
return property;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
function buildClassCode(classname: string, properties: AnalyzedProperty[], sourceFile: ts.SourceFile, typehint: string | null): string {
|
|
@@ -243,7 +243,7 @@ function visit(ctx: VisitContext, node: ts.Node): void {
|
|
|
243
243
|
return;
|
|
244
244
|
}
|
|
245
245
|
|
|
246
|
-
let analyzed = analyzeProperty(prop, ctx.
|
|
246
|
+
let analyzed = analyzeProperty(prop, ctx.typed);
|
|
247
247
|
|
|
248
248
|
if (!analyzed) {
|
|
249
249
|
node.forEachChild(n => visit(ctx, n));
|
|
@@ -269,14 +269,29 @@ function visit(ctx: VisitContext, node: ts.Node): void {
|
|
|
269
269
|
|
|
270
270
|
export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): ObjectTransformResult => {
|
|
271
271
|
let ctx: VisitContext = {
|
|
272
|
-
bindings,
|
|
273
272
|
calls: [],
|
|
274
273
|
isReactiveCall,
|
|
275
|
-
sourceFile
|
|
274
|
+
sourceFile,
|
|
275
|
+
typed: []
|
|
276
276
|
};
|
|
277
277
|
|
|
278
278
|
visit(ctx, sourceFile);
|
|
279
279
|
|
|
280
|
+
// A non-literal array is typed Reactive<T[]> on the object, so it must become a ReactiveArray
|
|
281
|
+
// exactly like a literal one; every such value in the file is typed in one request
|
|
282
|
+
if (ctx.typed.length > 0) {
|
|
283
|
+
let types = bindings.checker.getTypeAtLocation(ctx.typed.map(property => property.value));
|
|
284
|
+
|
|
285
|
+
for (let i = 0, n = ctx.typed.length; i < n; i++) {
|
|
286
|
+
let type = types[i];
|
|
287
|
+
|
|
288
|
+
if (type && (bindings.checker.isArrayType(type) || bindings.checker.isTupleType(type))) {
|
|
289
|
+
ctx.typed[i].isStatic = false;
|
|
290
|
+
ctx.typed[i].type = TYPES.Array;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
280
295
|
let prepend: string[] = [],
|
|
281
296
|
replacements: ReplacementIntent[] = [];
|
|
282
297
|
|
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
2
|
import { PACKAGE_NAME, REACTIVE_ARRAY } from '../constants';
|
|
3
|
-
import { effect, read, root, signal, write } from '../system';
|
|
3
|
+
import { dispose as disposeNode, effect, read, root, signal, write } from '../system';
|
|
4
4
|
import { isReactiveObject } from './object';
|
|
5
|
-
import type { Signal } from '../types';
|
|
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,7 +523,7 @@ 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,
|
|
@@ -438,7 +534,7 @@ class ReactiveArray<T> extends Array<T> {
|
|
|
438
534
|
before[i] = this[i];
|
|
439
535
|
}
|
|
440
536
|
|
|
441
|
-
|
|
537
|
+
rank(this, fn);
|
|
442
538
|
|
|
443
539
|
let buckets = new Map<T, { indices: number[]; next: number }>(),
|
|
444
540
|
// oxlint-disable-next-line unicorn/no-new-array -- preallocated to n, every slot is written next
|
|
@@ -470,23 +566,32 @@ class ReactiveArray<T> extends Array<T> {
|
|
|
470
566
|
}
|
|
471
567
|
|
|
472
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
|
|
473
571
|
let length = this.length,
|
|
474
|
-
|
|
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;
|
|
475
584
|
|
|
476
|
-
if (items.length > 0 ||
|
|
585
|
+
if (items.length > 0 || count > 0) {
|
|
586
|
+
place(this, resolved, count, items);
|
|
477
587
|
write(this._length, this.length);
|
|
478
588
|
|
|
479
|
-
for (let i = 0
|
|
589
|
+
for (let i = 0; i < count; i++) {
|
|
480
590
|
dispose(removed[i]);
|
|
481
591
|
}
|
|
482
592
|
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
let index = Math.trunc(start) || 0,
|
|
486
|
-
resolved = index < 0 ? Math.max(length + index, 0) : Math.min(index, length);
|
|
487
|
-
|
|
488
|
-
this.dispatch('splice', { deleteCount: removed.length, items, start: resolved });
|
|
489
|
-
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 });
|
|
490
595
|
}
|
|
491
596
|
|
|
492
597
|
return removed;
|
|
@@ -497,7 +602,9 @@ class ReactiveArray<T> extends Array<T> {
|
|
|
497
602
|
return this.length;
|
|
498
603
|
}
|
|
499
604
|
|
|
500
|
-
|
|
605
|
+
place(this, 0, 0, items);
|
|
606
|
+
|
|
607
|
+
let length = this.length;
|
|
501
608
|
|
|
502
609
|
write(this._length, length);
|
|
503
610
|
this.dispatch('unshift', { items });
|