@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.
@@ -76,35 +76,36 @@ writeFileSync(config, JSON.stringify({
76
76
  // One build pass: every module is transformed against one program and one checker, as tsc and vite
77
77
  // hand them in, so the coordinator's memo is shared across the pass. Each pass wraps the checker
78
78
  // anew, so its memo starts empty and every query the pass needs reaches tsgo once.
79
- function build(names: string[]): string[] {
79
+ async function build(names: string[]): Promise<string[]> {
80
80
  let checker: ts.Checker | null = null,
81
81
  code: string[] = [];
82
82
 
83
83
  for (let i = 0, n = names.length; i < n; i++) {
84
84
  let id = directory + '/' + names[i],
85
- project = languageService.update(config, id, FILES[names[i]]);
85
+ project = await languageService.update(config, id, FILES[names[i]]);
86
86
 
87
87
  checker ??= new Proxy(project.checker, {});
88
- code.push(coordinator.transform([pipeline], FILES[names[i]], project.program.getSourceFile(id)!, { checker, program: project.program }, directory, new Map()).code);
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();
89
90
  }
90
91
 
91
92
  return code;
92
93
  }
93
94
 
94
95
 
95
- afterAll(() => {
96
- languageService.dispose();
96
+ afterAll(async () => {
97
+ await languageService.dispose();
97
98
  rmSync(directory, { force: true, recursive: true });
98
99
  });
99
100
 
100
101
 
101
102
  test('compiler transform', async ({ bench }) => {
102
103
  await bench.compare(
103
- bench('every module of the app', () => {
104
- build(NAMES);
104
+ bench('every module of the app', async () => {
105
+ await build(NAMES);
105
106
  }),
106
- bench('modules without reactive (no import, unrelated import)', () => {
107
- build(['helpers.ts', 'plain.ts']);
107
+ bench('modules without reactive (no import, unrelated import)', async () => {
108
+ await build(['helpers.ts', 'plain.ts']);
108
109
  })
109
110
  );
110
111
  });
@@ -112,7 +113,7 @@ test('compiler transform', async ({ bench }) => {
112
113
  test('compiled output', async ({ bench }) => {
113
114
  let file = directory + '/runtime.compiled.ts';
114
115
 
115
- writeFileSync(file, build(['runtime.ts'])[0]);
116
+ writeFileSync(file, (await build(['runtime.ts']))[0]);
116
117
 
117
118
  let { run } = await import(pathToFileURL(file).href) as { run: () => number };
118
119
 
@@ -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>;
@@ -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) => node is ts.CallExpression;
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;
@@ -37,10 +37,37 @@ function cachesOf(program) {
37
37
  }
38
38
  return found;
39
39
  }
40
- function kindOf(checker, type) {
41
- let members = (type.flags & ts.TypeFlags.Union) !== 0 ? type.getTypes() : [type], result = null;
42
- for (let i = 0, n = members.length; i < n; i++) {
43
- let kind = kindOfType(checker, members[i]);
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,37 +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 types = bindings.checker.getTypeAtLocation(nodes);
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
- let type = types[i];
91
- if (!type) {
92
- continue;
93
- }
94
- let property = bindings.checker.getPropertyOfType(bindings.checker.getNonNullableType(type) ?? type, ARRAY_BRAND);
95
- if (property?.declarations?.some(declaration => files.has(declaration.path))) {
117
+ if (properties[i]?.declarations?.some(declaration => files.has(declaration.path))) {
96
118
  result.add(nodes[i]);
97
119
  }
98
120
  }
99
121
  return result;
100
122
  };
101
- const classify = (bindings, call) => {
123
+ const classify = async (bindings, call) => {
102
124
  let arg = call.arguments[0];
103
125
  if (!arg || call.arguments.length !== 1 || ts.isSpreadElement(arg)) {
104
126
  return null;
@@ -113,15 +135,18 @@ const classify = (bindings, call) => {
113
135
  if (ts.isObjectLiteralExpression(value)) {
114
136
  return TYPES.Object;
115
137
  }
116
- let type = bindings.checker.getTypeAtLocation(arg);
138
+ let type = await bindings.checker.getTypeAtLocation(arg);
117
139
  return type ? kindOf(bindings.checker, type) : null;
118
140
  };
119
- const create = (queries, program, sourceFile) => {
120
- 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
+ ]);
121
146
  return {
122
147
  checker: memoized,
123
- entries: new Set(references.exported(memoized, program, PACKAGE_NAME, ENTRYPOINT).map(references.key)),
124
- origins: references.origins(memoized, program, sourceFile),
148
+ entries: new Set(exported.map(references.key)),
149
+ origins,
125
150
  program,
126
151
  sourceFile
127
152
  };
@@ -132,52 +157,29 @@ const denotes = (bindings, expression) => {
132
157
  const holds = (bindings, found) => {
133
158
  return references.holds(bindings.checker, bindings.program, found, bindings.entries);
134
159
  };
135
- const isReactiveCall = (bindings, node) => {
160
+ const isReactiveCall = async (bindings, node) => {
136
161
  return ts.isCallExpression(node) && denotes(bindings, node.expression);
137
162
  };
138
163
  const kind = (bindings, found) => {
139
164
  if (!found) {
140
- return null;
165
+ return Promise.resolve(null);
141
166
  }
142
167
  let declaration = found.declaration;
143
168
  if ((declaration.kind !== ts.SyntaxKind.VariableDeclaration && declaration.kind !== ts.SyntaxKind.ExportAssignment) ||
144
169
  declaration.path.endsWith('.d.ts')) {
145
- return null;
170
+ return Promise.resolve(null);
146
171
  }
147
172
  let cache = cachesOf(bindings.program).kinds, key = references.key(declaration), known = cache.get(key);
148
- if (known !== undefined) {
149
- return known;
173
+ if (known === undefined) {
174
+ known = derive(bindings, declaration, key);
175
+ cache.set(key, known);
150
176
  }
151
- cache.set(key, null);
152
- let node = declaration.resolve(), result = null;
153
- if (node && ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
154
- let initializer = node.initializer && unwrap(node.initializer);
155
- if (initializer && isReactiveCall(bindings, initializer)) {
156
- result = classify(bindings, initializer);
157
- }
158
- else {
159
- let candidates = assignments(bindings, node.getSourceFile()).get(node.name.text) ?? [];
160
- for (let i = 0, n = candidates.length; i < n && result === null; i++) {
161
- let { call, target } = candidates[i], assigned = origin(bindings, target);
162
- if (assigned && references.key(assigned.declaration) === key && isReactiveCall(bindings, call)) {
163
- result = classify(bindings, call);
164
- }
165
- }
166
- }
167
- }
168
- else if (node && ts.isExportAssignment(node)) {
169
- let expression = unwrap(node.expression);
170
- if (isReactiveCall(bindings, expression)) {
171
- result = classify(bindings, expression);
172
- }
173
- }
174
- cache.set(key, result);
175
- return result;
177
+ return known;
176
178
  };
177
- const origin = (bindings, node) => {
179
+ const origin = async (bindings, node) => {
178
180
  return bindings.origins.get(node) ?? references.origin(bindings.checker, bindings.program, node);
179
181
  };
180
- const prefetch = (bindings, calls) => {
182
+ const prefetch = async (bindings, calls) => {
181
183
  let nodes = [];
182
184
  for (let i = 0, n = calls.length; i < n; i++) {
183
185
  let arg = calls[i].arguments[0];
@@ -194,7 +196,7 @@ const prefetch = (bindings, calls) => {
194
196
  nodes.push(arg);
195
197
  }
196
198
  if (nodes.length > 0) {
197
- bindings.checker.getTypeAtLocation(nodes);
199
+ await bindings.checker.getTypeAtLocation(nodes);
198
200
  }
199
201
  };
200
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;
@@ -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
- for (let [identifier, found] of bindings.origins) {
59
- if (!scope.holds(bindings, found)) {
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
- for (let [identifier, found] of bindings.origins) {
79
- if (!entries.has(identifier) && scope.kind(bindings, found) === null) {
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
- for (let i = 0, n = arrays.reactive.length; i < n; i++) {
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
- let found = ts.isIdentifier(root) ? scope.origin(bindings, root) : null;
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>;
@@ -174,7 +174,7 @@ 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
179
  calls: [],
180
180
  isReactiveCall,
@@ -183,10 +183,9 @@ export default (sourceFile, bindings, isReactiveCall) => {
183
183
  };
184
184
  visit(ctx, sourceFile);
185
185
  if (ctx.typed.length > 0) {
186
- let types = bindings.checker.getTypeAtLocation(ctx.typed.map(property => property.value));
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
187
  for (let i = 0, n = ctx.typed.length; i < n; i++) {
188
- let type = types[i];
189
- if (type && (bindings.checker.isArrayType(type) || bindings.checker.isTupleType(type))) {
188
+ if (arrays[i]) {
190
189
  ctx.typed[i].isStatic = false;
191
190
  ctx.typed[i].type = TYPES.Array;
192
191
  }
@@ -42,10 +42,10 @@ declare const _default: ({ root }?: {
42
42
  include: RegExp;
43
43
  };
44
44
  };
45
- handler: (this: unknown, code: string, id: string) => {
45
+ handler: (this: unknown, code: string, id: string) => Promise<{
46
46
  code: string;
47
47
  map: import("@esportsplus/typescript/compiler").SourceMapV3;
48
- } | null;
48
+ } | null>;
49
49
  };
50
50
  watchChange: (id: string) => void;
51
51
  };
@@ -11,5 +11,5 @@ type PrimitivesTransformResult = {
11
11
  replacements: ReplacementIntent[];
12
12
  };
13
13
  export default _default;
14
- declare function _default(sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): PrimitivesTransformResult;
14
+ declare function _default(sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): Promise<PrimitivesTransformResult>;
15
15
  export type { Failure };
@@ -1,9 +1,18 @@
1
1
  import { ts } from '@esportsplus/typescript';
2
2
  import { COMPOUND_OPERATORS, NAMESPACE, TYPES } from './constants.js';
3
3
  import scope from './bindings.js';
4
- function declare(ctx, node) {
4
+ function calls(ctx, node, found) {
5
5
  if (ctx.isReactiveCall(node)) {
6
- let call = node, kind = scope.classify(ctx.bindings, call);
6
+ found.push(node);
7
+ }
8
+ node.forEachChild(child => calls(ctx, child, found));
9
+ }
10
+ async function declare(ctx, sourceFile) {
11
+ let found = [];
12
+ calls(ctx, sourceFile, found);
13
+ let kinds = await Promise.all(found.map((call) => scope.classify(ctx.bindings, call)));
14
+ for (let i = 0, n = found.length; i < n; i++) {
15
+ let call = found[i], kind = kinds[i];
7
16
  ctx.calls.push(call);
8
17
  if (kind === null) {
9
18
  ctx.failures.push({
@@ -35,7 +44,6 @@ function declare(ctx, node) {
35
44
  }
36
45
  }
37
46
  }
38
- node.forEachChild(child => declare(ctx, child));
39
47
  }
40
48
  function isBound(call) {
41
49
  let node = call, parent = node.parent;
@@ -55,7 +63,7 @@ function isBound(call) {
55
63
  }
56
64
  return ts.isExportAssignment(parent);
57
65
  }
58
- function isNamespace(bindings, expression) {
66
+ async function isNamespace(bindings, expression) {
59
67
  let value = scope.unwrap(expression);
60
68
  if (ts.isAwaitExpression(value)) {
61
69
  value = scope.unwrap(value.expression);
@@ -63,7 +71,7 @@ function isNamespace(bindings, expression) {
63
71
  if (ts.isCallExpression(value)) {
64
72
  return value.expression.kind === ts.SyntaxKind.ImportKeyword;
65
73
  }
66
- return ts.isIdentifier(value) && scope.origin(bindings, value)?.declaration.kind === ts.SyntaxKind.SourceFile;
74
+ return ts.isIdentifier(value) && (await scope.origin(bindings, value))?.declaration.kind === ts.SyntaxKind.SourceFile;
67
75
  }
68
76
  function isPatternTarget(node) {
69
77
  let current = node;
@@ -175,10 +183,10 @@ function reference(ctx, node, kind) {
175
183
  }
176
184
  }
177
185
  }
178
- function rewrite(ctx) {
179
- let { bindings } = ctx;
180
- for (let [identifier, found] of bindings.origins) {
181
- let kind = scope.kind(bindings, found);
186
+ async function rewrite(ctx) {
187
+ let { bindings } = ctx, origins = [...bindings.origins], kinds = await Promise.all(origins.map(([, found]) => scope.kind(bindings, found)));
188
+ for (let i = 0, n = origins.length; i < n; i++) {
189
+ let [identifier] = origins[i], kind = kinds[i];
182
190
  if (kind !== TYPES.Signal && kind !== TYPES.Computed) {
183
191
  continue;
184
192
  }
@@ -189,21 +197,31 @@ function rewrite(ctx) {
189
197
  }
190
198
  reference(ctx, readNode(identifier), kind);
191
199
  }
192
- unpacked(ctx, bindings.sourceFile);
200
+ let elements = [];
201
+ unpacked(bindings.sourceFile, elements);
202
+ let copied = await Promise.all(elements.map(async ({ element, initializer }) => {
203
+ if (!(await isNamespace(bindings, initializer))) {
204
+ return false;
205
+ }
206
+ let kind = await scope.kind(bindings, await scope.origin(bindings, element));
207
+ return kind === TYPES.Signal || kind === TYPES.Computed;
208
+ }));
209
+ for (let i = 0, n = elements.length; i < n; i++) {
210
+ if (copied[i]) {
211
+ ctx.failures.push({ message: 'a reactive binding cannot be destructured (it would copy its signal); read it through the namespace', node: elements[i].element });
212
+ }
213
+ }
193
214
  }
194
- function unpacked(ctx, node) {
215
+ function unpacked(node, found) {
195
216
  if (ts.isBindingElement(node) && !node.propertyName && ts.isObjectBindingPattern(node.parent)) {
196
217
  let initializer = node.parent.parent.initializer;
197
- if (initializer && isNamespace(ctx.bindings, initializer)) {
198
- let kind = scope.kind(ctx.bindings, scope.origin(ctx.bindings, node));
199
- if (kind === TYPES.Signal || kind === TYPES.Computed) {
200
- ctx.failures.push({ message: 'a reactive binding cannot be destructured (it would copy its signal); read it through the namespace', node });
201
- }
218
+ if (initializer) {
219
+ found.push({ element: node, initializer });
202
220
  }
203
221
  }
204
- node.forEachChild(child => unpacked(ctx, child));
222
+ node.forEachChild(child => unpacked(child, found));
205
223
  }
206
- export default (sourceFile, bindings, isReactiveCall) => {
224
+ export default async (sourceFile, bindings, isReactiveCall) => {
207
225
  let ctx = {
208
226
  bindings,
209
227
  calls: [],
@@ -212,7 +230,7 @@ export default (sourceFile, bindings, isReactiveCall) => {
212
230
  replacements: [],
213
231
  tmpCounter: 0
214
232
  };
215
- declare(ctx, sourceFile);
216
- rewrite(ctx);
233
+ await declare(ctx, sourceFile);
234
+ await rewrite(ctx);
217
235
  return { calls: ctx.calls, failures: ctx.failures, replacements: ctx.replacements };
218
236
  };
package/build/system.js CHANGED
@@ -539,17 +539,18 @@ function makeComputed(fn, eager = false) {
539
539
  if (observer.depsTail === null) {
540
540
  self.height = observer.height;
541
541
  recompute(self);
542
+ link(self, observer);
542
543
  }
543
544
  else if (eager) {
544
545
  self.height = observer.height + 1;
545
546
  recompute(self);
547
+ link(self, observer);
546
548
  }
547
549
  else {
548
550
  self.height = observer.height + 1;
549
551
  insertIntoHeap(self);
550
552
  schedule();
551
553
  }
552
- link(self, observer);
553
554
  own(observer, self);
554
555
  }
555
556
  else {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "ICJR",
3
3
  "dependencies": {
4
- "@esportsplus/typescript": "^0.41.0",
4
+ "@esportsplus/typescript": "^0.42.0",
5
5
  "@esportsplus/utilities": "^0.28.6"
6
6
  },
7
7
  "devDependencies": {
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "type": "module",
43
43
  "types": "build/index.d.ts",
44
- "version": "0.42.0",
44
+ "version": "0.43.1",
45
45
  "scripts": {
46
46
  "agent:bench": "vitest bench --run",
47
47
  "agent:test": "pnpm typecheck && vitest run",
@@ -196,7 +196,7 @@ function write(access: ts.ElementAccessExpression, ctx: ArrayTransformResult): v
196
196
 
197
197
  // Reactive arrays are recognized by type, so `.length` and element writes compile wherever the
198
198
  // array came from: a local reactive([...]), an import, an object property, a parameter
199
- export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): ArrayTransformResult => {
199
+ export default async (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: IsReactiveCall): Promise<ArrayTransformResult> => {
200
200
  let candidates: Candidates = { lengths: [], reassignments: [], writes: [] },
201
201
  result: ArrayTransformResult = { failures: [], namespaced: false, reactive: [], replacements: [] };
202
202
 
@@ -205,7 +205,7 @@ export default (sourceFile: ts.SourceFile, bindings: Bindings, isReactiveCall: I
205
205
  result.namespaced = result.replacements.length > 0;
206
206
  collect(sourceFile, candidates);
207
207
 
208
- let reactive = scope.arrays(bindings, [
208
+ let reactive = await scope.arrays(bindings, [
209
209
  ...candidates.lengths.map(access => access.expression),
210
210
  ...candidates.reassignments.map(assignment => assignment.left),
211
211
  ...candidates.writes.map(access => access.expression)