signalium 0.3.8 → 1.0.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.
- package/.turbo/turbo-build.log +3 -3
- package/CHANGELOG.md +15 -0
- package/build/react.js +19 -0
- package/build/transform.js +19 -0
- package/dist/cjs/config.d.ts +8 -3
- package/dist/cjs/config.d.ts.map +1 -1
- package/dist/cjs/config.js +14 -8
- package/dist/cjs/config.js.map +1 -1
- package/dist/cjs/debug.d.ts +2 -2
- package/dist/cjs/debug.d.ts.map +1 -1
- package/dist/cjs/debug.js +3 -3
- package/dist/cjs/debug.js.map +1 -1
- package/dist/cjs/hooks.d.ts +14 -42
- package/dist/cjs/hooks.d.ts.map +1 -1
- package/dist/cjs/hooks.js +19 -240
- package/dist/cjs/hooks.js.map +1 -1
- package/dist/cjs/index.d.ts +5 -3
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +18 -18
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/internals/async.d.ts +50 -0
- package/dist/cjs/internals/async.d.ts.map +1 -0
- package/dist/cjs/internals/async.js +390 -0
- package/dist/cjs/internals/async.js.map +1 -0
- package/dist/cjs/internals/connect.d.ts +4 -0
- package/dist/cjs/internals/connect.d.ts.map +1 -0
- package/dist/cjs/internals/connect.js +37 -0
- package/dist/cjs/internals/connect.js.map +1 -0
- package/dist/cjs/internals/consumer.d.ts +6 -0
- package/dist/cjs/internals/consumer.d.ts.map +1 -0
- package/dist/cjs/internals/consumer.js +13 -0
- package/dist/cjs/internals/consumer.js.map +1 -0
- package/dist/cjs/internals/contexts.d.ts +33 -0
- package/dist/cjs/internals/contexts.d.ts.map +1 -0
- package/dist/cjs/internals/contexts.js +103 -0
- package/dist/cjs/internals/contexts.js.map +1 -0
- package/dist/cjs/internals/derived.d.ts +66 -0
- package/dist/cjs/internals/derived.d.ts.map +1 -0
- package/dist/cjs/internals/derived.js +128 -0
- package/dist/cjs/internals/derived.js.map +1 -0
- package/dist/cjs/internals/dirty.d.ts +5 -0
- package/dist/cjs/internals/dirty.d.ts.map +1 -0
- package/dist/cjs/internals/dirty.js +79 -0
- package/dist/cjs/internals/dirty.js.map +1 -0
- package/dist/cjs/internals/edge.d.ts +32 -0
- package/dist/cjs/internals/edge.d.ts.map +1 -0
- package/dist/cjs/internals/edge.js +59 -0
- package/dist/cjs/internals/edge.js.map +1 -0
- package/dist/cjs/internals/get.d.ts +10 -0
- package/dist/cjs/internals/get.d.ts.map +1 -0
- package/dist/cjs/internals/get.js +255 -0
- package/dist/cjs/internals/get.js.map +1 -0
- package/dist/cjs/internals/scheduling.d.ts +12 -0
- package/dist/cjs/internals/scheduling.d.ts.map +1 -0
- package/dist/cjs/internals/scheduling.js +117 -0
- package/dist/cjs/internals/scheduling.js.map +1 -0
- package/dist/cjs/internals/state.d.ts +18 -0
- package/dist/cjs/internals/state.d.ts.map +1 -0
- package/dist/cjs/internals/state.js +88 -0
- package/dist/cjs/internals/state.js.map +1 -0
- package/dist/cjs/internals/utils/debug-name.d.ts +2 -0
- package/dist/cjs/internals/utils/debug-name.d.ts.map +1 -0
- package/dist/cjs/internals/utils/debug-name.js +14 -0
- package/dist/cjs/internals/utils/debug-name.js.map +1 -0
- package/dist/cjs/internals/utils/equals.d.ts +3 -0
- package/dist/cjs/internals/utils/equals.d.ts.map +1 -0
- package/dist/cjs/internals/utils/equals.js +13 -0
- package/dist/cjs/internals/utils/equals.js.map +1 -0
- package/dist/cjs/internals/utils/hash.d.ts +7 -0
- package/dist/cjs/internals/utils/hash.d.ts.map +1 -0
- package/dist/cjs/internals/utils/hash.js +181 -0
- package/dist/cjs/internals/utils/hash.js.map +1 -0
- package/dist/cjs/internals/utils/stringify.d.ts +3 -0
- package/dist/cjs/internals/utils/stringify.d.ts.map +1 -0
- package/dist/cjs/{utils.js → internals/utils/stringify.js} +5 -27
- package/dist/cjs/internals/utils/stringify.js.map +1 -0
- package/dist/cjs/internals/utils/type-utils.d.ts +6 -0
- package/dist/cjs/internals/utils/type-utils.d.ts.map +1 -0
- package/dist/cjs/internals/utils/type-utils.js +22 -0
- package/dist/cjs/internals/utils/type-utils.js.map +1 -0
- package/dist/cjs/react/context.d.ts +1 -1
- package/dist/cjs/react/context.d.ts.map +1 -1
- package/dist/cjs/react/provider.d.ts +4 -3
- package/dist/cjs/react/provider.d.ts.map +1 -1
- package/dist/cjs/react/provider.js +7 -3
- package/dist/cjs/react/provider.js.map +1 -1
- package/dist/cjs/react/setup.d.ts.map +1 -1
- package/dist/cjs/react/setup.js +2 -1
- package/dist/cjs/react/setup.js.map +1 -1
- package/dist/cjs/react/signal-value.d.ts +5 -1
- package/dist/cjs/react/signal-value.d.ts.map +1 -1
- package/dist/cjs/react/signal-value.js +35 -45
- package/dist/cjs/react/signal-value.js.map +1 -1
- package/dist/cjs/trace.d.ts +32 -28
- package/dist/cjs/trace.d.ts.map +1 -1
- package/dist/cjs/trace.js +14 -16
- package/dist/cjs/trace.js.map +1 -1
- package/dist/cjs/transform.d.ts +6 -0
- package/dist/cjs/transform.d.ts.map +1 -0
- package/dist/cjs/transform.js +92 -0
- package/dist/cjs/transform.js.map +1 -0
- package/dist/cjs/types.d.ts +32 -40
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/esm/config.d.ts +8 -3
- package/dist/esm/config.d.ts.map +1 -1
- package/dist/esm/config.js +12 -7
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/debug.d.ts +2 -2
- package/dist/esm/debug.d.ts.map +1 -1
- package/dist/esm/debug.js +2 -2
- package/dist/esm/debug.js.map +1 -1
- package/dist/esm/hooks.d.ts +14 -42
- package/dist/esm/hooks.d.ts.map +1 -1
- package/dist/esm/hooks.js +17 -226
- package/dist/esm/hooks.js.map +1 -1
- package/dist/esm/index.d.ts +5 -3
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +5 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internals/async.d.ts +50 -0
- package/dist/esm/internals/async.d.ts.map +1 -0
- package/dist/esm/internals/async.js +383 -0
- package/dist/esm/internals/async.js.map +1 -0
- package/dist/esm/internals/connect.d.ts +4 -0
- package/dist/esm/internals/connect.d.ts.map +1 -0
- package/dist/esm/internals/connect.js +33 -0
- package/dist/esm/internals/connect.js.map +1 -0
- package/dist/esm/internals/consumer.d.ts +6 -0
- package/dist/esm/internals/consumer.d.ts.map +1 -0
- package/dist/esm/internals/consumer.js +9 -0
- package/dist/esm/internals/consumer.js.map +1 -0
- package/dist/esm/internals/contexts.d.ts +33 -0
- package/dist/esm/internals/contexts.d.ts.map +1 -0
- package/dist/esm/internals/contexts.js +92 -0
- package/dist/esm/internals/contexts.js.map +1 -0
- package/dist/esm/internals/derived.d.ts +66 -0
- package/dist/esm/internals/derived.d.ts.map +1 -0
- package/dist/esm/internals/derived.js +118 -0
- package/dist/esm/internals/derived.js.map +1 -0
- package/dist/esm/internals/dirty.d.ts +5 -0
- package/dist/esm/internals/dirty.d.ts.map +1 -0
- package/dist/esm/internals/dirty.js +75 -0
- package/dist/esm/internals/dirty.js.map +1 -0
- package/dist/esm/internals/edge.d.ts +32 -0
- package/dist/esm/internals/edge.d.ts.map +1 -0
- package/dist/esm/internals/edge.js +54 -0
- package/dist/esm/internals/edge.js.map +1 -0
- package/dist/esm/internals/get.d.ts +10 -0
- package/dist/esm/internals/get.d.ts.map +1 -0
- package/dist/esm/internals/get.js +247 -0
- package/dist/esm/internals/get.js.map +1 -0
- package/dist/esm/internals/scheduling.d.ts +12 -0
- package/dist/esm/internals/scheduling.d.ts.map +1 -0
- package/dist/esm/internals/scheduling.js +106 -0
- package/dist/esm/internals/scheduling.js.map +1 -0
- package/dist/esm/internals/state.d.ts +18 -0
- package/dist/esm/internals/state.d.ts.map +1 -0
- package/dist/esm/internals/state.js +82 -0
- package/dist/esm/internals/state.js.map +1 -0
- package/dist/esm/internals/utils/debug-name.d.ts +2 -0
- package/dist/esm/internals/utils/debug-name.d.ts.map +1 -0
- package/dist/esm/internals/utils/debug-name.js +11 -0
- package/dist/esm/internals/utils/debug-name.js.map +1 -0
- package/dist/esm/internals/utils/equals.d.ts +3 -0
- package/dist/esm/internals/utils/equals.d.ts.map +1 -0
- package/dist/esm/internals/utils/equals.js +9 -0
- package/dist/esm/internals/utils/equals.js.map +1 -0
- package/dist/esm/internals/utils/hash.d.ts +7 -0
- package/dist/esm/internals/utils/hash.d.ts.map +1 -0
- package/dist/esm/internals/utils/hash.js +174 -0
- package/dist/esm/internals/utils/hash.js.map +1 -0
- package/dist/esm/internals/utils/stringify.d.ts +3 -0
- package/dist/esm/internals/utils/stringify.d.ts.map +1 -0
- package/dist/esm/{utils.js → internals/utils/stringify.js} +4 -25
- package/dist/esm/internals/utils/stringify.js.map +1 -0
- package/dist/esm/internals/utils/type-utils.d.ts +6 -0
- package/dist/esm/internals/utils/type-utils.d.ts.map +1 -0
- package/dist/esm/internals/utils/type-utils.js +15 -0
- package/dist/esm/internals/utils/type-utils.js.map +1 -0
- package/dist/esm/react/context.d.ts +1 -1
- package/dist/esm/react/context.d.ts.map +1 -1
- package/dist/esm/react/provider.d.ts +4 -3
- package/dist/esm/react/provider.d.ts.map +1 -1
- package/dist/esm/react/provider.js +6 -2
- package/dist/esm/react/provider.js.map +1 -1
- package/dist/esm/react/setup.d.ts.map +1 -1
- package/dist/esm/react/setup.js +3 -2
- package/dist/esm/react/setup.js.map +1 -1
- package/dist/esm/react/signal-value.d.ts +5 -1
- package/dist/esm/react/signal-value.d.ts.map +1 -1
- package/dist/esm/react/signal-value.js +34 -45
- package/dist/esm/react/signal-value.js.map +1 -1
- package/dist/esm/trace.d.ts +32 -28
- package/dist/esm/trace.d.ts.map +1 -1
- package/dist/esm/trace.js +13 -15
- package/dist/esm/trace.js.map +1 -1
- package/dist/esm/transform.d.ts +6 -0
- package/dist/esm/transform.d.ts.map +1 -0
- package/dist/esm/transform.js +89 -0
- package/dist/esm/transform.js.map +1 -0
- package/dist/esm/types.d.ts +32 -40
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +23 -4
- package/src/__tests__/__snapshots__/context.test.ts.snap +2101 -0
- package/src/__tests__/__snapshots__/nesting.test.ts.snap +16201 -0
- package/src/__tests__/__snapshots__/params-and-state.test.ts.snap +1879 -0
- package/src/__tests__/async-task.test.ts +327 -0
- package/src/__tests__/context.test.ts +517 -0
- package/src/__tests__/nesting.test.ts +298 -0
- package/src/__tests__/params-and-state.test.ts +230 -0
- package/src/__tests__/reactive-async.test.ts +548 -0
- package/src/__tests__/reactive-sync.test.ts +130 -0
- package/src/__tests__/subscription.test.ts +510 -0
- package/src/__tests__/utils/async.ts +1 -1
- package/src/__tests__/utils/instrumented-hooks.ts +229 -124
- package/src/__tests__/utils/permute.ts +25 -14
- package/src/config.ts +19 -9
- package/src/debug.ts +2 -2
- package/src/hooks.ts +46 -380
- package/src/index.ts +7 -24
- package/src/internals/async.ts +556 -0
- package/src/internals/connect.ts +41 -0
- package/src/internals/consumer.ts +13 -0
- package/src/internals/contexts.ts +133 -0
- package/src/internals/derived.ts +208 -0
- package/src/internals/dirty.ts +91 -0
- package/src/internals/edge.ts +109 -0
- package/src/internals/get.ts +298 -0
- package/src/internals/scheduling.ts +140 -0
- package/src/internals/state.ts +111 -0
- package/src/internals/utils/debug-name.ts +14 -0
- package/src/internals/utils/equals.ts +12 -0
- package/src/internals/utils/hash.ts +221 -0
- package/src/{utils.ts → internals/utils/stringify.ts} +3 -29
- package/src/internals/utils/type-utils.ts +19 -0
- package/src/react/__tests__/async.test.tsx +704 -0
- package/src/react/__tests__/basic.test.tsx +95 -0
- package/src/react/__tests__/contexts.test.tsx +99 -0
- package/src/react/__tests__/subscriptions.test.tsx +49 -0
- package/src/react/__tests__/utils.tsx +40 -0
- package/src/react/context.ts +1 -1
- package/src/react/provider.tsx +12 -4
- package/src/react/setup.ts +3 -2
- package/src/react/signal-value.ts +47 -67
- package/src/trace.ts +43 -38
- package/src/transform.ts +113 -0
- package/src/types.ts +56 -46
- package/transform.js +19 -0
- package/vitest.workspace.ts +38 -2
- package/dist/cjs/scheduling.d.ts +0 -11
- package/dist/cjs/scheduling.d.ts.map +0 -1
- package/dist/cjs/scheduling.js +0 -108
- package/dist/cjs/scheduling.js.map +0 -1
- package/dist/cjs/signals.d.ts +0 -73
- package/dist/cjs/signals.d.ts.map +0 -1
- package/dist/cjs/signals.js +0 -632
- package/dist/cjs/signals.js.map +0 -1
- package/dist/cjs/utils.d.ts +0 -4
- package/dist/cjs/utils.d.ts.map +0 -1
- package/dist/cjs/utils.js.map +0 -1
- package/dist/esm/scheduling.d.ts +0 -11
- package/dist/esm/scheduling.d.ts.map +0 -1
- package/dist/esm/scheduling.js +0 -97
- package/dist/esm/scheduling.js.map +0 -1
- package/dist/esm/signals.d.ts +0 -73
- package/dist/esm/signals.d.ts.map +0 -1
- package/dist/esm/signals.js +0 -614
- package/dist/esm/signals.js.map +0 -1
- package/dist/esm/utils.d.ts +0 -4
- package/dist/esm/utils.d.ts.map +0 -1
- package/dist/esm/utils.js.map +0 -1
- package/src/__tests__/hooks/async-computed.test.ts +0 -190
- package/src/__tests__/hooks/async-task.test.ts +0 -334
- package/src/__tests__/hooks/computed.test.ts +0 -126
- package/src/__tests__/hooks/context.test.ts +0 -527
- package/src/__tests__/hooks/nesting.test.ts +0 -303
- package/src/__tests__/hooks/params-and-state.test.ts +0 -168
- package/src/__tests__/hooks/subscription.test.ts +0 -97
- package/src/__tests__/signals/async.test.ts +0 -416
- package/src/__tests__/signals/basic.test.ts +0 -399
- package/src/__tests__/signals/subscription.test.ts +0 -632
- package/src/__tests__/signals/watcher.test.ts +0 -253
- package/src/__tests__/utils/builders.ts +0 -22
- package/src/__tests__/utils/instrumented-signals.ts +0 -291
- package/src/react/__tests__/react.test.tsx +0 -227
- package/src/scheduling.ts +0 -130
- package/src/signals.ts +0 -824
@@ -1,303 +0,0 @@
|
|
1
|
-
import { describe, expect, test, vi } from 'vitest';
|
2
|
-
import { state } from '../../index.js';
|
3
|
-
import { nextTick } from '../utils/async.js';
|
4
|
-
import { permute } from '../utils/permute.js';
|
5
|
-
|
6
|
-
describe('nesting', () => {
|
7
|
-
permute(2, (create1, create2) => {
|
8
|
-
test('simple nesting', () => {
|
9
|
-
const inner = create2((a: number, b: number) => {
|
10
|
-
return a + b;
|
11
|
-
});
|
12
|
-
|
13
|
-
const outer = create1((a: number) => {
|
14
|
-
return inner(a, 2);
|
15
|
-
});
|
16
|
-
|
17
|
-
expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 1 });
|
18
|
-
expect(outer).withParams(2).toHaveValueAndCounts(4, { compute: 2 });
|
19
|
-
|
20
|
-
expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 2 });
|
21
|
-
expect(outer).withParams(2).toHaveValueAndCounts(4, { compute: 2 });
|
22
|
-
});
|
23
|
-
|
24
|
-
test('outer state + params', () => {
|
25
|
-
const val = state(1);
|
26
|
-
|
27
|
-
const inner = create2((a: number, b: number) => {
|
28
|
-
return a + b;
|
29
|
-
});
|
30
|
-
|
31
|
-
const outer = create1((a: number) => {
|
32
|
-
if (a > 1) {
|
33
|
-
return inner(a, 2)! + val.get();
|
34
|
-
}
|
35
|
-
|
36
|
-
return inner(a, 2);
|
37
|
-
});
|
38
|
-
|
39
|
-
expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 1 });
|
40
|
-
expect(outer).withParams(2).toHaveValueAndCounts(5, { compute: 2 });
|
41
|
-
|
42
|
-
val.set(2);
|
43
|
-
expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 2 });
|
44
|
-
expect(outer).withParams(2).toHaveValueAndCounts(6, { compute: 3 });
|
45
|
-
});
|
46
|
-
|
47
|
-
test('inner state + params', async () => {
|
48
|
-
const val = state(1);
|
49
|
-
|
50
|
-
const inner = create2((a: number, b: number) => {
|
51
|
-
if (a > 1) {
|
52
|
-
return a + b + val.get();
|
53
|
-
}
|
54
|
-
|
55
|
-
return a + b;
|
56
|
-
});
|
57
|
-
|
58
|
-
const outer = create1((a: number) => {
|
59
|
-
return inner(a, 2);
|
60
|
-
});
|
61
|
-
|
62
|
-
expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 1 });
|
63
|
-
expect(outer).withParams(2).toHaveValueAndCounts(5, { compute: 2 });
|
64
|
-
|
65
|
-
val.set(2);
|
66
|
-
|
67
|
-
expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 2 });
|
68
|
-
|
69
|
-
// Wait for async with subscriptions
|
70
|
-
await nextTick();
|
71
|
-
|
72
|
-
expect(outer).withParams(2).toHaveValueAndCounts(6, { compute: 3 });
|
73
|
-
});
|
74
|
-
});
|
75
|
-
|
76
|
-
permute(3, (create1, create2, create3) => {
|
77
|
-
test('simple nesting', () => {
|
78
|
-
const inner = create3((a: number, b: number, c: number) => {
|
79
|
-
return a + b + c;
|
80
|
-
});
|
81
|
-
|
82
|
-
const middle = create2((a: number, b: number) => {
|
83
|
-
return inner(a, b, 3);
|
84
|
-
});
|
85
|
-
|
86
|
-
const outer = create1((a: number) => {
|
87
|
-
return middle(a, 2);
|
88
|
-
});
|
89
|
-
|
90
|
-
expect(outer).withParams(1).toHaveValueAndCounts(6, { compute: 1 });
|
91
|
-
});
|
92
|
-
|
93
|
-
test('state + params one level deep', async () => {
|
94
|
-
const val = state(1);
|
95
|
-
|
96
|
-
const inner = create3((a: number, b: number, c: number) => {
|
97
|
-
return a + b + c;
|
98
|
-
});
|
99
|
-
|
100
|
-
const middle = create2((a: number, b: number) => {
|
101
|
-
if (a > 1) {
|
102
|
-
return inner(a, b, 3)! + val.get();
|
103
|
-
}
|
104
|
-
|
105
|
-
return inner(a, b, 3);
|
106
|
-
});
|
107
|
-
|
108
|
-
const outer = create1((a: number) => {
|
109
|
-
return middle(a, 2);
|
110
|
-
});
|
111
|
-
|
112
|
-
expect(outer).withParams(1).toHaveValueAndCounts(6, { compute: 1 });
|
113
|
-
expect(middle).toHaveCounts({ compute: 1 });
|
114
|
-
expect(inner).toHaveCounts({ compute: 1 });
|
115
|
-
|
116
|
-
expect(outer).withParams(2).toHaveValueAndCounts(8, { compute: 2 });
|
117
|
-
expect(middle).toHaveCounts({ compute: 2 });
|
118
|
-
expect(inner).toHaveCounts({ compute: 2 });
|
119
|
-
|
120
|
-
val.set(2);
|
121
|
-
|
122
|
-
expect(outer).withParams(1).toHaveValueAndCounts(6, { compute: 2 });
|
123
|
-
expect(middle).toHaveCounts({ compute: 2 });
|
124
|
-
expect(inner).toHaveCounts({ compute: 2 });
|
125
|
-
|
126
|
-
// Wait for async with subscriptions
|
127
|
-
await nextTick();
|
128
|
-
|
129
|
-
expect(outer).withParams(2).toHaveValueAndCounts(9, { compute: 3 });
|
130
|
-
expect(middle).toHaveCounts({ compute: 3 });
|
131
|
-
expect(inner).toHaveCounts({ compute: 2 });
|
132
|
-
});
|
133
|
-
|
134
|
-
test('state + params two levels deep', async () => {
|
135
|
-
const val = state(1);
|
136
|
-
|
137
|
-
const inner = create3((a: number, b: number, c: number) => {
|
138
|
-
if (a > 1) {
|
139
|
-
return a + b + c + val.get();
|
140
|
-
}
|
141
|
-
|
142
|
-
return a + b + c;
|
143
|
-
});
|
144
|
-
|
145
|
-
const middle = create2((a: number, b: number) => {
|
146
|
-
return inner(a, b, 3);
|
147
|
-
});
|
148
|
-
|
149
|
-
const outer = create1((a: number) => {
|
150
|
-
return middle(a, 2);
|
151
|
-
});
|
152
|
-
|
153
|
-
expect(outer).withParams(1).toHaveValueAndCounts(6, { compute: 1 });
|
154
|
-
expect(middle).toHaveCounts({ compute: 1 });
|
155
|
-
expect(inner).toHaveCounts({ compute: 1 });
|
156
|
-
|
157
|
-
expect(outer).withParams(2).toHaveValueAndCounts(8, { compute: 2 });
|
158
|
-
expect(middle).toHaveCounts({ compute: 2 });
|
159
|
-
expect(inner).toHaveCounts({ compute: 2 });
|
160
|
-
|
161
|
-
val.set(2);
|
162
|
-
|
163
|
-
// Wait for async with subscriptions
|
164
|
-
await nextTick();
|
165
|
-
|
166
|
-
// Flush all first
|
167
|
-
expect(outer).withParams(1).toHaveHookValue(6);
|
168
|
-
expect(outer).withParams(2).toHaveHookValue(9);
|
169
|
-
|
170
|
-
// Then check counts
|
171
|
-
expect(outer).toHaveCounts({ compute: 3 });
|
172
|
-
expect(middle).toHaveCounts({ compute: 3 });
|
173
|
-
expect(inner).toHaveCounts({ compute: 3 });
|
174
|
-
});
|
175
|
-
|
176
|
-
test('params + multiple children', async () => {
|
177
|
-
const inner1 = create3((a: number, b: number, c: number) => {
|
178
|
-
return a + b + c;
|
179
|
-
});
|
180
|
-
|
181
|
-
const inner2 = create2((a: number, b: number) => {
|
182
|
-
return a + b + inner1(a, b, 3)!;
|
183
|
-
});
|
184
|
-
|
185
|
-
const outer = create1((a: number, b: number) => {
|
186
|
-
return inner1(a, 2, 3)! + inner2(b, 2)!;
|
187
|
-
});
|
188
|
-
|
189
|
-
expect(outer).withParams(1, 2).toHaveValueAndCounts(17, { compute: 1 });
|
190
|
-
expect(outer).withParams(1, 2).toHaveValueAndCounts(17, { compute: 1 });
|
191
|
-
expect(inner1).toHaveCounts({ compute: 2 });
|
192
|
-
expect(inner2).toHaveCounts({ compute: 1 });
|
193
|
-
|
194
|
-
expect(outer).withParams(2, 2).toHaveValueAndCounts(18, { compute: 2 });
|
195
|
-
expect(outer).withParams(2, 2).toHaveValueAndCounts(18, { compute: 2 });
|
196
|
-
expect(inner1).toHaveCounts({ compute: 2 });
|
197
|
-
expect(inner2).toHaveCounts({ compute: 1 });
|
198
|
-
|
199
|
-
expect(outer).withParams(2, 3).toHaveValueAndCounts(20, { compute: 3 });
|
200
|
-
expect(outer).withParams(2, 3).toHaveValueAndCounts(20, { compute: 3 });
|
201
|
-
expect(inner1).toHaveCounts({ compute: 3 });
|
202
|
-
expect(inner2).toHaveCounts({ compute: 2 });
|
203
|
-
});
|
204
|
-
|
205
|
-
test('params + state + multiple children', async () => {
|
206
|
-
const val = state(1);
|
207
|
-
|
208
|
-
const inner1 = create3((a: number, b: number, c: number) => {
|
209
|
-
return a + b + c;
|
210
|
-
});
|
211
|
-
|
212
|
-
const inner2 = create2((a: number, b: number) => {
|
213
|
-
if (a > 1) {
|
214
|
-
return a + b + inner1(a, b, 3)! + val.get();
|
215
|
-
}
|
216
|
-
|
217
|
-
return a + b + inner1(a, b, 3)!;
|
218
|
-
});
|
219
|
-
|
220
|
-
const outer = create1((a: number, b: number) => {
|
221
|
-
return inner1(a, 2, 3)! + inner2(b, 2)!;
|
222
|
-
});
|
223
|
-
|
224
|
-
expect(outer).withParams(1, 2).toHaveValueAndCounts(18, { compute: 1 });
|
225
|
-
expect(inner1).toHaveCounts({ compute: 2 });
|
226
|
-
expect(inner2).toHaveCounts({ compute: 1 });
|
227
|
-
|
228
|
-
expect(outer).withParams(2, 2).toHaveValueAndCounts(19, { compute: 2 });
|
229
|
-
expect(inner1).toHaveCounts({ compute: 2 });
|
230
|
-
expect(inner2).toHaveCounts({ compute: 1 });
|
231
|
-
|
232
|
-
expect(outer).withParams(2, 3).toHaveValueAndCounts(21, { compute: 3 });
|
233
|
-
expect(inner1).toHaveCounts({ compute: 3 });
|
234
|
-
expect(inner2).toHaveCounts({ compute: 2 });
|
235
|
-
|
236
|
-
expect(outer).withParams(3, 3).toHaveValueAndCounts(22, { compute: 4 });
|
237
|
-
expect(inner1).toHaveCounts({ compute: 3 });
|
238
|
-
expect(inner2).toHaveCounts({ compute: 2 });
|
239
|
-
|
240
|
-
val.set(2);
|
241
|
-
|
242
|
-
// Wait for async with subscriptions
|
243
|
-
await nextTick();
|
244
|
-
|
245
|
-
// Flush all first
|
246
|
-
expect(outer).withParams(1, 2).toHaveHookValue(19);
|
247
|
-
expect(outer).withParams(2, 2).toHaveHookValue(20);
|
248
|
-
expect(outer).withParams(2, 3).toHaveHookValue(22);
|
249
|
-
expect(outer).withParams(3, 3).toHaveHookValue(23);
|
250
|
-
|
251
|
-
// Then check counts
|
252
|
-
expect(outer).toHaveCounts({ compute: 8 });
|
253
|
-
expect(inner1).toHaveCounts({ compute: 3 });
|
254
|
-
expect(inner2).toHaveCounts({ compute: 4 });
|
255
|
-
});
|
256
|
-
|
257
|
-
test('passing state as params + multiple children', async () => {
|
258
|
-
const stateA = state(1);
|
259
|
-
const stateB = state(2);
|
260
|
-
|
261
|
-
const inner1 = create2((a: number, s: typeof stateA) => {
|
262
|
-
return a + s.get();
|
263
|
-
});
|
264
|
-
|
265
|
-
const inner2 = create3((b: number, s: typeof stateB) => {
|
266
|
-
return b * s.get();
|
267
|
-
});
|
268
|
-
|
269
|
-
const outer = create1((x: number) => {
|
270
|
-
if (x > 2) {
|
271
|
-
return inner1(x, stateA)! + inner2(x, stateB)!;
|
272
|
-
}
|
273
|
-
return inner1(x, stateA);
|
274
|
-
});
|
275
|
-
|
276
|
-
expect(outer).withParams(1).toHaveValueAndCounts(2, { compute: 1 });
|
277
|
-
expect(inner1).toHaveCounts({ compute: 1 });
|
278
|
-
expect(inner2).toHaveCounts({ compute: 0 });
|
279
|
-
|
280
|
-
expect(outer).withParams(3).toHaveValueAndCounts(10, { compute: 2 });
|
281
|
-
expect(inner1).toHaveCounts({ compute: 2 });
|
282
|
-
expect(inner2).toHaveCounts({ compute: 1 });
|
283
|
-
|
284
|
-
stateA.set(2);
|
285
|
-
await nextTick();
|
286
|
-
|
287
|
-
expect(outer).withParams(1).toHaveHookValue(3);
|
288
|
-
expect(outer).withParams(3).toHaveHookValue(11);
|
289
|
-
expect(outer).toHaveCounts({ compute: 4 });
|
290
|
-
expect(inner1).toHaveCounts({ compute: 4 });
|
291
|
-
expect(inner2).toHaveCounts({ compute: 1 });
|
292
|
-
|
293
|
-
stateB.set(3);
|
294
|
-
|
295
|
-
await nextTick();
|
296
|
-
expect(outer).withParams(1).toHaveHookValue(3);
|
297
|
-
expect(outer).withParams(3).toHaveHookValue(14);
|
298
|
-
expect(outer).toHaveCounts({ compute: 5 });
|
299
|
-
expect(inner1).toHaveCounts({ compute: 4 });
|
300
|
-
expect(inner2).toHaveCounts({ compute: 2 });
|
301
|
-
});
|
302
|
-
});
|
303
|
-
});
|
@@ -1,168 +0,0 @@
|
|
1
|
-
import { describe, expect, test } from 'vitest';
|
2
|
-
import { state } from '../../index.js';
|
3
|
-
import { permute } from '../utils/permute.js';
|
4
|
-
|
5
|
-
describe('parameters and state reactivity', () => {
|
6
|
-
permute(1, createHook => {
|
7
|
-
test('Parameters can be passed to computed', async () => {
|
8
|
-
const getC = createHook((a: number, b: number) => {
|
9
|
-
return a + b;
|
10
|
-
});
|
11
|
-
|
12
|
-
expect(getC).withParams(1, 2).toHaveValueAndCounts(3, { compute: 1 });
|
13
|
-
expect(getC).withParams(2, 2).toHaveValueAndCounts(4, { compute: 2 });
|
14
|
-
});
|
15
|
-
|
16
|
-
test('Computed is not recomputed when the same parameters are passed', () => {
|
17
|
-
const getC = createHook((a: number, b: number) => {
|
18
|
-
return a + b;
|
19
|
-
});
|
20
|
-
|
21
|
-
expect(getC).withParams(1, 2).toHaveValueAndCounts(3, { compute: 1 });
|
22
|
-
expect(getC).withParams(1, 2).toHaveValueAndCounts(3, { compute: 1 });
|
23
|
-
});
|
24
|
-
|
25
|
-
test('Computed is recomputed when state changes', () => {
|
26
|
-
const stateValue = state(1);
|
27
|
-
const getC = createHook((a: number) => {
|
28
|
-
return a + stateValue.get();
|
29
|
-
});
|
30
|
-
|
31
|
-
expect(getC).withParams(1).toHaveValueAndCounts(2, { compute: 1 });
|
32
|
-
stateValue.set(2);
|
33
|
-
expect(getC).withParams(1).toHaveValueAndCounts(3, { compute: 2 });
|
34
|
-
});
|
35
|
-
|
36
|
-
test('Computed can return complex objects', () => {
|
37
|
-
const getC = createHook((a: number, b: number) => {
|
38
|
-
return {
|
39
|
-
sum: a + b,
|
40
|
-
product: a * b,
|
41
|
-
};
|
42
|
-
});
|
43
|
-
|
44
|
-
expect(getC).withParams(2, 3).toHaveValueAndCounts(
|
45
|
-
{
|
46
|
-
sum: 5,
|
47
|
-
product: 6,
|
48
|
-
},
|
49
|
-
{ compute: 1 },
|
50
|
-
);
|
51
|
-
|
52
|
-
expect(getC).withParams(2, 3).toHaveValueAndCounts(
|
53
|
-
{
|
54
|
-
sum: 5,
|
55
|
-
product: 6,
|
56
|
-
},
|
57
|
-
{ compute: 1 },
|
58
|
-
);
|
59
|
-
});
|
60
|
-
|
61
|
-
test('Computed can take array arguments', () => {
|
62
|
-
const getC = createHook((nums: number[]) => {
|
63
|
-
return nums.reduce((a, b) => a + b, 0);
|
64
|
-
});
|
65
|
-
|
66
|
-
expect(getC).withParams([1, 2, 3]).toHaveValueAndCounts(6, { compute: 1 });
|
67
|
-
expect(getC).withParams([1, 2, 3]).toHaveValueAndCounts(6, { compute: 1 });
|
68
|
-
expect(getC).withParams([4, 5, 6]).toHaveValueAndCounts(15, { compute: 2 });
|
69
|
-
});
|
70
|
-
|
71
|
-
test('Computed can take object arguments', () => {
|
72
|
-
const getC = createHook((obj: { x: number; y: number }) => {
|
73
|
-
return obj.x + obj.y;
|
74
|
-
});
|
75
|
-
|
76
|
-
expect(getC).withParams({ x: 1, y: 2 }).toHaveValueAndCounts(3, { compute: 1 });
|
77
|
-
expect(getC).withParams({ x: 1, y: 2 }).toHaveValueAndCounts(3, { compute: 1 });
|
78
|
-
expect(getC).withParams({ x: 3, y: 4 }).toHaveValueAndCounts(7, { compute: 2 });
|
79
|
-
});
|
80
|
-
|
81
|
-
test('Computed memoizes based on deep equality of arguments', () => {
|
82
|
-
const getC = createHook((obj: { x: number; y: number; nested: { a: number; b: number }; arr: number[] }) => {
|
83
|
-
return obj.x + obj.y + obj.nested.a + obj.nested.b + obj.arr.reduce((sum, n) => sum + n, 0);
|
84
|
-
});
|
85
|
-
|
86
|
-
expect(getC)
|
87
|
-
.withParams({ x: 1, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
|
88
|
-
.toHaveValueAndCounts(13, { compute: 1 });
|
89
|
-
|
90
|
-
expect(getC)
|
91
|
-
.withParams({ x: 1, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
|
92
|
-
.toHaveValueAndCounts(13, { compute: 1 });
|
93
|
-
|
94
|
-
expect(getC)
|
95
|
-
.withParams({ x: 2, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
|
96
|
-
.toHaveValueAndCounts(14, { compute: 2 });
|
97
|
-
|
98
|
-
expect(getC)
|
99
|
-
.withParams({ x: 2, y: 2, nested: { a: 3, b: 5 }, arr: [1, 2] })
|
100
|
-
.toHaveValueAndCounts(15, { compute: 3 });
|
101
|
-
|
102
|
-
expect(getC)
|
103
|
-
.withParams({ x: 2, y: 2, nested: { a: 3, b: 5 }, arr: [2, 2] })
|
104
|
-
.toHaveValueAndCounts(16, { compute: 4 });
|
105
|
-
});
|
106
|
-
|
107
|
-
test('Computed can use custom memoization function', () => {
|
108
|
-
const getC = createHook(
|
109
|
-
(obj: { x: number; y: number; nested: { a: number; b: number }; arr: number[] }) => {
|
110
|
-
return obj.x + obj.y + obj.nested.a + obj.nested.b + obj.arr.reduce((sum, n) => sum + n, 0);
|
111
|
-
},
|
112
|
-
{
|
113
|
-
paramKey: obj => {
|
114
|
-
return Object.entries(obj)
|
115
|
-
.map(([key, value]) => `${key}:${Array.isArray(value) ? 'array' : String(value)}`)
|
116
|
-
.join(',');
|
117
|
-
},
|
118
|
-
},
|
119
|
-
);
|
120
|
-
|
121
|
-
expect(getC)
|
122
|
-
.withParams({ x: 1, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
|
123
|
-
.toHaveValueAndCounts(13, { compute: 1 });
|
124
|
-
|
125
|
-
expect(getC)
|
126
|
-
.withParams({ x: 1, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
|
127
|
-
.toHaveValueAndCounts(13, { compute: 1 });
|
128
|
-
|
129
|
-
expect(getC)
|
130
|
-
.withParams({ x: 2, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
|
131
|
-
.toHaveValueAndCounts(14, { compute: 2 });
|
132
|
-
|
133
|
-
expect(getC)
|
134
|
-
.withParams({ x: 2, y: 2, nested: { a: 3, b: 5 }, arr: [1, 2] })
|
135
|
-
.toHaveValueAndCounts(14, { compute: 2 });
|
136
|
-
|
137
|
-
expect(getC)
|
138
|
-
.withParams({ x: 2, y: 2, nested: { a: 3, b: 5 }, arr: [2, 2] })
|
139
|
-
.toHaveValueAndCounts(14, { compute: 2 });
|
140
|
-
});
|
141
|
-
|
142
|
-
test('Computed can handle undefined arguments', () => {
|
143
|
-
const getC = createHook((a?: number, b?: number) => {
|
144
|
-
return (a ?? 0) + (b ?? 0);
|
145
|
-
});
|
146
|
-
|
147
|
-
expect(getC).withParams(undefined, 2).toHaveValueAndCounts(2, { compute: 1 });
|
148
|
-
expect(getC).withParams(undefined, 2).toHaveValueAndCounts(2, { compute: 1 });
|
149
|
-
expect(getC).withParams(1, undefined).toHaveValueAndCounts(1, { compute: 2 });
|
150
|
-
});
|
151
|
-
|
152
|
-
test('Computed can take state as argument and handle updates', () => {
|
153
|
-
const stateValue = state(1);
|
154
|
-
const getC = createHook((s: typeof stateValue) => {
|
155
|
-
return s.get() * 2;
|
156
|
-
});
|
157
|
-
|
158
|
-
expect(getC).withParams(stateValue).toHaveValueAndCounts(2, { compute: 1 });
|
159
|
-
expect(getC).withParams(stateValue).toHaveValueAndCounts(2, { compute: 1 });
|
160
|
-
|
161
|
-
stateValue.set(2);
|
162
|
-
expect(getC).withParams(stateValue).toHaveValueAndCounts(4, { compute: 2 });
|
163
|
-
|
164
|
-
stateValue.set(3);
|
165
|
-
expect(getC).withParams(stateValue).toHaveValueAndCounts(6, { compute: 3 });
|
166
|
-
});
|
167
|
-
});
|
168
|
-
});
|
@@ -1,97 +0,0 @@
|
|
1
|
-
import { describe, expect, test } from 'vitest';
|
2
|
-
import { state } from '../../index.js';
|
3
|
-
import { subscription } from '../utils/instrumented-hooks.js';
|
4
|
-
import { nextTick } from '../utils/async.js';
|
5
|
-
|
6
|
-
describe('subscriptions', () => {
|
7
|
-
test('Subscription can set initial value', () => {
|
8
|
-
const sub = subscription(({ set }) => {
|
9
|
-
set(1);
|
10
|
-
});
|
11
|
-
|
12
|
-
expect(sub).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
|
13
|
-
});
|
14
|
-
|
15
|
-
test('Subscription can update value', () => {
|
16
|
-
const value = state(1);
|
17
|
-
const sub = subscription(({ set }) => {
|
18
|
-
set(value.get());
|
19
|
-
|
20
|
-
return {
|
21
|
-
update: () => {
|
22
|
-
set(value.get());
|
23
|
-
},
|
24
|
-
};
|
25
|
-
});
|
26
|
-
|
27
|
-
expect(sub).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
|
28
|
-
|
29
|
-
value.set(2);
|
30
|
-
expect(sub).toHaveValueAndCounts(2, { compute: 2, internalSet: 2 });
|
31
|
-
});
|
32
|
-
|
33
|
-
test('Subscription can set multiple times', () => {
|
34
|
-
const sub = subscription(({ set }) => {
|
35
|
-
set(1);
|
36
|
-
set(2);
|
37
|
-
set(3);
|
38
|
-
});
|
39
|
-
|
40
|
-
expect(sub).toHaveValueAndCounts(3, { compute: 1, internalSet: 3 });
|
41
|
-
});
|
42
|
-
|
43
|
-
test('Subscription can access parameters', () => {
|
44
|
-
const sub = subscription(({ set }, value: number) => {
|
45
|
-
set(value);
|
46
|
-
});
|
47
|
-
|
48
|
-
expect(sub).withParams(1).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
|
49
|
-
expect(sub).withParams(2).toHaveValueAndCounts(2, { compute: 2, internalSet: 2 });
|
50
|
-
});
|
51
|
-
|
52
|
-
test('Subscription is recomputed when parameters change', () => {
|
53
|
-
const sub = subscription(({ set }, value: number) => {
|
54
|
-
set(value);
|
55
|
-
|
56
|
-
return {
|
57
|
-
update: () => {
|
58
|
-
set(value * 2);
|
59
|
-
},
|
60
|
-
};
|
61
|
-
});
|
62
|
-
|
63
|
-
expect(sub).withParams(1).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
|
64
|
-
expect(sub).withParams(2).toHaveValueAndCounts(2, { compute: 2, internalSet: 2 });
|
65
|
-
});
|
66
|
-
|
67
|
-
test('Subscription is not recomputed when same parameters are passed', () => {
|
68
|
-
const sub = subscription(({ set }, value: number) => {
|
69
|
-
set(value);
|
70
|
-
});
|
71
|
-
|
72
|
-
expect(sub).withParams(1).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
|
73
|
-
expect(sub).withParams(1).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
|
74
|
-
});
|
75
|
-
|
76
|
-
test('Subscription updates automatically when state changes', async () => {
|
77
|
-
const value = state(1);
|
78
|
-
const sub = subscription(({ set }) => {
|
79
|
-
set(value.get());
|
80
|
-
|
81
|
-
return {
|
82
|
-
update: () => {
|
83
|
-
set(value.get());
|
84
|
-
},
|
85
|
-
};
|
86
|
-
});
|
87
|
-
|
88
|
-
expect(sub).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
|
89
|
-
|
90
|
-
value.set(2);
|
91
|
-
|
92
|
-
await nextTick();
|
93
|
-
|
94
|
-
expect(sub).toHaveCounts({ compute: 2, internalSet: 2 });
|
95
|
-
expect(sub).toHaveHookValue(2);
|
96
|
-
});
|
97
|
-
});
|