@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
@@ -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
  });
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, flush, peek, read, signal, write } from '~/system';
2
+ import { computed, dispose, effect, flush, peek, read, signal, write } from '~/system';
3
3
 
4
4
 
5
5
  // Depth chosen well past the V8 call-stack frame limit (~10-15k): a recursive notify/update/dispose
@@ -44,7 +44,7 @@ describe('deep graphs', () => {
44
44
 
45
45
  expect(seen).toBe(DEPTH + 2);
46
46
 
47
- stop();
47
+ dispose(stop);
48
48
  }, 30000);
49
49
 
50
50
  it('a wide fan-out with deep branches settles (notify sibling traversal)', () => {
@@ -90,7 +90,7 @@ describe('deep graphs', () => {
90
90
 
91
91
  expect(observed).toBe(width * (2 + branchDepth));
92
92
 
93
- stop();
93
+ dispose(stop);
94
94
  });
95
95
 
96
96
  it('a diamond recomputes the sink once per source change (glitch-free, no redundant checks)', () => {
@@ -134,7 +134,7 @@ describe('deep graphs', () => {
134
134
  expect(cRuns).toBe(2);
135
135
  expect(peek(c)).toBe(7);
136
136
 
137
- stop();
137
+ dispose(stop);
138
138
  });
139
139
 
140
140
  it('a dynamic conditional dep does not over-recompute (DIRTY-break)', () => {
@@ -174,7 +174,7 @@ describe('deep graphs', () => {
174
174
 
175
175
  expect(cRuns).toBe(2);
176
176
 
177
- stop();
177
+ dispose(stop);
178
178
  });
179
179
 
180
180
  it('teardown of a 200k-deep chain completes without overflow; cleanup fires once per node', () => {
@@ -202,7 +202,7 @@ describe('deep graphs', () => {
202
202
  read(chain[DEPTH - 1]);
203
203
  });
204
204
 
205
- stop();
205
+ dispose(stop);
206
206
 
207
207
  expect(cleanups).toBe(DEPTH);
208
208
 
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, onCleanup, read, root, signal, write } from '~/system';
2
+ import { computed, dispose, effect, onCleanup, read, root, signal, write } from '~/system';
3
3
  import { ReactiveArray } from '~/reactive/array';
4
4
  import { ReactiveObject } from '~/reactive/object';
5
5
  import { waitFor } from './lib/wait-for';
@@ -14,7 +14,7 @@ describe('effect patterns', () => {
14
14
  let s = signal(0),
15
15
  runs = 0;
16
16
 
17
- let dispose = effect(() => {
17
+ let node = effect(() => {
18
18
  read(s);
19
19
  runs++;
20
20
  });
@@ -32,7 +32,7 @@ describe('effect patterns', () => {
32
32
 
33
33
  expect(runs).toBe(4);
34
34
 
35
- dispose();
35
+ dispose(node);
36
36
 
37
37
  write(s, 4);
38
38
  write(s, 5);
@@ -49,7 +49,7 @@ describe('effect patterns', () => {
49
49
  cleanupCalls = 0,
50
50
  effectValues: number[] = [];
51
51
 
52
- let dispose = effect(() => {
52
+ let node = effect(() => {
53
53
  let current = read(s);
54
54
 
55
55
  effectValues.push(current);
@@ -74,7 +74,7 @@ describe('effect patterns', () => {
74
74
  expect(effectValues).toEqual([0, 1, 2]);
75
75
  expect(cleanupCalls).toBe(2);
76
76
 
77
- dispose();
77
+ dispose(node);
78
78
 
79
79
  expect(cleanupCalls).toBe(3);
80
80
  });
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, read, signal, write } from '~/system';
2
+ import { computed, dispose, effect, read, signal, write } from '~/system';
3
3
  import { tick } from './lib/wait-for';
4
4
 
5
5
 
@@ -146,7 +146,7 @@ describe('effect error contract', () => {
146
146
  expect(captured.length).toBe(1);
147
147
  expect((captured[0] as Error).message).toBe('effect boom');
148
148
 
149
- stop();
149
+ dispose(stop);
150
150
  });
151
151
 
152
152
  it('effect rethrows on every failure, not just the first', async () => {
@@ -171,7 +171,7 @@ describe('effect error contract', () => {
171
171
  expect((captured[0] as Error).message).toBe('boom 1');
172
172
  expect((captured[1] as Error).message).toBe('boom 3');
173
173
 
174
- stop();
174
+ dispose(stop);
175
175
  });
176
176
 
177
177
  it('effect catching its own error internally rethrows nothing', async () => {
@@ -196,6 +196,6 @@ describe('effect error contract', () => {
196
196
  expect(errors.length).toBe(1);
197
197
  expect((errors[0] as Error).message).toBe('handled');
198
198
 
199
- stop();
199
+ dispose(stop);
200
200
  });
201
201
  });
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, effect, flush, peek, read, signal, write } from '~/system';
2
+ import { computed, dispose, effect, flush, peek, read, signal, write } from '~/system';
3
3
  import { waitFor } from './lib/wait-for';
4
4
 
5
5
 
@@ -85,7 +85,7 @@ describe('invalidate()', () => {
85
85
  expect(fetches).toBe(2);
86
86
  expect(read(node)).toBe(2);
87
87
 
88
- stop();
88
+ dispose(stop);
89
89
  });
90
90
 
91
91
  it('forces a side-effecting computed (the public effect encoding) to re-run without propagating', () => {
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { computed, owner as current, dispose, effect, flush, hasOwner, onCleanup, read, root, signal, untrack, write } from '~/system';
2
+ import { computed, owner as current, dispose, effect, flush, onCleanup, read, root, signal, untrack, write } from '~/system';
3
3
  import type { Computed } from '~/system';
4
4
  import { captureUncaught } from './lib/uncaught';
5
5
 
@@ -74,7 +74,7 @@ describe('ownership unlink', () => {
74
74
 
75
75
  it('child effect disposed early unlinks from its owner', () => {
76
76
  let runs = 0,
77
- stops: VoidFunction[] = [],
77
+ stops: Computed<unknown>[] = [],
78
78
  parent = owner(() => {
79
79
  for (let i = 0; i < 3; i++) {
80
80
  stops.push(effect(() => {
@@ -85,12 +85,12 @@ describe('ownership unlink', () => {
85
85
 
86
86
  expect(registrations(parent)).toBe(3);
87
87
 
88
- stops[0]();
88
+ dispose(stops[0]);
89
89
  expect(registrations(parent)).toBe(2);
90
90
  expect(parent.owned!.owner).toBe(parent);
91
91
 
92
- stops[2]();
93
- stops[1]();
92
+ dispose(stops[2]);
93
+ dispose(stops[1]);
94
94
  expect(registrations(parent)).toBe(0);
95
95
 
96
96
  dispose(parent);
@@ -155,9 +155,9 @@ describe('ownership churn', () => {
155
155
  it('10k child effects created + disposed leave no registrations behind', () => {
156
156
  let parent = owner(() => {
157
157
  for (let i = 0; i < 10000; i++) {
158
- effect(() => {
158
+ dispose(effect(() => {
159
159
  onCleanup(() => {});
160
- })();
160
+ }));
161
161
  }
162
162
  });
163
163
 
@@ -266,7 +266,7 @@ describe('ownership ordering', () => {
266
266
  it('an early-disposed child leaves the remaining order intact', () => {
267
267
  let log: string[] = [],
268
268
  s = signal(0),
269
- stop: VoidFunction | null = null;
269
+ stop: Computed<unknown> | null = null;
270
270
 
271
271
  effect(() => {
272
272
  if (read(s) === 0) {
@@ -285,7 +285,7 @@ describe('ownership ordering', () => {
285
285
  });
286
286
 
287
287
  flush();
288
- stop!();
288
+ dispose(stop!);
289
289
  expect(log).toEqual(['y']);
290
290
 
291
291
  write(s, 1);
@@ -322,7 +322,7 @@ describe('ownership ordering', () => {
322
322
  describe('ownership double dispose', () => {
323
323
  it('disposing a child twice is a no-op and leaves siblings registered', () => {
324
324
  let runs = 0,
325
- stop: VoidFunction | null = null,
325
+ stop: Computed<unknown> | null = null,
326
326
  parent = owner(() => {
327
327
  effect(() => {});
328
328
  stop = effect(() => {
@@ -331,8 +331,8 @@ describe('ownership double dispose', () => {
331
331
  effect(() => {});
332
332
  });
333
333
 
334
- stop!();
335
- stop!();
334
+ dispose(stop!);
335
+ dispose(stop!);
336
336
 
337
337
  expect(runs).toBe(1);
338
338
  expect(registrations(parent)).toBe(2);
@@ -364,7 +364,7 @@ describe('ownership double dispose', () => {
364
364
  describe('ownership dispose mid-drain', () => {
365
365
  it('a child cleanup disposing its parent and a sibling runs every cleanup once', () => {
366
366
  let counts = { a: 0, b: 0, c: 0, parent: 0 },
367
- stopB: VoidFunction | null = null,
367
+ stopB: Computed<unknown> | null = null,
368
368
  stopParent: VoidFunction | null = null;
369
369
 
370
370
  root((d) => {
@@ -373,7 +373,7 @@ describe('ownership dispose mid-drain', () => {
373
373
  effect(() => {
374
374
  onCleanup(() => {
375
375
  counts.a++;
376
- stopB!();
376
+ dispose(stopB!);
377
377
  stopParent!();
378
378
  });
379
379
  });
@@ -393,7 +393,7 @@ describe('ownership dispose mid-drain', () => {
393
393
  it('a child disposing its parent from its own cleanup unlinks first', () => {
394
394
  let counts = { child: 0, parent: 0, sibling: 0 },
395
395
  parent: Computed<unknown> | null = null,
396
- stop: VoidFunction | null = null;
396
+ stop: Computed<unknown> | null = null;
397
397
 
398
398
  parent = owner(() => {
399
399
  onCleanup(() => { counts.parent++; });
@@ -408,7 +408,7 @@ describe('ownership dispose mid-drain', () => {
408
408
  });
409
409
  });
410
410
 
411
- stop!();
411
+ dispose(stop!);
412
412
 
413
413
  expect(counts).toEqual({ child: 1, parent: 1, sibling: 1 });
414
414
  expect(registrations(parent)).toBe(0);
@@ -417,14 +417,14 @@ describe('ownership dispose mid-drain', () => {
417
417
  it('a re-run where one child disposes a later sibling skips it', () => {
418
418
  let log: string[] = [],
419
419
  s = signal(0),
420
- stopY: VoidFunction | null = null;
420
+ stopY: Computed<unknown> | null = null;
421
421
 
422
422
  effect(() => {
423
423
  if (read(s) === 0) {
424
424
  effect(() => {
425
425
  onCleanup(() => {
426
426
  log.push('x');
427
- stopY!();
427
+ dispose(stopY!);
428
428
  });
429
429
  });
430
430
  stopY = effect(() => {
@@ -550,20 +550,20 @@ describe('ownership throwing cleanup', () => {
550
550
  });
551
551
 
552
552
 
553
- describe('hasOwner', () => {
553
+ describe('owner() as an ownership probe', () => {
554
554
  it('is true inside a tracking root or a run (also untracked), false outside and in a detached root', () => {
555
555
  let seen: boolean[] = [],
556
556
  stop = effect(() => {
557
- seen.push(hasOwner());
558
- seen.push(untrack(() => hasOwner()));
559
- seen.push(untrack(() => root(() => hasOwner())));
557
+ seen.push(current() !== null);
558
+ seen.push(untrack(() => current() !== null));
559
+ seen.push(untrack(() => root(() => current() !== null)));
560
560
  });
561
561
 
562
- seen.push(hasOwner());
563
- seen.push(untrack(() => hasOwner()));
564
- root((d) => { seen.push(hasOwner()); d(); });
565
- root(() => { seen.push(hasOwner()); });
566
- stop();
562
+ seen.push(current() !== null);
563
+ seen.push(untrack(() => current() !== null));
564
+ root((d) => { seen.push(current() !== null); d(); });
565
+ root(() => { seen.push(current() !== null); });
566
+ dispose(stop);
567
567
 
568
568
  expect(seen).toEqual([true, true, false, false, false, true, false]);
569
569
  });
@@ -573,12 +573,12 @@ describe('hasOwner', () => {
573
573
  seen: boolean[] = [],
574
574
  stop = effect(() => {
575
575
  read(s);
576
- seen.push(untrack(() => hasOwner()));
576
+ seen.push(untrack(() => current() !== null));
577
577
  });
578
578
 
579
579
  write(s, 1);
580
580
  await Promise.resolve();
581
- stop();
581
+ dispose(stop);
582
582
 
583
583
  expect(seen).toEqual([true, true]);
584
584
  });
@@ -635,7 +635,7 @@ describe('root with an owner', () => {
635
635
  effect(() => { read(s); });
636
636
  }, node);
637
637
 
638
- expect(hasOwner()).toBe(false);
638
+ expect(current() !== null).toBe(false);
639
639
  expect(registrations(node!)).toBe(2);
640
640
 
641
641
  write(s, 1);