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
@@ -1,378 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import { state, computed } from './utils/instrumented.js';
3
-
4
- describe('Basic Signal functionality', () => {
5
- test('Can run basic computed', () => {
6
- const a = state(1);
7
- const b = state(2);
8
-
9
- const c = computed(() => {
10
- return a.get() + b.get();
11
- });
12
-
13
- expect(c).toHaveValueAndCounts(3, { compute: 1, get: 1 });
14
-
15
- // stability
16
- expect(c).toHaveValueAndCounts(3, { compute: 1, get: 2 });
17
- });
18
-
19
- test('Computeds can be updated', () => {
20
- const a = state(1);
21
- const b = state(2);
22
-
23
- const c = computed(() => {
24
- return a.get() + b.get();
25
- });
26
-
27
- expect(c).toHaveValueAndCounts(3, { compute: 1, get: 1 });
28
-
29
- a.set(2);
30
-
31
- expect(c).toHaveValueAndCounts(4, { compute: 2, get: 2 });
32
- });
33
-
34
- test('Does not update if value is the same', () => {
35
- const a = state(1);
36
- const b = state(2);
37
-
38
- const c = computed(() => {
39
- return a.get() + b.get();
40
- });
41
-
42
- expect(c).toHaveValueAndCounts(3, { compute: 1, get: 1 });
43
-
44
- a.set(1);
45
-
46
- expect(c).toHaveValueAndCounts(3, { compute: 1, get: 2 });
47
- });
48
-
49
- describe('Nesting', () => {
50
- test('Can nest computeds', () => {
51
- const a = state(1);
52
- const b = state(2);
53
- const c = state(2);
54
-
55
- const inner = computed(() => {
56
- return a.get() + b.get();
57
- });
58
-
59
- const outer = computed(() => {
60
- return inner.get() + c.get();
61
- });
62
-
63
- expect(inner).toHaveValueAndCounts(3, { compute: 1, get: 1 });
64
- expect(outer).toHaveValueAndCounts(5, { compute: 1, get: 1 });
65
-
66
- // stability
67
- expect(inner).toHaveValueAndCounts(3, { compute: 1, get: 3 });
68
- expect(outer).toHaveValueAndCounts(5, { compute: 1, get: 2 });
69
- });
70
-
71
- test('Can dirty inner computed and update parent', () => {
72
- const a = state(1);
73
- const b = state(2);
74
- const c = state(2);
75
-
76
- const inner = computed(() => {
77
- return a.get() + b.get();
78
- });
79
-
80
- const outer = computed(() => {
81
- return inner.get() + c.get();
82
- });
83
-
84
- expect(inner).toHaveValueAndCounts(3, { compute: 1, get: 1 });
85
- expect(outer).toHaveValueAndCounts(5, { compute: 1, get: 1 });
86
-
87
- a.set(2);
88
-
89
- expect(inner).toHaveValueAndCounts(4, { compute: 2, get: 3 });
90
- expect(outer).toHaveValueAndCounts(6, { compute: 2, get: 2 });
91
- });
92
-
93
- test('Can dirty outer computed and inner stays cached', () => {
94
- const a = state(1);
95
- const b = state(2);
96
- const c = state(2);
97
-
98
- const inner = computed(() => {
99
- return a.get() + b.get();
100
- });
101
-
102
- const outer = computed(() => {
103
- return inner.get() + c.get();
104
- });
105
-
106
- expect(inner).toHaveValueAndCounts(3, { compute: 1, get: 1 });
107
- expect(outer).toHaveValueAndCounts(5, { compute: 1, get: 1 });
108
-
109
- c.set(3);
110
-
111
- expect(inner).toHaveValueAndCounts(3, { compute: 1, get: 3 });
112
- expect(outer).toHaveValueAndCounts(6, { compute: 2, get: 2 });
113
- });
114
-
115
- test('Can nest multiple levels', () => {
116
- const a = state(1);
117
- const b = state(2);
118
- const c = state(2);
119
- const d = state(2);
120
-
121
- const inner = computed(() => {
122
- return a.get() + b.get();
123
- });
124
-
125
- const mid = computed(() => {
126
- return inner.get() + c.get();
127
- });
128
-
129
- const outer = computed(() => {
130
- return mid.get() + d.get();
131
- });
132
-
133
- expect(inner).toHaveValueAndCounts(3, { compute: 1 });
134
- expect(mid).toHaveValueAndCounts(5, { compute: 1 });
135
- expect(outer).toHaveValueAndCounts(7, { compute: 1 });
136
-
137
- a.set(2);
138
-
139
- expect(inner).toHaveValueAndCounts(4, { compute: 2 });
140
- expect(mid).toHaveValueAndCounts(6, { compute: 2 });
141
- expect(outer).toHaveValueAndCounts(8, { compute: 2 });
142
-
143
- c.set(3);
144
-
145
- expect(inner).toHaveValueAndCounts(4, { compute: 2 });
146
- expect(mid).toHaveValueAndCounts(7, { compute: 3 });
147
- expect(outer).toHaveValueAndCounts(9, { compute: 3 });
148
-
149
- d.set(3);
150
-
151
- expect(inner).toHaveValueAndCounts(4, { compute: 2 });
152
- expect(mid).toHaveValueAndCounts(7, { compute: 3 });
153
- expect(outer).toHaveValueAndCounts(10, { compute: 4 });
154
- });
155
- });
156
-
157
- describe('Propagation', () => {
158
- test('it works with multiple parents', () => {
159
- const a = state(1);
160
- const b = state(2);
161
- const c = state(2);
162
- const d = state(2);
163
-
164
- const inner = computed(() => {
165
- return a.get() + b.get();
166
- });
167
-
168
- const outer1 = computed(() => {
169
- return inner.get() + c.get();
170
- });
171
-
172
- const outer2 = computed(() => {
173
- return inner.get() + d.get();
174
- });
175
-
176
- expect(inner).toHaveValueAndCounts(3, { compute: 1 });
177
- expect(outer1).toHaveValueAndCounts(5, { compute: 1 });
178
- expect(outer2).toHaveValueAndCounts(5, { compute: 1 });
179
-
180
- a.set(2);
181
-
182
- expect(inner).toHaveValueAndCounts(4, { compute: 2 });
183
- expect(outer1).toHaveValueAndCounts(6, { compute: 2 });
184
- expect(outer2).toHaveValueAndCounts(6, { compute: 2 });
185
-
186
- b.set(3);
187
-
188
- expect(inner).toHaveValueAndCounts(5, { compute: 3 });
189
- expect(outer2).toHaveValueAndCounts(7, { compute: 3 });
190
- expect(outer1).toHaveValueAndCounts(7, { compute: 3 });
191
-
192
- c.set(3);
193
-
194
- expect(inner).toHaveValueAndCounts(5, { compute: 3 });
195
- expect(outer1).toHaveValueAndCounts(8, { compute: 4 });
196
- expect(outer2).toHaveValueAndCounts(7, { compute: 3 });
197
-
198
- d.set(3);
199
-
200
- expect(inner).toHaveValueAndCounts(5, { compute: 3 });
201
- expect(outer1).toHaveValueAndCounts(8, { compute: 4 });
202
- expect(outer2).toHaveValueAndCounts(8, { compute: 4 });
203
- });
204
-
205
- test('it stops propagation if the result is the same', () => {
206
- const a = state(1);
207
- const b = state(2);
208
- const c = state(2);
209
- const d = state(2);
210
-
211
- const inner = computed('inner', () => {
212
- return a.get() + b.get();
213
- });
214
-
215
- const outer1 = computed('outer1', () => {
216
- return inner.get() + c.get();
217
- });
218
-
219
- const outer2 = computed('outer2', () => {
220
- return inner.get() + d.get();
221
- });
222
-
223
- expect(() => {
224
- expect(outer1).toHaveValueAndCounts(5, { compute: 1 });
225
- expect(outer2).toHaveValueAndCounts(5, { compute: 1 });
226
- expect(inner).toHaveValueAndCounts(3, { compute: 1 });
227
- }).toHaveComputedOrder(['outer1', 'inner', 'outer2']);
228
-
229
- a.set(2);
230
- b.set(1);
231
-
232
- expect(() => {
233
- expect(outer1).toHaveValueAndCounts(5, { compute: 1 });
234
- expect(outer2).toHaveValueAndCounts(5, { compute: 1 });
235
- expect(inner).toHaveValueAndCounts(3, { compute: 2 });
236
- }).toHaveComputedOrder(['inner']);
237
-
238
- b.set(2);
239
-
240
- expect(() => {
241
- expect(outer2).toHaveValueAndCounts(6, { compute: 2 });
242
- expect(outer1).toHaveValueAndCounts(6, { compute: 2 });
243
- expect(inner).toHaveValueAndCounts(4, { compute: 3 });
244
- }).toHaveComputedOrder(['inner', 'outer2', 'outer1']);
245
- });
246
-
247
- test('it continues propagation if any child is different', () => {
248
- const a = state(1);
249
- const b = state(2);
250
- const c = state(2);
251
- const d = state(2);
252
-
253
- const inner1 = computed('inner1', () => {
254
- return a.get() + b.get();
255
- });
256
-
257
- const inner2 = computed('inner2', () => {
258
- return c.get();
259
- });
260
-
261
- const inner3 = computed('inner3', () => {
262
- return d.get();
263
- });
264
-
265
- const outer = computed('outer', () => {
266
- return inner1.get() + inner2.get() + inner3.get();
267
- });
268
-
269
- expect(() => {
270
- expect(outer).toHaveValueAndCounts(7, { compute: 1 });
271
- }).toHaveComputedOrder(['outer', 'inner1', 'inner2', 'inner3']);
272
-
273
- d.set(4);
274
- a.set(2);
275
- c.set(3);
276
- b.set(1);
277
-
278
- expect(() => {
279
- expect(outer).toHaveValueAndCounts(10, { compute: 2 });
280
- expect(inner1).toHaveCounts({ compute: 2 });
281
- expect(inner2).toHaveCounts({ compute: 2 });
282
- }).toHaveComputedOrder(['inner1', 'inner2', 'outer', 'inner3']);
283
- });
284
- });
285
-
286
- describe('Laziness', () => {
287
- test('it does not compute values that are not used', () => {
288
- const a = state(1);
289
- const b = state(2);
290
- const c = state(2);
291
- const d = state(2);
292
-
293
- const inner1 = computed(() => {
294
- return a.get() + b.get();
295
- });
296
-
297
- const inner2 = computed(() => {
298
- return c.get() + d.get();
299
- });
300
-
301
- const outer = computed(() => {
302
- if (inner1.get() <= 3) {
303
- return inner2.get();
304
- } else {
305
- return -1;
306
- }
307
- });
308
-
309
- expect(outer).toHaveValueAndCounts(4, { compute: 1 });
310
-
311
- a.set(2);
312
- c.set(3);
313
-
314
- expect(outer).toHaveValueAndCounts(-1, { compute: 2 });
315
- expect(inner1).toHaveCounts({ compute: 2 });
316
- expect(inner2).toHaveCounts({ compute: 1 });
317
- });
318
- });
319
-
320
- describe('Equality', () => {
321
- test('Does not update if value is the same (custom equality fn)', () => {
322
- const a = state(1);
323
- const b = state(2);
324
-
325
- const c = computed(
326
- () => {
327
- return a.get() + b.get();
328
- },
329
- {
330
- equals(prev, next) {
331
- return Math.abs(prev - next) < 2;
332
- },
333
- },
334
- );
335
-
336
- expect(c).toHaveValueAndCounts(3, { compute: 1 });
337
-
338
- a.set(2);
339
-
340
- expect(c).toHaveValueAndCounts(3, { compute: 2 });
341
- });
342
-
343
- test('It stops propagation if the result is the same (custom equality fn)', () => {
344
- const a = state(1);
345
- const b = state(2);
346
- const c = state(2);
347
- const d = state(2);
348
-
349
- const inner = computed(
350
- () => {
351
- return a.get() + b.get();
352
- },
353
- {
354
- equals(prev, next) {
355
- return Math.abs(prev - next) < 2;
356
- },
357
- },
358
- );
359
-
360
- const outer1 = computed(() => {
361
- return inner.get() + c.get();
362
- });
363
-
364
- const outer2 = computed(() => {
365
- return inner.get() + d.get();
366
- });
367
-
368
- expect(inner).toHaveValueAndCounts(3, { compute: 1 });
369
-
370
- a.set(2);
371
- b.set(2);
372
-
373
- expect(inner).toHaveValueAndCounts(3, { compute: 2 });
374
- expect(outer1).toHaveValueAndCounts(5, { compute: 1 });
375
- expect(outer2).toHaveValueAndCounts(5, { compute: 1 });
376
- });
377
- });
378
- });