signalium 0.2.7 → 0.3.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 (152) hide show
  1. package/.turbo/turbo-build.log +12 -0
  2. package/CHANGELOG.md +12 -0
  3. package/dist/cjs/config.d.ts +14 -5
  4. package/dist/cjs/config.d.ts.map +1 -1
  5. package/dist/cjs/config.js +23 -14
  6. package/dist/cjs/config.js.map +1 -1
  7. package/dist/cjs/debug.d.ts +3 -0
  8. package/dist/cjs/debug.d.ts.map +1 -0
  9. package/dist/cjs/debug.js +16 -0
  10. package/dist/cjs/debug.js.map +1 -0
  11. package/dist/cjs/hooks.d.ts +45 -0
  12. package/dist/cjs/hooks.d.ts.map +1 -0
  13. package/dist/cjs/hooks.js +260 -0
  14. package/dist/cjs/hooks.js.map +1 -0
  15. package/dist/cjs/index.d.ts +5 -3
  16. package/dist/cjs/index.d.ts.map +1 -1
  17. package/dist/cjs/index.js +21 -8
  18. package/dist/cjs/index.js.map +1 -1
  19. package/dist/cjs/react/context.d.ts +4 -0
  20. package/dist/cjs/react/context.d.ts.map +1 -0
  21. package/dist/cjs/react/context.js +10 -0
  22. package/dist/cjs/react/context.js.map +1 -0
  23. package/dist/cjs/react/index.d.ts +5 -0
  24. package/dist/cjs/react/index.d.ts.map +1 -0
  25. package/dist/cjs/react/index.js +12 -0
  26. package/dist/cjs/react/index.js.map +1 -0
  27. package/dist/cjs/react/provider.d.ts +7 -0
  28. package/dist/cjs/react/provider.d.ts.map +1 -0
  29. package/dist/cjs/react/provider.js +13 -0
  30. package/dist/cjs/react/provider.js.map +1 -0
  31. package/dist/cjs/react/signal-value.d.ts +3 -0
  32. package/dist/cjs/react/signal-value.d.ts.map +1 -0
  33. package/dist/cjs/react/signal-value.js +42 -0
  34. package/dist/cjs/react/signal-value.js.map +1 -0
  35. package/dist/cjs/react/state.d.ts +3 -0
  36. package/dist/cjs/react/state.d.ts.map +1 -0
  37. package/dist/cjs/react/state.js +13 -0
  38. package/dist/cjs/react/state.js.map +1 -0
  39. package/dist/cjs/scheduling.d.ts +5 -0
  40. package/dist/cjs/scheduling.d.ts.map +1 -1
  41. package/dist/cjs/scheduling.js +59 -5
  42. package/dist/cjs/scheduling.js.map +1 -1
  43. package/dist/cjs/signals.d.ts +28 -65
  44. package/dist/cjs/signals.d.ts.map +1 -1
  45. package/dist/cjs/signals.js +223 -65
  46. package/dist/cjs/signals.js.map +1 -1
  47. package/dist/cjs/trace.d.ts +127 -0
  48. package/dist/cjs/trace.d.ts.map +1 -0
  49. package/dist/cjs/trace.js +319 -0
  50. package/dist/cjs/trace.js.map +1 -0
  51. package/dist/cjs/types.d.ts +66 -0
  52. package/dist/cjs/types.d.ts.map +1 -0
  53. package/dist/cjs/types.js +3 -0
  54. package/dist/cjs/types.js.map +1 -0
  55. package/dist/cjs/utils.d.ts +4 -0
  56. package/dist/cjs/utils.d.ts.map +1 -0
  57. package/dist/cjs/utils.js +80 -0
  58. package/dist/cjs/utils.js.map +1 -0
  59. package/dist/esm/config.d.ts +14 -5
  60. package/dist/esm/config.d.ts.map +1 -1
  61. package/dist/esm/config.js +19 -11
  62. package/dist/esm/config.js.map +1 -1
  63. package/dist/esm/debug.d.ts +3 -0
  64. package/dist/esm/debug.d.ts.map +1 -0
  65. package/dist/esm/debug.js +3 -0
  66. package/dist/esm/debug.js.map +1 -0
  67. package/dist/esm/hooks.d.ts +45 -0
  68. package/dist/esm/hooks.d.ts.map +1 -0
  69. package/dist/esm/hooks.js +243 -0
  70. package/dist/esm/hooks.js.map +1 -0
  71. package/dist/esm/index.d.ts +5 -3
  72. package/dist/esm/index.d.ts.map +1 -1
  73. package/dist/esm/index.js +4 -2
  74. package/dist/esm/index.js.map +1 -1
  75. package/dist/esm/react/context.d.ts +4 -0
  76. package/dist/esm/react/context.d.ts.map +1 -0
  77. package/dist/esm/react/context.js +6 -0
  78. package/dist/esm/react/context.js.map +1 -0
  79. package/dist/esm/react/index.d.ts +5 -0
  80. package/dist/esm/react/index.d.ts.map +1 -0
  81. package/dist/esm/react/index.js +5 -0
  82. package/dist/esm/react/index.js.map +1 -0
  83. package/dist/esm/react/provider.d.ts +7 -0
  84. package/dist/esm/react/provider.d.ts.map +1 -0
  85. package/dist/esm/react/provider.js +10 -0
  86. package/dist/esm/react/provider.js.map +1 -0
  87. package/dist/esm/react/signal-value.d.ts +3 -0
  88. package/dist/esm/react/signal-value.d.ts.map +1 -0
  89. package/dist/esm/react/signal-value.js +38 -0
  90. package/dist/esm/react/signal-value.js.map +1 -0
  91. package/dist/esm/react/state.d.ts +3 -0
  92. package/dist/esm/react/state.d.ts.map +1 -0
  93. package/dist/esm/react/state.js +10 -0
  94. package/dist/esm/react/state.js.map +1 -0
  95. package/dist/esm/scheduling.d.ts +5 -0
  96. package/dist/esm/scheduling.d.ts.map +1 -1
  97. package/dist/esm/scheduling.js +51 -1
  98. package/dist/esm/scheduling.js.map +1 -1
  99. package/dist/esm/signals.d.ts +28 -65
  100. package/dist/esm/signals.d.ts.map +1 -1
  101. package/dist/esm/signals.js +215 -61
  102. package/dist/esm/signals.js.map +1 -1
  103. package/dist/esm/trace.d.ts +127 -0
  104. package/dist/esm/trace.d.ts.map +1 -0
  105. package/dist/esm/trace.js +311 -0
  106. package/dist/esm/trace.js.map +1 -0
  107. package/dist/esm/types.d.ts +66 -0
  108. package/dist/esm/types.d.ts.map +1 -0
  109. package/dist/esm/types.js +2 -0
  110. package/dist/esm/types.js.map +1 -0
  111. package/dist/esm/utils.d.ts +4 -0
  112. package/dist/esm/utils.d.ts.map +1 -0
  113. package/dist/esm/utils.js +75 -0
  114. package/dist/esm/utils.js.map +1 -0
  115. package/package.json +43 -2
  116. package/src/__tests__/hooks/async-computed.test.ts +190 -0
  117. package/src/__tests__/hooks/async-task.test.ts +227 -0
  118. package/src/__tests__/hooks/computed.test.ts +126 -0
  119. package/src/__tests__/hooks/context.test.ts +527 -0
  120. package/src/__tests__/hooks/nesting.test.ts +303 -0
  121. package/src/__tests__/hooks/params-and-state.test.ts +168 -0
  122. package/src/__tests__/hooks/subscription.test.ts +97 -0
  123. package/src/__tests__/signals/async.test.ts +416 -0
  124. package/src/__tests__/signals/basic.test.ts +399 -0
  125. package/src/__tests__/signals/subscription.test.ts +632 -0
  126. package/src/__tests__/signals/watcher.test.ts +253 -0
  127. package/src/__tests__/utils/async.ts +6 -0
  128. package/src/__tests__/utils/builders.ts +22 -0
  129. package/src/__tests__/utils/instrumented-hooks.ts +309 -0
  130. package/src/__tests__/utils/instrumented-signals.ts +281 -0
  131. package/src/__tests__/utils/permute.ts +74 -0
  132. package/src/config.ts +32 -17
  133. package/src/debug.ts +14 -0
  134. package/src/hooks.ts +429 -0
  135. package/src/index.ts +28 -3
  136. package/src/react/__tests__/react.test.tsx +135 -0
  137. package/src/react/context.ts +8 -0
  138. package/src/react/index.ts +4 -0
  139. package/src/react/provider.tsx +18 -0
  140. package/src/react/signal-value.ts +56 -0
  141. package/src/react/state.ts +13 -0
  142. package/src/scheduling.ts +69 -1
  143. package/src/signals.ts +331 -157
  144. package/src/trace.ts +449 -0
  145. package/src/types.ts +86 -0
  146. package/src/utils.ts +83 -0
  147. package/tsconfig.json +2 -1
  148. package/vitest.workspace.ts +24 -0
  149. package/src/__tests__/async.test.ts +0 -426
  150. package/src/__tests__/basic.test.ts +0 -378
  151. package/src/__tests__/subscription.test.ts +0 -645
  152. package/src/__tests__/utils/instrumented.ts +0 -326
@@ -0,0 +1,303 @@
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
+ });
@@ -0,0 +1,168 @@
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
+ });
@@ -0,0 +1,97 @@
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
+ });