@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/README.md
CHANGED
|
@@ -112,18 +112,18 @@ state.userId = 2;
|
|
|
112
112
|
### Effects
|
|
113
113
|
|
|
114
114
|
```typescript
|
|
115
|
-
import { effect, reactive } from '@esportsplus/reactivity';
|
|
115
|
+
import { dispose, effect, reactive } from '@esportsplus/reactivity';
|
|
116
116
|
|
|
117
117
|
let count = reactive(0);
|
|
118
118
|
|
|
119
|
-
let
|
|
119
|
+
let node = effect(() => {
|
|
120
120
|
console.log('Count is:', count);
|
|
121
121
|
});
|
|
122
122
|
|
|
123
123
|
count = 1; // logs: Count is: 1
|
|
124
124
|
count = 2; // logs: Count is: 2
|
|
125
125
|
|
|
126
|
-
|
|
126
|
+
dispose(node); // stops the effect
|
|
127
127
|
```
|
|
128
128
|
|
|
129
129
|
## Transformer Plugins
|
|
@@ -221,7 +221,7 @@ let user = new ReactiveObject_1();
|
|
|
221
221
|
| `reactive(() => expr)` | Creates a computed value (compile-time only) |
|
|
222
222
|
| `reactive({...})` | Creates a reactive object with signals and computeds |
|
|
223
223
|
| `reactive([...])` | Creates a reactive array |
|
|
224
|
-
| `effect(fn, apply?)` | Runs a function that re-executes when dependencies change. Returns
|
|
224
|
+
| `effect(fn, apply?)` | Runs a function that re-executes when dependencies change. Returns the effect's node (`Computed<T>`); stop it with `dispose(node)`. Optional `apply(value, prev)` runs untracked after each run with `fn`'s return value |
|
|
225
225
|
| `root(fn, owner?)` | Creates an untracked scope. If `fn` accepts an argument, a dispose function is provided. With `owner`, the scope belongs to that node instead of whatever is running: `root(() => …, owner)` runs `fn` owned by `owner` (to re-enter it after an `await`), `root((dispose) => …, owner)` creates a child root of `owner`. Reads stay untracked. Throws on a disposed owner |
|
|
226
226
|
| `owner()` | The node that owns what is created now: the running effect/computed, else the running root's scope, else `null`. Its `.owner` chain reaches every enclosing owner |
|
|
227
227
|
| `onCleanup(fn)` | Registers a cleanup function for the current effect/computed |
|
|
@@ -240,7 +240,9 @@ These operate directly on signal/computed nodes — the transformer emits them,
|
|
|
240
240
|
| `untrack(fn)` | Runs `fn` without tracking dependencies; returns `fn`'s result. Ownership is unchanged: effects, computeds, child roots and `onCleanup` created inside are owned by the running effect/computed (or the enclosing `root((dispose) => …)`) and torn down on its next re-run or dispose. Use a zero-arg `root(() => …)` for deliberately detached work |
|
|
241
241
|
| `batch(fn)` | Groups writes so dependent effects defer until `fn` returns; pair with `flush()` for a synchronous transaction |
|
|
242
242
|
| `flush()` | Synchronously settles all pending computed/effect updates |
|
|
243
|
-
| `dispose(
|
|
243
|
+
| `dispose(node)` | Disposes a computed or effect node and its dependencies |
|
|
244
|
+
| `pause(effect)` | Stops an effect from running: a change that reaches it marks it stale instead. Called from inside the effect's own run, it abandons that run and keeps the dependencies the run had not read. Throws for a computed; a no-op on a disposed effect |
|
|
245
|
+
| `resume(effect)` | Lets a paused effect run again. If a change reached it while paused it runs once (on the next microtask, or now with `flush()`); otherwise nothing runs. Throws for a computed; a no-op on a disposed effect |
|
|
244
246
|
| `computed.invalidate(computed)` | Forces re-derivation of a computed on next read (re-dispatches an async computed's promise for a refetch) |
|
|
245
247
|
| `signal.selector(signal, key)` | Per-key selector: within a tracking scope, subscribes to whether `signal`'s value equals `key`, so only key-matching observers re-run on write (O(1) fan-out) |
|
|
246
248
|
|
package/bench/compiler.bench.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { pathToFileURL } from 'node:url';
|
|
5
5
|
import { coordinator, languageService } from '@esportsplus/typescript/compiler';
|
|
6
|
+
import type { ts } from '@esportsplus/typescript';
|
|
6
7
|
|
|
7
8
|
import pipeline from '~/compiler/index';
|
|
8
9
|
|
|
@@ -72,13 +73,22 @@ writeFileSync(config, JSON.stringify({
|
|
|
72
73
|
}));
|
|
73
74
|
|
|
74
75
|
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
76
|
+
// One build pass: every module is transformed against one program and one checker, as tsc and vite
|
|
77
|
+
// hand them in, so the coordinator's memo is shared across the pass. Each pass wraps the checker
|
|
78
|
+
// anew, so its memo starts empty and every query the pass needs reaches tsgo once.
|
|
79
|
+
function build(names: string[]): string[] {
|
|
80
|
+
let checker: ts.Checker | null = null,
|
|
81
|
+
code: string[] = [];
|
|
80
82
|
|
|
81
|
-
|
|
83
|
+
for (let i = 0, n = names.length; i < n; i++) {
|
|
84
|
+
let id = directory + '/' + names[i],
|
|
85
|
+
project = languageService.update(config, id, FILES[names[i]]);
|
|
86
|
+
|
|
87
|
+
checker ??= new Proxy(project.checker, {});
|
|
88
|
+
code.push(coordinator.transform([pipeline], FILES[names[i]], project.program.getSourceFile(id)!, { checker, program: project.program }, directory, new Map()).code);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return code;
|
|
82
92
|
}
|
|
83
93
|
|
|
84
94
|
|
|
@@ -91,13 +101,10 @@ afterAll(() => {
|
|
|
91
101
|
test('compiler transform', async ({ bench }) => {
|
|
92
102
|
await bench.compare(
|
|
93
103
|
bench('every module of the app', () => {
|
|
94
|
-
|
|
95
|
-
transform(NAMES[i]);
|
|
96
|
-
}
|
|
104
|
+
build(NAMES);
|
|
97
105
|
}),
|
|
98
106
|
bench('modules without reactive (no import, unrelated import)', () => {
|
|
99
|
-
|
|
100
|
-
transform('plain.ts');
|
|
107
|
+
build(['helpers.ts', 'plain.ts']);
|
|
101
108
|
})
|
|
102
109
|
);
|
|
103
110
|
});
|
|
@@ -105,7 +112,7 @@ test('compiler transform', async ({ bench }) => {
|
|
|
105
112
|
test('compiled output', async ({ bench }) => {
|
|
106
113
|
let file = directory + '/runtime.compiled.ts';
|
|
107
114
|
|
|
108
|
-
writeFileSync(file,
|
|
115
|
+
writeFileSync(file, build(['runtime.ts'])[0]);
|
|
109
116
|
|
|
110
117
|
let { run } = await import(pathToFileURL(file).href) as { run: () => number };
|
|
111
118
|
|
package/bench/ownership.bench.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { test } from 'vitest';
|
|
2
|
-
import { computed, dispose, effect, flush, onCleanup, read, root, signal, write } from '~/system';
|
|
2
|
+
import { computed, dispose as disposeNode, effect, flush, onCleanup, read, root, signal, write } from '~/system';
|
|
3
3
|
import type { Computed } from '~/system';
|
|
4
4
|
|
|
5
|
+
// Vitest's module runner turns each imported call into a namespace getter read; one local binding keeps that
|
|
6
|
+
// out of the measured loops
|
|
7
|
+
const dispose = disposeNode;
|
|
8
|
+
|
|
5
9
|
|
|
6
10
|
const CHURN = 1000;
|
|
7
11
|
|
|
@@ -41,9 +45,9 @@ test('ownership churn', async ({ bench }) => {
|
|
|
41
45
|
bench(`${CHURN} child effects created + disposed under one owner`, () => {
|
|
42
46
|
let owner = computed(() => {
|
|
43
47
|
for (let i = 0; i < CHURN; i++) {
|
|
44
|
-
effect(() => {
|
|
48
|
+
dispose(effect(() => {
|
|
45
49
|
onCleanup(noop);
|
|
46
|
-
})
|
|
50
|
+
}));
|
|
47
51
|
}
|
|
48
52
|
|
|
49
53
|
return 0;
|
|
@@ -110,7 +114,7 @@ test('ownership effect re-run', async ({ bench }) => {
|
|
|
110
114
|
})
|
|
111
115
|
);
|
|
112
116
|
|
|
113
|
-
stop
|
|
117
|
+
dispose(stop);
|
|
114
118
|
});
|
|
115
119
|
|
|
116
120
|
|
|
@@ -208,5 +212,5 @@ test('ownership single cleanup', async ({ bench }) => {
|
|
|
208
212
|
})
|
|
209
213
|
);
|
|
210
214
|
|
|
211
|
-
stop
|
|
215
|
+
dispose(stop);
|
|
212
216
|
});
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { test } from 'vitest';
|
|
2
2
|
import { ReactiveArray } from '~/reactive/array';
|
|
3
|
-
import { effect } from '~/system';
|
|
3
|
+
import { dispose as disposeNode, effect } from '~/system';
|
|
4
|
+
|
|
5
|
+
// Vitest's module runner turns each imported call into a namespace getter read; one local binding keeps that
|
|
6
|
+
// out of the measured loops
|
|
7
|
+
const dispose = disposeNode;
|
|
4
8
|
|
|
5
9
|
|
|
6
10
|
test('ReactiveArray creation', async ({ bench }) => {
|
|
@@ -235,6 +239,6 @@ test('ReactiveArray reactive length', async ({ bench }) => {
|
|
|
235
239
|
arr.$length;
|
|
236
240
|
});
|
|
237
241
|
|
|
238
|
-
stop
|
|
242
|
+
dispose(stop);
|
|
239
243
|
}).run();
|
|
240
244
|
});
|
package/bench/system.bench.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { test } from 'vitest';
|
|
2
|
-
import { computed, dispose, effect, onCleanup, read, root, signal, write } from '~/system';
|
|
2
|
+
import { computed, dispose as disposeNode, effect, onCleanup, read, root, signal, write } from '~/system';
|
|
3
3
|
import type { Computed } from '~/system';
|
|
4
4
|
|
|
5
|
+
// Vitest's module runner turns each imported call into a namespace getter read; one local binding keeps that
|
|
6
|
+
// out of the measured loops
|
|
7
|
+
const dispose = disposeNode;
|
|
8
|
+
|
|
5
9
|
|
|
6
10
|
test('signal', async ({ bench }) => {
|
|
7
11
|
await bench.compare(
|
|
@@ -93,7 +97,7 @@ test('effect', async ({ bench }) => {
|
|
|
93
97
|
bench('create + dispose effect', () => {
|
|
94
98
|
let stop = effect(() => {});
|
|
95
99
|
|
|
96
|
-
stop
|
|
100
|
+
dispose(stop);
|
|
97
101
|
}),
|
|
98
102
|
bench('effect with 1 signal', () => {
|
|
99
103
|
let s = signal(0);
|
|
@@ -102,7 +106,7 @@ test('effect', async ({ bench }) => {
|
|
|
102
106
|
read(s);
|
|
103
107
|
});
|
|
104
108
|
|
|
105
|
-
stop
|
|
109
|
+
dispose(stop);
|
|
106
110
|
}),
|
|
107
111
|
bench('effect with 10 signals', () => {
|
|
108
112
|
let signals: ReturnType<typeof signal<number>>[] = [];
|
|
@@ -117,7 +121,7 @@ test('effect', async ({ bench }) => {
|
|
|
117
121
|
}
|
|
118
122
|
});
|
|
119
123
|
|
|
120
|
-
stop
|
|
124
|
+
dispose(stop);
|
|
121
125
|
})
|
|
122
126
|
);
|
|
123
127
|
});
|
|
@@ -245,7 +249,7 @@ test('effect stress', async ({ bench }) => {
|
|
|
245
249
|
for (let i = 0; i < 1000; i++) {
|
|
246
250
|
let stop = effect(() => {});
|
|
247
251
|
|
|
248
|
-
stop
|
|
252
|
+
dispose(stop);
|
|
249
253
|
}
|
|
250
254
|
}).run();
|
|
251
255
|
});
|
|
@@ -32,7 +32,7 @@ function assignments(bindings, file) {
|
|
|
32
32
|
function cachesOf(program) {
|
|
33
33
|
let found = caches.get(program);
|
|
34
34
|
if (!found) {
|
|
35
|
-
found = {
|
|
35
|
+
found = { assignments: new Map(), kinds: new Map() };
|
|
36
36
|
caches.set(program, found);
|
|
37
37
|
}
|
|
38
38
|
return found;
|
|
@@ -85,19 +85,14 @@ const arrays = (bindings, nodes) => {
|
|
|
85
85
|
if (files.size === 0) {
|
|
86
86
|
return result;
|
|
87
87
|
}
|
|
88
|
-
let
|
|
88
|
+
let types = bindings.checker.getTypeAtLocation(nodes);
|
|
89
89
|
for (let i = 0, n = nodes.length; i < n; i++) {
|
|
90
90
|
let type = types[i];
|
|
91
91
|
if (!type) {
|
|
92
92
|
continue;
|
|
93
93
|
}
|
|
94
|
-
let
|
|
95
|
-
if (
|
|
96
|
-
let property = bindings.checker.getPropertyOfType(bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND);
|
|
97
|
-
branded = property?.declarations?.some(declaration => files.has(declaration.path)) === true;
|
|
98
|
-
cache.set(type.id, branded);
|
|
99
|
-
}
|
|
100
|
-
if (branded) {
|
|
94
|
+
let property = bindings.checker.getPropertyOfType(bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND);
|
|
95
|
+
if (property?.declarations?.some(declaration => files.has(declaration.path))) {
|
|
101
96
|
result.add(nodes[i]);
|
|
102
97
|
}
|
|
103
98
|
}
|
package/build/compiler/object.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ts } from '@esportsplus/typescript';
|
|
2
2
|
import { code, uid } from '@esportsplus/typescript/compiler';
|
|
3
3
|
import { NAMESPACE, TYPES } from './constants.js';
|
|
4
|
-
function analyzeProperty(prop,
|
|
4
|
+
function analyzeProperty(prop, typed) {
|
|
5
5
|
if (!ts.isPropertyAssignment(prop)) {
|
|
6
6
|
return null;
|
|
7
7
|
}
|
|
@@ -37,16 +37,16 @@ function analyzeProperty(prop, bindings) {
|
|
|
37
37
|
}
|
|
38
38
|
return { isStatic, key, type: TYPES.Array, value };
|
|
39
39
|
}
|
|
40
|
-
let
|
|
41
|
-
if (type && (bindings.checker.isArrayType(type) || bindings.checker.isTupleType(type))) {
|
|
42
|
-
return { isStatic: false, key, type: TYPES.Array, value };
|
|
43
|
-
}
|
|
44
|
-
return {
|
|
40
|
+
let property = {
|
|
45
41
|
isStatic: isStaticValue(value),
|
|
46
42
|
key,
|
|
47
43
|
type: TYPES.Signal,
|
|
48
44
|
value
|
|
49
45
|
};
|
|
46
|
+
if (!isStaticValue(unwrapped)) {
|
|
47
|
+
typed.push(property);
|
|
48
|
+
}
|
|
49
|
+
return property;
|
|
50
50
|
}
|
|
51
51
|
function buildClassCode(classname, properties, sourceFile, typehint) {
|
|
52
52
|
let accessors = [], body = [], constraint = [], fields = [], generics = [], parameters = [], setters = 0;
|
|
@@ -156,7 +156,7 @@ function visit(ctx, node) {
|
|
|
156
156
|
node.forEachChild(n => visit(ctx, n));
|
|
157
157
|
return;
|
|
158
158
|
}
|
|
159
|
-
let analyzed = analyzeProperty(prop, ctx.
|
|
159
|
+
let analyzed = analyzeProperty(prop, ctx.typed);
|
|
160
160
|
if (!analyzed) {
|
|
161
161
|
node.forEachChild(n => visit(ctx, n));
|
|
162
162
|
return;
|
|
@@ -176,12 +176,22 @@ function visit(ctx, node) {
|
|
|
176
176
|
}
|
|
177
177
|
export default (sourceFile, bindings, isReactiveCall) => {
|
|
178
178
|
let ctx = {
|
|
179
|
-
bindings,
|
|
180
179
|
calls: [],
|
|
181
180
|
isReactiveCall,
|
|
182
|
-
sourceFile
|
|
181
|
+
sourceFile,
|
|
182
|
+
typed: []
|
|
183
183
|
};
|
|
184
184
|
visit(ctx, sourceFile);
|
|
185
|
+
if (ctx.typed.length > 0) {
|
|
186
|
+
let types = bindings.checker.getTypeAtLocation(ctx.typed.map(property => property.value));
|
|
187
|
+
for (let i = 0, n = ctx.typed.length; i < n; i++) {
|
|
188
|
+
let type = types[i];
|
|
189
|
+
if (type && (bindings.checker.isArrayType(type) || bindings.checker.isTupleType(type))) {
|
|
190
|
+
ctx.typed[i].isStatic = false;
|
|
191
|
+
ctx.typed[i].type = TYPES.Array;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
185
195
|
let prepend = [], replacements = [];
|
|
186
196
|
for (let i = 0, n = ctx.calls.length; i < n; i++) {
|
|
187
197
|
let call = ctx.calls[i], typehint = call.typehint;
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
declare const _default: ({ root }?: {
|
|
2
2
|
root?: string;
|
|
3
3
|
}) => {
|
|
4
|
+
buildEnd: () => void;
|
|
4
5
|
buildStart: () => void;
|
|
5
|
-
closeBundle: () => void;
|
|
6
|
-
closeWatcher: () => void
|
|
6
|
+
closeBundle: () => Promise<void> | undefined;
|
|
7
|
+
closeWatcher: () => Promise<void>;
|
|
7
8
|
configResolved: (config: unknown) => void;
|
|
8
9
|
enforce: 'pre';
|
|
9
10
|
handleHotUpdate: <M>(ctx: {
|
|
@@ -11,16 +12,29 @@ declare const _default: ({ root }?: {
|
|
|
11
12
|
modules: M[];
|
|
12
13
|
server: {
|
|
13
14
|
moduleGraph: {
|
|
15
|
+
getModuleById?(id: string): M | undefined;
|
|
14
16
|
getModulesByFile(file: string): Set<M> | undefined;
|
|
15
17
|
invalidateModule(module: M, seen: Set<M>, timestamp: number, isHmr: boolean): void;
|
|
16
18
|
};
|
|
17
19
|
};
|
|
18
20
|
timestamp: number;
|
|
19
21
|
}) => M[] | undefined;
|
|
22
|
+
load: {
|
|
23
|
+
filter: {
|
|
24
|
+
id: RegExp;
|
|
25
|
+
};
|
|
26
|
+
handler: (id: string) => string | null;
|
|
27
|
+
};
|
|
20
28
|
name: string;
|
|
21
29
|
renderChunk: (code: string, chunk: {
|
|
22
30
|
fileName: string;
|
|
23
31
|
}) => null;
|
|
32
|
+
resolveId: {
|
|
33
|
+
filter: {
|
|
34
|
+
id: RegExp;
|
|
35
|
+
};
|
|
36
|
+
handler: (id: string) => string | null;
|
|
37
|
+
};
|
|
24
38
|
transform: {
|
|
25
39
|
filter: {
|
|
26
40
|
id: {
|
|
@@ -28,7 +42,7 @@ declare const _default: ({ root }?: {
|
|
|
28
42
|
include: RegExp;
|
|
29
43
|
};
|
|
30
44
|
};
|
|
31
|
-
handler: (code: string, id: string) => {
|
|
45
|
+
handler: (this: unknown, code: string, id: string) => {
|
|
32
46
|
code: string;
|
|
33
47
|
map: import("@esportsplus/typescript/compiler").SourceMapV3;
|
|
34
48
|
} | null;
|
package/build/constants.d.ts
CHANGED
|
@@ -16,5 +16,7 @@ declare const STATE_COMPUTED: number;
|
|
|
16
16
|
declare const STATE_ERROR: number;
|
|
17
17
|
declare const STATE_EFFECT: number;
|
|
18
18
|
declare const STATE_DISPOSED: number;
|
|
19
|
+
declare const STATE_PAUSED: number;
|
|
20
|
+
declare const STATE_STALE: number;
|
|
19
21
|
declare const STATE_NOTIFY_MASK: number;
|
|
20
|
-
export { COMPUTED, PACKAGE_NAME, REACTIVE_ARRAY, REACTIVE_OBJECT, 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 };
|
|
22
|
+
export { COMPUTED, PACKAGE_NAME, REACTIVE_ARRAY, REACTIVE_OBJECT, 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 };
|
package/build/constants.js
CHANGED
|
@@ -16,5 +16,7 @@ const STATE_COMPUTED = 1 << 4;
|
|
|
16
16
|
const STATE_ERROR = 1 << 5;
|
|
17
17
|
const STATE_EFFECT = 1 << 6;
|
|
18
18
|
const STATE_DISPOSED = 1 << 7;
|
|
19
|
+
const STATE_PAUSED = 1 << 8;
|
|
20
|
+
const STATE_STALE = 1 << 9;
|
|
19
21
|
const STATE_NOTIFY_MASK = (STATE_CHECK | STATE_DIRTY);
|
|
20
|
-
export { COMPUTED, PACKAGE_NAME, REACTIVE_ARRAY, REACTIVE_OBJECT, 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 };
|
|
22
|
+
export { COMPUTED, PACKAGE_NAME, REACTIVE_ARRAY, REACTIVE_OBJECT, 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 };
|
package/build/reactive/array.js
CHANGED
|
@@ -1,12 +1,63 @@
|
|
|
1
1
|
import { isArray } from '@esportsplus/utilities';
|
|
2
2
|
import { PACKAGE_NAME, REACTIVE_ARRAY } from '../constants.js';
|
|
3
|
-
import { effect, read, root, signal, write } from '../system.js';
|
|
3
|
+
import { dispose as disposeNode, effect, read, root, signal, write } from '../system.js';
|
|
4
4
|
import { isReactiveObject } from './object.js';
|
|
5
5
|
function dispose(value) {
|
|
6
6
|
if (isReactiveObject(value)) {
|
|
7
7
|
value.dispose();
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
+
function move(array, from, to) {
|
|
11
|
+
if (from in array) {
|
|
12
|
+
array[to] = array[from];
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
delete array[to];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function place(array, start, count, items) {
|
|
19
|
+
let length = array.length, n = items.length, total = length - count + n;
|
|
20
|
+
if (n < count) {
|
|
21
|
+
for (let i = start + count; i < length; i++) {
|
|
22
|
+
move(array, i, i - count + n);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else if (n > count) {
|
|
26
|
+
let shift = n - count;
|
|
27
|
+
for (let i = length; i < total; i++) {
|
|
28
|
+
if (i < start + n) {
|
|
29
|
+
array[i] = items[i - start];
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
move(array, i - shift, i);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
for (let i = length - 1; i >= start + n; i--) {
|
|
36
|
+
move(array, i - shift, i);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (let i = 0; i < n; i++) {
|
|
40
|
+
array[start + i] = items[i];
|
|
41
|
+
}
|
|
42
|
+
if (array.length !== total) {
|
|
43
|
+
array.length = total;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function rank(array, fn) {
|
|
47
|
+
let n = array.length, values = [];
|
|
48
|
+
for (let i = 0; i < n; i++) {
|
|
49
|
+
if (i in array) {
|
|
50
|
+
values.push(array[i]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
values.sort(fn);
|
|
54
|
+
for (let i = 0, m = values.length; i < m; i++) {
|
|
55
|
+
array[i] = values[i];
|
|
56
|
+
}
|
|
57
|
+
for (let i = values.length; i < n; i++) {
|
|
58
|
+
delete array[i];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
10
61
|
class ReactiveArray extends Array {
|
|
11
62
|
_length;
|
|
12
63
|
batched = null;
|
|
@@ -100,7 +151,7 @@ class ReactiveArray extends Array {
|
|
|
100
151
|
let batched = this.batched;
|
|
101
152
|
if (batched !== null) {
|
|
102
153
|
this.batched = null;
|
|
103
|
-
batched.stop
|
|
154
|
+
disposeNode(batched.stop);
|
|
104
155
|
}
|
|
105
156
|
}
|
|
106
157
|
clear() {
|
|
@@ -115,13 +166,14 @@ class ReactiveArray extends Array {
|
|
|
115
166
|
let item = items[i];
|
|
116
167
|
if (isArray(item)) {
|
|
117
168
|
for (let j = 0, o = item.length; j < o; j++) {
|
|
118
|
-
|
|
119
|
-
|
|
169
|
+
let value = item[j];
|
|
170
|
+
added.push(value);
|
|
171
|
+
this[this.length] = value;
|
|
120
172
|
}
|
|
121
173
|
}
|
|
122
174
|
else {
|
|
123
175
|
added.push(item);
|
|
124
|
-
|
|
176
|
+
this[this.length] = item;
|
|
125
177
|
}
|
|
126
178
|
}
|
|
127
179
|
if (added.length) {
|
|
@@ -235,14 +287,26 @@ class ReactiveArray extends Array {
|
|
|
235
287
|
if (!items.length) {
|
|
236
288
|
return this.length;
|
|
237
289
|
}
|
|
238
|
-
let length =
|
|
290
|
+
let length = this.length;
|
|
291
|
+
for (let i = 0, n = items.length; i < n; i++) {
|
|
292
|
+
this[length++] = items[i];
|
|
293
|
+
}
|
|
239
294
|
write(this._length, length);
|
|
240
295
|
this.dispatch('push', { items });
|
|
241
296
|
this.record({ items, op: 'push' });
|
|
242
297
|
return length;
|
|
243
298
|
}
|
|
244
299
|
reverse() {
|
|
245
|
-
|
|
300
|
+
for (let lower = 0, upper = this.length - 1; lower < upper; lower++, upper--) {
|
|
301
|
+
let value = this[lower], within = lower in this;
|
|
302
|
+
move(this, upper, lower);
|
|
303
|
+
if (within) {
|
|
304
|
+
this[upper] = value;
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
delete this[upper];
|
|
308
|
+
}
|
|
309
|
+
}
|
|
246
310
|
this.dispatch('reverse');
|
|
247
311
|
this.record({ op: 'reverse' });
|
|
248
312
|
return this;
|
|
@@ -251,7 +315,8 @@ class ReactiveArray extends Array {
|
|
|
251
315
|
if (this.length === 0) {
|
|
252
316
|
return undefined;
|
|
253
317
|
}
|
|
254
|
-
let item =
|
|
318
|
+
let item = this[0];
|
|
319
|
+
place(this, 0, 1, []);
|
|
255
320
|
dispose(item);
|
|
256
321
|
write(this._length, this.length);
|
|
257
322
|
this.dispatch('shift', { item });
|
|
@@ -260,14 +325,14 @@ class ReactiveArray extends Array {
|
|
|
260
325
|
}
|
|
261
326
|
sort(fn) {
|
|
262
327
|
if (this.listeners.sort === undefined && this.batched === null) {
|
|
263
|
-
|
|
328
|
+
rank(this, fn);
|
|
264
329
|
}
|
|
265
330
|
else {
|
|
266
331
|
let n = this.length, before = new Array(n);
|
|
267
332
|
for (let i = 0; i < n; i++) {
|
|
268
333
|
before[i] = this[i];
|
|
269
334
|
}
|
|
270
|
-
|
|
335
|
+
rank(this, fn);
|
|
271
336
|
let buckets = new Map(), order = new Array(n);
|
|
272
337
|
for (let i = 0; i < n; i++) {
|
|
273
338
|
let value = before[i], bucket = buckets.get(value);
|
|
@@ -288,15 +353,21 @@ class ReactiveArray extends Array {
|
|
|
288
353
|
return this;
|
|
289
354
|
}
|
|
290
355
|
splice(start, deleteCount = this.length, ...items) {
|
|
291
|
-
let length = this.length,
|
|
292
|
-
|
|
356
|
+
let length = this.length, index = Math.trunc(start) || 0, resolved = index < 0 ? Math.max(length + index, 0) : Math.min(index, length), count = Math.min(Math.max(Math.trunc(deleteCount) || 0, 0), length - resolved), removed = [];
|
|
357
|
+
for (let i = 0; i < count; i++) {
|
|
358
|
+
if (resolved + i in this) {
|
|
359
|
+
removed[i] = this[resolved + i];
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
removed.length = count;
|
|
363
|
+
if (items.length > 0 || count > 0) {
|
|
364
|
+
place(this, resolved, count, items);
|
|
293
365
|
write(this._length, this.length);
|
|
294
|
-
for (let i = 0
|
|
366
|
+
for (let i = 0; i < count; i++) {
|
|
295
367
|
dispose(removed[i]);
|
|
296
368
|
}
|
|
297
|
-
|
|
298
|
-
this.
|
|
299
|
-
this.record({ deleteCount: removed.length, items, op: 'splice', start: resolved });
|
|
369
|
+
this.dispatch('splice', { deleteCount: count, items, start: resolved });
|
|
370
|
+
this.record({ deleteCount: count, items, op: 'splice', start: resolved });
|
|
300
371
|
}
|
|
301
372
|
return removed;
|
|
302
373
|
}
|
|
@@ -304,7 +375,8 @@ class ReactiveArray extends Array {
|
|
|
304
375
|
if (!items.length) {
|
|
305
376
|
return this.length;
|
|
306
377
|
}
|
|
307
|
-
|
|
378
|
+
place(this, 0, 0, items);
|
|
379
|
+
let length = this.length;
|
|
308
380
|
write(this._length, length);
|
|
309
381
|
this.dispatch('unshift', { items });
|
|
310
382
|
this.record({ items, op: 'unshift' });
|
package/build/system.d.ts
CHANGED
|
@@ -5,15 +5,16 @@ declare namespace computed {
|
|
|
5
5
|
var invalidate: <T>(c: Computed<T>) => void;
|
|
6
6
|
}
|
|
7
7
|
declare const dispose: <T>(computed: Computed<T>) => void;
|
|
8
|
-
declare const effect: <T>(fn: Computed<T>['fn'], apply?: (value: T, prev: T | undefined) => void) =>
|
|
8
|
+
declare const effect: <T>(fn: Computed<T>['fn'], apply?: (value: T, prev: T | undefined) => void) => Computed<T | undefined>;
|
|
9
9
|
declare const flush: () => void;
|
|
10
|
-
declare const hasOwner: () => boolean;
|
|
11
10
|
declare const isComputed: (value: unknown) => value is Computed<unknown>;
|
|
12
11
|
declare const isSignal: (value: unknown) => value is Signal<unknown>;
|
|
13
12
|
declare const onCleanup: (fn: VoidFunction) => typeof fn;
|
|
14
13
|
declare const peek: <T>(node: Signal<T> | Computed<T>) => T;
|
|
15
14
|
declare const read: <T>(node: Signal<T> | Computed<T>) => T;
|
|
16
15
|
declare const owner: () => Computed<unknown> | null;
|
|
16
|
+
declare const pause: <T>(node: Computed<T>) => void;
|
|
17
|
+
declare const resume: <T>(node: Computed<T>) => void;
|
|
17
18
|
declare const root: <T>(fn: ((dispose: VoidFunction) => T) | (() => T), parent?: Computed<unknown> | null) => T;
|
|
18
19
|
declare function signal<T>(value: T, equals?: ((a: T, b: T) => boolean) | null): Signal<T>;
|
|
19
20
|
declare namespace signal {
|
|
@@ -21,5 +22,5 @@ declare namespace signal {
|
|
|
21
22
|
}
|
|
22
23
|
declare const untrack: <T>(fn: () => T) => T;
|
|
23
24
|
declare const write: <T>(signal: Signal<T>, value: T) => void;
|
|
24
|
-
export { batch, computed, dispose, effect, flush,
|
|
25
|
+
export { batch, computed, dispose, effect, flush, isComputed, isSignal, onCleanup, owner, pause, peek, read, resume, root, signal, untrack, write };
|
|
25
26
|
export type { Computed, Signal };
|