@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.
@@ -1,4 +1,4 @@
1
- import { describe, expect, it } from 'vitest';
1
+ import { beforeAll, describe, expect, it } from 'vitest';
2
2
  import { ts } from '@esportsplus/typescript';
3
3
  import { coordinator, languageService } from '@esportsplus/typescript/compiler';
4
4
  import type { Plugin, ReplacementIntent } from '@esportsplus/typescript/compiler';
@@ -10,7 +10,7 @@ import pipeline from '~/compiler/index';
10
10
  // Stands in for template: rewrites every `tag` tagged template, which reactivity's own rewrites may contain
11
11
  const FOREIGN: Plugin = {
12
12
  name: 'foreign',
13
- transform: ({ sourceFile }) => {
13
+ transform: async ({ sourceFile }) => {
14
14
  let replacements: ReplacementIntent[] = [],
15
15
  visit = (node: ts.Node): void => {
16
16
  if (ts.isTaggedTemplateExpression(node) && ts.isIdentifier(node.tag) && node.tag.text === 'tag') {
@@ -29,39 +29,43 @@ const FOREIGN: Plugin = {
29
29
  const PRELUDE = "import { reactive } from '@esportsplus/reactivity';\n";
30
30
 
31
31
 
32
- function compile(code: string, plugins: Plugin[] = [pipeline]): string {
32
+ async function compile(code: string, plugins: Plugin[] = [pipeline]): Promise<string> {
33
33
  code = PRELUDE + code;
34
34
 
35
- let { checker, program, sourceFile } = languageService.scratch(process.cwd() + '/composed.ts', code);
35
+ let { checker, program, sourceFile } = await languageService.scratch(process.cwd() + '/composed.ts', code);
36
36
 
37
- return coordinator.transform(plugins, code, sourceFile, { checker, program }, process.cwd(), new Map()).code;
37
+ return (await coordinator.transform(plugins, code, sourceFile, { checker, program }, process.cwd(), new Map())).code;
38
38
  }
39
39
 
40
40
 
41
41
  describe('nested rewrites', () => {
42
- let output = compile([
43
- 'let x = reactive(0);',
44
- 'let y = reactive(1);',
45
- 'x = y;',
46
- 'x += y;',
47
- 'let arr = reactive([y]);',
48
- 'arr[0] = y;',
49
- 'arr[1] += y;',
50
- 'arr[y] = 2;',
51
- 'let at = arr[y];',
52
- 'arr.length = y;',
53
- 'arr = [y];',
54
- 'let o = reactive({ a: y, items: [y] });',
55
- 'let s = reactive({ ...{ b: 1 }, c: y });',
56
- 'let e = { y: { y } };'
57
- ].join('\n'));
58
-
59
- it('reads a signal on the right of a write to another', () => {
42
+ let output: string;
43
+
44
+ beforeAll(async () => {
45
+ output = await compile([
46
+ 'let x = reactive(0);',
47
+ 'let y = reactive(1);',
48
+ 'x = y;',
49
+ 'x += y;',
50
+ 'let arr = reactive([y]);',
51
+ 'arr[0] = y;',
52
+ 'arr[1] += y;',
53
+ 'arr[y] = 2;',
54
+ 'let at = arr[y];',
55
+ 'arr.length = y;',
56
+ 'arr = [y];',
57
+ 'let o = reactive({ a: y, items: [y] });',
58
+ 'let s = reactive({ ...{ b: 1 }, c: y });',
59
+ 'let e = { y: { y } };'
60
+ ].join('\n'));
61
+ });
62
+
63
+ it('reads a signal on the right of a write to another', async () => {
60
64
  expect(output).toContain(`${NAMESPACE}.write(x, ${NAMESPACE}.read(y));`);
61
65
  expect(output).toContain(`${NAMESPACE}.write(x, x.value + ${NAMESPACE}.read(y));`);
62
66
  });
63
67
 
64
- it('reads a signal inside array lowerings', () => {
68
+ it('reads a signal inside array lowerings', async () => {
65
69
  expect(output).toContain(`let arr = new ${NAMESPACE}.ReactiveArray([${NAMESPACE}.read(y)]);`);
66
70
  expect(output).toContain(`arr.$set(0, ${NAMESPACE}.read(y));`);
67
71
  expect(output).toContain(`((_a, _k) => _a.$set(_k, _a[_k] + (${NAMESPACE}.read(y))))(arr, 1);`);
@@ -71,7 +75,7 @@ describe('nested rewrites', () => {
71
75
  expect(output).toContain(`arr.splice(0, arr.length, ...[${NAMESPACE}.read(y)]);`);
72
76
  });
73
77
 
74
- it('reads a signal inside object lowerings', () => {
78
+ it('reads a signal inside object lowerings', async () => {
75
79
  expect(output).toMatch(new RegExp(`let o = +new ReactiveObject_\\w+\\(${NAMESPACE}\\.read\\(y\\), \\[${NAMESPACE}\\.read\\(y\\)\\]\\);`));
76
80
  expect(output).toContain(`let s = ${NAMESPACE}.reactive({ ...{ b: 1 }, c: ${NAMESPACE}.read(y) });`);
77
81
  expect(output).toContain(`let e = { y: { y: ${NAMESPACE}.read(y) } };`);
@@ -93,8 +97,8 @@ describe('composition with another plugin', () => {
93
97
  ].join('\n');
94
98
 
95
99
  for (let [order, plugins] of [['before', [FOREIGN, pipeline]], ['after', [pipeline, FOREIGN]]] as const) {
96
- it(`keeps the other plugin's replacements inside every lowering when it runs ${order}`, () => {
97
- let output = compile(code, [...plugins]);
100
+ it(`keeps the other plugin's replacements inside every lowering when it runs ${order}`, async () => {
101
+ let output = await compile(code, [...plugins]);
98
102
 
99
103
  expect(output).not.toContain('tag`');
100
104
  expect(output).toContain(`let count = ${NAMESPACE}.signal(TAGGED);`);
@@ -88,8 +88,8 @@ afterEach(() => {
88
88
 
89
89
 
90
90
  describe('compiler coverage', () => {
91
- it('compiles reads of bindings imported by name, namespace, default, barrel and string key', () => {
92
- let output = project(FILES).transform('reads.ts')!.code;
91
+ it('compiles reads of bindings imported by name, namespace, default, barrel and string key', async () => {
92
+ let output = (await project(FILES).transform('reads.ts'))!.code;
93
93
 
94
94
  expect(output).toContain(`export let a = () => ${NAMESPACE}.read(count) + 1;`);
95
95
  expect(output).toContain(`export let b = () => ${NAMESPACE}.read(S.count) + 1;`);
@@ -101,8 +101,8 @@ describe('compiler coverage', () => {
101
101
  expect(output).toContain(`export let h = () => ${NAMESPACE}.read(S['count']) + 1;`);
102
102
  });
103
103
 
104
- it('compiles reactive() reached through renames, namespaces, string keys and const aliases', () => {
105
- let output = project(FILES).transform('entries.ts')!.code;
104
+ it('compiles reactive() reached through renames, namespaces, string keys and const aliases', async () => {
105
+ let output = (await project(FILES).transform('entries.ts'))!.code;
106
106
 
107
107
  for (let name of ['a', 'b', 'c', 'd', 'e', 'f']) {
108
108
  expect(output).toMatch(new RegExp(`export let ${name} = ${NAMESPACE}\\.signal\\(0\\);`));
@@ -111,13 +111,13 @@ describe('compiler coverage', () => {
111
111
  expect(output).toContain(`${NAMESPACE}.read(a) + ${NAMESPACE}.read(b)`);
112
112
  });
113
113
 
114
- it('passes every array property of a reactive object to its constructor', () => {
115
- let output = project(FILES).transform('state.ts')!.code;
114
+ it('passes every array property of a reactive object to its constructor', async () => {
115
+ let output = (await project(FILES).transform('state.ts'))!.code;
116
116
 
117
117
  expect(output).toMatch(/export let store = +new ReactiveObject_\w+\(\[1\]\);/);
118
118
  });
119
119
 
120
- it('fails the build on every use of reactive that cannot be compiled, with its location', () => {
120
+ it('fails the build on every use of reactive that cannot be compiled, with its location', async () => {
121
121
  let files = {
122
122
  'escape.ts': [
123
123
  "import { reactive } from '@esportsplus/reactivity';",
@@ -128,24 +128,24 @@ describe('compiler coverage', () => {
128
128
  },
129
129
  { transform } = project(files);
130
130
 
131
- expect(() => transform('escape.ts')).toThrow(/escape\.ts:3:6[\s\S]*escape\.ts:4:6/);
131
+ await expect(transform('escape.ts')).rejects.toThrow(/escape\.ts:3:6[\s\S]*escape\.ts:4:6/);
132
132
  });
133
133
 
134
- it('fails the build when a signal is destructured out of a module namespace', () => {
134
+ it('fails the build when a signal is destructured out of a module namespace', async () => {
135
135
  let files = {
136
136
  'copy.ts': "import * as S from './state';\nconst { count } = S;\nexport let n = count;",
137
137
  'state.ts': "import { reactive } from '@esportsplus/reactivity';\nexport let count = reactive(0);"
138
138
  },
139
139
  { transform } = project(files);
140
140
 
141
- expect(() => transform('copy.ts')).toThrow(/copy\.ts:2:9.*destructured/);
141
+ await expect(transform('copy.ts')).rejects.toThrow(/copy\.ts:2:9.*destructured/);
142
142
  });
143
143
 
144
- it('reports the modules a compile relied on so hosts can recompile it when they change', () => {
144
+ it('reports the modules a compile relied on so hosts can recompile it when they change', async () => {
145
145
  let { directory, plugin, transform } = project(FILES),
146
146
  dependent = { id: directory + '/reads.ts' };
147
147
 
148
- transform('reads.ts');
148
+ await transform('reads.ts');
149
149
 
150
150
  let result = plugin.handleHotUpdate({
151
151
  file: directory + '/state.ts',
@@ -67,7 +67,7 @@ const FILES: Record<string, string> = {
67
67
  let fixtures: string[] = [];
68
68
 
69
69
 
70
- function compile(name: string, files: Record<string, string> = FILES): string {
70
+ async function compile(name: string, files: Record<string, string> = FILES): Promise<string> {
71
71
  let directory = mkdtempSync(join(process.cwd(), '.fixture-origins-')).replace(/\\/g, '/');
72
72
 
73
73
  fixtures.push(directory);
@@ -85,7 +85,7 @@ function compile(name: string, files: Record<string, string> = FILES): string {
85
85
 
86
86
  plugin.configResolved({ command: 'build', root: directory });
87
87
 
88
- return plugin.transform.handler(files[name], directory + '/' + name)?.code ?? files[name];
88
+ return (await plugin.transform.handler(files[name], directory + '/' + name))?.code ?? files[name];
89
89
  }
90
90
 
91
91
 
@@ -99,58 +99,58 @@ afterEach(() => {
99
99
 
100
100
 
101
101
  describe('origins reached under any name', () => {
102
- it('compiles reactive() imported under a local alias, and leaves a parameter shadowing it', () => {
103
- let output = compile('local.ts');
102
+ it('compiles reactive() imported under a local alias, and leaves a parameter shadowing it', async () => {
103
+ let output = await compile('local.ts');
104
104
 
105
105
  expect(output).toContain(`export let count = ${NAMESPACE}.signal(1);`);
106
106
  expect(output).toContain(`export let next = ${NAMESPACE}.read(count) + 1;`);
107
107
  expect(output).toContain('export function shadow(x: (n: number) => number) { return x(1); }');
108
108
  });
109
109
 
110
- it('reads a let assigned with reactive() later, before and after the assignment', () => {
111
- let output = compile('local.ts');
110
+ it('reads a let assigned with reactive() later, before and after the assignment', async () => {
111
+ let output = await compile('local.ts');
112
112
 
113
113
  expect(output).toContain(`export function before() { return ${NAMESPACE}.read(late); }`);
114
114
  expect(output).toContain(`late = ${NAMESPACE}.signal(0);`);
115
115
  expect(output).toContain(`export let after = ${NAMESPACE}.read(late) + 1;`);
116
116
  });
117
117
 
118
- it('reads a let assigned with reactive() in another module', () => {
119
- let output = compile('consumer.ts');
118
+ it('reads a let assigned with reactive() in another module', async () => {
119
+ let output = await compile('consumer.ts');
120
120
 
121
121
  expect(output).toContain(`${NAMESPACE}.read(later)`);
122
122
  expect(output).toContain(`${NAMESPACE}.read(app.later)`);
123
123
  });
124
124
 
125
- it('compiles reactive() re-exported renamed through a central module, by name and through its namespace', () => {
126
- let output = compile('consumer.ts');
125
+ it('compiles reactive() re-exported renamed through a central module, by name and through its namespace', async () => {
126
+ let output = await compile('consumer.ts');
127
127
 
128
128
  for (let name of ['a', 'b', 'c', 'd']) {
129
129
  expect(output).toContain(`export let ${name} = ${NAMESPACE}.signal(0);`);
130
130
  }
131
131
  });
132
132
 
133
- it('compiles reactive() held by an exported const', () => {
134
- expect(compile('consumer.ts')).toContain(`export let e = ${NAMESPACE}.signal(0);`);
133
+ it('compiles reactive() held by an exported const', async () => {
134
+ expect(await compile('consumer.ts')).toContain(`export let e = ${NAMESPACE}.signal(0);`);
135
135
  });
136
136
 
137
- it('reads a default export re-exported renamed, and bindings through export * two deep', () => {
138
- let output = compile('consumer.ts');
137
+ it('reads a default export re-exported renamed, and bindings through export * two deep', async () => {
138
+ let output = await compile('consumer.ts');
139
139
 
140
140
  expect(output).toContain(`export let f = () => ${NAMESPACE}.read(fallback) + ${NAMESPACE}.read(deepCount) + ${NAMESPACE}.read(later);`);
141
141
  expect(output).toContain(`export let g = () => ${NAMESPACE}.read(app.later) + ${NAMESPACE}.read(app.deepCount);`);
142
142
  });
143
143
 
144
- it('compiles reactive() reached through export * two deep', () => {
145
- let output = compile('star.ts');
144
+ it('compiles reactive() reached through export * two deep', async () => {
145
+ let output = await compile('star.ts');
146
146
 
147
147
  expect(output).toContain(`export let s = ${NAMESPACE}.signal(0);`);
148
148
  expect(output).toContain(`export let t = ${NAMESPACE}.read(s) + 1;`);
149
149
  });
150
150
 
151
- it('leaves a module that imports nothing untouched', () => {
151
+ it('leaves a module that imports nothing untouched', async () => {
152
152
  let code = 'let reactive = (n: number) => n;\nexport let x = reactive(1);';
153
153
 
154
- expect(compile('plain.ts', { ...FILES, 'plain.ts': code })).toBe(code);
154
+ expect(await compile('plain.ts', { ...FILES, 'plain.ts': code })).toBe(code);
155
155
  });
156
156
  });
@@ -76,7 +76,7 @@ const FILES: Record<string, string> = {
76
76
  let fixtures: string[] = [];
77
77
 
78
78
 
79
- function compile(name: string): string | null {
79
+ async function compile(name: string): Promise<string | null> {
80
80
  let directory = mkdtempSync(join(process.cwd(), '.fixture-output-')).replace(/\\/g, '/');
81
81
 
82
82
  fixtures.push(directory);
@@ -94,7 +94,7 @@ function compile(name: string): string | null {
94
94
 
95
95
  plugin.configResolved({ command: 'build', root: directory });
96
96
 
97
- return plugin.transform.handler(FILES[name], directory + '/' + name)?.code ?? null;
97
+ return (await plugin.transform.handler(FILES[name], directory + '/' + name))?.code ?? null;
98
98
  }
99
99
 
100
100
 
@@ -109,8 +109,8 @@ afterEach(() => {
109
109
 
110
110
  describe('compiled output', () => {
111
111
  for (let name of Object.keys(FILES)) {
112
- it(name, () => {
113
- expect(compile(name)).toMatchSnapshot();
112
+ it(name, async () => {
113
+ expect(await compile(name)).toMatchSnapshot();
114
114
  });
115
115
  }
116
116
  });
@@ -367,3 +367,71 @@ describe('scheduling from a top-level creation run', () => {
367
367
  await waitFor(() => inner === 1, 'the inner effect runs');
368
368
  });
369
369
  });
370
+
371
+
372
+ describe('deferred inner effects', () => {
373
+ // The parent tracks a dependency before creating the inner effect, so the inner effect's creation run is deferred.
374
+ // That run sets its first value; it must not count as a change the parent depends on, or the parent re-runs,
375
+ // creates a new inner effect, and loops. The guard ends such a loop so a regression fails instead of hanging.
376
+ it('an inner effect returning a value does not re-run its parent', async () => {
377
+ let inner = 0,
378
+ outer = 0,
379
+ s = signal(0);
380
+
381
+ effect(() => {
382
+ read(s);
383
+ outer++;
384
+
385
+ if (outer > 20) {
386
+ return;
387
+ }
388
+
389
+ effect(() => {
390
+ inner++;
391
+
392
+ return true;
393
+ });
394
+ });
395
+
396
+ await waitFor(() => inner >= 1, 'the inner effect runs');
397
+ await Promise.resolve();
398
+ await Promise.resolve();
399
+
400
+ expect(outer).toBe(1);
401
+ expect(inner).toBe(1);
402
+
403
+ write(s, 1);
404
+ await waitFor(() => outer === 2, 'the parent re-runs on its own dependency');
405
+ await Promise.resolve();
406
+ await Promise.resolve();
407
+
408
+ expect(outer).toBe(2);
409
+ expect(inner).toBe(2);
410
+ });
411
+
412
+ it('an inner effect with an apply phase does not re-run its parent', async () => {
413
+ let applied: boolean[] = [],
414
+ outer = 0,
415
+ path = signal('/');
416
+
417
+ effect(() => {
418
+ read(path);
419
+ outer++;
420
+
421
+ if (outer > 20) {
422
+ return;
423
+ }
424
+
425
+ effect(() => read(path) === '/', (home) => {
426
+ applied.push(home);
427
+ });
428
+ });
429
+
430
+ await waitFor(() => applied.length >= 1, 'the inner effect applies');
431
+ await Promise.resolve();
432
+ await Promise.resolve();
433
+
434
+ expect(outer).toBe(1);
435
+ expect(applied).toEqual([true]);
436
+ });
437
+ });