@esportsplus/reactivity 0.41.0 → 0.43.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 +24 -16
- 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/array.d.ts +1 -1
- package/build/compiler/array.js +2 -2
- package/build/compiler/bindings.d.ts +9 -9
- package/build/compiler/bindings.js +59 -62
- package/build/compiler/index.d.ts +2 -2
- package/build/compiler/index.js +19 -13
- package/build/compiler/object.d.ts +1 -1
- package/build/compiler/object.js +19 -10
- package/build/compiler/plugins/vite.d.ts +18 -4
- package/build/compiler/primitives.d.ts +1 -1
- package/build/compiler/primitives.js +38 -20
- 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/array.ts +2 -2
- package/src/compiler/bindings.ts +82 -86
- package/src/compiler/index.ts +25 -14
- package/src/compiler/object.ts +31 -17
- package/src/compiler/primitives.ts +61 -25
- 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/compiler/compiler.test.ts +169 -156
- package/test/compiler/composition.test.ts +31 -27
- package/test/compiler/coverage.test.ts +12 -12
- package/test/compiler/origins.test.ts +18 -18
- package/test/compiler/output.test.ts +4 -4
- 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,32 +73,39 @@ 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
|
+
async function build(names: string[]): Promise<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 = await languageService.update(config, id, FILES[names[i]]);
|
|
86
|
+
|
|
87
|
+
checker ??= new Proxy(project.checker, {});
|
|
88
|
+
code.push((await coordinator.transform([pipeline], FILES[names[i]], (await project.program.getSourceFile(id))!, { checker, program: project.program }, directory, new Map())).code);
|
|
89
|
+
project.release();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return code;
|
|
82
93
|
}
|
|
83
94
|
|
|
84
95
|
|
|
85
|
-
afterAll(() => {
|
|
86
|
-
languageService.dispose();
|
|
96
|
+
afterAll(async () => {
|
|
97
|
+
await languageService.dispose();
|
|
87
98
|
rmSync(directory, { force: true, recursive: true });
|
|
88
99
|
});
|
|
89
100
|
|
|
90
101
|
|
|
91
102
|
test('compiler transform', async ({ bench }) => {
|
|
92
103
|
await bench.compare(
|
|
93
|
-
bench('every module of the app', () => {
|
|
94
|
-
|
|
95
|
-
transform(NAMES[i]);
|
|
96
|
-
}
|
|
104
|
+
bench('every module of the app', async () => {
|
|
105
|
+
await build(NAMES);
|
|
97
106
|
}),
|
|
98
|
-
bench('modules without reactive (no import, unrelated import)', () => {
|
|
99
|
-
|
|
100
|
-
transform('plain.ts');
|
|
107
|
+
bench('modules without reactive (no import, unrelated import)', async () => {
|
|
108
|
+
await build(['helpers.ts', 'plain.ts']);
|
|
101
109
|
})
|
|
102
110
|
);
|
|
103
111
|
});
|
|
@@ -105,7 +113,7 @@ test('compiler transform', async ({ bench }) => {
|
|
|
105
113
|
test('compiled output', async ({ bench }) => {
|
|
106
114
|
let file = directory + '/runtime.compiled.ts';
|
|
107
115
|
|
|
108
|
-
writeFileSync(file,
|
|
116
|
+
writeFileSync(file, (await build(['runtime.ts']))[0]);
|
|
109
117
|
|
|
110
118
|
let { run } = await import(pathToFileURL(file).href) as { run: () => number };
|
|
111
119
|
|
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
|
});
|
|
@@ -9,4 +9,4 @@ type ArrayTransformResult = {
|
|
|
9
9
|
replacements: ReplacementIntent[];
|
|
10
10
|
};
|
|
11
11
|
export default _default;
|
|
12
|
-
declare function _default(sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): ArrayTransformResult
|
|
12
|
+
declare function _default(sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): Promise<ArrayTransformResult>;
|
package/build/compiler/array.js
CHANGED
|
@@ -122,12 +122,12 @@ function write(access, ctx) {
|
|
|
122
122
|
node: unary
|
|
123
123
|
});
|
|
124
124
|
}
|
|
125
|
-
export default (sourceFile, bindings, isReactiveCall) => {
|
|
125
|
+
export default async (sourceFile, bindings, isReactiveCall) => {
|
|
126
126
|
let candidates = { lengths: [], reassignments: [], writes: [] }, result = { failures: [], namespaced: false, reactive: [], replacements: [] };
|
|
127
127
|
declare({ isReactiveCall, replacements: result.replacements, sourceFile }, sourceFile);
|
|
128
128
|
result.namespaced = result.replacements.length > 0;
|
|
129
129
|
collect(sourceFile, candidates);
|
|
130
|
-
let reactive = scope.arrays(bindings, [
|
|
130
|
+
let reactive = await scope.arrays(bindings, [
|
|
131
131
|
...candidates.lengths.map(access => access.expression),
|
|
132
132
|
...candidates.reassignments.map(assignment => assignment.left),
|
|
133
133
|
...candidates.writes.map(access => access.expression)
|
|
@@ -2,15 +2,15 @@ import { ts } from '@esportsplus/typescript';
|
|
|
2
2
|
import type { Origin } from '@esportsplus/typescript/compiler';
|
|
3
3
|
import { TYPES } from './constants.js';
|
|
4
4
|
import type { Bindings } from './types.js';
|
|
5
|
-
declare const arrays: (bindings: Bindings, nodes: ts.Node[]) => Set<ts.Node
|
|
6
|
-
declare const classify: (bindings: Bindings, call: ts.CallExpression) => TYPES | null
|
|
7
|
-
declare const create: (queries: ts.Checker, program: ts.Program, sourceFile: ts.SourceFile) => Bindings
|
|
8
|
-
declare const denotes: (bindings: Bindings, expression: ts.Expression) => boolean
|
|
9
|
-
declare const holds: (bindings: Bindings, found: Origin | null) => boolean
|
|
10
|
-
declare const isReactiveCall: (bindings: Bindings, node: ts.Node) =>
|
|
11
|
-
declare const kind: (bindings: Bindings, found: Origin | null) => TYPES | null
|
|
12
|
-
declare const origin: (bindings: Bindings, node: ts.Node) => Origin | null
|
|
13
|
-
declare const prefetch: (bindings: Bindings, calls: ts.CallExpression[]) => void
|
|
5
|
+
declare const arrays: (bindings: Bindings, nodes: ts.Node[]) => Promise<Set<ts.Node>>;
|
|
6
|
+
declare const classify: (bindings: Bindings, call: ts.CallExpression) => Promise<TYPES | null>;
|
|
7
|
+
declare const create: (queries: ts.Checker, program: ts.Program, sourceFile: ts.SourceFile) => Promise<Bindings>;
|
|
8
|
+
declare const denotes: (bindings: Bindings, expression: ts.Expression) => Promise<boolean>;
|
|
9
|
+
declare const holds: (bindings: Bindings, found: Origin | null) => Promise<boolean>;
|
|
10
|
+
declare const isReactiveCall: (bindings: Bindings, node: ts.Node) => Promise<boolean>;
|
|
11
|
+
declare const kind: (bindings: Bindings, found: Origin | null) => Promise<TYPES | null>;
|
|
12
|
+
declare const origin: (bindings: Bindings, node: ts.Node) => Promise<Origin | null>;
|
|
13
|
+
declare const prefetch: (bindings: Bindings, calls: ts.CallExpression[]) => Promise<void>;
|
|
14
14
|
declare const unwrap: (expression: ts.Expression) => ts.Expression;
|
|
15
15
|
declare const _default: {
|
|
16
16
|
arrays: typeof arrays;
|
|
@@ -32,15 +32,42 @@ 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;
|
|
39
39
|
}
|
|
40
|
-
function
|
|
41
|
-
let
|
|
42
|
-
|
|
43
|
-
let
|
|
40
|
+
async function derive(bindings, declaration, key) {
|
|
41
|
+
let node = await declaration.resolve();
|
|
42
|
+
if (node && ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
|
|
43
|
+
let initializer = node.initializer && unwrap(node.initializer);
|
|
44
|
+
if (initializer && await isReactiveCall(bindings, initializer)) {
|
|
45
|
+
return classify(bindings, initializer);
|
|
46
|
+
}
|
|
47
|
+
let candidates = assignments(bindings, node.getSourceFile()).get(node.name.text) ?? [];
|
|
48
|
+
for (let i = 0, n = candidates.length; i < n; i++) {
|
|
49
|
+
let { call, target } = candidates[i], assigned = await origin(bindings, target);
|
|
50
|
+
if (assigned && references.key(assigned.declaration) === key && await isReactiveCall(bindings, call)) {
|
|
51
|
+
let result = await classify(bindings, call);
|
|
52
|
+
if (result !== null) {
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
if (node && ts.isExportAssignment(node)) {
|
|
60
|
+
let expression = unwrap(node.expression);
|
|
61
|
+
if (await isReactiveCall(bindings, expression)) {
|
|
62
|
+
return classify(bindings, expression);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
async function kindOf(checker, type) {
|
|
68
|
+
let members = (type.flags & ts.TypeFlags.Union) !== 0 ? await type.getTypes() ?? [] : [type], kinds = await Promise.all(members.map((member) => kindOfType(checker, member))), result = null;
|
|
69
|
+
for (let i = 0, n = kinds.length; i < n; i++) {
|
|
70
|
+
let kind = kinds[i];
|
|
44
71
|
if (kind === null || (result !== null && result !== kind)) {
|
|
45
72
|
return null;
|
|
46
73
|
}
|
|
@@ -48,9 +75,9 @@ function kindOf(checker, type) {
|
|
|
48
75
|
}
|
|
49
76
|
return result;
|
|
50
77
|
}
|
|
51
|
-
function kindOfType(checker, type) {
|
|
78
|
+
async function kindOfType(checker, type) {
|
|
52
79
|
if ((type.flags & ts.TypeFlags.TypeParameter) !== 0) {
|
|
53
|
-
let constraint = checker.getBaseConstraintOfType(type);
|
|
80
|
+
let constraint = await checker.getBaseConstraintOfType(type);
|
|
54
81
|
return constraint && constraint !== type ? kindOf(checker, constraint) : null;
|
|
55
82
|
}
|
|
56
83
|
if ((type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Never | ts.TypeFlags.Unknown)) !== 0) {
|
|
@@ -68,42 +95,32 @@ function kindOfType(checker, type) {
|
|
|
68
95
|
ts.TypeFlags.Void)) !== 0) {
|
|
69
96
|
return TYPES.Signal;
|
|
70
97
|
}
|
|
71
|
-
if (checker.isArrayType(type) || checker.isTupleType(type)) {
|
|
98
|
+
if (await checker.isArrayType(type) || await checker.isTupleType(type)) {
|
|
72
99
|
return TYPES.Array;
|
|
73
100
|
}
|
|
74
|
-
if (checker.getSignaturesOfType(type, ts.SignatureKind.Call).length > 0) {
|
|
101
|
+
if ((await checker.getSignaturesOfType(type, ts.SignatureKind.Call)).length > 0) {
|
|
75
102
|
return TYPES.Computed;
|
|
76
103
|
}
|
|
77
104
|
return (type.flags & (ts.TypeFlags.Intersection | ts.TypeFlags.NonPrimitive | ts.TypeFlags.Object)) !== 0 ? TYPES.Object : null;
|
|
78
105
|
}
|
|
79
|
-
const arrays = (bindings, nodes) => {
|
|
106
|
+
const arrays = async (bindings, nodes) => {
|
|
80
107
|
let result = new Set();
|
|
81
108
|
if (nodes.length === 0) {
|
|
82
109
|
return result;
|
|
83
110
|
}
|
|
84
|
-
let files = new Set(references.exported(bindings.checker, bindings.program, PACKAGE_NAME, REACTIVE_ARRAY).map(declaration => declaration.path));
|
|
111
|
+
let files = new Set((await references.exported(bindings.checker, bindings.program, PACKAGE_NAME, REACTIVE_ARRAY)).map(declaration => declaration.path));
|
|
85
112
|
if (files.size === 0) {
|
|
86
113
|
return result;
|
|
87
114
|
}
|
|
88
|
-
let
|
|
115
|
+
let types = await bindings.checker.getTypeAtLocation(nodes), properties = await Promise.all(types.map(async (type) => type && bindings.checker.getPropertyOfType(await bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND)));
|
|
89
116
|
for (let i = 0, n = nodes.length; i < n; i++) {
|
|
90
|
-
|
|
91
|
-
if (!type) {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
let branded = cache.get(type.id);
|
|
95
|
-
if (branded === undefined) {
|
|
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) {
|
|
117
|
+
if (properties[i]?.declarations?.some(declaration => files.has(declaration.path))) {
|
|
101
118
|
result.add(nodes[i]);
|
|
102
119
|
}
|
|
103
120
|
}
|
|
104
121
|
return result;
|
|
105
122
|
};
|
|
106
|
-
const classify = (bindings, call) => {
|
|
123
|
+
const classify = async (bindings, call) => {
|
|
107
124
|
let arg = call.arguments[0];
|
|
108
125
|
if (!arg || call.arguments.length !== 1 || ts.isSpreadElement(arg)) {
|
|
109
126
|
return null;
|
|
@@ -118,15 +135,18 @@ const classify = (bindings, call) => {
|
|
|
118
135
|
if (ts.isObjectLiteralExpression(value)) {
|
|
119
136
|
return TYPES.Object;
|
|
120
137
|
}
|
|
121
|
-
let type = bindings.checker.getTypeAtLocation(arg);
|
|
138
|
+
let type = await bindings.checker.getTypeAtLocation(arg);
|
|
122
139
|
return type ? kindOf(bindings.checker, type) : null;
|
|
123
140
|
};
|
|
124
|
-
const create = (queries, program, sourceFile) => {
|
|
125
|
-
let memoized = checker.cached(queries, program)
|
|
141
|
+
const create = async (queries, program, sourceFile) => {
|
|
142
|
+
let memoized = checker.cached(queries, program), [exported, origins] = await Promise.all([
|
|
143
|
+
references.exported(memoized, program, PACKAGE_NAME, ENTRYPOINT),
|
|
144
|
+
references.origins(memoized, program, sourceFile)
|
|
145
|
+
]);
|
|
126
146
|
return {
|
|
127
147
|
checker: memoized,
|
|
128
|
-
entries: new Set(
|
|
129
|
-
origins
|
|
148
|
+
entries: new Set(exported.map(references.key)),
|
|
149
|
+
origins,
|
|
130
150
|
program,
|
|
131
151
|
sourceFile
|
|
132
152
|
};
|
|
@@ -137,52 +157,29 @@ const denotes = (bindings, expression) => {
|
|
|
137
157
|
const holds = (bindings, found) => {
|
|
138
158
|
return references.holds(bindings.checker, bindings.program, found, bindings.entries);
|
|
139
159
|
};
|
|
140
|
-
const isReactiveCall = (bindings, node) => {
|
|
160
|
+
const isReactiveCall = async (bindings, node) => {
|
|
141
161
|
return ts.isCallExpression(node) && denotes(bindings, node.expression);
|
|
142
162
|
};
|
|
143
163
|
const kind = (bindings, found) => {
|
|
144
164
|
if (!found) {
|
|
145
|
-
return null;
|
|
165
|
+
return Promise.resolve(null);
|
|
146
166
|
}
|
|
147
167
|
let declaration = found.declaration;
|
|
148
168
|
if ((declaration.kind !== ts.SyntaxKind.VariableDeclaration && declaration.kind !== ts.SyntaxKind.ExportAssignment) ||
|
|
149
169
|
declaration.path.endsWith('.d.ts')) {
|
|
150
|
-
return null;
|
|
170
|
+
return Promise.resolve(null);
|
|
151
171
|
}
|
|
152
172
|
let cache = cachesOf(bindings.program).kinds, key = references.key(declaration), known = cache.get(key);
|
|
153
|
-
if (known
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
cache.set(key, null);
|
|
157
|
-
let node = declaration.resolve(), result = null;
|
|
158
|
-
if (node && ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
|
|
159
|
-
let initializer = node.initializer && unwrap(node.initializer);
|
|
160
|
-
if (initializer && isReactiveCall(bindings, initializer)) {
|
|
161
|
-
result = classify(bindings, initializer);
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
let candidates = assignments(bindings, node.getSourceFile()).get(node.name.text) ?? [];
|
|
165
|
-
for (let i = 0, n = candidates.length; i < n && result === null; i++) {
|
|
166
|
-
let { call, target } = candidates[i], assigned = origin(bindings, target);
|
|
167
|
-
if (assigned && references.key(assigned.declaration) === key && isReactiveCall(bindings, call)) {
|
|
168
|
-
result = classify(bindings, call);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
173
|
+
if (known === undefined) {
|
|
174
|
+
known = derive(bindings, declaration, key);
|
|
175
|
+
cache.set(key, known);
|
|
172
176
|
}
|
|
173
|
-
|
|
174
|
-
let expression = unwrap(node.expression);
|
|
175
|
-
if (isReactiveCall(bindings, expression)) {
|
|
176
|
-
result = classify(bindings, expression);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
cache.set(key, result);
|
|
180
|
-
return result;
|
|
177
|
+
return known;
|
|
181
178
|
};
|
|
182
|
-
const origin = (bindings, node) => {
|
|
179
|
+
const origin = async (bindings, node) => {
|
|
183
180
|
return bindings.origins.get(node) ?? references.origin(bindings.checker, bindings.program, node);
|
|
184
181
|
};
|
|
185
|
-
const prefetch = (bindings, calls) => {
|
|
182
|
+
const prefetch = async (bindings, calls) => {
|
|
186
183
|
let nodes = [];
|
|
187
184
|
for (let i = 0, n = calls.length; i < n; i++) {
|
|
188
185
|
let arg = calls[i].arguments[0];
|
|
@@ -199,7 +196,7 @@ const prefetch = (bindings, calls) => {
|
|
|
199
196
|
nodes.push(arg);
|
|
200
197
|
}
|
|
201
198
|
if (nodes.length > 0) {
|
|
202
|
-
bindings.checker.getTypeAtLocation(nodes);
|
|
199
|
+
await bindings.checker.getTypeAtLocation(nodes);
|
|
203
200
|
}
|
|
204
201
|
};
|
|
205
202
|
const unwrap = (expression) => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ImportIntent, ReplacementIntent, TransformContext } from '@esportsplus/typescript/compiler';
|
|
2
2
|
declare const _default: {
|
|
3
3
|
name: string;
|
|
4
|
-
transform: (ctx: TransformContext) => {
|
|
4
|
+
transform: (ctx: TransformContext) => Promise<{
|
|
5
5
|
dependencies?: undefined;
|
|
6
6
|
} | {
|
|
7
7
|
dependencies: string[];
|
|
@@ -10,6 +10,6 @@ declare const _default: {
|
|
|
10
10
|
prepend: string[];
|
|
11
11
|
replacements: ReplacementIntent[];
|
|
12
12
|
dependencies: string[];
|
|
13
|
-
}
|
|
13
|
+
}>;
|
|
14
14
|
};
|
|
15
15
|
export default _default;
|
package/build/compiler/index.js
CHANGED
|
@@ -50,15 +50,17 @@ function siteOf(identifier) {
|
|
|
50
50
|
}
|
|
51
51
|
export default {
|
|
52
52
|
name: PACKAGE_NAME,
|
|
53
|
-
transform: (ctx) => {
|
|
53
|
+
transform: async (ctx) => {
|
|
54
54
|
if (!REGEX_IMPORT.test(ctx.code)) {
|
|
55
55
|
return {};
|
|
56
56
|
}
|
|
57
|
-
let { checker, program, sourceFile } = ctx, bindings = scope.create(checker, program, sourceFile), calls = new Set(), dependencies = new Set(), entries = new Set(), failures = [], self = sourceFile.fileName.toLowerCase();
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
let { checker, program, sourceFile } = ctx, bindings = await scope.create(checker, program, sourceFile), calls = new Set(), dependencies = new Set(), entries = new Set(), failures = [], origins = [...bindings.origins], self = sourceFile.fileName.toLowerCase();
|
|
58
|
+
let held = await Promise.all(origins.map(([, found]) => scope.holds(bindings, found)));
|
|
59
|
+
for (let i = 0, n = origins.length; i < n; i++) {
|
|
60
|
+
if (!held[i]) {
|
|
60
61
|
continue;
|
|
61
62
|
}
|
|
63
|
+
let identifier = origins[i][0];
|
|
62
64
|
entries.add(identifier);
|
|
63
65
|
let site = siteOf(identifier), parent = site.parent;
|
|
64
66
|
while (parent && ts.isParenthesizedExpression(parent)) {
|
|
@@ -74,9 +76,11 @@ export default {
|
|
|
74
76
|
});
|
|
75
77
|
}
|
|
76
78
|
}
|
|
77
|
-
scope.prefetch(bindings, [...calls]);
|
|
78
|
-
|
|
79
|
-
|
|
79
|
+
await scope.prefetch(bindings, [...calls]);
|
|
80
|
+
let kinds = await Promise.all(origins.map(async ([identifier, found]) => entries.has(identifier) ? null : scope.kind(bindings, found)));
|
|
81
|
+
for (let i = 0, n = origins.length; i < n; i++) {
|
|
82
|
+
let [identifier, found] = origins[i];
|
|
83
|
+
if (!entries.has(identifier) && kinds[i] === null) {
|
|
80
84
|
continue;
|
|
81
85
|
}
|
|
82
86
|
for (let file of [...found.through, found.declaration.path]) {
|
|
@@ -90,21 +94,23 @@ export default {
|
|
|
90
94
|
prepend: [],
|
|
91
95
|
replacements: []
|
|
92
96
|
};
|
|
93
|
-
let lowered = primitives(sourceFile, bindings, isReactiveCall);
|
|
97
|
+
let lowered = await primitives(sourceFile, bindings, isReactiveCall);
|
|
94
98
|
failures.push(...lowered.failures);
|
|
95
99
|
intents.replacements.push(...lowered.replacements);
|
|
96
|
-
let objects = object(sourceFile, bindings, isReactiveCall);
|
|
100
|
+
let objects = await object(sourceFile, bindings, isReactiveCall);
|
|
97
101
|
intents.prepend.push(...objects.prepend);
|
|
98
102
|
intents.replacements.push(...objects.replacements);
|
|
99
|
-
let arrays = array(sourceFile, bindings, isReactiveCall);
|
|
103
|
+
let arrays = await array(sourceFile, bindings, isReactiveCall);
|
|
100
104
|
failures.push(...arrays.failures);
|
|
101
105
|
intents.replacements.push(...arrays.replacements);
|
|
102
|
-
|
|
103
|
-
let root = arrays.reactive[i];
|
|
106
|
+
let declared = await Promise.all(arrays.reactive.map(async (root) => {
|
|
104
107
|
while (ts.isPropertyAccessExpression(root) || ts.isElementAccessExpression(root) || ts.isParenthesizedExpression(root)) {
|
|
105
108
|
root = root.expression;
|
|
106
109
|
}
|
|
107
|
-
|
|
110
|
+
return ts.isIdentifier(root) ? scope.origin(bindings, root) : null;
|
|
111
|
+
}));
|
|
112
|
+
for (let i = 0, n = declared.length; i < n; i++) {
|
|
113
|
+
let found = declared[i];
|
|
108
114
|
if (found && found.declaration.path.toLowerCase() !== self) {
|
|
109
115
|
dependencies.add(found.declaration.path);
|
|
110
116
|
}
|
|
@@ -6,4 +6,4 @@ type ObjectTransformResult = {
|
|
|
6
6
|
replacements: ReplacementIntent[];
|
|
7
7
|
};
|
|
8
8
|
export default _default;
|
|
9
|
-
declare function _default(sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): ObjectTransformResult
|
|
9
|
+
declare function _default(sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): Promise<ObjectTransformResult>;
|
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;
|
|
@@ -174,14 +174,23 @@ function visit(ctx, node) {
|
|
|
174
174
|
}
|
|
175
175
|
node.forEachChild(n => visit(ctx, n));
|
|
176
176
|
}
|
|
177
|
-
export default (sourceFile, bindings, isReactiveCall) => {
|
|
177
|
+
export default async (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 = await bindings.checker.getTypeAtLocation(ctx.typed.map(property => property.value)), arrays = await Promise.all(types.map(async (type) => !!type && (await bindings.checker.isArrayType(type) || await bindings.checker.isTupleType(type))));
|
|
187
|
+
for (let i = 0, n = ctx.typed.length; i < n; i++) {
|
|
188
|
+
if (arrays[i]) {
|
|
189
|
+
ctx.typed[i].isStatic = false;
|
|
190
|
+
ctx.typed[i].type = TYPES.Array;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
185
194
|
let prepend = [], replacements = [];
|
|
186
195
|
for (let i = 0, n = ctx.calls.length; i < n; i++) {
|
|
187
196
|
let call = ctx.calls[i], typehint = call.typehint;
|