@ersbeth/picoflow 1.0.0 → 1.1.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 (180) hide show
  1. package/.cursor/plans/unifier-flowresource-avec-flowderivation-c9506e24.plan.md +372 -0
  2. package/README.md +25 -171
  3. package/biome.json +4 -1
  4. package/dist/picoflow.js +1129 -661
  5. package/dist/types/flow/base/flowDisposable.d.ts +67 -0
  6. package/dist/types/flow/base/flowDisposable.d.ts.map +1 -0
  7. package/dist/types/flow/base/flowEffect.d.ts +127 -0
  8. package/dist/types/flow/base/flowEffect.d.ts.map +1 -0
  9. package/dist/types/flow/base/flowGraph.d.ts +97 -0
  10. package/dist/types/flow/base/flowGraph.d.ts.map +1 -0
  11. package/dist/types/flow/base/flowSignal.d.ts +134 -0
  12. package/dist/types/flow/base/flowSignal.d.ts.map +1 -0
  13. package/dist/types/flow/base/flowTracker.d.ts +15 -0
  14. package/dist/types/flow/base/flowTracker.d.ts.map +1 -0
  15. package/dist/types/flow/base/index.d.ts +7 -0
  16. package/dist/types/flow/base/index.d.ts.map +1 -0
  17. package/dist/types/flow/base/utils.d.ts +20 -0
  18. package/dist/types/flow/base/utils.d.ts.map +1 -0
  19. package/dist/types/{advanced/array.d.ts → flow/collections/flowArray.d.ts} +50 -12
  20. package/dist/types/flow/collections/flowArray.d.ts.map +1 -0
  21. package/dist/types/flow/collections/flowMap.d.ts +224 -0
  22. package/dist/types/flow/collections/flowMap.d.ts.map +1 -0
  23. package/dist/types/flow/collections/index.d.ts +3 -0
  24. package/dist/types/flow/collections/index.d.ts.map +1 -0
  25. package/dist/types/flow/index.d.ts +4 -0
  26. package/dist/types/flow/index.d.ts.map +1 -0
  27. package/dist/types/flow/nodes/async/flowConstantAsync.d.ts +137 -0
  28. package/dist/types/flow/nodes/async/flowConstantAsync.d.ts.map +1 -0
  29. package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts +137 -0
  30. package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts.map +1 -0
  31. package/dist/types/flow/nodes/async/flowNodeAsync.d.ts +343 -0
  32. package/dist/types/flow/nodes/async/flowNodeAsync.d.ts.map +1 -0
  33. package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts +81 -0
  34. package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts.map +1 -0
  35. package/dist/types/flow/nodes/async/flowStateAsync.d.ts +111 -0
  36. package/dist/types/flow/nodes/async/flowStateAsync.d.ts.map +1 -0
  37. package/dist/types/flow/nodes/async/index.d.ts +6 -0
  38. package/dist/types/flow/nodes/async/index.d.ts.map +1 -0
  39. package/dist/types/flow/nodes/index.d.ts +3 -0
  40. package/dist/types/flow/nodes/index.d.ts.map +1 -0
  41. package/dist/types/flow/nodes/sync/flowConstant.d.ts +108 -0
  42. package/dist/types/flow/nodes/sync/flowConstant.d.ts.map +1 -0
  43. package/dist/types/flow/nodes/sync/flowDerivation.d.ts +100 -0
  44. package/dist/types/flow/nodes/sync/flowDerivation.d.ts.map +1 -0
  45. package/dist/types/flow/nodes/sync/flowNode.d.ts +314 -0
  46. package/dist/types/flow/nodes/sync/flowNode.d.ts.map +1 -0
  47. package/dist/types/flow/nodes/sync/flowReadonly.d.ts +57 -0
  48. package/dist/types/flow/nodes/sync/flowReadonly.d.ts.map +1 -0
  49. package/dist/types/flow/nodes/sync/flowState.d.ts +96 -0
  50. package/dist/types/flow/nodes/sync/flowState.d.ts.map +1 -0
  51. package/dist/types/flow/nodes/sync/index.d.ts +6 -0
  52. package/dist/types/flow/nodes/sync/index.d.ts.map +1 -0
  53. package/dist/types/index.d.ts +1 -4
  54. package/dist/types/index.d.ts.map +1 -1
  55. package/dist/types/solid/converters.d.ts +34 -44
  56. package/dist/types/solid/converters.d.ts.map +1 -1
  57. package/dist/types/solid/primitives.d.ts +1 -0
  58. package/dist/types/solid/primitives.d.ts.map +1 -1
  59. package/docs/.vitepress/config.mts +1 -1
  60. package/docs/api/typedoc-sidebar.json +81 -1
  61. package/package.json +60 -58
  62. package/src/flow/base/flowDisposable.ts +71 -0
  63. package/src/flow/base/flowEffect.ts +171 -0
  64. package/src/flow/base/flowGraph.ts +288 -0
  65. package/src/flow/base/flowSignal.ts +207 -0
  66. package/src/flow/base/flowTracker.ts +17 -0
  67. package/src/flow/base/index.ts +6 -0
  68. package/src/flow/base/utils.ts +19 -0
  69. package/src/flow/collections/flowArray.ts +409 -0
  70. package/src/flow/collections/flowMap.ts +398 -0
  71. package/src/flow/collections/index.ts +2 -0
  72. package/src/flow/index.ts +3 -0
  73. package/src/flow/nodes/async/flowConstantAsync.ts +142 -0
  74. package/src/flow/nodes/async/flowDerivationAsync.ts +143 -0
  75. package/src/flow/nodes/async/flowNodeAsync.ts +474 -0
  76. package/src/flow/nodes/async/flowReadonlyAsync.ts +81 -0
  77. package/src/flow/nodes/async/flowStateAsync.ts +116 -0
  78. package/src/flow/nodes/async/index.ts +5 -0
  79. package/src/flow/nodes/await/advanced/index.ts +5 -0
  80. package/src/{advanced → flow/nodes/await/advanced}/resource.ts +37 -3
  81. package/src/{advanced → flow/nodes/await/advanced}/resourceAsync.ts +35 -3
  82. package/src/{advanced → flow/nodes/await/advanced}/stream.ts +40 -2
  83. package/src/{advanced → flow/nodes/await/advanced}/streamAsync.ts +38 -3
  84. package/src/flow/nodes/await/flowConstantAwait.ts +154 -0
  85. package/src/flow/nodes/await/flowDerivationAwait.ts +154 -0
  86. package/src/flow/nodes/await/flowNodeAwait.ts +508 -0
  87. package/src/flow/nodes/await/flowReadonlyAwait.ts +89 -0
  88. package/src/flow/nodes/await/flowStateAwait.ts +130 -0
  89. package/src/flow/nodes/await/index.ts +5 -0
  90. package/src/flow/nodes/index.ts +3 -0
  91. package/src/flow/nodes/sync/flowConstant.ts +111 -0
  92. package/src/flow/nodes/sync/flowDerivation.ts +105 -0
  93. package/src/flow/nodes/sync/flowNode.ts +439 -0
  94. package/src/flow/nodes/sync/flowReadonly.ts +57 -0
  95. package/src/flow/nodes/sync/flowState.ts +101 -0
  96. package/src/flow/nodes/sync/index.ts +5 -0
  97. package/src/index.ts +1 -47
  98. package/src/solid/converters.ts +59 -198
  99. package/src/solid/primitives.ts +4 -0
  100. package/test/base/flowEffect.test.ts +108 -0
  101. package/test/base/flowGraph.test.ts +485 -0
  102. package/test/base/flowSignal.test.ts +372 -0
  103. package/test/collections/flowArray.asyncStates.test.ts +1553 -0
  104. package/test/collections/flowArray.scalars.test.ts +1129 -0
  105. package/test/collections/flowArray.states.test.ts +1365 -0
  106. package/test/collections/flowMap.asyncStates.test.ts +1105 -0
  107. package/test/collections/flowMap.scalars.test.ts +877 -0
  108. package/test/collections/flowMap.states.test.ts +1097 -0
  109. package/test/nodes/async/flowConstantAsync.test.ts +860 -0
  110. package/test/nodes/async/flowDerivationAsync.test.ts +1517 -0
  111. package/test/nodes/async/flowStateAsync.test.ts +1387 -0
  112. package/test/{resource.test.ts → nodes/await/advanced/resource.test.ts} +21 -19
  113. package/test/{resourceAsync.test.ts → nodes/await/advanced/resourceAsync.test.ts} +3 -1
  114. package/test/{stream.test.ts → nodes/await/advanced/stream.test.ts} +30 -28
  115. package/test/{streamAsync.test.ts → nodes/await/advanced/streamAsync.test.ts} +16 -14
  116. package/test/nodes/await/flowConstantAwait.test.ts +643 -0
  117. package/test/nodes/await/flowDerivationAwait.test.ts +1583 -0
  118. package/test/nodes/await/flowStateAwait.test.ts +999 -0
  119. package/test/nodes/mixed/derivation.test.ts +1527 -0
  120. package/test/nodes/sync/flowConstant.test.ts +620 -0
  121. package/test/nodes/sync/flowDerivation.test.ts +1373 -0
  122. package/test/nodes/sync/flowState.test.ts +945 -0
  123. package/test/solid/converters.test.ts +721 -0
  124. package/test/solid/primitives.test.ts +1031 -0
  125. package/tsconfig.json +2 -1
  126. package/vitest.config.ts +7 -1
  127. package/IMPLEMENTATION_GUIDE.md +0 -1578
  128. package/dist/types/advanced/array.d.ts.map +0 -1
  129. package/dist/types/advanced/index.d.ts +0 -9
  130. package/dist/types/advanced/index.d.ts.map +0 -1
  131. package/dist/types/advanced/map.d.ts +0 -166
  132. package/dist/types/advanced/map.d.ts.map +0 -1
  133. package/dist/types/advanced/resource.d.ts +0 -78
  134. package/dist/types/advanced/resource.d.ts.map +0 -1
  135. package/dist/types/advanced/resourceAsync.d.ts +0 -56
  136. package/dist/types/advanced/resourceAsync.d.ts.map +0 -1
  137. package/dist/types/advanced/stream.d.ts +0 -117
  138. package/dist/types/advanced/stream.d.ts.map +0 -1
  139. package/dist/types/advanced/streamAsync.d.ts +0 -97
  140. package/dist/types/advanced/streamAsync.d.ts.map +0 -1
  141. package/dist/types/basic/constant.d.ts +0 -60
  142. package/dist/types/basic/constant.d.ts.map +0 -1
  143. package/dist/types/basic/derivation.d.ts +0 -89
  144. package/dist/types/basic/derivation.d.ts.map +0 -1
  145. package/dist/types/basic/disposable.d.ts +0 -82
  146. package/dist/types/basic/disposable.d.ts.map +0 -1
  147. package/dist/types/basic/effect.d.ts +0 -67
  148. package/dist/types/basic/effect.d.ts.map +0 -1
  149. package/dist/types/basic/index.d.ts +0 -10
  150. package/dist/types/basic/index.d.ts.map +0 -1
  151. package/dist/types/basic/observable.d.ts +0 -83
  152. package/dist/types/basic/observable.d.ts.map +0 -1
  153. package/dist/types/basic/signal.d.ts +0 -69
  154. package/dist/types/basic/signal.d.ts.map +0 -1
  155. package/dist/types/basic/state.d.ts +0 -47
  156. package/dist/types/basic/state.d.ts.map +0 -1
  157. package/dist/types/basic/trackingContext.d.ts +0 -33
  158. package/dist/types/basic/trackingContext.d.ts.map +0 -1
  159. package/dist/types/creators.d.ts +0 -340
  160. package/dist/types/creators.d.ts.map +0 -1
  161. package/src/advanced/array.ts +0 -222
  162. package/src/advanced/index.ts +0 -12
  163. package/src/advanced/map.ts +0 -193
  164. package/src/basic/constant.ts +0 -97
  165. package/src/basic/derivation.ts +0 -147
  166. package/src/basic/disposable.ts +0 -86
  167. package/src/basic/effect.ts +0 -104
  168. package/src/basic/index.ts +0 -9
  169. package/src/basic/observable.ts +0 -109
  170. package/src/basic/signal.ts +0 -145
  171. package/src/basic/state.ts +0 -60
  172. package/src/basic/trackingContext.ts +0 -45
  173. package/src/creators.ts +0 -395
  174. package/test/array.test.ts +0 -600
  175. package/test/constant.test.ts +0 -44
  176. package/test/derivation.test.ts +0 -539
  177. package/test/effect.test.ts +0 -29
  178. package/test/map.test.ts +0 -240
  179. package/test/signal.test.ts +0 -72
  180. package/test/state.test.ts +0 -212
@@ -0,0 +1,620 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { constant, effect, state } from "#package";
3
+
4
+ describe("flowConstant", () => {
5
+ describe("unit", () => {
6
+ describe("initialization", () => {
7
+ it("should initialize with computed value", async () => {
8
+ const $constant = constant(() => 1);
9
+ expect(await $constant.pick()).toBe(1);
10
+ });
11
+
12
+ it("should compute value lazily on first access", async () => {
13
+ const computeFn = vi.fn(() => 42);
14
+ const $constant = constant(computeFn);
15
+
16
+ // Function should not be called yet
17
+ expect(computeFn).not.toHaveBeenCalled();
18
+
19
+ // First access triggers computation
20
+ const value = await $constant.pick();
21
+ expect(value).toBe(42);
22
+ expect(computeFn).toHaveBeenCalledTimes(1);
23
+ });
24
+
25
+ it("should call compute function only once", async () => {
26
+ const computeFn = vi.fn(() => 100);
27
+ const $constant = constant(computeFn);
28
+
29
+ // Multiple accesses should only call compute once
30
+ await $constant.pick();
31
+ await $constant.pick();
32
+ await $constant.pick();
33
+
34
+ expect(computeFn).toHaveBeenCalledTimes(1);
35
+ });
36
+
37
+ it("should cache value after first computation", async () => {
38
+ const computeFn = vi.fn(() => "cached");
39
+ const $constant = constant(computeFn);
40
+
41
+ const value1 = await $constant.pick();
42
+ const value2 = await $constant.pick();
43
+ const value3 = await $constant.pick();
44
+
45
+ expect(value1).toBe("cached");
46
+ expect(value2).toBe("cached");
47
+ expect(value3).toBe("cached");
48
+ expect(computeFn).toHaveBeenCalledTimes(1);
49
+ });
50
+
51
+ it("should handle number values", async () => {
52
+ const $constant = constant(() => 42);
53
+ expect(await $constant.pick()).toBe(42);
54
+ });
55
+
56
+ it("should handle string values", async () => {
57
+ const $constant = constant(() => "hello");
58
+ expect(await $constant.pick()).toBe("hello");
59
+ });
60
+
61
+ it("should handle object values", async () => {
62
+ const obj = { a: 1, b: 2 };
63
+ const $constant = constant(() => obj);
64
+ const value = await $constant.pick();
65
+ expect(value).toBe(obj);
66
+ expect(value).toEqual({ a: 1, b: 2 });
67
+ });
68
+
69
+ it("should handle array values", async () => {
70
+ const arr = [1, 2, 3];
71
+ const $constant = constant(() => arr);
72
+ const value = await $constant.pick();
73
+ expect(value).toBe(arr);
74
+ expect(value).toEqual([1, 2, 3]);
75
+ });
76
+
77
+ it("should handle undefined values", async () => {
78
+ const $constant = constant(() => undefined);
79
+ const value = await $constant.pick();
80
+ expect(value).toBeUndefined();
81
+ });
82
+
83
+ it("should handle null values", async () => {
84
+ const $constant = constant(() => null);
85
+ const value = await $constant.pick();
86
+ expect(value).toBeNull();
87
+ });
88
+ });
89
+
90
+ describe("get", () => {
91
+ it("should return value with tracking context", () => {
92
+ const $constant = constant(() => 5);
93
+ const $tracker = state(0); // Use state as FlowTracker
94
+ const value = $constant.get($tracker);
95
+ expect(value).toBe(5);
96
+ });
97
+
98
+ it("should compute value on first get call", () => {
99
+ const computeFn = vi.fn(() => 10);
100
+ const $constant = constant(computeFn);
101
+ const $tracker = state(0);
102
+
103
+ expect(computeFn).not.toHaveBeenCalled();
104
+ const value = $constant.get($tracker);
105
+ expect(value).toBe(10);
106
+ expect(computeFn).toHaveBeenCalledTimes(1);
107
+ });
108
+
109
+ it("should return cached value on subsequent get calls", () => {
110
+ const computeFn = vi.fn(() => 20);
111
+ const $constant = constant(computeFn);
112
+ const $tracker = state(0);
113
+
114
+ const value1 = $constant.get($tracker);
115
+ const value2 = $constant.get($tracker);
116
+ const value3 = $constant.get($tracker);
117
+
118
+ expect(value1).toBe(20);
119
+ expect(value2).toBe(20);
120
+ expect(value3).toBe(20);
121
+ expect(computeFn).toHaveBeenCalledTimes(1);
122
+ });
123
+
124
+ it("should throw error when get is called after disposal", () => {
125
+ const $constant = constant(() => 1);
126
+ const $tracker = state(0);
127
+
128
+ $constant.get($tracker);
129
+ $constant.dispose();
130
+
131
+ expect(() => $constant.get($tracker)).toThrow(
132
+ "[PicoFlow] Primitive is disposed",
133
+ );
134
+ });
135
+ });
136
+
137
+ describe("pick", () => {
138
+ it("should return value without tracking", async () => {
139
+ const $constant = constant(() => 15);
140
+ const value = await $constant.pick();
141
+ expect(value).toBe(15);
142
+ });
143
+
144
+ it("should compute value on first pick call", async () => {
145
+ const computeFn = vi.fn(() => 25);
146
+ const $constant = constant(computeFn);
147
+
148
+ expect(computeFn).not.toHaveBeenCalled();
149
+ const value = await $constant.pick();
150
+ expect(value).toBe(25);
151
+ expect(computeFn).toHaveBeenCalledTimes(1);
152
+ });
153
+
154
+ it("should return cached value on subsequent pick calls", async () => {
155
+ const computeFn = vi.fn(() => 30);
156
+ const $constant = constant(computeFn);
157
+
158
+ const value1 = await $constant.pick();
159
+ const value2 = await $constant.pick();
160
+ const value3 = await $constant.pick();
161
+
162
+ expect(value1).toBe(30);
163
+ expect(value2).toBe(30);
164
+ expect(value3).toBe(30);
165
+ expect(computeFn).toHaveBeenCalledTimes(1);
166
+ });
167
+
168
+ it("should throw error when pick is called after disposal", async () => {
169
+ const $constant = constant(() => 1);
170
+
171
+ expect(await $constant.pick()).toBe(1);
172
+
173
+ $constant.dispose();
174
+
175
+ await expect($constant.pick()).rejects.toThrow(
176
+ "[PicoFlow] Primitive is disposed",
177
+ );
178
+ });
179
+ });
180
+
181
+ describe("watch", () => {
182
+ it("should register dependency when watch is called", () => {
183
+ const $constant = constant(() => 35);
184
+ const $tracker = state(0);
185
+
186
+ expect(() => $constant.watch($tracker)).not.toThrow();
187
+ });
188
+
189
+ it("should compute value when watch is called", () => {
190
+ const computeFn = vi.fn(() => 40);
191
+ const $constant = constant(computeFn);
192
+ const $tracker = state(0);
193
+
194
+ expect(computeFn).not.toHaveBeenCalled();
195
+ $constant.watch($tracker);
196
+ expect(computeFn).toHaveBeenCalledTimes(1);
197
+ });
198
+
199
+ it("should throw error when watch is called after disposal", () => {
200
+ const $constant = constant(() => 1);
201
+ const $tracker = state(0);
202
+
203
+ $constant.watch($tracker);
204
+ $constant.dispose();
205
+
206
+ expect(() => $constant.watch($tracker)).toThrow(
207
+ "[PicoFlow] Primitive is disposed",
208
+ );
209
+ });
210
+ });
211
+
212
+ describe("subscribe", () => {
213
+ it("should return a disposer function", () => {
214
+ const $constant = constant(() => 50);
215
+ const listener = vi.fn();
216
+ const disposer = $constant.subscribe(listener);
217
+
218
+ expect(typeof disposer).toBe("function");
219
+ });
220
+
221
+ it("should call listener immediately with current value", async () => {
222
+ const $constant = constant(() => 55);
223
+ const listener = vi.fn();
224
+
225
+ $constant.subscribe(listener);
226
+
227
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
228
+ expect(listener).toHaveBeenLastCalledWith(55);
229
+ });
230
+
231
+ it("should not call listener after disposal of constant", async () => {
232
+ const $constant = constant(() => 60);
233
+ const listener = vi.fn();
234
+
235
+ $constant.subscribe(listener);
236
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
237
+
238
+ $constant.dispose();
239
+
240
+ // Wait to ensure listener is not called again
241
+ await new Promise((resolve) => setTimeout(resolve, 50));
242
+ expect(listener).toHaveBeenCalledTimes(1);
243
+ });
244
+
245
+ it("should not call listener after disposer is called", async () => {
246
+ const $constant = constant(() => 65);
247
+ const listener = vi.fn();
248
+
249
+ const disposer = $constant.subscribe(listener);
250
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
251
+
252
+ disposer();
253
+
254
+ // Wait to ensure listener is not called again
255
+ await new Promise((resolve) => setTimeout(resolve, 50));
256
+ expect(listener).toHaveBeenCalledTimes(1);
257
+ });
258
+
259
+ it("should support multiple subscriptions", async () => {
260
+ const $constant = constant(() => 70);
261
+ const listener1 = vi.fn();
262
+ const listener2 = vi.fn();
263
+ const listener3 = vi.fn();
264
+
265
+ $constant.subscribe(listener1);
266
+ $constant.subscribe(listener2);
267
+ $constant.subscribe(listener3);
268
+
269
+ await vi.waitFor(() => {
270
+ expect(listener1).toHaveBeenCalledTimes(1);
271
+ expect(listener2).toHaveBeenCalledTimes(1);
272
+ expect(listener3).toHaveBeenCalledTimes(1);
273
+ });
274
+
275
+ expect(listener1).toHaveBeenLastCalledWith(70);
276
+ expect(listener2).toHaveBeenLastCalledWith(70);
277
+ expect(listener3).toHaveBeenLastCalledWith(70);
278
+ });
279
+
280
+ it("should throw error when subscribe is called after disposal", () => {
281
+ const $constant = constant(() => 1);
282
+ const listener = vi.fn();
283
+
284
+ $constant.dispose();
285
+
286
+ expect(() => $constant.subscribe(listener)).toThrow(
287
+ "[PicoFlow] Primitive is disposed",
288
+ );
289
+ });
290
+ });
291
+
292
+ describe("disposal", () => {
293
+ it("should have disposed property set to false initially", () => {
294
+ const $constant = constant(() => 1);
295
+ expect($constant.disposed).toBe(false);
296
+ });
297
+
298
+ it("should have disposed property set to true after disposal", () => {
299
+ const $constant = constant(() => 1);
300
+ $constant.dispose();
301
+ expect($constant.disposed).toBe(true);
302
+ });
303
+
304
+ it("should throw error when disposed twice", () => {
305
+ const $constant = constant(() => 1);
306
+ $constant.dispose();
307
+ expect(() => $constant.dispose()).toThrow(
308
+ "[PicoFlow] Primitive is disposed",
309
+ );
310
+ });
311
+ });
312
+
313
+ describe("special cases", () => {
314
+ it("should handle constant with compute function using closure to capture values", () => {
315
+ let externalValue = 10;
316
+ const $constant = constant(() => {
317
+ // Closure captures external value at computation time
318
+ return externalValue * 2;
319
+ });
320
+
321
+ // Constant computes once with captured value
322
+ expect($constant.get(state(0))).toBe(20);
323
+
324
+ // Changing external value should not affect constant (it's cached)
325
+ externalValue = 20;
326
+ expect($constant.get(state(0))).toBe(20); // Still cached value
327
+ });
328
+ });
329
+
330
+ describe("error handling", () => {
331
+ describe("compute function errors", () => {
332
+ it("should propagate error when pick is called", async () => {
333
+ const error = new Error("Compute error");
334
+ const $constant = constant(() => {
335
+ throw error;
336
+ });
337
+
338
+ await expect($constant.pick()).rejects.toThrow("Compute error");
339
+ });
340
+
341
+ it("should propagate error when get is called", () => {
342
+ const error = new Error("Get error");
343
+ const $constant = constant(() => {
344
+ throw error;
345
+ });
346
+
347
+ expect(() => $constant.get(state(0))).toThrow("Get error");
348
+ });
349
+ });
350
+
351
+ describe("disposed constant", () => {
352
+ it("should throw error when pick is called after disposal", async () => {
353
+ const $constant = constant(() => 1);
354
+
355
+ $constant.dispose();
356
+
357
+ await expect($constant.pick()).rejects.toThrow(
358
+ "[PicoFlow] Primitive is disposed",
359
+ );
360
+ });
361
+
362
+ it("should throw error when get is called after disposal", () => {
363
+ const $constant = constant(() => 1);
364
+ const $tracker = state(0);
365
+
366
+ $constant.dispose();
367
+
368
+ expect(() => $constant.get($tracker)).toThrow(
369
+ "[PicoFlow] Primitive is disposed",
370
+ );
371
+ });
372
+ });
373
+ });
374
+ });
375
+
376
+ describe("with effect", () => {
377
+ describe("get", () => {
378
+ it("should create reactive dependency when get is used in effect", async () => {
379
+ const $constant = constant(() => 100);
380
+ const effectFn = vi.fn();
381
+
382
+ effect((t) => {
383
+ $constant.get(t);
384
+ effectFn();
385
+ });
386
+
387
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
388
+ });
389
+
390
+ it("should not re-execute effect when constant value does not change", async () => {
391
+ const $constant = constant(() => 200);
392
+ const effectFn = vi.fn();
393
+
394
+ effect((t) => {
395
+ $constant.get(t);
396
+ effectFn();
397
+ });
398
+
399
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
400
+
401
+ // Wait to ensure effect doesn't re-run
402
+ await new Promise((resolve) => setTimeout(resolve, 50));
403
+ expect(effectFn).toHaveBeenCalledTimes(1);
404
+ });
405
+
406
+ it("should not re-execute effect when constant value is immutable", async () => {
407
+ let capturedValue = 5;
408
+ const $constant = constant(() => capturedValue * 3);
409
+
410
+ const effectFn = vi.fn();
411
+ effect((t) => {
412
+ const value = $constant.get(t);
413
+ effectFn(value);
414
+ });
415
+
416
+ // Initial execution with computed value (5 * 3 = 15)
417
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
418
+ expect(effectFn).toHaveBeenLastCalledWith(15);
419
+
420
+ // Change captured value - constant is cached, so effect should not re-run
421
+ capturedValue = 10;
422
+ await new Promise((resolve) => setTimeout(resolve, 50));
423
+
424
+ // Effect should not re-run because constant is immutable and cached
425
+ expect(effectFn).toHaveBeenCalledTimes(1);
426
+ });
427
+
428
+ it("should support multiple effects depending on same constant", async () => {
429
+ const $constant = constant(() => 300);
430
+ const effectFn1 = vi.fn();
431
+ const effectFn2 = vi.fn();
432
+ const effectFn3 = vi.fn();
433
+
434
+ effect((t) => {
435
+ $constant.get(t);
436
+ effectFn1();
437
+ });
438
+
439
+ effect((t) => {
440
+ $constant.get(t);
441
+ effectFn2();
442
+ });
443
+
444
+ effect((t) => {
445
+ $constant.get(t);
446
+ effectFn3();
447
+ });
448
+
449
+ await vi.waitFor(() => {
450
+ expect(effectFn1).toHaveBeenCalledTimes(1);
451
+ expect(effectFn2).toHaveBeenCalledTimes(1);
452
+ expect(effectFn3).toHaveBeenCalledTimes(1);
453
+ });
454
+ });
455
+ });
456
+
457
+ describe("watch", () => {
458
+ it("should register dependency when watch is used in effect", async () => {
459
+ const $constant = constant(() => 400);
460
+ const effectFn = vi.fn();
461
+
462
+ effect((t) => {
463
+ $constant.watch(t);
464
+ effectFn();
465
+ });
466
+
467
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
468
+ });
469
+
470
+ it("should not trigger re-runs when constant is watched (immutable)", async () => {
471
+ const $constant = constant(() => 500);
472
+ const effectFn = vi.fn();
473
+
474
+ effect((t) => {
475
+ $constant.watch(t);
476
+ effectFn();
477
+ });
478
+
479
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
480
+
481
+ // Wait to ensure effect doesn't re-run
482
+ await new Promise((resolve) => setTimeout(resolve, 50));
483
+ expect(effectFn).toHaveBeenCalledTimes(1);
484
+ });
485
+ });
486
+
487
+ describe("subscribe", () => {
488
+ it("should call listener immediately when subscribe is used", async () => {
489
+ const $constant = constant(() => 600);
490
+ const listener = vi.fn();
491
+
492
+ $constant.subscribe(listener);
493
+
494
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
495
+ expect(listener).toHaveBeenLastCalledWith(600);
496
+ });
497
+
498
+ it("should not call listener after constant disposal", async () => {
499
+ const $constant = constant(() => 700);
500
+ const listener = vi.fn();
501
+
502
+ $constant.subscribe(listener);
503
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
504
+
505
+ $constant.dispose();
506
+
507
+ // Wait to ensure listener is not called again
508
+ await new Promise((resolve) => setTimeout(resolve, 50));
509
+ expect(listener).toHaveBeenCalledTimes(1);
510
+ });
511
+
512
+ it("should support multiple subscriptions", async () => {
513
+ const $constant = constant(() => 800);
514
+ const listener1 = vi.fn();
515
+ const listener2 = vi.fn();
516
+
517
+ $constant.subscribe(listener1);
518
+ $constant.subscribe(listener2);
519
+
520
+ await vi.waitFor(() => {
521
+ expect(listener1).toHaveBeenCalledTimes(1);
522
+ expect(listener2).toHaveBeenCalledTimes(1);
523
+ });
524
+
525
+ expect(listener1).toHaveBeenLastCalledWith(800);
526
+ expect(listener2).toHaveBeenLastCalledWith(800);
527
+ });
528
+ });
529
+
530
+ describe("disposal", () => {
531
+ it("should dispose effects when constant is disposed", async () => {
532
+ const $constant = constant(() => 900);
533
+ const effectFn = vi.fn();
534
+ const $effect = effect((t) => {
535
+ $constant.get(t);
536
+ effectFn();
537
+ });
538
+
539
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
540
+ expect($effect.disposed).toBe(false);
541
+
542
+ $constant.dispose();
543
+
544
+ expect($effect.disposed).toBe(true);
545
+ });
546
+
547
+ it("should not dispose effects when constant is disposed with self option", async () => {
548
+ const $constant = constant(() => 1000);
549
+ const effectFn = vi.fn();
550
+ const $effect = effect((t) => {
551
+ $constant.get(t);
552
+ effectFn();
553
+ });
554
+
555
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
556
+ expect($effect.disposed).toBe(false);
557
+
558
+ $constant.dispose({ self: true });
559
+
560
+ expect($effect.disposed).toBe(false);
561
+ expect($constant.disposed).toBe(true);
562
+ });
563
+
564
+ it("should unregister effects when constant is disposed with self option", async () => {
565
+ const $constant = constant(() => 1100);
566
+ const effectFn = vi.fn();
567
+ const $effect = effect((t) => {
568
+ $constant.get(t);
569
+ effectFn();
570
+ });
571
+
572
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
573
+
574
+ $constant.dispose({ self: true });
575
+
576
+ // Effect should still be active but not notified
577
+ expect($effect.disposed).toBe(false);
578
+
579
+ // But constant is disposed so operations should fail
580
+ const $tracker = state(0);
581
+ expect(() => $constant.get($tracker)).toThrow(
582
+ "[PicoFlow] Primitive is disposed",
583
+ );
584
+ });
585
+ });
586
+
587
+ describe("error handling", () => {
588
+ describe("compute function errors", () => {
589
+ it("should propagate error when used inside an effect", async () => {
590
+ const error = new Error("Effect compute error");
591
+ const $constant = constant(() => {
592
+ throw error;
593
+ });
594
+
595
+ const $effect = effect((t) => {
596
+ $constant.get(t);
597
+ });
598
+
599
+ await expect($effect.settled).rejects.toThrow("Effect compute error");
600
+ });
601
+ });
602
+
603
+ describe("disposed constant", () => {
604
+ it("should throw error when creating an effect with a disposed constant", async () => {
605
+ const $constant = constant(() => 1);
606
+
607
+ $constant.dispose();
608
+
609
+ const $effect = effect((t) => {
610
+ $constant.get(t);
611
+ });
612
+
613
+ await expect($effect.settled).rejects.toThrow(
614
+ "[PicoFlow] Primitive is disposed",
615
+ );
616
+ });
617
+ });
618
+ });
619
+ });
620
+ });