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