@ersbeth/picoflow 2.0.1 → 2.0.3

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 (154) hide show
  1. package/README.md +10 -0
  2. package/SKILL.md +106 -0
  3. package/dist/types/api/base/flowSubscribable.d.ts +3 -3
  4. package/dist/types/api/base/flowSubscribable.d.ts.map +1 -1
  5. package/package.json +5 -1
  6. package/.gitlab-ci.yml +0 -24
  7. package/.vscode/settings.json +0 -5
  8. package/CHANGELOG.md +0 -94
  9. package/biome.json +0 -47
  10. package/docs/.vitepress/config.mts +0 -145
  11. package/docs/api/functions/array.md +0 -35
  12. package/docs/api/functions/constant.md +0 -33
  13. package/docs/api/functions/constantAsync.md +0 -69
  14. package/docs/api/functions/derivation.md +0 -34
  15. package/docs/api/functions/derivationAsync.md +0 -34
  16. package/docs/api/functions/from.md +0 -129
  17. package/docs/api/functions/isDisposable.md +0 -27
  18. package/docs/api/functions/map.md +0 -36
  19. package/docs/api/functions/signal.md +0 -21
  20. package/docs/api/functions/state.md +0 -67
  21. package/docs/api/functions/stateAsync.md +0 -69
  22. package/docs/api/functions/subscribe.md +0 -40
  23. package/docs/api/functions/writableDerivation.md +0 -33
  24. package/docs/api/functions/writableDerivationAsync.md +0 -34
  25. package/docs/api/index.md +0 -61
  26. package/docs/api/interfaces/FlowArray.md +0 -439
  27. package/docs/api/interfaces/FlowConstant.md +0 -220
  28. package/docs/api/interfaces/FlowConstantAsync.md +0 -221
  29. package/docs/api/interfaces/FlowDerivation.md +0 -241
  30. package/docs/api/interfaces/FlowDerivationAsync.md +0 -242
  31. package/docs/api/interfaces/FlowDisposable.md +0 -59
  32. package/docs/api/interfaces/FlowEffect.md +0 -64
  33. package/docs/api/interfaces/FlowMap.md +0 -374
  34. package/docs/api/interfaces/FlowObservable.md +0 -155
  35. package/docs/api/interfaces/FlowSignal.md +0 -156
  36. package/docs/api/interfaces/FlowState.md +0 -269
  37. package/docs/api/interfaces/FlowStateAsync.md +0 -268
  38. package/docs/api/interfaces/FlowSubscribable.md +0 -55
  39. package/docs/api/interfaces/FlowTracker.md +0 -61
  40. package/docs/api/interfaces/FlowValue.md +0 -222
  41. package/docs/api/interfaces/FlowWritableDerivation.md +0 -292
  42. package/docs/api/interfaces/FlowWritableDerivationAsync.md +0 -293
  43. package/docs/api/type-aliases/DerivationFunction.md +0 -28
  44. package/docs/api/type-aliases/DerivationFunctionAsync.md +0 -28
  45. package/docs/api/type-aliases/FlowArrayAction.md +0 -60
  46. package/docs/api/type-aliases/FlowDataTracker.md +0 -33
  47. package/docs/api/type-aliases/FlowMapAction.md +0 -48
  48. package/docs/api/type-aliases/FlowOnDataListener.md +0 -33
  49. package/docs/api/type-aliases/FlowOnErrorListener.md +0 -27
  50. package/docs/api/type-aliases/FlowOnPendingListener.md +0 -21
  51. package/docs/api/type-aliases/FlowReadonly.md +0 -22
  52. package/docs/api/type-aliases/InitFunction.md +0 -21
  53. package/docs/api/type-aliases/InitFunctionAsync.md +0 -21
  54. package/docs/api/type-aliases/NotPromise.md +0 -21
  55. package/docs/api/type-aliases/UpdateFunction.md +0 -27
  56. package/docs/api/type-aliases/UpdateFunctionAsync.md +0 -27
  57. package/docs/api/typedoc-sidebar.json +0 -65
  58. package/docs/examples/examples.md +0 -2311
  59. package/docs/examples/patterns.md +0 -649
  60. package/docs/guide/advanced/architecture.md +0 -1234
  61. package/docs/guide/advanced/disposal.md +0 -426
  62. package/docs/guide/advanced/migration-v1.md +0 -464
  63. package/docs/guide/advanced/migration-v2.md +0 -204
  64. package/docs/guide/advanced/solidjs.md +0 -135
  65. package/docs/guide/introduction/concepts.md +0 -57
  66. package/docs/guide/introduction/conventions.md +0 -30
  67. package/docs/guide/introduction/getting-started.md +0 -139
  68. package/docs/guide/introduction/lifecycle.md +0 -368
  69. package/docs/guide/primitives/array.md +0 -286
  70. package/docs/guide/primitives/constant.md +0 -207
  71. package/docs/guide/primitives/derivations.md +0 -281
  72. package/docs/guide/primitives/effects.md +0 -372
  73. package/docs/guide/primitives/map.md +0 -265
  74. package/docs/guide/primitives/overview.md +0 -92
  75. package/docs/guide/primitives/signal.md +0 -222
  76. package/docs/guide/primitives/state.md +0 -272
  77. package/docs/index.md +0 -47
  78. package/docs/public/logo.svg +0 -1
  79. package/src/api/base/flowDisposable.ts +0 -44
  80. package/src/api/base/flowObservable.ts +0 -28
  81. package/src/api/base/flowSubscribable.ts +0 -87
  82. package/src/api/base/flowTracker.ts +0 -7
  83. package/src/api/base/index.ts +0 -4
  84. package/src/api/index.ts +0 -2
  85. package/src/api/nodes/async/flowConstantAsync.ts +0 -36
  86. package/src/api/nodes/async/flowDerivationAsync.ts +0 -42
  87. package/src/api/nodes/async/flowStateAsync.ts +0 -47
  88. package/src/api/nodes/async/flowWritableDerivationAsync.ts +0 -33
  89. package/src/api/nodes/async/index.ts +0 -4
  90. package/src/api/nodes/collections/flowArray.ts +0 -155
  91. package/src/api/nodes/collections/flowMap.ts +0 -115
  92. package/src/api/nodes/collections/index.ts +0 -2
  93. package/src/api/nodes/flowEffect.ts +0 -42
  94. package/src/api/nodes/flowSignal.ts +0 -28
  95. package/src/api/nodes/flowValue.ts +0 -37
  96. package/src/api/nodes/index.ts +0 -7
  97. package/src/api/nodes/sync/flowConstant.ts +0 -33
  98. package/src/api/nodes/sync/flowDerivation.ts +0 -41
  99. package/src/api/nodes/sync/flowState.ts +0 -45
  100. package/src/api/nodes/sync/flowWritableDerivation.ts +0 -31
  101. package/src/api/nodes/sync/index.ts +0 -4
  102. package/src/api/nodes/utils.ts +0 -24
  103. package/src/base/disposable.ts +0 -18
  104. package/src/base/executionStack.ts +0 -42
  105. package/src/base/index.ts +0 -5
  106. package/src/base/node.ts +0 -98
  107. package/src/base/observable.ts +0 -92
  108. package/src/base/observer.ts +0 -51
  109. package/src/converters/index.ts +0 -1
  110. package/src/converters/solid.ts +0 -109
  111. package/src/index.ts +0 -2
  112. package/src/nodes/arrayNode.ts +0 -180
  113. package/src/nodes/effectNode.ts +0 -58
  114. package/src/nodes/index.ts +0 -7
  115. package/src/nodes/mapNode.ts +0 -125
  116. package/src/nodes/signalNode.ts +0 -19
  117. package/src/nodes/valueAsyncNode.ts +0 -85
  118. package/src/nodes/valueNode.ts +0 -148
  119. package/src/nodes/valueSyncNode.ts +0 -125
  120. package/src/schedulers/asyncResolver.ts +0 -78
  121. package/src/schedulers/asyncScheduler.ts +0 -66
  122. package/src/schedulers/index.ts +0 -4
  123. package/src/schedulers/pendingError.ts +0 -13
  124. package/src/schedulers/scheduler.ts +0 -9
  125. package/src/schedulers/syncResolver.ts +0 -69
  126. package/src/schedulers/syncScheduler.ts +0 -55
  127. package/test/base/pendingError.test.ts +0 -67
  128. package/test/converters/solid.derivation.browser.test.tsx +0 -69
  129. package/test/converters/solid.node.test.ts +0 -654
  130. package/test/converters/solid.state.browser.test.tsx +0 -1592
  131. package/test/reactivity/flowSignal.test.ts +0 -226
  132. package/test/reactivity/nodes/async/asyncScheduler/asyncResolver.test.ts +0 -593
  133. package/test/reactivity/nodes/async/asyncScheduler/asyncScheduler.test.ts +0 -317
  134. package/test/reactivity/nodes/async/flowConstantAsync.test.ts +0 -652
  135. package/test/reactivity/nodes/async/flowDerivation.test.ts +0 -898
  136. package/test/reactivity/nodes/async/flowDerivationAsync.test.ts +0 -1716
  137. package/test/reactivity/nodes/async/flowStateAsync.test.ts +0 -708
  138. package/test/reactivity/nodes/async/flowWritableDerivationAsync.test.ts +0 -614
  139. package/test/reactivity/nodes/collections/flowArray.asyncStates.test.ts +0 -1289
  140. package/test/reactivity/nodes/collections/flowArray.scalars.test.ts +0 -961
  141. package/test/reactivity/nodes/collections/flowArray.states.test.ts +0 -1035
  142. package/test/reactivity/nodes/collections/flowMap.asyncStates.test.ts +0 -960
  143. package/test/reactivity/nodes/collections/flowMap.scalars.test.ts +0 -775
  144. package/test/reactivity/nodes/collections/flowMap.states.test.ts +0 -958
  145. package/test/reactivity/nodes/sync/flowConstant.test.ts +0 -377
  146. package/test/reactivity/nodes/sync/flowDerivation.test.ts +0 -896
  147. package/test/reactivity/nodes/sync/flowState.test.ts +0 -341
  148. package/test/reactivity/nodes/sync/flowWritableDerivation.test.ts +0 -603
  149. package/test/vitest.d.ts +0 -10
  150. package/tsconfig.json +0 -37
  151. package/typedoc.json +0 -37
  152. package/vite.config.ts +0 -31
  153. package/vitest.browser.config.ts +0 -21
  154. package/vitest.config.ts +0 -17
@@ -1,708 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import { stateAsync, subscribe } from "~";
3
- import { EffectNode } from "~/nodes";
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
- expect(initFn).not.toHaveBeenCalled();
18
-
19
- const value = await $state.pick();
20
- expect(value).toBe(42);
21
- expect(initFn).toHaveBeenCalledTimes(1);
22
- });
23
- });
24
-
25
- describe("set", () => {
26
- it("should update value with Promise", async () => {
27
- const $state = stateAsync(Promise.resolve(1));
28
- expect(await $state.pick()).toBe(1);
29
-
30
- $state.set(Promise.resolve(2));
31
- expect(await $state.pick()).toBe(2);
32
- });
33
-
34
- it("should update value with updater function", async () => {
35
- const $state = stateAsync(Promise.resolve(1));
36
- expect(await $state.pick()).toBe(1);
37
-
38
- $state.set(async (current) => (current !== undefined ? current + 1 : 0));
39
- expect(await $state.pick()).toBe(2);
40
- });
41
-
42
- it("should return void", () => {
43
- const $state = stateAsync(Promise.resolve(1));
44
- const result = $state.set(Promise.resolve(2));
45
- expect(result).toBeUndefined();
46
- });
47
-
48
- it("should notify when set with same value", async () => {
49
- const $state = stateAsync(Promise.resolve(1));
50
- const onData = vi.fn();
51
- $state.subscribe(onData);
52
-
53
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
54
- expect(onData).toHaveBeenLastCalledWith(1);
55
-
56
- $state.set(Promise.resolve(1));
57
-
58
- await new Promise((resolve) => setTimeout(resolve, 50));
59
- expect(onData).toHaveBeenCalledTimes(3);
60
- });
61
-
62
- it("should notify when set with different value", async () => {
63
- const $state = stateAsync(Promise.resolve(1));
64
- const onData = vi.fn();
65
- $state.subscribe(onData);
66
-
67
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
68
- expect(onData).toHaveBeenLastCalledWith(1);
69
-
70
- $state.set(Promise.resolve(2));
71
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
72
- expect(onData).toHaveBeenLastCalledWith(2);
73
- });
74
-
75
- it("should notify when updater returns same value", async () => {
76
- const $state = stateAsync(Promise.resolve(5));
77
- const onData = vi.fn();
78
- $state.subscribe(onData);
79
-
80
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
81
-
82
- $state.set(async (current) => current ?? 0);
83
-
84
- await new Promise((resolve) => setTimeout(resolve, 50));
85
- expect(onData).toHaveBeenCalledTimes(3);
86
- });
87
-
88
- it("should notify when updater returns different value", async () => {
89
- const $state = stateAsync(Promise.resolve(5));
90
- const onData = vi.fn();
91
- $state.subscribe(onData);
92
-
93
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
94
- expect(onData).toHaveBeenLastCalledWith(5);
95
-
96
- $state.set(async (current) => (current ? current + 1 : 0));
97
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
98
- expect(onData).toHaveBeenLastCalledWith(6);
99
- });
100
-
101
- it("should handle async updater function", async () => {
102
- const $state = stateAsync(Promise.resolve(1));
103
- expect(await $state.pick()).toBe(1);
104
- $state.set(async (current) => {
105
- await new Promise((resolve) => setTimeout(resolve, 10));
106
- return (current ?? 0) + 10;
107
- });
108
-
109
- expect(await $state.pick()).toBe(11);
110
- });
111
-
112
- it("should handle Promise rejection in set", async () => {
113
- const $state = stateAsync(Promise.resolve(1));
114
- const error = new Error("Set error");
115
- $state.set(Promise.reject(error));
116
- await expect($state.pick()).rejects.toThrow(error);
117
- });
118
-
119
- it("should throw error when set is called after disposal", async () => {
120
- const $state = stateAsync(Promise.resolve(1));
121
-
122
- expect(await $state.pick()).toBe(1);
123
- $state.set(Promise.resolve(2));
124
- expect(await $state.pick()).toBe(2);
125
-
126
- $state.dispose();
127
-
128
- expect(() => $state.set(Promise.resolve(3))).toThrow("[PicoFlow] Primitive is disposed");
129
- });
130
- });
131
-
132
- describe("subscribe", () => {
133
- it("should return an effect", () => {
134
- const $state = stateAsync(Promise.resolve(50));
135
- const onData = vi.fn();
136
- const effect = $state.subscribe(onData);
137
-
138
- expect(effect).toBeInstanceOf(EffectNode);
139
- });
140
-
141
- it("should call onData immediately with current value", async () => {
142
- const $state = stateAsync(Promise.resolve(55));
143
- const onData = vi.fn();
144
-
145
- $state.subscribe(onData);
146
-
147
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
148
- expect(onData).toHaveBeenLastCalledWith(55);
149
- });
150
-
151
- it("should call onData with lazy init value", async () => {
152
- const $state = stateAsync(async () => 60);
153
- const onData = vi.fn();
154
-
155
- $state.subscribe(onData);
156
-
157
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
158
- expect(onData).toHaveBeenLastCalledWith(60);
159
- });
160
-
161
- it("should call onData when value is updated via set()", async () => {
162
- const $state = stateAsync(Promise.resolve(65));
163
- const onData = vi.fn();
164
-
165
- $state.subscribe(onData);
166
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
167
-
168
- $state.set(Promise.resolve(70));
169
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
170
- expect(onData).toHaveBeenLastCalledWith(70);
171
- });
172
-
173
- // it("should not call onData when set() with same value", async () => {
174
- // const $state = stateAsync(Promise.resolve(75));
175
- // const onData = vi.fn();
176
-
177
- // $state.subscribe(onData);
178
- // await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
179
-
180
- // $state.set(Promise.resolve(80));
181
- // await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
182
-
183
- // $state.set(Promise.resolve(80));
184
- // await new Promise((resolve) => setTimeout(resolve, 50));
185
- // expect(onData).toHaveBeenCalledTimes(2);
186
- // });
187
-
188
- it("should not call onData after disposer is called", async () => {
189
- const $state = stateAsync(Promise.resolve(85));
190
- const onData = vi.fn();
191
-
192
- const effect = $state.subscribe(onData);
193
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
194
-
195
- effect.dispose();
196
-
197
- $state.set(Promise.resolve(90));
198
- await new Promise((resolve) => setTimeout(resolve, 50));
199
- expect(onData).toHaveBeenCalledTimes(1);
200
- });
201
-
202
- it("should support multiple subscriptions", async () => {
203
- const $state = stateAsync(Promise.resolve(95));
204
- const onData1 = vi.fn();
205
- const onData2 = vi.fn();
206
- const onData3 = vi.fn();
207
-
208
- $state.subscribe(onData1);
209
- $state.subscribe(onData2);
210
- $state.subscribe(onData3);
211
-
212
- await vi.waitFor(() => {
213
- expect(onData1).toHaveBeenCalledTimes(1);
214
- expect(onData2).toHaveBeenCalledTimes(1);
215
- expect(onData3).toHaveBeenCalledTimes(1);
216
- });
217
-
218
- expect(onData1).toHaveBeenLastCalledWith(95);
219
- expect(onData2).toHaveBeenLastCalledWith(95);
220
- expect(onData3).toHaveBeenLastCalledWith(95);
221
- });
222
-
223
- it("should support multiple subscriptions with notifications via set()", async () => {
224
- const $state = stateAsync(Promise.resolve(100));
225
- const onData1 = vi.fn();
226
- const onData2 = vi.fn();
227
-
228
- $state.subscribe(onData1);
229
- $state.subscribe(onData2);
230
-
231
- await vi.waitFor(() => {
232
- expect(onData1).toHaveBeenCalledTimes(1);
233
- expect(onData2).toHaveBeenCalledTimes(1);
234
- });
235
-
236
- $state.set(Promise.resolve(200));
237
- await vi.waitFor(() => {
238
- expect(onData1).toHaveBeenCalledTimes(3);
239
- expect(onData2).toHaveBeenCalledTimes(3);
240
- });
241
-
242
- expect(onData1).toHaveBeenLastCalledWith(200);
243
- expect(onData2).toHaveBeenLastCalledWith(200);
244
- });
245
-
246
- it("should throw error when subscribe is called after disposal", () => {
247
- const $state = stateAsync(Promise.resolve(1));
248
- const onData = vi.fn();
249
-
250
- $state.dispose();
251
-
252
- expect(() => $state.subscribe(onData)).toThrow("[PicoFlow] Primitive is disposed");
253
- });
254
- });
255
-
256
- describe("trigger", () => {
257
- it("should return void", () => {
258
- const $state = stateAsync(Promise.resolve(1));
259
- const result = $state.trigger();
260
- expect(result).toBeUndefined();
261
- });
262
-
263
- it("should allow multiple triggers", async () => {
264
- const $state = stateAsync(Promise.resolve(1));
265
- $state.trigger();
266
- $state.trigger();
267
- $state.trigger();
268
-
269
- // Should not throw
270
- expect(await $state.pick()).toBe(1);
271
- });
272
- });
273
-
274
- describe("nesting", () => {
275
- it("should handle nested states with set()", async () => {
276
- const $stateA = stateAsync(Promise.resolve(1));
277
- const $stateB = stateAsync(Promise.resolve(2));
278
- const $stateC = stateAsync(
279
- Promise.resolve({
280
- a: $stateA,
281
- b: $stateB,
282
- }),
283
- );
284
-
285
- const value = await $stateC.pick();
286
- expect(value.a).toBe($stateA);
287
- expect(value.b).toBe($stateB);
288
-
289
- $stateA.set(Promise.resolve(3));
290
- expect(await $stateA.pick()).toBe(3);
291
- });
292
-
293
- it("should handle state with complex object values and set()", async () => {
294
- const complexObj = {
295
- nested: { deep: { value: 42 } },
296
- array: [1, 2, 3],
297
- };
298
- const $state = stateAsync(Promise.resolve(complexObj));
299
- const value = await $state.pick();
300
- expect(value).toBe(complexObj);
301
- expect(value.nested.deep.value).toBe(42);
302
- expect(value.array).toEqual([1, 2, 3]);
303
-
304
- const newObj = {
305
- nested: { deep: { value: 100 } },
306
- array: [4, 5, 6],
307
- };
308
- $state.set(Promise.resolve(newObj));
309
- const newValue = await $state.pick();
310
- expect(newValue.nested.deep.value).toBe(100);
311
- expect(newValue.array).toEqual([4, 5, 6]);
312
- });
313
- });
314
-
315
- describe("error handling", () => {
316
- describe("disposed state", () => {
317
- it("should throw error when set is called after disposal", async () => {
318
- const $state = stateAsync(Promise.resolve(1));
319
-
320
- await $state.pick();
321
- $state.set(Promise.resolve(2));
322
- $state.dispose();
323
-
324
- expect(() => $state.set(Promise.resolve(3))).toThrow("[PicoFlow] Primitive is disposed");
325
- });
326
-
327
- it("should throw error when trigger is called after disposal", () => {
328
- const $state = stateAsync(Promise.resolve(1));
329
- $state.dispose();
330
- expect(() => $state.trigger()).toThrow("[PicoFlow] Primitive is disposed");
331
- });
332
- });
333
- });
334
- });
335
-
336
- describe("with effect", () => {
337
- describe("get", () => {
338
- it("should create reactive dependency when get is used in effect", async () => {
339
- const $state = stateAsync(Promise.resolve(100));
340
- const onData = vi.fn();
341
- const onError = vi.fn();
342
-
343
- subscribe((t) => $state.get(t), onData, onError);
344
-
345
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
346
- await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(0));
347
- expect(onData).toHaveBeenLastCalledWith(100);
348
- });
349
-
350
- it("should call effect with initial value", async () => {
351
- const $state = stateAsync(Promise.resolve(1));
352
- const onData = vi.fn();
353
- const onError = vi.fn();
354
-
355
- subscribe((t) => $state.get(t), onData, onError);
356
-
357
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
358
- await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(0));
359
- expect(onData).toHaveBeenLastCalledWith(1);
360
- });
361
-
362
- it("should call effect with lazy init value", async () => {
363
- const $state = stateAsync(async () => 1);
364
- const onData = vi.fn();
365
- const onError = vi.fn();
366
-
367
- subscribe((t) => $state.get(t), onData, onError);
368
-
369
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
370
- await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(0));
371
- expect(onData).toHaveBeenLastCalledWith(1);
372
- });
373
-
374
- it("should call effect when value is updated via set()", async () => {
375
- const $state = stateAsync(Promise.resolve(1));
376
- const onData = vi.fn();
377
- const onError = vi.fn();
378
-
379
- subscribe((t) => $state.get(t), onData, onError);
380
-
381
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
382
- expect(onData).toHaveBeenLastCalledWith(1);
383
-
384
- $state.set(Promise.resolve(2));
385
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
386
- expect(onData).toHaveBeenLastCalledWith(2);
387
- });
388
-
389
- it("should not call effect when set() with same value", async () => {
390
- const $state = stateAsync(Promise.resolve(1));
391
- const onData = vi.fn();
392
- const onError = vi.fn();
393
-
394
- subscribe((t) => $state.get(t), onData, onError);
395
-
396
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
397
-
398
- $state.set(Promise.resolve(2));
399
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
400
- expect(onData).toHaveBeenLastCalledWith(2);
401
-
402
- $state.set(Promise.resolve(2));
403
- await new Promise((resolve) => setTimeout(resolve, 50));
404
- expect(onData).toHaveBeenCalledTimes(5);
405
- expect(onData).toHaveBeenLastCalledWith(2);
406
- expect(onError).toHaveBeenCalledTimes(0);
407
- });
408
-
409
- it("should not call effect after effect is disposed", async () => {
410
- const $state = stateAsync(Promise.resolve(1));
411
- const onData = vi.fn();
412
- const onError = vi.fn();
413
- const $effect = subscribe((t) => $state.get(t), onData, onError);
414
-
415
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
416
- expect(onData).toHaveBeenLastCalledWith(1);
417
-
418
- $state.set(Promise.resolve(2));
419
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
420
- expect(onData).toHaveBeenLastCalledWith(2);
421
-
422
- $effect.dispose();
423
-
424
- $state.set(Promise.resolve(3));
425
- await new Promise((resolve) => setTimeout(resolve, 50));
426
- expect(onData).toHaveBeenCalledTimes(3);
427
- });
428
-
429
- it("should support effect depending on multiple states", async () => {
430
- const $stateA = stateAsync(Promise.resolve(5));
431
- const $stateB = stateAsync(Promise.resolve(10));
432
- const onData = vi.fn();
433
- const onError = vi.fn();
434
-
435
- subscribe(
436
- (t) => ({ a: $stateA.get(t), b: $stateB.get(t) }),
437
- (data) => onData(data.a, data.b),
438
- onError,
439
- );
440
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
441
- expect(onData).toHaveBeenLastCalledWith(5, 10);
442
-
443
- $stateA.set(Promise.resolve(6));
444
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
445
- expect(onData).toHaveBeenLastCalledWith(6, 10);
446
-
447
- $stateB.set(Promise.resolve(11));
448
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(5));
449
- expect(onData).toHaveBeenLastCalledWith(6, 11);
450
- });
451
-
452
- it("should support multiple effects depending on same state", async () => {
453
- const $state = stateAsync(Promise.resolve(200));
454
- const onData1 = vi.fn();
455
- const onData2 = vi.fn();
456
- const onData3 = vi.fn();
457
-
458
- const onError1 = vi.fn();
459
- const onError2 = vi.fn();
460
- const onError3 = vi.fn();
461
-
462
- subscribe((t) => $state.get(t), onData1, onError1);
463
- subscribe((t) => $state.get(t), onData2, onError2);
464
- subscribe((t) => $state.get(t), onData3, onError3);
465
-
466
- await vi.waitFor(() => {
467
- expect(onData1).toHaveBeenCalledTimes(1);
468
- expect(onData2).toHaveBeenCalledTimes(1);
469
- expect(onData3).toHaveBeenCalledTimes(1);
470
- });
471
-
472
- expect(onError1).toHaveBeenCalledTimes(0);
473
- expect(onError2).toHaveBeenCalledTimes(0);
474
- expect(onError3).toHaveBeenCalledTimes(0);
475
-
476
- expect(onData1).toHaveBeenLastCalledWith(200);
477
- expect(onData2).toHaveBeenLastCalledWith(200);
478
- expect(onData3).toHaveBeenLastCalledWith(200);
479
-
480
- $state.set(Promise.resolve(300));
481
- await vi.waitFor(() => {
482
- expect(onData1).toHaveBeenCalledTimes(3);
483
- expect(onData2).toHaveBeenCalledTimes(3);
484
- expect(onData3).toHaveBeenCalledTimes(3);
485
- });
486
- });
487
- });
488
-
489
- describe("watch", () => {
490
- it("should register dependency when watch is used in effect", async () => {
491
- const $state = stateAsync(Promise.resolve(400));
492
- const onData = vi.fn();
493
- const onError = vi.fn();
494
-
495
- subscribe((t) => $state.watch(t), onData, onError);
496
-
497
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
498
- });
499
-
500
- it("should trigger re-runs when state changes via set() after watch", async () => {
501
- const $state = stateAsync(Promise.resolve(500));
502
- const onData = vi.fn();
503
- const onError = vi.fn();
504
-
505
- subscribe((t) => $state.watch(t), onData, onError);
506
-
507
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
508
-
509
- $state.set(Promise.resolve(600));
510
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
511
- });
512
- });
513
-
514
- describe("error handling", () => {
515
- describe("compute function errors", () => {
516
- it("should propagate error when lazy init throws in effect", async () => {
517
- const error = new Error("Effect init error");
518
- const $state = stateAsync(async () => {
519
- await Promise.resolve();
520
- throw error;
521
- });
522
- const onData = vi.fn();
523
- const onError = vi.fn();
524
-
525
- subscribe((t) => $state.get(t), onData, onError);
526
-
527
- await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(1));
528
- expect(onData).toHaveBeenCalledTimes(0);
529
- expect(onError).toHaveBeenLastCalledWith(error);
530
- });
531
- });
532
-
533
- describe("disposed state", () => {
534
- it("should throw error when creating an effect with a disposed state", async () => {
535
- const $state = stateAsync(Promise.resolve(1));
536
- const onData = vi.fn();
537
- const onError = vi.fn();
538
-
539
- $state.dispose();
540
-
541
- subscribe((t) => $state.get(t), onData, onError);
542
-
543
- await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(1));
544
- expect(onData).toHaveBeenCalledTimes(0);
545
- expect(onError).toHaveBeenLastCalledWith(expect.any(Error));
546
- });
547
- });
548
-
549
- describe("set errors", () => {
550
- it("should handle Promise rejection during set() in effect", async () => {
551
- const $state = stateAsync(Promise.resolve(1));
552
- const onData = vi.fn();
553
- const onError = vi.fn();
554
-
555
- subscribe((t) => $state.get(t), onData, onError);
556
-
557
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
558
-
559
- const error = new Error("Set error");
560
- $state.set(Promise.reject(error));
561
-
562
- await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(2));
563
- expect(onError).toHaveBeenLastCalledWith(error);
564
- });
565
- });
566
- });
567
-
568
- describe("disposal", () => {
569
- it("should not dispose effects when state is disposed", async () => {
570
- const $state = stateAsync(Promise.resolve(1400));
571
- const onData = vi.fn();
572
- const onError = vi.fn();
573
-
574
- const $effect = subscribe((t) => $state.get(t), onData, onError);
575
-
576
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
577
- expect($effect.disposed).toBe(false);
578
-
579
- $state.dispose();
580
-
581
- expect($effect.disposed).toBe(false);
582
- expect($state.disposed).toBe(true);
583
- });
584
-
585
- it("should unregister effects when state is disposed", async () => {
586
- const $state = stateAsync(Promise.resolve(1500));
587
- const onData = vi.fn();
588
- const onError = vi.fn();
589
-
590
- const $effect = subscribe((t) => $state.get(t), onData, onError);
591
-
592
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
593
-
594
- $state.dispose();
595
-
596
- // Effect should still be active but not notified
597
- expect($effect.disposed).toBe(false);
598
-
599
- // But state is disposed so operations should fail
600
- await expect($state.pick()).rejects.toThrow("[PicoFlow] Primitive is disposed");
601
- });
602
- });
603
-
604
- describe("complex cases", () => {
605
- it("should handle nested states in effects with set()", async () => {
606
- const $stateA = stateAsync(Promise.resolve(1));
607
- const $stateB = stateAsync(Promise.resolve(2));
608
- const $stateC = stateAsync(
609
- Promise.resolve({
610
- a: $stateA,
611
- b: $stateB,
612
- }),
613
- );
614
-
615
- const onData = vi.fn();
616
- const onError = vi.fn();
617
-
618
- subscribe(
619
- (t) => ({
620
- a: $stateC.get(t).a.get(t),
621
- b: $stateC.get(t).b.get(t),
622
- }),
623
- (data) => onData(data.a, data.b),
624
- onError,
625
- );
626
-
627
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
628
- expect(onData).toHaveBeenLastCalledWith(1, 2);
629
-
630
- $stateA.set(Promise.resolve(3));
631
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
632
- expect(onData).toHaveBeenLastCalledWith(3, 2);
633
- });
634
-
635
- it("should handle effect depending on multiple states with set()", async () => {
636
- const $stateA = stateAsync(Promise.resolve(10));
637
- const $stateB = stateAsync(Promise.resolve(20));
638
- const $stateC = stateAsync(Promise.resolve(30));
639
- const onData = vi.fn();
640
- const onError = vi.fn();
641
-
642
- subscribe(
643
- (t) => ({
644
- a: $stateA.get(t),
645
- b: $stateB.get(t),
646
- c: $stateC.get(t),
647
- }),
648
- (data) => onData(data.a, data.b, data.c),
649
- onError,
650
- );
651
-
652
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
653
- expect(onData).toHaveBeenLastCalledWith(10, 20, 30);
654
-
655
- $stateA.set(Promise.resolve(11));
656
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
657
- expect(onData).toHaveBeenLastCalledWith(11, 20, 30);
658
-
659
- $stateB.set(Promise.resolve(21));
660
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(5));
661
- expect(onData).toHaveBeenLastCalledWith(11, 21, 30);
662
- });
663
- });
664
-
665
- describe("execution order", () => {
666
- it("should execute multiple effects in order", async () => {
667
- const $state = stateAsync(Promise.resolve(1));
668
- const onData = vi.fn();
669
-
670
- subscribe((t) => `effect1-${$state.get(t)}`, onData);
671
-
672
- subscribe((t) => `effect2-${$state.get(t)}`, onData);
673
-
674
- subscribe((t) => `effect3-${$state.get(t)}`, onData);
675
-
676
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
677
-
678
- expect(onData).toHaveBeenNthCalledWith(1, "effect1-1");
679
- expect(onData).toHaveBeenNthCalledWith(2, "effect2-1");
680
- expect(onData).toHaveBeenNthCalledWith(3, "effect3-1");
681
- });
682
-
683
- it("should handle async set() in effects", async () => {
684
- const $state = stateAsync(Promise.resolve(1));
685
- const onData = vi.fn();
686
-
687
- subscribe((t) => `effect-${$state.get(t)}`, onData);
688
-
689
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
690
-
691
- onData("before-set");
692
- $state.set(
693
- (async () => {
694
- await new Promise((resolve) => setTimeout(resolve, 10));
695
- return 2;
696
- })(),
697
- );
698
- onData("after-set");
699
-
700
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(4));
701
- expect(onData).toHaveBeenNthCalledWith(1, "effect-1");
702
- expect(onData).toHaveBeenNthCalledWith(2, "before-set");
703
- expect(onData).toHaveBeenNthCalledWith(3, "after-set");
704
- expect(onData).toHaveBeenNthCalledWith(4, "effect-2");
705
- });
706
- });
707
- });
708
- });