@esportsplus/reactivity 0.42.0 → 0.43.1
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/bench/compiler.bench.ts +11 -10
- 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 +58 -56
- 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 +3 -4
- package/build/compiler/plugins/vite.d.ts +2 -2
- package/build/compiler/primitives.d.ts +1 -1
- package/build/compiler/primitives.js +38 -20
- package/build/system.js +2 -1
- package/package.json +2 -2
- package/src/compiler/array.ts +2 -2
- package/src/compiler/bindings.ts +80 -74
- package/src/compiler/index.ts +25 -14
- package/src/compiler/object.ts +4 -5
- package/src/compiler/primitives.ts +61 -25
- package/src/system.ts +6 -2
- 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/effects.test.ts +68 -0
package/src/compiler/bindings.ts
CHANGED
|
@@ -8,10 +8,11 @@ import type { Bindings } from './types';
|
|
|
8
8
|
type Assignment = { call: ts.CallExpression; target: ts.Identifier };
|
|
9
9
|
|
|
10
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
|
|
11
|
+
// makes), but kind() runs on every read of a binding and re-deriving it per read dominates a warm
|
|
12
|
+
// transform; concurrent transforms reading one binding await the one derivation
|
|
12
13
|
type Caches = {
|
|
13
14
|
assignments: Map<string, Map<string, Assignment[]>>;
|
|
14
|
-
kinds: Map<string, TYPES | null
|
|
15
|
+
kinds: Map<string, Promise<TYPES | null>>;
|
|
15
16
|
};
|
|
16
17
|
|
|
17
18
|
|
|
@@ -74,14 +75,55 @@ function cachesOf(program: ts.Program): Caches {
|
|
|
74
75
|
return found;
|
|
75
76
|
}
|
|
76
77
|
|
|
78
|
+
// Kind of the binding a declaration introduces (see kind())
|
|
79
|
+
async function derive(bindings: Bindings, declaration: Origin['declaration'], key: string): Promise<TYPES | null> {
|
|
80
|
+
let node = await declaration.resolve() as ts.Node | undefined;
|
|
81
|
+
|
|
82
|
+
if (node && ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
|
|
83
|
+
let initializer = node.initializer && unwrap(node.initializer);
|
|
84
|
+
|
|
85
|
+
if (initializer && await isReactiveCall(bindings, initializer)) {
|
|
86
|
+
return classify(bindings, initializer as ts.CallExpression);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let candidates = assignments(bindings, node.getSourceFile()).get(node.name.text) ?? [];
|
|
90
|
+
|
|
91
|
+
for (let i = 0, n = candidates.length; i < n; i++) {
|
|
92
|
+
let { call, target } = candidates[i],
|
|
93
|
+
assigned = await origin(bindings, target);
|
|
94
|
+
|
|
95
|
+
if (assigned && references.key(assigned.declaration) === key && await isReactiveCall(bindings, call)) {
|
|
96
|
+
let result = await classify(bindings, call);
|
|
97
|
+
|
|
98
|
+
if (result !== null) {
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (node && ts.isExportAssignment(node)) {
|
|
108
|
+
let expression = unwrap(node.expression);
|
|
109
|
+
|
|
110
|
+
if (await isReactiveCall(bindings, expression)) {
|
|
111
|
+
return classify(bindings, expression as ts.CallExpression);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
77
118
|
// Every member of a union must agree: `number | undefined` is a signal, `number[] | undefined` is
|
|
78
119
|
// neither a signal nor an array at runtime
|
|
79
|
-
function kindOf(checker: ts.Checker, type: ts.Type): TYPES | null {
|
|
80
|
-
let members = (type.flags & ts.TypeFlags.Union) !== 0 ? (type as ts.UnionType).getTypes() : [type],
|
|
120
|
+
async function kindOf(checker: ts.Checker, type: ts.Type): Promise<TYPES | null> {
|
|
121
|
+
let members = (type.flags & ts.TypeFlags.Union) !== 0 ? await (type as ts.UnionType).getTypes() ?? [] : [type],
|
|
122
|
+
kinds = await Promise.all(members.map((member) => kindOfType(checker, member))),
|
|
81
123
|
result: TYPES | null = null;
|
|
82
124
|
|
|
83
|
-
for (let i = 0, n =
|
|
84
|
-
let kind =
|
|
125
|
+
for (let i = 0, n = kinds.length; i < n; i++) {
|
|
126
|
+
let kind = kinds[i];
|
|
85
127
|
|
|
86
128
|
if (kind === null || (result !== null && result !== kind)) {
|
|
87
129
|
return null;
|
|
@@ -94,9 +136,9 @@ function kindOf(checker: ts.Checker, type: ts.Type): TYPES | null {
|
|
|
94
136
|
}
|
|
95
137
|
|
|
96
138
|
// Kind of one non-union type as a reactive() argument; null when the type cannot say
|
|
97
|
-
function kindOfType(checker: ts.Checker, type: ts.Type): TYPES | null {
|
|
139
|
+
async function kindOfType(checker: ts.Checker, type: ts.Type): Promise<TYPES | null> {
|
|
98
140
|
if ((type.flags & ts.TypeFlags.TypeParameter) !== 0) {
|
|
99
|
-
let constraint = checker.getBaseConstraintOfType(type);
|
|
141
|
+
let constraint = await checker.getBaseConstraintOfType(type);
|
|
100
142
|
|
|
101
143
|
return constraint && constraint !== type ? kindOf(checker, constraint) : null;
|
|
102
144
|
}
|
|
@@ -120,11 +162,11 @@ function kindOfType(checker: ts.Checker, type: ts.Type): TYPES | null {
|
|
|
120
162
|
return TYPES.Signal;
|
|
121
163
|
}
|
|
122
164
|
|
|
123
|
-
if (checker.isArrayType(type) || checker.isTupleType(type)) {
|
|
165
|
+
if (await checker.isArrayType(type) || await checker.isTupleType(type)) {
|
|
124
166
|
return TYPES.Array;
|
|
125
167
|
}
|
|
126
168
|
|
|
127
|
-
if (checker.getSignaturesOfType(type, ts.SignatureKind.Call).length > 0) {
|
|
169
|
+
if ((await checker.getSignaturesOfType(type, ts.SignatureKind.Call)).length > 0) {
|
|
128
170
|
return TYPES.Computed;
|
|
129
171
|
}
|
|
130
172
|
|
|
@@ -136,31 +178,24 @@ function kindOfType(checker: ts.Checker, type: ts.Type): TYPES | null {
|
|
|
136
178
|
// Expressions whose type is a ReactiveArray (`Reactive<T[]>` carries the class's members), resolved
|
|
137
179
|
// in one checker round-trip. Found by type, a reactive array is recognized wherever it flows:
|
|
138
180
|
// imports, object properties, parameters, return values.
|
|
139
|
-
const arrays = (bindings: Bindings, nodes: ts.Node[]): Set<ts.Node
|
|
181
|
+
const arrays = async (bindings: Bindings, nodes: ts.Node[]): Promise<Set<ts.Node>> => {
|
|
140
182
|
let result = new Set<ts.Node>();
|
|
141
183
|
|
|
142
184
|
if (nodes.length === 0) {
|
|
143
185
|
return result;
|
|
144
186
|
}
|
|
145
187
|
|
|
146
|
-
let files = new Set(references.exported(bindings.checker, bindings.program, PACKAGE_NAME, REACTIVE_ARRAY).map(declaration => declaration.path));
|
|
188
|
+
let files = new Set((await references.exported(bindings.checker, bindings.program, PACKAGE_NAME, REACTIVE_ARRAY)).map(declaration => declaration.path));
|
|
147
189
|
|
|
148
190
|
if (files.size === 0) {
|
|
149
191
|
return result;
|
|
150
192
|
}
|
|
151
193
|
|
|
152
|
-
let types = bindings.checker.getTypeAtLocation(nodes)
|
|
194
|
+
let types = await bindings.checker.getTypeAtLocation(nodes),
|
|
195
|
+
properties = await Promise.all(types.map(async (type) => type && bindings.checker.getPropertyOfType(await bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND)));
|
|
153
196
|
|
|
154
197
|
for (let i = 0, n = nodes.length; i < n; i++) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (!type) {
|
|
158
|
-
continue;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
let property = bindings.checker.getPropertyOfType(bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND);
|
|
162
|
-
|
|
163
|
-
if (property?.declarations?.some(declaration => files.has(declaration.path))) {
|
|
198
|
+
if (properties[i]?.declarations?.some(declaration => files.has(declaration.path))) {
|
|
164
199
|
result.add(nodes[i]);
|
|
165
200
|
}
|
|
166
201
|
}
|
|
@@ -170,7 +205,7 @@ const arrays = (bindings: Bindings, nodes: ts.Node[]): Set<ts.Node> => {
|
|
|
170
205
|
|
|
171
206
|
// Kind of a reactive() call from its argument: literal syntax first, the argument's type otherwise.
|
|
172
207
|
// Null when no kind is provable (no/extra arguments, `any`, `unknown`, a mixed union).
|
|
173
|
-
const classify = (bindings: Bindings, call: ts.CallExpression): TYPES | null => {
|
|
208
|
+
const classify = async (bindings: Bindings, call: ts.CallExpression): Promise<TYPES | null> => {
|
|
174
209
|
let arg = call.arguments[0];
|
|
175
210
|
|
|
176
211
|
if (!arg || call.arguments.length !== 1 || ts.isSpreadElement(arg)) {
|
|
@@ -191,46 +226,50 @@ const classify = (bindings: Bindings, call: ts.CallExpression): TYPES | null =>
|
|
|
191
226
|
return TYPES.Object;
|
|
192
227
|
}
|
|
193
228
|
|
|
194
|
-
let type = bindings.checker.getTypeAtLocation(arg);
|
|
229
|
+
let type = await bindings.checker.getTypeAtLocation(arg);
|
|
195
230
|
|
|
196
231
|
return type ? kindOf(bindings.checker, type) : null;
|
|
197
232
|
};
|
|
198
233
|
|
|
199
234
|
// Memoized per program (an already memoized checker is kept as is), so batched and repeated queries
|
|
200
235
|
// share one cache whoever hands the checker in
|
|
201
|
-
const create = (queries: ts.Checker, program: ts.Program, sourceFile: ts.SourceFile): Bindings => {
|
|
202
|
-
let memoized = checker.cached(queries, program)
|
|
236
|
+
const create = async (queries: ts.Checker, program: ts.Program, sourceFile: ts.SourceFile): Promise<Bindings> => {
|
|
237
|
+
let memoized = checker.cached(queries, program),
|
|
238
|
+
[exported, origins] = await Promise.all([
|
|
239
|
+
references.exported(memoized, program, PACKAGE_NAME, ENTRYPOINT),
|
|
240
|
+
references.origins(memoized, program, sourceFile)
|
|
241
|
+
]);
|
|
203
242
|
|
|
204
243
|
return {
|
|
205
244
|
checker: memoized,
|
|
206
|
-
entries: new Set(
|
|
207
|
-
origins
|
|
245
|
+
entries: new Set(exported.map(references.key)),
|
|
246
|
+
origins,
|
|
208
247
|
program,
|
|
209
248
|
sourceFile
|
|
210
249
|
};
|
|
211
250
|
};
|
|
212
251
|
|
|
213
252
|
// Whether a callee expression denotes `reactive`: `reactive`, an alias, `ns.reactive`, `ns['reactive']`
|
|
214
|
-
const denotes = (bindings: Bindings, expression: ts.Expression): boolean => {
|
|
253
|
+
const denotes = (bindings: Bindings, expression: ts.Expression): Promise<boolean> => {
|
|
215
254
|
return references.denotes(bindings.checker, bindings.program, expression, bindings.entries);
|
|
216
255
|
};
|
|
217
256
|
|
|
218
257
|
// Whether a value holds `reactive`: the export itself, reached through any import, re-export or
|
|
219
258
|
// namespace, or a const binding (`const r = reactive`, `const { reactive: r } = ns`) holding it
|
|
220
|
-
const holds = (bindings: Bindings, found: Origin | null): boolean => {
|
|
259
|
+
const holds = (bindings: Bindings, found: Origin | null): Promise<boolean> => {
|
|
221
260
|
return references.holds(bindings.checker, bindings.program, found, bindings.entries);
|
|
222
261
|
};
|
|
223
262
|
|
|
224
|
-
const isReactiveCall = (bindings: Bindings, node: ts.Node):
|
|
263
|
+
const isReactiveCall = async (bindings: Bindings, node: ts.Node): Promise<boolean> => {
|
|
225
264
|
return ts.isCallExpression(node) && denotes(bindings, node.expression);
|
|
226
265
|
};
|
|
227
266
|
|
|
228
267
|
// Reactive kind of the binding a declaration introduces, wherever it is read from: a variable
|
|
229
268
|
// initialized (or assigned) with reactive(), or a module's `export default reactive(...)`.
|
|
230
269
|
// Ambient declarations (.d.ts) never are: a published package's signals ship as Signal-typed.
|
|
231
|
-
const kind = (bindings: Bindings, found: Origin | null): TYPES | null => {
|
|
270
|
+
const kind = (bindings: Bindings, found: Origin | null): Promise<TYPES | null> => {
|
|
232
271
|
if (!found) {
|
|
233
|
-
return null;
|
|
272
|
+
return Promise.resolve(null);
|
|
234
273
|
}
|
|
235
274
|
|
|
236
275
|
let declaration = found.declaration;
|
|
@@ -239,61 +278,28 @@ const kind = (bindings: Bindings, found: Origin | null): TYPES | null => {
|
|
|
239
278
|
(declaration.kind !== ts.SyntaxKind.VariableDeclaration && declaration.kind !== ts.SyntaxKind.ExportAssignment) ||
|
|
240
279
|
declaration.path.endsWith('.d.ts')
|
|
241
280
|
) {
|
|
242
|
-
return null;
|
|
281
|
+
return Promise.resolve(null);
|
|
243
282
|
}
|
|
244
283
|
|
|
245
284
|
let cache = cachesOf(bindings.program).kinds,
|
|
246
285
|
key = references.key(declaration),
|
|
247
286
|
known = cache.get(key);
|
|
248
287
|
|
|
249
|
-
if (known
|
|
250
|
-
|
|
288
|
+
if (known === undefined) {
|
|
289
|
+
known = derive(bindings, declaration, key);
|
|
290
|
+
cache.set(key, known);
|
|
251
291
|
}
|
|
252
292
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
let node = declaration.resolve() as ts.Node | undefined,
|
|
256
|
-
result: TYPES | null = null;
|
|
257
|
-
|
|
258
|
-
if (node && ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
|
|
259
|
-
let initializer = node.initializer && unwrap(node.initializer);
|
|
260
|
-
|
|
261
|
-
if (initializer && isReactiveCall(bindings, initializer)) {
|
|
262
|
-
result = classify(bindings, initializer);
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
let candidates = assignments(bindings, node.getSourceFile()).get(node.name.text) ?? [];
|
|
266
|
-
|
|
267
|
-
for (let i = 0, n = candidates.length; i < n && result === null; i++) {
|
|
268
|
-
let { call, target } = candidates[i],
|
|
269
|
-
assigned = origin(bindings, target);
|
|
270
|
-
|
|
271
|
-
if (assigned && references.key(assigned.declaration) === key && isReactiveCall(bindings, call)) {
|
|
272
|
-
result = classify(bindings, call);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
else if (node && ts.isExportAssignment(node)) {
|
|
278
|
-
let expression = unwrap(node.expression);
|
|
279
|
-
|
|
280
|
-
if (isReactiveCall(bindings, expression)) {
|
|
281
|
-
result = classify(bindings, expression);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
cache.set(key, result);
|
|
286
|
-
|
|
287
|
-
return result;
|
|
293
|
+
return known;
|
|
288
294
|
};
|
|
289
295
|
|
|
290
|
-
const origin = (bindings: Bindings, node: ts.Node): Origin | null => {
|
|
296
|
+
const origin = async (bindings: Bindings, node: ts.Node): Promise<Origin | null> => {
|
|
291
297
|
return bindings.origins.get(node) ?? references.origin(bindings.checker, bindings.program, node);
|
|
292
298
|
};
|
|
293
299
|
|
|
294
300
|
// Types of the arguments classify() reads from the checker, requested in one round-trip; the
|
|
295
301
|
// memoized checker then answers each classify() from that request
|
|
296
|
-
const prefetch = (bindings: Bindings, calls: ts.CallExpression[]): void => {
|
|
302
|
+
const prefetch = async (bindings: Bindings, calls: ts.CallExpression[]): Promise<void> => {
|
|
297
303
|
let nodes: ts.Node[] = [];
|
|
298
304
|
|
|
299
305
|
for (let i = 0, n = calls.length; i < n; i++) {
|
|
@@ -318,7 +324,7 @@ const prefetch = (bindings: Bindings, calls: ts.CallExpression[]): void => {
|
|
|
318
324
|
}
|
|
319
325
|
|
|
320
326
|
if (nodes.length > 0) {
|
|
321
|
-
bindings.checker.getTypeAtLocation(nodes);
|
|
327
|
+
await bindings.checker.getTypeAtLocation(nodes);
|
|
322
328
|
}
|
|
323
329
|
};
|
|
324
330
|
|
package/src/compiler/index.ts
CHANGED
|
@@ -90,24 +90,29 @@ function siteOf(identifier: ts.Node): ts.Node {
|
|
|
90
90
|
// resolved against the program and compiled exactly where its values resolve to reactive declarations
|
|
91
91
|
export default {
|
|
92
92
|
name: PACKAGE_NAME,
|
|
93
|
-
transform: (ctx: TransformContext) => {
|
|
93
|
+
transform: async (ctx: TransformContext) => {
|
|
94
94
|
if (!REGEX_IMPORT.test(ctx.code)) {
|
|
95
95
|
return {};
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
let { checker, program, sourceFile } = ctx,
|
|
99
|
-
bindings = scope.create(checker, program, sourceFile),
|
|
99
|
+
bindings = await scope.create(checker, program, sourceFile),
|
|
100
100
|
calls = new Set<ts.CallExpression>(),
|
|
101
101
|
dependencies = new Set<string>(),
|
|
102
102
|
entries = new Set<ts.Node>(),
|
|
103
103
|
failures: Failure[] = [],
|
|
104
|
+
origins = [...bindings.origins],
|
|
104
105
|
self = sourceFile.fileName.toLowerCase();
|
|
105
106
|
|
|
106
|
-
|
|
107
|
-
|
|
107
|
+
let held = await Promise.all(origins.map(([, found]) => scope.holds(bindings, found)));
|
|
108
|
+
|
|
109
|
+
for (let i = 0, n = origins.length; i < n; i++) {
|
|
110
|
+
if (!held[i]) {
|
|
108
111
|
continue;
|
|
109
112
|
}
|
|
110
113
|
|
|
114
|
+
let identifier = origins[i][0];
|
|
115
|
+
|
|
111
116
|
entries.add(identifier);
|
|
112
117
|
|
|
113
118
|
let site = siteOf(identifier),
|
|
@@ -129,10 +134,14 @@ export default {
|
|
|
129
134
|
}
|
|
130
135
|
|
|
131
136
|
// Every call is known before any binding is classified, so their argument types arrive in one request
|
|
132
|
-
scope.prefetch(bindings, [...calls]);
|
|
137
|
+
await scope.prefetch(bindings, [...calls]);
|
|
138
|
+
|
|
139
|
+
let kinds = await Promise.all(origins.map(async ([identifier, found]) => entries.has(identifier) ? null : scope.kind(bindings, found)));
|
|
133
140
|
|
|
134
|
-
for (let
|
|
135
|
-
|
|
141
|
+
for (let i = 0, n = origins.length; i < n; i++) {
|
|
142
|
+
let [identifier, found] = origins[i];
|
|
143
|
+
|
|
144
|
+
if (!entries.has(identifier) && kinds[i] === null) {
|
|
136
145
|
continue;
|
|
137
146
|
}
|
|
138
147
|
|
|
@@ -151,30 +160,32 @@ export default {
|
|
|
151
160
|
};
|
|
152
161
|
|
|
153
162
|
// Signals and computeds first: every call is classified and every binding read is rewritten
|
|
154
|
-
let lowered = primitives(sourceFile, bindings, isReactiveCall);
|
|
163
|
+
let lowered = await primitives(sourceFile, bindings, isReactiveCall);
|
|
155
164
|
|
|
156
165
|
failures.push(...lowered.failures);
|
|
157
166
|
intents.replacements.push(...lowered.replacements);
|
|
158
167
|
|
|
159
|
-
let objects = object(sourceFile, bindings, isReactiveCall);
|
|
168
|
+
let objects = await object(sourceFile, bindings, isReactiveCall);
|
|
160
169
|
|
|
161
170
|
intents.prepend.push(...objects.prepend);
|
|
162
171
|
intents.replacements.push(...objects.replacements);
|
|
163
172
|
|
|
164
|
-
let arrays = array(sourceFile, bindings, isReactiveCall);
|
|
173
|
+
let arrays = await array(sourceFile, bindings, isReactiveCall);
|
|
165
174
|
|
|
166
175
|
failures.push(...arrays.failures);
|
|
167
176
|
intents.replacements.push(...arrays.replacements);
|
|
168
177
|
|
|
169
178
|
// An array is reactive because of where it was declared; that module shapes this compile
|
|
170
|
-
|
|
171
|
-
let root = arrays.reactive[i];
|
|
172
|
-
|
|
179
|
+
let declared = await Promise.all(arrays.reactive.map(async (root) => {
|
|
173
180
|
while (ts.isPropertyAccessExpression(root) || ts.isElementAccessExpression(root) || ts.isParenthesizedExpression(root)) {
|
|
174
181
|
root = root.expression;
|
|
175
182
|
}
|
|
176
183
|
|
|
177
|
-
|
|
184
|
+
return ts.isIdentifier(root) ? scope.origin(bindings, root) : null;
|
|
185
|
+
}));
|
|
186
|
+
|
|
187
|
+
for (let i = 0, n = declared.length; i < n; i++) {
|
|
188
|
+
let found = declared[i];
|
|
178
189
|
|
|
179
190
|
if (found && found.declaration.path.toLowerCase() !== self) {
|
|
180
191
|
dependencies.add(found.declaration.path);
|
package/src/compiler/object.ts
CHANGED
|
@@ -267,7 +267,7 @@ function visit(ctx: VisitContext, node: ts.Node): void {
|
|
|
267
267
|
}
|
|
268
268
|
|
|
269
269
|
|
|
270
|
-
export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): ObjectTransformResult => {
|
|
270
|
+
export default async (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): Promise<ObjectTransformResult> => {
|
|
271
271
|
let ctx: VisitContext = {
|
|
272
272
|
calls: [],
|
|
273
273
|
isReactiveCall,
|
|
@@ -280,12 +280,11 @@ export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: I
|
|
|
280
280
|
// A non-literal array is typed Reactive<T[]> on the object, so it must become a ReactiveArray
|
|
281
281
|
// exactly like a literal one; every such value in the file is typed in one request
|
|
282
282
|
if (ctx.typed.length > 0) {
|
|
283
|
-
let types = bindings.checker.getTypeAtLocation(ctx.typed.map(property => property.value))
|
|
283
|
+
let types = await bindings.checker.getTypeAtLocation(ctx.typed.map(property => property.value)),
|
|
284
|
+
arrays = await Promise.all(types.map(async (type) => !!type && (await bindings.checker.isArrayType(type) || await bindings.checker.isTupleType(type))));
|
|
284
285
|
|
|
285
286
|
for (let i = 0, n = ctx.typed.length; i < n; i++) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
if (type && (bindings.checker.isArrayType(type) || bindings.checker.isTupleType(type))) {
|
|
287
|
+
if (arrays[i]) {
|
|
289
288
|
ctx.typed[i].isStatic = false;
|
|
290
289
|
ctx.typed[i].type = TYPES.Array;
|
|
291
290
|
}
|
|
@@ -23,14 +23,35 @@ type TransformContext = {
|
|
|
23
23
|
tmpCounter: number;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
+
// A binding element of a pattern destructuring `initializer`
|
|
27
|
+
type Unpacked = {
|
|
28
|
+
element: ts.BindingElement;
|
|
29
|
+
initializer: ts.Expression;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
// Every reactive() call, in source order
|
|
34
|
+
function calls(ctx: TransformContext, node: ts.Node, found: ts.CallExpression[]): void {
|
|
35
|
+
if (ctx.isReactiveCall(node)) {
|
|
36
|
+
found.push(node);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
node.forEachChild(child => calls(ctx, child, found));
|
|
40
|
+
}
|
|
26
41
|
|
|
27
42
|
// Pass 1: lower every reactive() call. A signal or computed is only ever reached through the
|
|
28
43
|
// variable it initializes (every read of that variable is rewritten), so anywhere else its signal
|
|
29
44
|
// would leak unread and the call is rejected.
|
|
30
|
-
function declare(ctx: TransformContext,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
45
|
+
async function declare(ctx: TransformContext, sourceFile: ts.SourceFile): Promise<void> {
|
|
46
|
+
let found: ts.CallExpression[] = [];
|
|
47
|
+
|
|
48
|
+
calls(ctx, sourceFile, found);
|
|
49
|
+
|
|
50
|
+
let kinds = await Promise.all(found.map((call) => scope.classify(ctx.bindings, call)));
|
|
51
|
+
|
|
52
|
+
for (let i = 0, n = found.length; i < n; i++) {
|
|
53
|
+
let call = found[i],
|
|
54
|
+
kind = kinds[i];
|
|
34
55
|
|
|
35
56
|
ctx.calls.push(call);
|
|
36
57
|
|
|
@@ -68,8 +89,6 @@ function declare(ctx: TransformContext, node: ts.Node): void {
|
|
|
68
89
|
}
|
|
69
90
|
}
|
|
70
91
|
}
|
|
71
|
-
|
|
72
|
-
node.forEachChild(child => declare(ctx, child));
|
|
73
92
|
}
|
|
74
93
|
|
|
75
94
|
// `let x = reactive(...)`, `x = reactive(...)` or `export default reactive(...)`
|
|
@@ -101,7 +120,7 @@ function isBound(call: ts.CallExpression): boolean {
|
|
|
101
120
|
}
|
|
102
121
|
|
|
103
122
|
// A module namespace object: `import * as ns` (or an alias of it) or an `await import(...)`
|
|
104
|
-
function isNamespace(bindings: Bindings, expression: ts.Expression): boolean {
|
|
123
|
+
async function isNamespace(bindings: Bindings, expression: ts.Expression): Promise<boolean> {
|
|
105
124
|
let value = scope.unwrap(expression);
|
|
106
125
|
|
|
107
126
|
if (ts.isAwaitExpression(value)) {
|
|
@@ -112,7 +131,7 @@ function isNamespace(bindings: Bindings, expression: ts.Expression): boolean {
|
|
|
112
131
|
return value.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
113
132
|
}
|
|
114
133
|
|
|
115
|
-
return ts.isIdentifier(value) && scope.origin(bindings, value)?.declaration.kind === ts.SyntaxKind.SourceFile;
|
|
134
|
+
return ts.isIdentifier(value) && (await scope.origin(bindings, value))?.declaration.kind === ts.SyntaxKind.SourceFile;
|
|
116
135
|
}
|
|
117
136
|
|
|
118
137
|
// Destructuring assignment targets (`({ count } = o)`, `[count] = xs`) write through a pattern
|
|
@@ -270,11 +289,14 @@ function reference(ctx: TransformContext, node: ts.Expression, kind: TYPES): voi
|
|
|
270
289
|
// Pass 2: rewrite every read and write of a signal or computed binding, resolved by declaration so
|
|
271
290
|
// a binding imported from another module (named, default, namespace, through a barrel) compiles
|
|
272
291
|
// exactly like a local one. Destructuring one out of a module namespace would copy its signal.
|
|
273
|
-
function rewrite(ctx: TransformContext): void {
|
|
274
|
-
let { bindings } = ctx
|
|
292
|
+
async function rewrite(ctx: TransformContext): Promise<void> {
|
|
293
|
+
let { bindings } = ctx,
|
|
294
|
+
origins = [...bindings.origins],
|
|
295
|
+
kinds = await Promise.all(origins.map(([, found]) => scope.kind(bindings, found)));
|
|
275
296
|
|
|
276
|
-
for (let
|
|
277
|
-
let
|
|
297
|
+
for (let i = 0, n = origins.length; i < n; i++) {
|
|
298
|
+
let [identifier] = origins[i],
|
|
299
|
+
kind = kinds[i];
|
|
278
300
|
|
|
279
301
|
if (kind !== TYPES.Signal && kind !== TYPES.Computed) {
|
|
280
302
|
continue;
|
|
@@ -290,29 +312,43 @@ function rewrite(ctx: TransformContext): void {
|
|
|
290
312
|
reference(ctx, readNode(identifier), kind);
|
|
291
313
|
}
|
|
292
314
|
|
|
293
|
-
|
|
315
|
+
let elements: Unpacked[] = [];
|
|
316
|
+
|
|
317
|
+
unpacked(bindings.sourceFile, elements);
|
|
318
|
+
|
|
319
|
+
let copied = await Promise.all(elements.map(async ({ element, initializer }) => {
|
|
320
|
+
if (!(await isNamespace(bindings, initializer))) {
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
let kind = await scope.kind(bindings, await scope.origin(bindings, element));
|
|
325
|
+
|
|
326
|
+
return kind === TYPES.Signal || kind === TYPES.Computed;
|
|
327
|
+
}));
|
|
328
|
+
|
|
329
|
+
for (let i = 0, n = elements.length; i < n; i++) {
|
|
330
|
+
if (copied[i]) {
|
|
331
|
+
ctx.failures.push({ message: 'a reactive binding cannot be destructured (it would copy its signal); read it through the namespace', node: elements[i].element });
|
|
332
|
+
}
|
|
333
|
+
}
|
|
294
334
|
}
|
|
295
335
|
|
|
296
336
|
// `const { count } = ns` names no property to resolve, so each element of a pattern destructuring
|
|
297
|
-
// a module namespace is resolved as the export it copies
|
|
298
|
-
function unpacked(
|
|
337
|
+
// an initializer (a module namespace, once resolved) is resolved as the export it copies
|
|
338
|
+
function unpacked(node: ts.Node, found: Unpacked[]): void {
|
|
299
339
|
if (ts.isBindingElement(node) && !node.propertyName && ts.isObjectBindingPattern(node.parent)) {
|
|
300
340
|
let initializer = (node.parent.parent as { initializer?: ts.Expression }).initializer;
|
|
301
341
|
|
|
302
|
-
if (initializer
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
if (kind === TYPES.Signal || kind === TYPES.Computed) {
|
|
306
|
-
ctx.failures.push({ message: 'a reactive binding cannot be destructured (it would copy its signal); read it through the namespace', node });
|
|
307
|
-
}
|
|
342
|
+
if (initializer) {
|
|
343
|
+
found.push({ element: node, initializer });
|
|
308
344
|
}
|
|
309
345
|
}
|
|
310
346
|
|
|
311
|
-
node.forEachChild(child => unpacked(
|
|
347
|
+
node.forEachChild(child => unpacked(child, found));
|
|
312
348
|
}
|
|
313
349
|
|
|
314
350
|
|
|
315
|
-
export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): PrimitivesTransformResult => {
|
|
351
|
+
export default async (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): Promise<PrimitivesTransformResult> => {
|
|
316
352
|
let ctx: TransformContext = {
|
|
317
353
|
bindings,
|
|
318
354
|
calls: [],
|
|
@@ -322,8 +358,8 @@ export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: I
|
|
|
322
358
|
tmpCounter: 0
|
|
323
359
|
};
|
|
324
360
|
|
|
325
|
-
declare(ctx, sourceFile);
|
|
326
|
-
rewrite(ctx);
|
|
361
|
+
await declare(ctx, sourceFile);
|
|
362
|
+
await rewrite(ctx);
|
|
327
363
|
|
|
328
364
|
return { calls: ctx.calls, failures: ctx.failures, replacements: ctx.replacements };
|
|
329
365
|
};
|
package/src/system.ts
CHANGED
|
@@ -812,21 +812,25 @@ function makeComputed<T>(fn: Computed<T>['fn'], eager: boolean = false): Compute
|
|
|
812
812
|
if (observer.depsTail === null) {
|
|
813
813
|
self.height = observer.height;
|
|
814
814
|
recompute(self);
|
|
815
|
+
link(self, observer);
|
|
815
816
|
}
|
|
816
817
|
else if (eager) {
|
|
817
818
|
// computed() must know fn's return type to pick sync vs async. This probe runs BEFORE
|
|
818
|
-
// link()
|
|
819
|
+
// link(), so self has no subs yet — recompute's propagate is a no-op and cannot
|
|
819
820
|
// re-run the parent. Deferring here (as effect() still does) would leave value unset.
|
|
820
821
|
self.height = observer.height + 1;
|
|
821
822
|
recompute(self);
|
|
823
|
+
link(self, observer);
|
|
822
824
|
}
|
|
823
825
|
else {
|
|
826
|
+
// Deferred, so not linked: the parent has not read a value, and the creation run setting one later is
|
|
827
|
+
// no change it depends on. Linked, that run would re-run the parent, whose re-run creates another node,
|
|
828
|
+
// looping. A parent that reads the node links through read().
|
|
824
829
|
self.height = observer.height + 1;
|
|
825
830
|
insertIntoHeap(self);
|
|
826
831
|
schedule();
|
|
827
832
|
}
|
|
828
833
|
|
|
829
|
-
link(self, observer);
|
|
830
834
|
own(observer, self as Computed<unknown>);
|
|
831
835
|
}
|
|
832
836
|
else {
|