@ersbeth/picoflow 1.0.1 → 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 +17 -1
  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,1387 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { effect, state, stateAsync } from "#package";
3
+ import { FlowNodeAsync } from "../../../src/flow/nodes/async/flowNodeAsync";
4
+
5
+ describe("flowStateAsync", () => {
6
+ describe("unit", () => {
7
+ describe("initialization", () => {
8
+ it("should initialize with direct Promise", async () => {
9
+ const $state = stateAsync(Promise.resolve(1));
10
+ expect(await $state.pick()).toBe(1);
11
+ });
12
+
13
+ it("should initialize lazily with function", async () => {
14
+ const initFn = vi.fn(() => Promise.resolve(42));
15
+ const $state = stateAsync(initFn);
16
+
17
+ // Function should not be called yet
18
+ expect(initFn).not.toHaveBeenCalled();
19
+
20
+ // First access triggers initialization
21
+ const value = await $state.pick();
22
+ expect(value).toBe(42);
23
+ expect(initFn).toHaveBeenCalledTimes(1);
24
+ });
25
+
26
+ it("should call lazy init function only once", async () => {
27
+ const initFn = vi.fn(() => Promise.resolve(100));
28
+ const $state = stateAsync(initFn);
29
+
30
+ // Multiple accesses should only call init once
31
+ await $state.pick();
32
+ await $state.pick();
33
+ await $state.pick();
34
+
35
+ expect(initFn).toHaveBeenCalledTimes(1);
36
+ });
37
+
38
+ it("should cache value after lazy initialization", async () => {
39
+ const initFn = vi.fn(() => Promise.resolve("cached"));
40
+ const $state = stateAsync(initFn);
41
+
42
+ const value1 = await $state.pick();
43
+ const value2 = await $state.pick();
44
+ const value3 = await $state.pick();
45
+
46
+ expect(value1).toBe("cached");
47
+ expect(value2).toBe("cached");
48
+ expect(value3).toBe("cached");
49
+ expect(initFn).toHaveBeenCalledTimes(1);
50
+ });
51
+
52
+ it("should handle number values", async () => {
53
+ const $state = stateAsync(Promise.resolve(42));
54
+ expect(await $state.pick()).toBe(42);
55
+ });
56
+
57
+ it("should handle string values", async () => {
58
+ const $state = stateAsync(Promise.resolve("hello"));
59
+ expect(await $state.pick()).toBe("hello");
60
+ });
61
+
62
+ it("should handle object values", async () => {
63
+ const obj = { a: 1, b: 2 };
64
+ const $state = stateAsync(Promise.resolve(obj));
65
+ const value = await $state.pick();
66
+ expect(value).toBe(obj);
67
+ expect(value).toEqual({ a: 1, b: 2 });
68
+ });
69
+
70
+ it("should handle array values", async () => {
71
+ const arr = [1, 2, 3];
72
+ const $state = stateAsync(Promise.resolve(arr));
73
+ const value = await $state.pick();
74
+ expect(value).toBe(arr);
75
+ expect(value).toEqual([1, 2, 3]);
76
+ });
77
+
78
+ it("should handle null values", async () => {
79
+ const $state = stateAsync(Promise.resolve(null));
80
+ const value = await $state.pick();
81
+ expect(value).toBeNull();
82
+ });
83
+
84
+ it("should handle undefined values", async () => {
85
+ const $state = stateAsync(Promise.resolve(undefined));
86
+ const value = await $state.pick();
87
+ expect(value).toBeUndefined();
88
+ });
89
+
90
+ it("should handle Promise rejection in initialization", async () => {
91
+ const $state = stateAsync(Promise.reject(new Error("Init error")));
92
+ await expect($state.pick()).rejects.toThrow("Init error");
93
+ });
94
+ });
95
+
96
+ describe("get", () => {
97
+ it("should return Promise with tracking context", async () => {
98
+ const $state = stateAsync(Promise.resolve(5));
99
+ const $tracker = state(0);
100
+ const promise = $state.get($tracker);
101
+
102
+ expect(promise).toBeInstanceOf(Promise);
103
+ const value = await promise;
104
+ expect(value).toBe(5);
105
+ });
106
+
107
+ it("should compute lazy value on first get call", async () => {
108
+ const initFn = vi.fn(() => Promise.resolve(20));
109
+ const $state = stateAsync(initFn);
110
+ const $tracker = state(0);
111
+
112
+ expect(initFn).not.toHaveBeenCalled();
113
+ const promise = $state.get($tracker);
114
+ expect(promise).toBeInstanceOf(Promise);
115
+ const value = await promise;
116
+ expect(value).toBe(20);
117
+ expect(initFn).toHaveBeenCalledTimes(1);
118
+ });
119
+
120
+ it("should return cached value on subsequent get calls", async () => {
121
+ const initFn = vi.fn(() => Promise.resolve(20));
122
+ const $state = stateAsync(initFn);
123
+ const $tracker = state(0);
124
+
125
+ const promise1 = $state.get($tracker);
126
+ const promise2 = $state.get($tracker);
127
+ const promise3 = $state.get($tracker);
128
+
129
+ const value1 = await promise1;
130
+ const value2 = await promise2;
131
+ const value3 = await promise3;
132
+
133
+ expect(value1).toBe(20);
134
+ expect(value2).toBe(20);
135
+ expect(value3).toBe(20);
136
+ expect(initFn).toHaveBeenCalledTimes(1);
137
+ });
138
+
139
+ it("should throw error when get is called after disposal", async () => {
140
+ const $state = stateAsync(Promise.resolve(1));
141
+ const $tracker = state(0);
142
+
143
+ await $state.get($tracker);
144
+ $state.dispose();
145
+
146
+ await expect($state.get($tracker)).rejects.toThrow(
147
+ "[PicoFlow] Primitive is disposed",
148
+ );
149
+ });
150
+ });
151
+
152
+ describe("pick", () => {
153
+ it("should return Promise without tracking", async () => {
154
+ const $state = stateAsync(Promise.resolve(15));
155
+ const promise = $state.pick();
156
+
157
+ expect(promise).toBeInstanceOf(Promise);
158
+ const value = await promise;
159
+ expect(value).toBe(15);
160
+ });
161
+
162
+ it("should compute lazy value on first pick call", async () => {
163
+ const initFn = vi.fn(() => Promise.resolve(25));
164
+ const $state = stateAsync(initFn);
165
+
166
+ expect(initFn).not.toHaveBeenCalled();
167
+ const promise = $state.pick();
168
+ expect(promise).toBeInstanceOf(Promise);
169
+ const value = await promise;
170
+ expect(value).toBe(25);
171
+ expect(initFn).toHaveBeenCalledTimes(1);
172
+ });
173
+
174
+ it("should return cached value on subsequent pick calls", async () => {
175
+ const initFn = vi.fn(() => Promise.resolve(30));
176
+ const $state = stateAsync(initFn);
177
+
178
+ const value1 = await $state.pick();
179
+ const value2 = await $state.pick();
180
+ const value3 = await $state.pick();
181
+
182
+ expect(value1).toBe(30);
183
+ expect(value2).toBe(30);
184
+ expect(value3).toBe(30);
185
+ expect(initFn).toHaveBeenCalledTimes(1);
186
+ });
187
+
188
+ it("should throw error when pick is called after disposal", async () => {
189
+ const $state = stateAsync(Promise.resolve(1));
190
+
191
+ expect(await $state.pick()).toBe(1);
192
+
193
+ $state.dispose();
194
+
195
+ await expect($state.pick()).rejects.toThrow(
196
+ "[PicoFlow] Primitive is disposed",
197
+ );
198
+ });
199
+ });
200
+
201
+ describe("set", () => {
202
+ it("should update value with Promise", async () => {
203
+ const $state = stateAsync(Promise.resolve(1));
204
+ expect(await $state.pick()).toBe(1);
205
+
206
+ await $state.set(Promise.resolve(2));
207
+ expect(await $state.pick()).toBe(2);
208
+ });
209
+
210
+ it("should update value with updater function", async () => {
211
+ const $state = stateAsync(Promise.resolve(1));
212
+ expect(await $state.pick()).toBe(1);
213
+
214
+ await $state.set(async (state) => state + 1);
215
+ expect(await $state.pick()).toBe(2);
216
+ });
217
+
218
+ it("should return a Promise", () => {
219
+ const $state = stateAsync(Promise.resolve(1));
220
+ const result = $state.set(Promise.resolve(2));
221
+ expect(result).toBeInstanceOf(Promise);
222
+ });
223
+
224
+ it("should not notify when set with same value", async () => {
225
+ const $state = stateAsync(Promise.resolve(1));
226
+ const listener = vi.fn();
227
+ $state.subscribe(async (value) => listener(await value));
228
+
229
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
230
+
231
+ await $state.set(Promise.resolve(1));
232
+
233
+ // Wait to ensure no additional calls
234
+ await new Promise((resolve) => setTimeout(resolve, 50));
235
+ expect(listener).toHaveBeenCalledTimes(1);
236
+ });
237
+
238
+ it("should notify when set with different value", async () => {
239
+ const $state = stateAsync(Promise.resolve(1));
240
+ const listener = vi.fn();
241
+ $state.subscribe(async (value) => listener(await value));
242
+
243
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
244
+
245
+ await $state.set(Promise.resolve(2));
246
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(2));
247
+ expect(listener).toHaveBeenLastCalledWith(2);
248
+ });
249
+
250
+ it("should not notify when updater returns same value", async () => {
251
+ const $state = stateAsync(Promise.resolve(5));
252
+ const listener = vi.fn();
253
+ $state.subscribe(async (value) => listener(await value));
254
+
255
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
256
+
257
+ await $state.set(async (current) => current);
258
+
259
+ // Wait to ensure no additional calls
260
+ await new Promise((resolve) => setTimeout(resolve, 50));
261
+ expect(listener).toHaveBeenCalledTimes(1);
262
+ });
263
+
264
+ it("should notify when updater returns different value", async () => {
265
+ const $state = stateAsync(Promise.resolve(5));
266
+ const listener = vi.fn();
267
+ $state.subscribe(async (value) => listener(await value));
268
+
269
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
270
+
271
+ await $state.set(async (current) => current + 1);
272
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(2));
273
+ expect(listener).toHaveBeenLastCalledWith(6);
274
+ });
275
+
276
+ it("should handle async updater function", async () => {
277
+ const $state = stateAsync(Promise.resolve(1));
278
+
279
+ await $state.set(async (current) => {
280
+ await new Promise((resolve) => setTimeout(resolve, 10));
281
+ return current + 10;
282
+ });
283
+
284
+ expect(await $state.pick()).toBe(11);
285
+ });
286
+
287
+ it("should handle Promise rejection in set", async () => {
288
+ const $state = stateAsync(Promise.resolve(1));
289
+
290
+ await expect(
291
+ $state.set(Promise.reject(new Error("Set error"))),
292
+ ).rejects.toThrow("Set error");
293
+ });
294
+
295
+ it("should return Promise<void>", async () => {
296
+ const $state = stateAsync(Promise.resolve(1));
297
+ const result = await $state.set(Promise.resolve(2));
298
+ expect(result).toBeUndefined();
299
+ });
300
+
301
+ it("should throw error when set is called after disposal", async () => {
302
+ const $state = stateAsync(Promise.resolve(1));
303
+
304
+ expect(await $state.pick()).toBe(1);
305
+
306
+ await $state.set(Promise.resolve(2));
307
+ expect(await $state.pick()).toBe(2);
308
+
309
+ $state.dispose();
310
+
311
+ await expect($state.set(Promise.resolve(3))).rejects.toThrow(
312
+ "[PicoFlow] Primitive is disposed",
313
+ );
314
+ });
315
+ });
316
+
317
+ describe("watch", () => {
318
+ it("should register dependency when watch is called", () => {
319
+ const $state = stateAsync(Promise.resolve(35));
320
+ const $tracker = state(0);
321
+
322
+ expect(() => $state.watch($tracker)).not.toThrow();
323
+ });
324
+
325
+ it("should compute lazy value when watch is called", async () => {
326
+ const initFn = vi.fn(() => Promise.resolve(40));
327
+ const $state = stateAsync(initFn);
328
+ const $tracker = state(0);
329
+
330
+ expect(initFn).not.toHaveBeenCalled();
331
+ $state.watch($tracker);
332
+ expect(initFn).toHaveBeenCalledTimes(1);
333
+ });
334
+
335
+ it("should throw error when watch is called after disposal", async () => {
336
+ const $state = stateAsync(Promise.resolve(1));
337
+ const $tracker = state(0);
338
+
339
+ await $state.watch($tracker);
340
+ $state.dispose();
341
+
342
+ await expect($state.watch($tracker)).rejects.toThrow(
343
+ "[PicoFlow] Primitive is disposed",
344
+ );
345
+ });
346
+ });
347
+
348
+ describe("subscribe", () => {
349
+ it("should return a disposer function", () => {
350
+ const $state = stateAsync(Promise.resolve(50));
351
+ const listener = vi.fn();
352
+ const disposer = $state.subscribe(listener);
353
+
354
+ expect(typeof disposer).toBe("function");
355
+ });
356
+
357
+ it("should call listener immediately with current value", async () => {
358
+ const $state = stateAsync(Promise.resolve(55));
359
+ const listener = vi.fn();
360
+
361
+ $state.subscribe(async (value) => listener(await value));
362
+
363
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
364
+ expect(listener).toHaveBeenLastCalledWith(55);
365
+ });
366
+
367
+ it("should call listener with lazy init value", async () => {
368
+ const $state = stateAsync(async () => 60);
369
+ const listener = vi.fn();
370
+
371
+ $state.subscribe(async (value) => listener(await value));
372
+
373
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
374
+ expect(listener).toHaveBeenLastCalledWith(60);
375
+ });
376
+
377
+ it("should call listener when value is updated", async () => {
378
+ const $state = stateAsync(Promise.resolve(65));
379
+ const listener = vi.fn();
380
+
381
+ $state.subscribe(async (value) => listener(await value));
382
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
383
+
384
+ await $state.set(Promise.resolve(70));
385
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(2));
386
+ expect(listener).toHaveBeenLastCalledWith(70);
387
+ });
388
+
389
+ it("should not call listener when updated with same value", async () => {
390
+ const $state = stateAsync(Promise.resolve(75));
391
+ const listener = vi.fn();
392
+
393
+ $state.subscribe(async (value) => listener(await value));
394
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
395
+
396
+ await $state.set(Promise.resolve(80));
397
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(2));
398
+
399
+ await $state.set(Promise.resolve(80));
400
+ await new Promise((resolve) => setTimeout(resolve, 50));
401
+ expect(listener).toHaveBeenCalledTimes(2);
402
+ });
403
+
404
+ it("should not call listener after disposer is called", async () => {
405
+ const $state = stateAsync(Promise.resolve(85));
406
+ const listener = vi.fn();
407
+
408
+ const unsubscribe = $state.subscribe(async (value) =>
409
+ listener(await value),
410
+ );
411
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
412
+
413
+ unsubscribe();
414
+
415
+ await $state.set(Promise.resolve(90));
416
+ await new Promise((resolve) => setTimeout(resolve, 50));
417
+ expect(listener).toHaveBeenCalledTimes(1);
418
+ });
419
+
420
+ it("should support multiple subscriptions", async () => {
421
+ const $state = stateAsync(Promise.resolve(95));
422
+ const listener1 = vi.fn();
423
+ const listener2 = vi.fn();
424
+ const listener3 = vi.fn();
425
+
426
+ $state.subscribe(async (value) => listener1(await value));
427
+ $state.subscribe(async (value) => listener2(await value));
428
+ $state.subscribe(async (value) => listener3(await value));
429
+
430
+ await vi.waitFor(() => {
431
+ expect(listener1).toHaveBeenCalledTimes(1);
432
+ expect(listener2).toHaveBeenCalledTimes(1);
433
+ expect(listener3).toHaveBeenCalledTimes(1);
434
+ });
435
+
436
+ expect(listener1).toHaveBeenLastCalledWith(95);
437
+ expect(listener2).toHaveBeenLastCalledWith(95);
438
+ expect(listener3).toHaveBeenLastCalledWith(95);
439
+ });
440
+ });
441
+
442
+ describe("trigger", () => {
443
+ it("should return a Promise", () => {
444
+ const $state = stateAsync(Promise.resolve(1));
445
+ const result = $state.trigger();
446
+ expect(result).toBeInstanceOf(Promise);
447
+ });
448
+
449
+ it("should allow multiple triggers", async () => {
450
+ const $state = stateAsync(Promise.resolve(1));
451
+ const promise1 = $state.trigger();
452
+ const promise2 = $state.trigger();
453
+ const promise3 = $state.trigger();
454
+
455
+ expect(promise1).toBeInstanceOf(Promise);
456
+ expect(promise2).toBeInstanceOf(Promise);
457
+ expect(promise3).toBeInstanceOf(Promise);
458
+
459
+ await Promise.all([promise1, promise2, promise3]);
460
+ });
461
+ });
462
+
463
+ describe("disposal", () => {
464
+ it("should have disposed property set to false initially", () => {
465
+ const $state = stateAsync(Promise.resolve(1));
466
+ expect($state.disposed).toBe(false);
467
+ });
468
+
469
+ it("should have disposed property set to true after disposal", () => {
470
+ const $state = stateAsync(Promise.resolve(1));
471
+ $state.dispose();
472
+ expect($state.disposed).toBe(true);
473
+ });
474
+
475
+ it("should throw error when disposed twice", () => {
476
+ const $state = stateAsync(Promise.resolve(1));
477
+ $state.dispose();
478
+ expect(() => $state.dispose()).toThrow(
479
+ "[PicoFlow] Primitive is disposed",
480
+ );
481
+ });
482
+
483
+ it("should accept self option without throwing", () => {
484
+ const $state = stateAsync(Promise.resolve(1));
485
+ expect(() => $state.dispose({ self: true })).not.toThrow();
486
+ expect($state.disposed).toBe(true);
487
+ });
488
+
489
+ it("should accept default options without throwing", () => {
490
+ const $state = stateAsync(Promise.resolve(1));
491
+ expect(() => $state.dispose()).not.toThrow();
492
+ expect($state.disposed).toBe(true);
493
+ });
494
+ });
495
+
496
+ describe("special cases", () => {
497
+ it("should handle lazy initialization with function returning different values", async () => {
498
+ let counter = 0;
499
+ const $state = stateAsync(async () => {
500
+ counter++;
501
+ return counter;
502
+ });
503
+
504
+ // First access should return 1
505
+ const $tracker = state(0);
506
+ expect(await $state.get($tracker)).toBe(1);
507
+
508
+ // Subsequent accesses should return cached value (1), not 2
509
+ expect(await $state.get($tracker)).toBe(1);
510
+ expect(await $state.get($tracker)).toBe(1);
511
+ expect(counter).toBe(1);
512
+ });
513
+
514
+ it("should handle nested states", async () => {
515
+ const $stateA = stateAsync(Promise.resolve(1));
516
+ const $stateB = stateAsync(Promise.resolve(2));
517
+ const $stateC = stateAsync(
518
+ Promise.resolve({
519
+ a: $stateA,
520
+ b: $stateB,
521
+ }),
522
+ );
523
+
524
+ const value = await $stateC.pick();
525
+ expect(value.a).toBe($stateA);
526
+ expect(value.b).toBe($stateB);
527
+ });
528
+
529
+ it("should handle state with complex object values", async () => {
530
+ const complexObj = {
531
+ nested: { deep: { value: 42 } },
532
+ array: [1, 2, 3],
533
+ };
534
+ const $state = stateAsync(Promise.resolve(complexObj));
535
+ const value = await $state.pick();
536
+ expect(value).toBe(complexObj);
537
+ expect(value.nested.deep.value).toBe(42);
538
+ expect(value.array).toEqual([1, 2, 3]);
539
+ });
540
+ });
541
+
542
+ describe("error handling", () => {
543
+ describe("compute function errors", () => {
544
+ it("should propagate error when pick is called with throwing init", async () => {
545
+ const error = new Error("Init pick error");
546
+ const $state = stateAsync(async () => {
547
+ throw error;
548
+ });
549
+
550
+ await expect($state.pick()).rejects.toThrow("Init pick error");
551
+ });
552
+
553
+ it("should propagate error when get is called with throwing init", async () => {
554
+ const error = new Error("Init get error");
555
+ const $state = stateAsync(async () => {
556
+ throw error;
557
+ });
558
+
559
+ const $tracker = state(0);
560
+ await expect($state.get($tracker)).rejects.toThrow("Init get error");
561
+ });
562
+ });
563
+
564
+ describe("disposed state", () => {
565
+ it("should throw error when pick is called after disposal", async () => {
566
+ const $state = stateAsync(Promise.resolve(1));
567
+
568
+ await $state.pick();
569
+ $state.dispose();
570
+
571
+ await expect($state.pick()).rejects.toThrow(
572
+ "[PicoFlow] Primitive is disposed",
573
+ );
574
+ });
575
+
576
+ it("should throw error when get is called after disposal", async () => {
577
+ const $state = stateAsync(Promise.resolve(1));
578
+ const $tracker = state(0);
579
+
580
+ await $state.get($tracker);
581
+ $state.dispose();
582
+
583
+ await expect($state.get($tracker)).rejects.toThrow(
584
+ "[PicoFlow] Primitive is disposed",
585
+ );
586
+ });
587
+
588
+ it("should throw error when set is called after disposal", async () => {
589
+ const $state = stateAsync(Promise.resolve(1));
590
+
591
+ await $state.pick();
592
+ await $state.set(Promise.resolve(2));
593
+ $state.dispose();
594
+
595
+ await expect($state.set(Promise.resolve(3))).rejects.toThrow(
596
+ "[PicoFlow] Primitive is disposed",
597
+ );
598
+ });
599
+
600
+ it("should throw error when watch is called after disposal", async () => {
601
+ const $state = stateAsync(Promise.resolve(1));
602
+ const $tracker = state(0);
603
+
604
+ await $state.watch($tracker);
605
+ $state.dispose();
606
+
607
+ await expect($state.watch($tracker)).rejects.toThrow(
608
+ "[PicoFlow] Primitive is disposed",
609
+ );
610
+ });
611
+
612
+ it("should throw error when subscribe is called after disposal", () => {
613
+ const $state = stateAsync(Promise.resolve(1));
614
+ const listener = vi.fn();
615
+
616
+ $state.dispose();
617
+
618
+ expect(() => $state.subscribe(listener)).toThrow(
619
+ "[PicoFlow] Primitive is disposed",
620
+ );
621
+ });
622
+
623
+ it("should throw error when trigger is called after disposal", async () => {
624
+ const $state = stateAsync(Promise.resolve(1));
625
+ $state.dispose();
626
+ await expect($state.trigger()).rejects.toThrow(
627
+ "[PicoFlow] Primitive is disposed",
628
+ );
629
+ });
630
+ });
631
+ });
632
+
633
+ describe("FlowNodeAsync computed mode (derivation)", () => {
634
+ it("should compute value lazily", async () => {
635
+ const $source = stateAsync(Promise.resolve(10));
636
+ const computeFn = vi.fn(async (t) => {
637
+ const val = await $source.get(t);
638
+ return val * 2;
639
+ });
640
+
641
+ const $computed = new FlowNodeAsync(computeFn);
642
+ expect(computeFn).not.toHaveBeenCalled();
643
+
644
+ const value = await $computed.pick();
645
+ expect(value).toBe(20);
646
+ expect(computeFn).toHaveBeenCalledTimes(1);
647
+ });
648
+
649
+ it("should mark as dirty when dependency changes", async () => {
650
+ const $source = stateAsync(Promise.resolve(10));
651
+ const $computed = new FlowNodeAsync(async (t) => {
652
+ const val = await $source.get(t);
653
+ return val * 2;
654
+ });
655
+
656
+ await $computed.pick();
657
+ expect(await $computed.pick()).toBe(20);
658
+
659
+ await $source.set(Promise.resolve(15));
660
+ // Should recompute on next access
661
+ expect(await $computed.pick()).toBe(30);
662
+ });
663
+
664
+ it("should not recompute immediately when marked dirty", async () => {
665
+ const $source = stateAsync(Promise.resolve(10));
666
+ const computeFn = vi.fn(async (t) => {
667
+ const val = await $source.get(t);
668
+ return val * 2;
669
+ });
670
+
671
+ const $computed = new FlowNodeAsync(computeFn);
672
+ await $computed.pick();
673
+ expect(computeFn).toHaveBeenCalledTimes(1);
674
+
675
+ await $source.set(Promise.resolve(15));
676
+ // Should not recompute yet
677
+ expect(computeFn).toHaveBeenCalledTimes(1);
678
+
679
+ // Recomputes on next access
680
+ await $computed.pick();
681
+ expect(computeFn).toHaveBeenCalledTimes(2);
682
+ });
683
+
684
+ it("should allow temporary override with set()", async () => {
685
+ const $source = stateAsync(Promise.resolve(10));
686
+ const $computed = new FlowNodeAsync(async (t) => {
687
+ const val = await $source.get(t);
688
+ return val * 2;
689
+ });
690
+
691
+ expect(await $computed.pick()).toBe(20);
692
+
693
+ // Override temporarily
694
+ await $computed.set(Promise.resolve(100));
695
+ expect(await $computed.pick()).toBe(100);
696
+
697
+ // Dependency change clears override
698
+ await $source.set(Promise.resolve(15));
699
+ expect(await $computed.pick()).toBe(30);
700
+ });
701
+
702
+ it("should handle dynamic dependencies", async () => {
703
+ const $source1 = stateAsync(Promise.resolve(10));
704
+ const $source2 = stateAsync(Promise.resolve(20));
705
+ const $condition = stateAsync(Promise.resolve(true));
706
+
707
+ const $computed = new FlowNodeAsync(async (t) => {
708
+ const cond = await $condition.get(t);
709
+ if (cond) {
710
+ return await $source1.get(t);
711
+ }
712
+ return await $source2.get(t);
713
+ });
714
+
715
+ expect(await $computed.pick()).toBe(10);
716
+
717
+ await $condition.set(Promise.resolve(false));
718
+ expect(await $computed.pick()).toBe(20);
719
+
720
+ // Should track source2 now
721
+ await $source2.set(Promise.resolve(25));
722
+ expect(await $computed.pick()).toBe(25);
723
+ });
724
+ });
725
+
726
+ describe("cache and performance", () => {
727
+ it("should cache Promise until value changes", async () => {
728
+ const $state = stateAsync(Promise.resolve(42));
729
+
730
+ const promise1 = $state.pick();
731
+ const promise2 = $state.pick();
732
+ const promise3 = $state.pick();
733
+
734
+ const [value1, value2, value3] = await Promise.all([
735
+ promise1,
736
+ promise2,
737
+ promise3,
738
+ ]);
739
+
740
+ expect(value1).toBe(42);
741
+ expect(value2).toBe(42);
742
+ expect(value3).toBe(42);
743
+ });
744
+
745
+ it("should not recompute if not dirty", async () => {
746
+ const computeFn = vi.fn(() => Promise.resolve(50));
747
+ const $state = stateAsync(computeFn);
748
+
749
+ await $state.pick();
750
+ expect(computeFn).toHaveBeenCalledTimes(1);
751
+
752
+ await $state.pick();
753
+ await $state.pick();
754
+ await $state.pick();
755
+
756
+ expect(computeFn).toHaveBeenCalledTimes(1);
757
+ });
758
+
759
+ it("should handle concurrent access", async () => {
760
+ const $state = stateAsync(Promise.resolve(100));
761
+
762
+ const promises = Array.from({ length: 10 }, () => $state.pick());
763
+ const values = await Promise.all(promises);
764
+
765
+ values.forEach((value) => {
766
+ expect(value).toBe(100);
767
+ });
768
+ });
769
+ });
770
+ });
771
+
772
+ describe("with effect", () => {
773
+ describe("get", () => {
774
+ it("should create reactive dependency when get is used in effect", async () => {
775
+ const $state = stateAsync(Promise.resolve(100));
776
+ const effectFn = vi.fn();
777
+
778
+ effect(async (t) => {
779
+ await $state.get(t);
780
+ effectFn();
781
+ });
782
+
783
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
784
+ });
785
+
786
+ it("should call effect with initial value", async () => {
787
+ const $state = stateAsync(Promise.resolve(1));
788
+ const effectFn = vi.fn();
789
+ effect(async (t) => effectFn(await $state.get(t)));
790
+
791
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
792
+ expect(effectFn).toHaveBeenLastCalledWith(1);
793
+ });
794
+
795
+ it("should call effect with lazy init value", async () => {
796
+ const $state = stateAsync(async () => 1);
797
+ const effectFn = vi.fn();
798
+ effect(async (t) => effectFn(await $state.get(t)));
799
+
800
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
801
+ expect(effectFn).toHaveBeenLastCalledWith(1);
802
+ });
803
+
804
+ it("should call effect when value is updated", async () => {
805
+ const $state = stateAsync(Promise.resolve(1));
806
+ const effectFn = vi.fn();
807
+ effect(async (t) => effectFn(await $state.get(t)));
808
+
809
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
810
+ expect(effectFn).toHaveBeenLastCalledWith(1);
811
+
812
+ await $state.set(Promise.resolve(2));
813
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
814
+ expect(effectFn).toHaveBeenLastCalledWith(2);
815
+ });
816
+
817
+ it("should not call effect when updated with same value", async () => {
818
+ const $state = stateAsync(Promise.resolve(1));
819
+ const effectFn = vi.fn();
820
+ effect(async (t) => effectFn(await $state.get(t)));
821
+
822
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
823
+
824
+ await $state.set(Promise.resolve(2));
825
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
826
+ expect(effectFn).toHaveBeenLastCalledWith(2);
827
+
828
+ await $state.set(Promise.resolve(2));
829
+ await new Promise((resolve) => setTimeout(resolve, 50));
830
+ expect(effectFn).toHaveBeenCalledTimes(2);
831
+ });
832
+
833
+ it("should not call effect after effect is disposed", async () => {
834
+ const $state = stateAsync(Promise.resolve(1));
835
+ const effectFn = vi.fn();
836
+ const $effect = effect(async (t) => effectFn(await $state.get(t)));
837
+
838
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
839
+ expect(effectFn).toHaveBeenLastCalledWith(1);
840
+
841
+ await $state.set(Promise.resolve(2));
842
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
843
+ expect(effectFn).toHaveBeenLastCalledWith(2);
844
+
845
+ $effect.dispose();
846
+
847
+ await $state.set(Promise.resolve(3));
848
+ await new Promise((resolve) => setTimeout(resolve, 50));
849
+ expect(effectFn).toHaveBeenCalledTimes(2);
850
+ });
851
+
852
+ it("should support multiple effects depending on same state", async () => {
853
+ const $state = stateAsync(Promise.resolve(200));
854
+ const effectFn1 = vi.fn();
855
+ const effectFn2 = vi.fn();
856
+ const effectFn3 = vi.fn();
857
+
858
+ effect(async (t) => {
859
+ await $state.get(t);
860
+ effectFn1();
861
+ });
862
+
863
+ effect(async (t) => {
864
+ await $state.get(t);
865
+ effectFn2();
866
+ });
867
+
868
+ effect(async (t) => {
869
+ await $state.get(t);
870
+ effectFn3();
871
+ });
872
+
873
+ await vi.waitFor(() => {
874
+ expect(effectFn1).toHaveBeenCalledTimes(1);
875
+ expect(effectFn2).toHaveBeenCalledTimes(1);
876
+ expect(effectFn3).toHaveBeenCalledTimes(1);
877
+ });
878
+
879
+ await $state.set(Promise.resolve(300));
880
+ await vi.waitFor(() => {
881
+ expect(effectFn1).toHaveBeenCalledTimes(2);
882
+ expect(effectFn2).toHaveBeenCalledTimes(2);
883
+ expect(effectFn3).toHaveBeenCalledTimes(2);
884
+ });
885
+ });
886
+
887
+ it("should support effect with get and untracked get mixed", async () => {
888
+ const $state1 = stateAsync(Promise.resolve(10));
889
+ const $state2 = stateAsync(Promise.resolve(20));
890
+ const effectFn = vi.fn();
891
+
892
+ effect(async (t) => {
893
+ const tracked = await $state1.get(t);
894
+ const untracked = await $state2.get(null);
895
+ effectFn(tracked, untracked);
896
+ });
897
+
898
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
899
+ expect(effectFn).toHaveBeenLastCalledWith(10, 20);
900
+
901
+ // Changing state1 should trigger effect
902
+ await $state1.set(Promise.resolve(11));
903
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
904
+ expect(effectFn).toHaveBeenLastCalledWith(11, 20);
905
+
906
+ // Changing state2 should not trigger effect (untracked)
907
+ await $state2.set(Promise.resolve(21));
908
+ await new Promise((resolve) => setTimeout(resolve, 50));
909
+ expect(effectFn).toHaveBeenCalledTimes(2);
910
+ });
911
+
912
+ it("should support effect depending on multiple states", async () => {
913
+ const $stateA = stateAsync(Promise.resolve(5));
914
+ const $stateB = stateAsync(Promise.resolve(10));
915
+ const effectFn = vi.fn();
916
+
917
+ effect(async (t) => {
918
+ const a = await $stateA.get(t);
919
+ const b = await $stateB.get(t);
920
+ effectFn(a, b);
921
+ });
922
+
923
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
924
+ expect(effectFn).toHaveBeenLastCalledWith(5, 10);
925
+
926
+ await $stateA.set(Promise.resolve(6));
927
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
928
+ expect(effectFn).toHaveBeenLastCalledWith(6, 10);
929
+
930
+ await $stateB.set(Promise.resolve(11));
931
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
932
+ expect(effectFn).toHaveBeenLastCalledWith(6, 11);
933
+ });
934
+ });
935
+
936
+ describe("watch", () => {
937
+ it("should register dependency when watch is used in effect", async () => {
938
+ const $state = stateAsync(Promise.resolve(400));
939
+ const effectFn = vi.fn();
940
+
941
+ effect(async (t) => {
942
+ await $state.watch(t);
943
+ effectFn();
944
+ });
945
+
946
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
947
+ });
948
+
949
+ it("should trigger re-runs when state changes after watch", async () => {
950
+ const $state = stateAsync(Promise.resolve(500));
951
+ const effectFn = vi.fn();
952
+
953
+ effect(async (t) => {
954
+ await $state.watch(t);
955
+ effectFn();
956
+ });
957
+
958
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
959
+
960
+ await $state.set(Promise.resolve(600));
961
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
962
+ });
963
+ });
964
+
965
+ describe("subscribe", () => {
966
+ it("should create effect internally when subscribe is used", async () => {
967
+ const $state = stateAsync(Promise.resolve(700));
968
+ const listener = vi.fn();
969
+
970
+ $state.subscribe(async (value) => listener(await value));
971
+
972
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
973
+ expect(listener).toHaveBeenLastCalledWith(700);
974
+ });
975
+
976
+ it("should support multiple subscriptions with effects", async () => {
977
+ const $state = stateAsync(Promise.resolve(800));
978
+ const listener1 = vi.fn();
979
+ const listener2 = vi.fn();
980
+
981
+ $state.subscribe(async (value) => listener1(await value));
982
+ $state.subscribe(async (value) => listener2(await value));
983
+
984
+ await vi.waitFor(() => {
985
+ expect(listener1).toHaveBeenCalledTimes(1);
986
+ expect(listener2).toHaveBeenCalledTimes(1);
987
+ });
988
+
989
+ await $state.set(Promise.resolve(900));
990
+ await vi.waitFor(() => {
991
+ expect(listener1).toHaveBeenCalledTimes(2);
992
+ expect(listener2).toHaveBeenCalledTimes(2);
993
+ });
994
+ });
995
+
996
+ it("should dispose effect when disposer is called", async () => {
997
+ const $state = stateAsync(Promise.resolve(1000));
998
+ const listener = vi.fn();
999
+
1000
+ const unsubscribe = $state.subscribe(async (value) =>
1001
+ listener(await value),
1002
+ );
1003
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(1));
1004
+
1005
+ await $state.set(Promise.resolve(1100));
1006
+ await vi.waitFor(() => expect(listener).toHaveBeenCalledTimes(2));
1007
+
1008
+ unsubscribe();
1009
+
1010
+ await $state.set(Promise.resolve(1200));
1011
+ await new Promise((resolve) => setTimeout(resolve, 50));
1012
+ expect(listener).toHaveBeenCalledTimes(2);
1013
+ });
1014
+ });
1015
+
1016
+ describe("error handling", () => {
1017
+ describe("compute function errors", () => {
1018
+ it("should propagate error when lazy init throws in effect", async () => {
1019
+ const error = new Error("Effect init error");
1020
+ const $state = stateAsync(async () => {
1021
+ throw error;
1022
+ });
1023
+
1024
+ const $effect = effect(async (t) => {
1025
+ await $state.get(t);
1026
+ });
1027
+
1028
+ await expect($effect.settled).rejects.toThrow("Effect init error");
1029
+ });
1030
+ });
1031
+
1032
+ describe("disposed state", () => {
1033
+ it("should throw error when creating an effect with a disposed state", async () => {
1034
+ const $state = stateAsync(Promise.resolve(1));
1035
+
1036
+ $state.dispose();
1037
+
1038
+ const $effect = effect(async (t) => {
1039
+ await $state.get(t);
1040
+ });
1041
+
1042
+ await expect($effect.settled).rejects.toThrow(
1043
+ "[PicoFlow] Primitive is disposed",
1044
+ );
1045
+ });
1046
+ });
1047
+ });
1048
+
1049
+ describe("disposal", () => {
1050
+ it("should dispose effects when state is disposed", async () => {
1051
+ const $state = stateAsync(Promise.resolve(1300));
1052
+ const effectFn = vi.fn();
1053
+ const $effect = effect(async (t) => {
1054
+ await $state.get(t);
1055
+ effectFn();
1056
+ });
1057
+
1058
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1059
+ expect($effect.disposed).toBe(false);
1060
+
1061
+ $state.dispose();
1062
+
1063
+ expect($effect.disposed).toBe(true);
1064
+ });
1065
+
1066
+ it("should not dispose effects when state is disposed with self option", async () => {
1067
+ const $state = stateAsync(Promise.resolve(1400));
1068
+ const effectFn = vi.fn();
1069
+ const $effect = effect(async (t) => {
1070
+ await $state.get(t);
1071
+ effectFn();
1072
+ });
1073
+
1074
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1075
+ expect($effect.disposed).toBe(false);
1076
+
1077
+ $state.dispose({ self: true });
1078
+
1079
+ expect($effect.disposed).toBe(false);
1080
+ expect($state.disposed).toBe(true);
1081
+ });
1082
+
1083
+ it("should unregister effects when state is disposed with self option", async () => {
1084
+ const $state = stateAsync(Promise.resolve(1500));
1085
+ const effectFn = vi.fn();
1086
+ const $effect = effect(async (t) => {
1087
+ await $state.get(t);
1088
+ effectFn();
1089
+ });
1090
+
1091
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1092
+
1093
+ $state.dispose({ self: true });
1094
+
1095
+ // Effect should still be active but not notified
1096
+ expect($effect.disposed).toBe(false);
1097
+
1098
+ // But state is disposed so operations should fail
1099
+ const $tracker = state(0);
1100
+ await expect($state.get($tracker)).rejects.toThrow(
1101
+ "[PicoFlow] Primitive is disposed",
1102
+ );
1103
+ });
1104
+ });
1105
+
1106
+ describe("complex cases", () => {
1107
+ it("should handle nested states in effects", async () => {
1108
+ const $stateA = stateAsync(Promise.resolve(1));
1109
+ const $stateB = stateAsync(Promise.resolve(2));
1110
+ const $stateC = stateAsync(
1111
+ Promise.resolve({
1112
+ a: $stateA,
1113
+ b: $stateB,
1114
+ }),
1115
+ );
1116
+
1117
+ const effectFn = vi.fn();
1118
+ effect(async (t) => {
1119
+ const a = await (await $stateC.get(t)).a.get(t);
1120
+ effectFn(a);
1121
+ });
1122
+
1123
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1124
+ expect(effectFn).toHaveBeenLastCalledWith(1);
1125
+
1126
+ await $stateA.set(Promise.resolve(3));
1127
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1128
+ expect(effectFn).toHaveBeenLastCalledWith(3);
1129
+ });
1130
+
1131
+ it("should handle effect depending on multiple states", async () => {
1132
+ const $stateA = stateAsync(Promise.resolve(10));
1133
+ const $stateB = stateAsync(Promise.resolve(20));
1134
+ const $stateC = stateAsync(Promise.resolve(30));
1135
+ const effectFn = vi.fn();
1136
+
1137
+ effect(async (t) => {
1138
+ const a = await $stateA.get(t);
1139
+ const b = await $stateB.get(t);
1140
+ const c = await $stateC.get(t);
1141
+ effectFn(a, b, c);
1142
+ });
1143
+
1144
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1145
+ expect(effectFn).toHaveBeenLastCalledWith(10, 20, 30);
1146
+
1147
+ await $stateA.set(Promise.resolve(11));
1148
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1149
+ expect(effectFn).toHaveBeenLastCalledWith(11, 20, 30);
1150
+
1151
+ await $stateB.set(Promise.resolve(21));
1152
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1153
+ expect(effectFn).toHaveBeenLastCalledWith(11, 21, 30);
1154
+ });
1155
+
1156
+ // TODO: Fix this test
1157
+ it.skip("should handle state chaining (state A -> state B -> state C)", async () => {
1158
+ const $stateA = stateAsync(Promise.resolve(100));
1159
+ const $stateB = stateAsync(Promise.resolve(200));
1160
+ const $stateC = stateAsync(Promise.resolve(300));
1161
+ const effectFn = vi.fn();
1162
+
1163
+ // Effect watches A, which triggers B, which triggers C
1164
+ effect(async (t) => {
1165
+ const a = await $stateA.get(t);
1166
+ if (a > 150) {
1167
+ await $stateB.set(Promise.resolve(250));
1168
+ }
1169
+ });
1170
+
1171
+ effect(async (t) => {
1172
+ const b = await $stateB.get(t);
1173
+ if (b > 220) {
1174
+ await $stateC.set(Promise.resolve(350));
1175
+ }
1176
+ });
1177
+
1178
+ effect(async (t) => {
1179
+ const c = await $stateC.get(t);
1180
+ effectFn(c);
1181
+ });
1182
+
1183
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1184
+
1185
+ await $stateA.set(Promise.resolve(200));
1186
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1187
+ });
1188
+ });
1189
+
1190
+ describe("with effects - computed mode", () => {
1191
+ it("should call effect with computed state", async () => {
1192
+ const $source = stateAsync(Promise.resolve(10));
1193
+ const $computed = new FlowNodeAsync(async (t) => {
1194
+ const val = await $source.get(t);
1195
+ return val * 2;
1196
+ });
1197
+
1198
+ const effectFn = vi.fn();
1199
+ effect(async (t) => effectFn(await $computed.get(t)));
1200
+
1201
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1202
+ expect(effectFn).toHaveBeenLastCalledWith(20);
1203
+ });
1204
+
1205
+ it("should re-execute effect when computed dependency changes", async () => {
1206
+ const $source = stateAsync(Promise.resolve(10));
1207
+ const $computed = new FlowNodeAsync(async (t) => {
1208
+ const val = await $source.get(t);
1209
+ return val * 2;
1210
+ });
1211
+
1212
+ const effectFn = vi.fn();
1213
+ effect(async (t) => effectFn(await $computed.get(t)));
1214
+
1215
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1216
+ expect(effectFn).toHaveBeenLastCalledWith(20);
1217
+
1218
+ await $source.set(Promise.resolve(15));
1219
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1220
+ expect(effectFn).toHaveBeenLastCalledWith(30);
1221
+ });
1222
+
1223
+ it("should handle dynamic dependencies in computed state", async () => {
1224
+ const $source1 = stateAsync(Promise.resolve(10));
1225
+ const $source2 = stateAsync(Promise.resolve(20));
1226
+ const $condition = stateAsync(Promise.resolve(true));
1227
+
1228
+ const $computed = new FlowNodeAsync(async (t) => {
1229
+ const cond = await $condition.get(t);
1230
+ if (cond) {
1231
+ return await $source1.get(t);
1232
+ }
1233
+ return await $source2.get(t);
1234
+ });
1235
+
1236
+ const effectFn = vi.fn();
1237
+ effect(async (t) => effectFn(await $computed.get(t)));
1238
+
1239
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1240
+ expect(effectFn).toHaveBeenLastCalledWith(10);
1241
+
1242
+ await $condition.set(Promise.resolve(false));
1243
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1244
+ expect(effectFn).toHaveBeenLastCalledWith(20);
1245
+
1246
+ await $source2.set(Promise.resolve(25));
1247
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1248
+ expect(effectFn).toHaveBeenLastCalledWith(25);
1249
+ });
1250
+
1251
+ it("should handle temporary override in computed state", async () => {
1252
+ const $source = stateAsync(Promise.resolve(10));
1253
+ const $computed = new FlowNodeAsync(async (t) => {
1254
+ const val = await $source.get(t);
1255
+ return val * 2;
1256
+ });
1257
+
1258
+ const effectFn = vi.fn();
1259
+ effect(async (t) => effectFn(await $computed.get(t)));
1260
+
1261
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1262
+ expect(effectFn).toHaveBeenLastCalledWith(20);
1263
+
1264
+ // Override temporarily
1265
+ await $computed.set(Promise.resolve(100));
1266
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1267
+ expect(effectFn).toHaveBeenLastCalledWith(100);
1268
+
1269
+ // Dependency change clears override
1270
+ await $source.set(Promise.resolve(15));
1271
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1272
+ expect(effectFn).toHaveBeenLastCalledWith(30);
1273
+ });
1274
+ });
1275
+
1276
+ describe("with effects - errors", () => {
1277
+ it("should handle Promise rejection in effect", async () => {
1278
+ const $state = stateAsync(Promise.reject(new Error("State error")));
1279
+ const effectFn = vi.fn();
1280
+
1281
+ effect(async (t) => {
1282
+ try {
1283
+ await $state.get(t);
1284
+ } catch (error) {
1285
+ effectFn(error);
1286
+ }
1287
+ });
1288
+
1289
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1290
+ expect(effectFn).toHaveBeenLastCalledWith(
1291
+ expect.objectContaining({ message: "State error" }),
1292
+ );
1293
+ });
1294
+
1295
+ it("should handle Promise rejection during set in effect", async () => {
1296
+ const $state = stateAsync(Promise.resolve(1));
1297
+ const effectFn = vi.fn();
1298
+
1299
+ effect(async (t) => {
1300
+ const val = await $state.get(t);
1301
+ effectFn(val);
1302
+ });
1303
+
1304
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1305
+
1306
+ await expect(
1307
+ $state.set(Promise.reject(new Error("Set error"))),
1308
+ ).rejects.toThrow("Set error");
1309
+ });
1310
+ });
1311
+
1312
+ describe("with effects - execution order", () => {
1313
+ it("should execute multiple effects in order", async () => {
1314
+ const $state = stateAsync(Promise.resolve(1));
1315
+ const executionOrder: string[] = [];
1316
+
1317
+ effect(async (t) => {
1318
+ const val = await $state.get(t);
1319
+ executionOrder.push(`effect1-${val}`);
1320
+ });
1321
+
1322
+ effect(async (t) => {
1323
+ const val = await $state.get(t);
1324
+ executionOrder.push(`effect2-${val}`);
1325
+ });
1326
+
1327
+ effect(async (t) => {
1328
+ const val = await $state.get(t);
1329
+ executionOrder.push(`effect3-${val}`);
1330
+ });
1331
+
1332
+ await vi.waitFor(() => expect(executionOrder.length).toBe(3));
1333
+ expect(executionOrder).toContain("effect1-1");
1334
+ expect(executionOrder).toContain("effect2-1");
1335
+ expect(executionOrder).toContain("effect3-1");
1336
+ });
1337
+
1338
+ it("should handle async set() in effects", async () => {
1339
+ const $state = stateAsync(Promise.resolve(1));
1340
+ const executionOrder: string[] = [];
1341
+
1342
+ effect(async (t) => {
1343
+ const val = await $state.get(t);
1344
+ executionOrder.push(`effect-${val}`);
1345
+ });
1346
+
1347
+ await vi.waitFor(() => expect(executionOrder.length).toBe(1));
1348
+
1349
+ executionOrder.push("before-set");
1350
+ await $state.set(
1351
+ (async () => {
1352
+ await new Promise((resolve) => setTimeout(resolve, 10));
1353
+ return 2;
1354
+ })(),
1355
+ );
1356
+ executionOrder.push("after-set");
1357
+
1358
+ await vi.waitFor(() =>
1359
+ expect(executionOrder.length).toBeGreaterThan(2),
1360
+ );
1361
+ expect(executionOrder).toContain("effect-2");
1362
+ });
1363
+
1364
+ it("should handle computed state execution order", async () => {
1365
+ const $source = stateAsync(Promise.resolve(10));
1366
+ const $computed = new FlowNodeAsync(async (t) => {
1367
+ const val = await $source.get(t);
1368
+ return val * 2;
1369
+ });
1370
+
1371
+ const executionOrder: string[] = [];
1372
+
1373
+ effect(async (t) => {
1374
+ const val = await $computed.get(t);
1375
+ executionOrder.push(`effect-${val}`);
1376
+ });
1377
+
1378
+ await vi.waitFor(() => expect(executionOrder.length).toBe(1));
1379
+ expect(executionOrder).toContain("effect-20");
1380
+
1381
+ await $source.set(Promise.resolve(15));
1382
+ await vi.waitFor(() => expect(executionOrder.length).toBe(2));
1383
+ expect(executionOrder).toContain("effect-30");
1384
+ });
1385
+ });
1386
+ });
1387
+ });