@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.
Files changed (49) hide show
  1. package/README.md +7 -5
  2. package/bench/compiler.bench.ts +24 -16
  3. package/bench/lib/reactive-adapter.ts +1 -1
  4. package/bench/ownership.bench.ts +9 -5
  5. package/bench/reactive/array.bench.ts +6 -2
  6. package/bench/system.bench.ts +9 -5
  7. package/build/compiler/array.d.ts +1 -1
  8. package/build/compiler/array.js +2 -2
  9. package/build/compiler/bindings.d.ts +9 -9
  10. package/build/compiler/bindings.js +59 -62
  11. package/build/compiler/index.d.ts +2 -2
  12. package/build/compiler/index.js +19 -13
  13. package/build/compiler/object.d.ts +1 -1
  14. package/build/compiler/object.js +19 -10
  15. package/build/compiler/plugins/vite.d.ts +18 -4
  16. package/build/compiler/primitives.d.ts +1 -1
  17. package/build/compiler/primitives.js +38 -20
  18. package/build/constants.d.ts +3 -1
  19. package/build/constants.js +3 -1
  20. package/build/reactive/array.js +89 -17
  21. package/build/system.d.ts +4 -3
  22. package/build/system.js +48 -16
  23. package/package.json +2 -2
  24. package/src/compiler/array.ts +2 -2
  25. package/src/compiler/bindings.ts +82 -86
  26. package/src/compiler/index.ts +25 -14
  27. package/src/compiler/object.ts +31 -17
  28. package/src/compiler/primitives.ts +61 -25
  29. package/src/constants.ts +6 -1
  30. package/src/reactive/array.ts +130 -23
  31. package/src/system.ts +66 -23
  32. package/test/async-hardening.test.ts +2 -2
  33. package/test/async-iterable.test.ts +3 -3
  34. package/test/compiler/compiler.test.ts +169 -156
  35. package/test/compiler/composition.test.ts +31 -27
  36. package/test/compiler/coverage.test.ts +12 -12
  37. package/test/compiler/origins.test.ts +18 -18
  38. package/test/compiler/output.test.ts +4 -4
  39. package/test/deep-graphs.test.ts +6 -6
  40. package/test/effects.test.ts +5 -5
  41. package/test/errors.test.ts +4 -4
  42. package/test/invalidate.test.ts +2 -2
  43. package/test/ownership.test.ts +30 -30
  44. package/test/pause.test.ts +267 -0
  45. package/test/reactive/array.test.ts +5 -5
  46. package/test/reactive/reactive.test.ts +3 -3
  47. package/test/signal-selector.test.ts +4 -4
  48. package/test/system.test.ts +10 -9
  49. package/test/untrack.test.ts +9 -9
@@ -7,13 +7,12 @@ import type { Bindings } from './types';
7
7
 
8
8
  type Assignment = { call: ts.CallExpression; target: ts.Identifier };
9
9
 
10
- // Per program snapshot: answers about declarations and types hold for the snapshot's lifetime. `arrays`
11
- // and `kinds` hold answers built from queries the shared checker does not memoize (getPropertyOfType,
12
- // getNonNullableType, isArrayType), so dropping them costs a round-trip per array candidate or read
10
+ // Per program snapshot. `kinds` saves no checker round-trip (the shared memo answers every query it
11
+ // makes), but kind() runs on every read of a binding and re-deriving it per read dominates a warm
12
+ // transform; concurrent transforms reading one binding await the one derivation
13
13
  type Caches = {
14
- arrays: Map<number, boolean>;
15
14
  assignments: Map<string, Map<string, Assignment[]>>;
16
- kinds: Map<string, TYPES | null>;
15
+ kinds: Map<string, Promise<TYPES | null>>;
17
16
  };
18
17
 
19
18
 
@@ -69,21 +68,62 @@ function cachesOf(program: ts.Program): Caches {
69
68
  let found = caches.get(program);
70
69
 
71
70
  if (!found) {
72
- found = { arrays: new Map(), assignments: new Map(), kinds: new Map() };
71
+ found = { assignments: new Map(), kinds: new Map() };
73
72
  caches.set(program, found);
74
73
  }
75
74
 
76
75
  return found;
77
76
  }
78
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
+
79
118
  // Every member of a union must agree: `number | undefined` is a signal, `number[] | undefined` is
80
119
  // neither a signal nor an array at runtime
81
- function kindOf(checker: ts.Checker, type: ts.Type): TYPES | null {
82
- 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))),
83
123
  result: TYPES | null = null;
84
124
 
85
- for (let i = 0, n = members.length; i < n; i++) {
86
- let kind = kindOfType(checker, members[i]);
125
+ for (let i = 0, n = kinds.length; i < n; i++) {
126
+ let kind = kinds[i];
87
127
 
88
128
  if (kind === null || (result !== null && result !== kind)) {
89
129
  return null;
@@ -96,9 +136,9 @@ function kindOf(checker: ts.Checker, type: ts.Type): TYPES | null {
96
136
  }
97
137
 
98
138
  // Kind of one non-union type as a reactive() argument; null when the type cannot say
99
- function kindOfType(checker: ts.Checker, type: ts.Type): TYPES | null {
139
+ async function kindOfType(checker: ts.Checker, type: ts.Type): Promise<TYPES | null> {
100
140
  if ((type.flags & ts.TypeFlags.TypeParameter) !== 0) {
101
- let constraint = checker.getBaseConstraintOfType(type);
141
+ let constraint = await checker.getBaseConstraintOfType(type);
102
142
 
103
143
  return constraint && constraint !== type ? kindOf(checker, constraint) : null;
104
144
  }
@@ -122,11 +162,11 @@ function kindOfType(checker: ts.Checker, type: ts.Type): TYPES | null {
122
162
  return TYPES.Signal;
123
163
  }
124
164
 
125
- if (checker.isArrayType(type) || checker.isTupleType(type)) {
165
+ if (await checker.isArrayType(type) || await checker.isTupleType(type)) {
126
166
  return TYPES.Array;
127
167
  }
128
168
 
129
- if (checker.getSignaturesOfType(type, ts.SignatureKind.Call).length > 0) {
169
+ if ((await checker.getSignaturesOfType(type, ts.SignatureKind.Call)).length > 0) {
130
170
  return TYPES.Computed;
131
171
  }
132
172
 
@@ -138,39 +178,24 @@ function kindOfType(checker: ts.Checker, type: ts.Type): TYPES | null {
138
178
  // Expressions whose type is a ReactiveArray (`Reactive<T[]>` carries the class's members), resolved
139
179
  // in one checker round-trip. Found by type, a reactive array is recognized wherever it flows:
140
180
  // imports, object properties, parameters, return values.
141
- const arrays = (bindings: Bindings, nodes: ts.Node[]): Set<ts.Node> => {
181
+ const arrays = async (bindings: Bindings, nodes: ts.Node[]): Promise<Set<ts.Node>> => {
142
182
  let result = new Set<ts.Node>();
143
183
 
144
184
  if (nodes.length === 0) {
145
185
  return result;
146
186
  }
147
187
 
148
- 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));
149
189
 
150
190
  if (files.size === 0) {
151
191
  return result;
152
192
  }
153
193
 
154
- let cache = cachesOf(bindings.program).arrays,
155
- 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)));
156
196
 
157
197
  for (let i = 0, n = nodes.length; i < n; i++) {
158
- let type = types[i];
159
-
160
- if (!type) {
161
- continue;
162
- }
163
-
164
- let branded = cache.get(type.id);
165
-
166
- if (branded === undefined) {
167
- let property = bindings.checker.getPropertyOfType(bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND);
168
-
169
- branded = property?.declarations?.some(declaration => files.has(declaration.path)) === true;
170
- cache.set(type.id, branded);
171
- }
172
-
173
- if (branded) {
198
+ if (properties[i]?.declarations?.some(declaration => files.has(declaration.path))) {
174
199
  result.add(nodes[i]);
175
200
  }
176
201
  }
@@ -180,7 +205,7 @@ const arrays = (bindings: Bindings, nodes: ts.Node[]): Set<ts.Node> => {
180
205
 
181
206
  // Kind of a reactive() call from its argument: literal syntax first, the argument's type otherwise.
182
207
  // Null when no kind is provable (no/extra arguments, `any`, `unknown`, a mixed union).
183
- const classify = (bindings: Bindings, call: ts.CallExpression): TYPES | null => {
208
+ const classify = async (bindings: Bindings, call: ts.CallExpression): Promise<TYPES | null> => {
184
209
  let arg = call.arguments[0];
185
210
 
186
211
  if (!arg || call.arguments.length !== 1 || ts.isSpreadElement(arg)) {
@@ -201,46 +226,50 @@ const classify = (bindings: Bindings, call: ts.CallExpression): TYPES | null =>
201
226
  return TYPES.Object;
202
227
  }
203
228
 
204
- let type = bindings.checker.getTypeAtLocation(arg);
229
+ let type = await bindings.checker.getTypeAtLocation(arg);
205
230
 
206
231
  return type ? kindOf(bindings.checker, type) : null;
207
232
  };
208
233
 
209
234
  // Memoized per program (an already memoized checker is kept as is), so batched and repeated queries
210
235
  // share one cache whoever hands the checker in
211
- const create = (queries: ts.Checker, program: ts.Program, sourceFile: ts.SourceFile): Bindings => {
212
- 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
+ ]);
213
242
 
214
243
  return {
215
244
  checker: memoized,
216
- entries: new Set(references.exported(memoized, program, PACKAGE_NAME, ENTRYPOINT).map(references.key)),
217
- origins: references.origins(memoized, program, sourceFile),
245
+ entries: new Set(exported.map(references.key)),
246
+ origins,
218
247
  program,
219
248
  sourceFile
220
249
  };
221
250
  };
222
251
 
223
252
  // Whether a callee expression denotes `reactive`: `reactive`, an alias, `ns.reactive`, `ns['reactive']`
224
- const denotes = (bindings: Bindings, expression: ts.Expression): boolean => {
253
+ const denotes = (bindings: Bindings, expression: ts.Expression): Promise<boolean> => {
225
254
  return references.denotes(bindings.checker, bindings.program, expression, bindings.entries);
226
255
  };
227
256
 
228
257
  // Whether a value holds `reactive`: the export itself, reached through any import, re-export or
229
258
  // namespace, or a const binding (`const r = reactive`, `const { reactive: r } = ns`) holding it
230
- const holds = (bindings: Bindings, found: Origin | null): boolean => {
259
+ const holds = (bindings: Bindings, found: Origin | null): Promise<boolean> => {
231
260
  return references.holds(bindings.checker, bindings.program, found, bindings.entries);
232
261
  };
233
262
 
234
- const isReactiveCall = (bindings: Bindings, node: ts.Node): node is ts.CallExpression => {
263
+ const isReactiveCall = async (bindings: Bindings, node: ts.Node): Promise<boolean> => {
235
264
  return ts.isCallExpression(node) && denotes(bindings, node.expression);
236
265
  };
237
266
 
238
267
  // Reactive kind of the binding a declaration introduces, wherever it is read from: a variable
239
268
  // initialized (or assigned) with reactive(), or a module's `export default reactive(...)`.
240
269
  // Ambient declarations (.d.ts) never are: a published package's signals ship as Signal-typed.
241
- const kind = (bindings: Bindings, found: Origin | null): TYPES | null => {
270
+ const kind = (bindings: Bindings, found: Origin | null): Promise<TYPES | null> => {
242
271
  if (!found) {
243
- return null;
272
+ return Promise.resolve(null);
244
273
  }
245
274
 
246
275
  let declaration = found.declaration;
@@ -249,61 +278,28 @@ const kind = (bindings: Bindings, found: Origin | null): TYPES | null => {
249
278
  (declaration.kind !== ts.SyntaxKind.VariableDeclaration && declaration.kind !== ts.SyntaxKind.ExportAssignment) ||
250
279
  declaration.path.endsWith('.d.ts')
251
280
  ) {
252
- return null;
281
+ return Promise.resolve(null);
253
282
  }
254
283
 
255
284
  let cache = cachesOf(bindings.program).kinds,
256
285
  key = references.key(declaration),
257
286
  known = cache.get(key);
258
287
 
259
- if (known !== undefined) {
260
- return known;
261
- }
262
-
263
- cache.set(key, null);
264
-
265
- let node = declaration.resolve() as ts.Node | undefined,
266
- result: TYPES | null = null;
267
-
268
- if (node && ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
269
- let initializer = node.initializer && unwrap(node.initializer);
270
-
271
- if (initializer && isReactiveCall(bindings, initializer)) {
272
- result = classify(bindings, initializer);
273
- }
274
- else {
275
- let candidates = assignments(bindings, node.getSourceFile()).get(node.name.text) ?? [];
276
-
277
- for (let i = 0, n = candidates.length; i < n && result === null; i++) {
278
- let { call, target } = candidates[i],
279
- assigned = origin(bindings, target);
280
-
281
- if (assigned && references.key(assigned.declaration) === key && isReactiveCall(bindings, call)) {
282
- result = classify(bindings, call);
283
- }
284
- }
285
- }
288
+ if (known === undefined) {
289
+ known = derive(bindings, declaration, key);
290
+ cache.set(key, known);
286
291
  }
287
- else if (node && ts.isExportAssignment(node)) {
288
- let expression = unwrap(node.expression);
289
-
290
- if (isReactiveCall(bindings, expression)) {
291
- result = classify(bindings, expression);
292
- }
293
- }
294
-
295
- cache.set(key, result);
296
292
 
297
- return result;
293
+ return known;
298
294
  };
299
295
 
300
- const origin = (bindings: Bindings, node: ts.Node): Origin | null => {
296
+ const origin = async (bindings: Bindings, node: ts.Node): Promise<Origin | null> => {
301
297
  return bindings.origins.get(node) ?? references.origin(bindings.checker, bindings.program, node);
302
298
  };
303
299
 
304
300
  // Types of the arguments classify() reads from the checker, requested in one round-trip; the
305
301
  // memoized checker then answers each classify() from that request
306
- const prefetch = (bindings: Bindings, calls: ts.CallExpression[]): void => {
302
+ const prefetch = async (bindings: Bindings, calls: ts.CallExpression[]): Promise<void> => {
307
303
  let nodes: ts.Node[] = [];
308
304
 
309
305
  for (let i = 0, n = calls.length; i < n; i++) {
@@ -328,7 +324,7 @@ const prefetch = (bindings: Bindings, calls: ts.CallExpression[]): void => {
328
324
  }
329
325
 
330
326
  if (nodes.length > 0) {
331
- bindings.checker.getTypeAtLocation(nodes);
327
+ await bindings.checker.getTypeAtLocation(nodes);
332
328
  }
333
329
  };
334
330
 
@@ -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
- for (let [identifier, found] of bindings.origins) {
107
- if (!scope.holds(bindings, found)) {
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 [identifier, found] of bindings.origins) {
135
- if (!entries.has(identifier) && scope.kind(bindings, found) === null) {
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
- for (let i = 0, n = arrays.reactive.length; i < n; i++) {
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
- let found = ts.isIdentifier(root) ? scope.origin(bindings, root) : null;
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);
@@ -26,14 +26,16 @@ interface ReactiveObjectCall {
26
26
  }
27
27
 
28
28
  interface VisitContext {
29
- bindings: Bindings;
30
29
  calls: ReactiveObjectCall[];
31
30
  isReactiveCall: IsReactiveCall;
32
31
  sourceFile: ts.SourceFile;
32
+ typed: AnalyzedProperty[];
33
33
  }
34
34
 
35
35
 
36
- function analyzeProperty(prop: ts.ObjectLiteralElementLike, bindings: Bindings): AnalyzedProperty | null {
36
+ // Syntax decides every property except a non-literal value, recorded in `typed` as a signal until
37
+ // its type says whether it is an array
38
+ function analyzeProperty(prop: ts.ObjectLiteralElementLike, typed: AnalyzedProperty[]): AnalyzedProperty | null {
37
39
  if (!ts.isPropertyAssignment(prop)) {
38
40
  return null;
39
41
  }
@@ -86,20 +88,18 @@ function analyzeProperty(prop: ts.ObjectLiteralElementLike, bindings: Bindings):
86
88
  return { isStatic, key, type: TYPES.Array, value };
87
89
  }
88
90
 
89
- // A non-literal array is typed Reactive<T[]> on the object, so it must become a ReactiveArray
90
- // exactly like a literal one
91
- let type = isStaticValue(unwrapped) ? undefined : bindings.checker.getTypeAtLocation(value);
91
+ let property: AnalyzedProperty = {
92
+ isStatic: isStaticValue(value),
93
+ key,
94
+ type: TYPES.Signal,
95
+ value
96
+ };
92
97
 
93
- if (type && (bindings.checker.isArrayType(type) || bindings.checker.isTupleType(type))) {
94
- return { isStatic: false, key, type: TYPES.Array, value };
98
+ if (!isStaticValue(unwrapped)) {
99
+ typed.push(property);
95
100
  }
96
101
 
97
- return {
98
- isStatic: isStaticValue(value),
99
- key,
100
- type: TYPES.Signal,
101
- value
102
- };
102
+ return property;
103
103
  }
104
104
 
105
105
  function buildClassCode(classname: string, properties: AnalyzedProperty[], sourceFile: ts.SourceFile, typehint: string | null): string {
@@ -243,7 +243,7 @@ function visit(ctx: VisitContext, node: ts.Node): void {
243
243
  return;
244
244
  }
245
245
 
246
- let analyzed = analyzeProperty(prop, ctx.bindings);
246
+ let analyzed = analyzeProperty(prop, ctx.typed);
247
247
 
248
248
  if (!analyzed) {
249
249
  node.forEachChild(n => visit(ctx, n));
@@ -267,16 +267,30 @@ 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
- bindings,
273
272
  calls: [],
274
273
  isReactiveCall,
275
- sourceFile
274
+ sourceFile,
275
+ typed: []
276
276
  };
277
277
 
278
278
  visit(ctx, sourceFile);
279
279
 
280
+ // A non-literal array is typed Reactive<T[]> on the object, so it must become a ReactiveArray
281
+ // exactly like a literal one; every such value in the file is typed in one request
282
+ if (ctx.typed.length > 0) {
283
+ let types = 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))));
285
+
286
+ for (let i = 0, n = ctx.typed.length; i < n; i++) {
287
+ if (arrays[i]) {
288
+ ctx.typed[i].isStatic = false;
289
+ ctx.typed[i].type = TYPES.Array;
290
+ }
291
+ }
292
+ }
293
+
280
294
  let prepend: string[] = [],
281
295
  replacements: ReplacementIntent[] = [];
282
296
 
@@ -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, node: ts.Node): void {
31
- if (ctx.isReactiveCall(node)) {
32
- let call = node,
33
- kind = scope.classify(ctx.bindings, call);
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 [identifier, found] of bindings.origins) {
277
- let kind = scope.kind(bindings, found);
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
- unpacked(ctx, bindings.sourceFile);
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(ctx: TransformContext, node: ts.Node): void {
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 && isNamespace(ctx.bindings, initializer)) {
303
- let kind = scope.kind(ctx.bindings, scope.origin(ctx.bindings, node));
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(ctx, child));
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/constants.ts CHANGED
@@ -35,6 +35,11 @@ const STATE_EFFECT = 1 << 6;
35
35
  // Set by dispose(): a node that can never run or own anything again
36
36
  const STATE_DISPOSED = 1 << 7;
37
37
 
38
+ // Set by pause(): runs that reach the effect are skipped and marked STATE_STALE, which resume() replays once
39
+ const STATE_PAUSED = 1 << 8;
40
+
41
+ const STATE_STALE = 1 << 9;
42
+
38
43
  const STATE_NOTIFY_MASK = (STATE_CHECK | STATE_DIRTY);
39
44
 
40
45
 
@@ -44,5 +49,5 @@ export {
44
49
  REACTIVE_ARRAY, REACTIVE_OBJECT,
45
50
  SIGNAL,
46
51
  STABILIZER_DEFERRED, STABILIZER_IDLE, STABILIZER_RESCHEDULE, STABILIZER_RUNNING, STABILIZER_SCHEDULED,
47
- STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_RECOMPUTING
52
+ STATE_CHECK, STATE_COMPUTED, STATE_DIRTY, STATE_DISPOSED, STATE_EFFECT, STATE_ERROR, STATE_IN_HEAP, STATE_NOTIFY_MASK, STATE_PAUSED, STATE_RECOMPUTING, STATE_STALE
48
53
  };