@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,960 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import { type FlowStateAsync, map, stateAsync, subscribe } from "~";
3
-
4
- describe("FlowMap", () => {
5
- describe("unit", () => {
6
- describe("initialization", () => {
7
- it("should initialize with provided Record of FlowStateAsync values", async () => {
8
- const $map = map({
9
- key1: stateAsync(Promise.resolve(1)),
10
- key2: stateAsync(Promise.resolve(2)),
11
- key3: stateAsync(Promise.resolve(3)),
12
- });
13
-
14
- const values = await Promise.all(Array.from((await $map.pick()).values()).map((value) => value.pick()));
15
-
16
- expect(values).toEqual([1, 2, 3]);
17
- });
18
-
19
- it("should initialize with provided Map of FlowStateAsync values", async () => {
20
- const $value1 = stateAsync(Promise.resolve(1));
21
- const $value2 = stateAsync(Promise.resolve(2));
22
- const initialMap = new Map([
23
- ["key1", $value1],
24
- ["key2", $value2],
25
- ]);
26
- const $map = map(initialMap);
27
-
28
- expect(Array.from((await $map.pick()).entries())).toEqual([
29
- ["key1", $value1],
30
- ["key2", $value2],
31
- ]);
32
- });
33
-
34
- it("should initialize with empty map when no value provided", async () => {
35
- const $map = map<string, FlowStateAsync<number>>();
36
-
37
- expect(Array.from((await $map.pick()).entries())).toEqual([]);
38
- expect((await $map.pick()).size).toBe(0);
39
- });
40
-
41
- it("should initialize $lastAction with set action on creation", async () => {
42
- const $value1 = stateAsync(Promise.resolve(1));
43
- const $value2 = stateAsync(Promise.resolve(2));
44
- const $map = map({
45
- key1: $value1,
46
- key2: $value2,
47
- });
48
- const action = await $map.$lastAction.pick();
49
- expect(action.type).toBe("set");
50
- if (action.type === "set") {
51
- expect(action.setMap.size).toBe(2);
52
- expect(action.setMap.get("key1")).toBe($value1);
53
- expect(action.setMap.get("key2")).toBe($value2);
54
- }
55
- });
56
-
57
- it("should initialize $lastAction with empty map when no value provided", async () => {
58
- const $map = map<string, FlowStateAsync<number>>();
59
- const action = await $map.$lastAction.pick();
60
- expect(action.type).toBe("set");
61
- if (action.type === "set") {
62
- expect(action.setMap.size).toBe(0);
63
- }
64
- });
65
- });
66
-
67
- describe("disposal", () => {
68
- it("should have disposed property set to false initially and true after disposal", () => {
69
- const $map = map({
70
- key1: stateAsync(Promise.resolve(1)),
71
- key2: stateAsync(Promise.resolve(2)),
72
- });
73
- expect($map.disposed).toBe(false);
74
- $map.dispose();
75
- expect($map.disposed).toBe(true);
76
- });
77
-
78
- it("should throw error when disposed twice", () => {
79
- const $map = map({
80
- key1: stateAsync(Promise.resolve(1)),
81
- key2: stateAsync(Promise.resolve(2)),
82
- });
83
- $map.dispose();
84
- expect(() => $map.dispose()).toThrow("[PicoFlow] Primitive is disposed");
85
- });
86
-
87
- it("should throw when pick is called after disposal", async () => {
88
- const $map = map<string, FlowStateAsync<number>>({
89
- key1: stateAsync(Promise.resolve(1)),
90
- key2: stateAsync(Promise.resolve(2)),
91
- key3: stateAsync(Promise.resolve(3)),
92
- });
93
- $map.dispose();
94
- await expect($map.pick()).rejects.toThrow("[PicoFlow] Primitive is disposed");
95
- });
96
-
97
- it("should throw when set is called after disposal", () => {
98
- const $map = map<string, FlowStateAsync<number>>({
99
- key1: stateAsync(Promise.resolve(1)),
100
- });
101
- $map.dispose();
102
- expect(() => $map.set(new Map([["key2", stateAsync(Promise.resolve(2))]]))).toThrow(
103
- "[PicoFlow] Primitive is disposed",
104
- );
105
- });
106
-
107
- it("should throw when add is called after disposal", () => {
108
- const $map = map<string, FlowStateAsync<number>>();
109
- $map.dispose();
110
- expect(() => $map.add("key1", stateAsync(Promise.resolve(2)))).toThrow(
111
- "[PicoFlow] Primitive is disposed",
112
- );
113
- });
114
-
115
- it("should throw when update is called after disposal", () => {
116
- const $map = map<string, FlowStateAsync<number>>();
117
- $map.add("key1", stateAsync(Promise.resolve(1)));
118
- $map.dispose();
119
- expect(() => $map.update("key1", stateAsync(Promise.resolve(2)))).toThrow(
120
- "[PicoFlow] Primitive is disposed",
121
- );
122
- });
123
-
124
- it("should throw when delete is called after disposal", () => {
125
- const $map = map<string, FlowStateAsync<number>>({
126
- key1: stateAsync(Promise.resolve(1)),
127
- key2: stateAsync(Promise.resolve(2)),
128
- key3: stateAsync(Promise.resolve(3)),
129
- });
130
- $map.dispose();
131
- expect(() => $map.delete("key1")).toThrow("[PicoFlow] Primitive is disposed");
132
- });
133
-
134
- it("should throw when clear is called after disposal", () => {
135
- const $map = map<string, FlowStateAsync<number>>({
136
- key1: stateAsync(Promise.resolve(1)),
137
- key2: stateAsync(Promise.resolve(2)),
138
- });
139
- $map.dispose();
140
- expect(() => $map.clear()).toThrow("[PicoFlow] Primitive is disposed");
141
- });
142
-
143
- it("should dispose values when FlowMap is disposed", () => {
144
- const $map = map<string, FlowStateAsync<number>>();
145
- const $value1 = stateAsync(Promise.resolve(1));
146
- const $value2 = stateAsync(Promise.resolve(2));
147
- const $value3 = stateAsync(Promise.resolve(3));
148
-
149
- $map.add("key1", $value1);
150
- $map.add("key2", $value2);
151
- $map.add("key3", $value3);
152
-
153
- expect($value1.disposed).toBe(false);
154
- expect($value2.disposed).toBe(false);
155
- expect($value3.disposed).toBe(false);
156
-
157
- $map.dispose();
158
-
159
- expect($value1.disposed).toBe(true);
160
- expect($value2.disposed).toBe(true);
161
- expect($value3.disposed).toBe(true);
162
- });
163
-
164
- it("should dispose values with provided options when FlowMap is disposed", async () => {
165
- const $map = map<string, FlowStateAsync<number>>();
166
- const $value1 = stateAsync(Promise.resolve(1));
167
- const $value2 = stateAsync(Promise.resolve(2));
168
- const onData = vi.fn((_value: number) => undefined);
169
-
170
- $map.add("key1", $value1);
171
- $map.add("key2", $value2);
172
-
173
- const $effect = subscribe((t) => $value1.get(t), onData);
174
-
175
- await vi.waitFor(() => expect($effect.disposed).toBe(false));
176
-
177
- $map.dispose();
178
-
179
- expect($value1.disposed).toBe(true);
180
- expect($value2.disposed).toBe(true);
181
- expect($effect.disposed).toBe(false);
182
-
183
- $effect.dispose();
184
- });
185
-
186
- it("should handle non-disposable values gracefully", async () => {
187
- const $map = map<string, FlowStateAsync<number>>();
188
- $map.add("key1", stateAsync(Promise.resolve(1)));
189
- $map.add("key2", stateAsync(Promise.resolve(2)));
190
- $map.add("key3", stateAsync(Promise.resolve(3)));
191
-
192
- expect((await $map.pick()).size).toBe(3);
193
-
194
- expect(() => $map.dispose()).not.toThrow();
195
- });
196
- });
197
-
198
- describe("set", () => {
199
- it("should replace entire map when set is called", async () => {
200
- const $map = map<string, FlowStateAsync<number>>({
201
- key1: stateAsync(Promise.resolve(1)),
202
- key2: stateAsync(Promise.resolve(2)),
203
- });
204
- const $newValue1 = stateAsync(Promise.resolve(3));
205
- const $newValue2 = stateAsync(Promise.resolve(4));
206
- const newMap = new Map([
207
- ["key3", $newValue1],
208
- ["key4", $newValue2],
209
- ]);
210
-
211
- $map.set(newMap);
212
- expect(Array.from((await $map.pick()).entries())).toEqual([
213
- ["key3", $newValue1],
214
- ["key4", $newValue2],
215
- ]);
216
- });
217
-
218
- it("should update $lastAction with set action when set is called", async () => {
219
- const $map = map<string, FlowStateAsync<number>>({
220
- key1: stateAsync(Promise.resolve(1)),
221
- key2: stateAsync(Promise.resolve(2)),
222
- });
223
- const $newValue1 = stateAsync(Promise.resolve(3));
224
- const $newValue2 = stateAsync(Promise.resolve(4));
225
- const newMap = new Map([
226
- ["key3", $newValue1],
227
- ["key4", $newValue2],
228
- ]);
229
-
230
- $map.set(newMap);
231
- const action = await $map.$lastAction.pick();
232
- expect(action.type).toBe("set");
233
- if (action.type === "set") {
234
- expect(action.setMap).toBe(newMap);
235
- expect(action.clearedMap).toBeInstanceOf(Map);
236
- }
237
- });
238
- });
239
-
240
- describe("add", () => {
241
- it("should add key-value pair to map", async () => {
242
- const $map = map<string, FlowStateAsync<number>>();
243
-
244
- $map.add("key1", stateAsync(Promise.resolve(1)));
245
- expect(await (await $map.pick()).get("key1")?.pick()).toBe(1);
246
- expect((await $map.pick()).size).toBe(1);
247
- });
248
-
249
- it("should add multiple keys successively", async () => {
250
- const $map = map<string, FlowStateAsync<number>>();
251
-
252
- $map.add("key1", stateAsync(Promise.resolve(1)));
253
- $map.add("key2", stateAsync(Promise.resolve(2)));
254
- $map.add("key3", stateAsync(Promise.resolve(3)));
255
-
256
- expect((await $map.pick()).size).toBe(3);
257
- expect(await (await $map.pick()).get("key1")?.pick()).toBe(1);
258
- expect(await (await $map.pick()).get("key2")?.pick()).toBe(2);
259
- expect(await (await $map.pick()).get("key3")?.pick()).toBe(3);
260
- });
261
-
262
- it("should throw when key already exists", () => {
263
- const $map = map<string, FlowStateAsync<number>>();
264
- $map.add("key1", stateAsync(Promise.resolve(1)));
265
- expect(() => $map.add("key1", stateAsync(Promise.resolve(2)))).toThrow("[PicoFlow] Key already exists");
266
- });
267
-
268
- it("should update $lastAction with add action when add is called", async () => {
269
- const $map = map<string, FlowStateAsync<number>>();
270
- const $value = stateAsync(Promise.resolve(1));
271
- $map.add("key1", $value);
272
- const action = await $map.$lastAction.pick();
273
- expect(action).toEqual({
274
- type: "add",
275
- key: "key1",
276
- addedValue: $value,
277
- });
278
- });
279
-
280
- it("should have FlowStateAsync value in $lastAction when add is called", async () => {
281
- const $map = map<string, FlowStateAsync<number>>();
282
- const $value = stateAsync(Promise.resolve(100));
283
- $map.add("key1", $value);
284
- const action = await $map.$lastAction.pick();
285
- expect(action.type).toBe("add");
286
- if (action.type === "add") {
287
- expect(action.addedValue).toBe($value);
288
- expect(await action.addedValue.pick()).toBe(100);
289
- }
290
- });
291
-
292
- it("should trigger reactivity when add is called", async () => {
293
- const $map = map<string, FlowStateAsync<number>>();
294
- const onData = vi.fn();
295
- subscribe((t) => $map.get(t), onData);
296
-
297
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
298
-
299
- $map.add("key1", stateAsync(Promise.resolve(1)));
300
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
301
- });
302
- });
303
-
304
- describe("update", () => {
305
- it("should update existing key-value pair", async () => {
306
- const $map = map<string, FlowStateAsync<number>>();
307
- $map.add("key1", stateAsync(Promise.resolve(1)));
308
-
309
- const $newValue = stateAsync(Promise.resolve(2));
310
- $map.update("key1", $newValue);
311
- expect(await (await $map.pick()).get("key1")?.pick()).toBe(2);
312
- });
313
-
314
- it("should update multiple keys successively", async () => {
315
- const $map = map<string, FlowStateAsync<number>>();
316
- $map.add("key1", stateAsync(Promise.resolve(1)));
317
- $map.add("key2", stateAsync(Promise.resolve(2)));
318
-
319
- $map.update("key1", stateAsync(Promise.resolve(10)));
320
- $map.update("key2", stateAsync(Promise.resolve(20)));
321
-
322
- expect(await (await $map.pick()).get("key1")?.pick()).toBe(10);
323
- expect(await (await $map.pick()).get("key2")?.pick()).toBe(20);
324
- });
325
-
326
- it("should throw when key does not exist", () => {
327
- const $map = map<string, FlowStateAsync<number>>();
328
- expect(() => $map.update("key1", stateAsync(Promise.resolve(1)))).toThrow(
329
- "[PicoFlow] Key does not exist",
330
- );
331
- });
332
-
333
- it("should update $lastAction with update action when update is called", async () => {
334
- const $map = map<string, FlowStateAsync<number>>();
335
- $map.add("key1", stateAsync(Promise.resolve(1)));
336
- const $newValue = stateAsync(Promise.resolve(2));
337
- $map.update("key1", $newValue);
338
- const action = await $map.$lastAction.pick();
339
- expect(action).toEqual({
340
- type: "update",
341
- key: "key1",
342
- setValue: $newValue,
343
- clearedValue: expect.any(Object),
344
- });
345
- });
346
-
347
- it("should have FlowStateAsync value in $lastAction when update is called", async () => {
348
- const $map = map<string, FlowStateAsync<number>>();
349
- $map.add("key1", stateAsync(Promise.resolve(1)));
350
- const $newValue = stateAsync(Promise.resolve(200));
351
- $map.update("key1", $newValue);
352
- const action = await $map.$lastAction.pick();
353
- expect(action.type).toBe("update");
354
- if (action.type === "update") {
355
- expect(action.setValue).toBe($newValue);
356
- expect(await action.setValue.pick()).toBe(200);
357
- }
358
- });
359
-
360
- it("should trigger reactivity when update is called", async () => {
361
- const $map = map<string, FlowStateAsync<number>>();
362
- $map.add("key1", stateAsync(Promise.resolve(1)));
363
- const onData = vi.fn();
364
- subscribe((t) => $map.get(t), onData);
365
-
366
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
367
-
368
- $map.update("key1", stateAsync(Promise.resolve(2)));
369
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
370
- });
371
- });
372
-
373
- describe("delete", () => {
374
- it("should delete key from map", async () => {
375
- const $map = map<string, FlowStateAsync<number>>();
376
- $map.add("key1", stateAsync(Promise.resolve(1)));
377
- $map.add("key2", stateAsync(Promise.resolve(2)));
378
-
379
- $map.delete("key1");
380
- expect(await (await $map.pick()).get("key1")?.pick()).toBe(undefined);
381
- expect(await (await $map.pick()).get("key2")?.pick()).toBe(2);
382
- expect((await $map.pick()).size).toBe(1);
383
- });
384
-
385
- it("should delete multiple keys successively", async () => {
386
- const $map = map<string, FlowStateAsync<number>>();
387
- $map.add("key1", stateAsync(Promise.resolve(1)));
388
- $map.add("key2", stateAsync(Promise.resolve(2)));
389
- $map.add("key3", stateAsync(Promise.resolve(3)));
390
-
391
- $map.delete("key1");
392
- $map.delete("key2");
393
-
394
- expect((await $map.pick()).size).toBe(1);
395
- expect(await (await $map.pick()).get("key3")?.pick()).toBe(3);
396
- });
397
-
398
- it("should throw when key does not exist", () => {
399
- const $map = map<string, FlowStateAsync<number>>();
400
- expect(() => $map.delete("key1")).toThrow("[PicoFlow] Key does not exist");
401
- });
402
-
403
- it("should update $lastAction with delete action when delete is called", async () => {
404
- const $map = map<string, FlowStateAsync<number>>();
405
- const $value = stateAsync(Promise.resolve(1));
406
- $map.add("key1", $value);
407
- $map.delete("key1");
408
- const action = await $map.$lastAction.pick();
409
- expect(action.type).toBe("delete");
410
- if (action.type === "delete") {
411
- expect(action.key).toBe("key1");
412
- expect(action.removedValue).toBe($value);
413
- }
414
- });
415
-
416
- it("should trigger reactivity when delete is called", async () => {
417
- const $map = map<string, FlowStateAsync<number>>();
418
- $map.add("key1", stateAsync(Promise.resolve(1)));
419
- const onData = vi.fn();
420
- subscribe((t) => $map.get(t), onData);
421
-
422
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
423
-
424
- $map.delete("key1");
425
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
426
- });
427
- });
428
-
429
- describe("clear", () => {
430
- it("should remove all entries from map", async () => {
431
- const $map = map<string, FlowStateAsync<number>>({
432
- key1: stateAsync(Promise.resolve(1)),
433
- key2: stateAsync(Promise.resolve(2)),
434
- key3: stateAsync(Promise.resolve(3)),
435
- });
436
- expect((await $map.pick()).size).toBe(3);
437
-
438
- $map.clear();
439
- expect((await $map.pick()).size).toBe(0);
440
- });
441
-
442
- it("should update $lastAction with clear action when clear is called", async () => {
443
- const $map = map<string, FlowStateAsync<number>>({
444
- key1: stateAsync(Promise.resolve(1)),
445
- key2: stateAsync(Promise.resolve(2)),
446
- });
447
- $map.clear();
448
- const action = await $map.$lastAction.pick();
449
- expect(action.type).toBe("clear");
450
- if (action.type === "clear") {
451
- expect(action.clearedMap).toBeInstanceOf(Map);
452
- expect(action.clearedMap.size).toBe(2);
453
- }
454
- });
455
- });
456
-
457
- describe("$lastAction", () => {
458
- describe("set action", () => {
459
- it("should initialize $lastAction with set action on creation", async () => {
460
- const $value1 = stateAsync(Promise.resolve(1));
461
- const $value2 = stateAsync(Promise.resolve(2));
462
- const $map = map({
463
- key1: $value1,
464
- key2: $value2,
465
- });
466
- const action = await $map.$lastAction.pick();
467
- expect(action).toEqual({
468
- type: "set",
469
- setMap: expect.any(Map),
470
- clearedMap: expect.any(Map),
471
- });
472
- if (action.type === "set") {
473
- expect(action.setMap.get("key1")).toBe($value1);
474
- expect(action.setMap.get("key2")).toBe($value2);
475
- }
476
- });
477
-
478
- it("should update $lastAction with set action when set is called", async () => {
479
- const $map = map<string, FlowStateAsync<number>>({
480
- key1: stateAsync(Promise.resolve(1)),
481
- });
482
- const $newValue = stateAsync(Promise.resolve(2));
483
- const newMap = new Map([["key2", $newValue]]);
484
- $map.set(newMap);
485
- const action = await $map.$lastAction.pick();
486
- expect(action).toEqual({
487
- type: "set",
488
- setMap: newMap,
489
- clearedMap: expect.any(Map),
490
- });
491
- });
492
-
493
- it("should have correct structure for set action (type + setMap + clearedMap with FlowStateAsync)", async () => {
494
- const $value = stateAsync(Promise.resolve(1));
495
- const $map = map({ key1: $value });
496
- const action = await $map.$lastAction.pick();
497
- expect(action).toHaveProperty("type", "set");
498
- if (action.type === "set") {
499
- expect(action).toHaveProperty("setMap");
500
- expect(action).toHaveProperty("clearedMap");
501
- expect(action.setMap).toBeInstanceOf(Map);
502
- expect(action.clearedMap).toBeInstanceOf(Map);
503
- expect(action.setMap.get("key1")).toBe($value);
504
- }
505
- });
506
- });
507
-
508
- describe("add action", () => {
509
- it("should update $lastAction with add action when add is called", async () => {
510
- const $map = map<string, FlowStateAsync<number>>();
511
- const $value = stateAsync(Promise.resolve(1));
512
- $map.add("key1", $value);
513
- const action = await $map.$lastAction.pick();
514
- expect(action).toEqual({
515
- type: "add",
516
- key: "key1",
517
- addedValue: $value,
518
- });
519
- });
520
-
521
- it("should have correct structure for add action (type + key + FlowStateAsync addedValue)", async () => {
522
- const $map = map<string, FlowStateAsync<number>>();
523
- const $value = stateAsync(Promise.resolve(100));
524
- $map.add("key1", $value);
525
- const action = await $map.$lastAction.pick();
526
- expect(action).toHaveProperty("type", "add");
527
- if (action.type === "add") {
528
- expect(action).toHaveProperty("key");
529
- expect(action).toHaveProperty("addedValue");
530
- expect(action.key).toBe("key1");
531
- expect(action.addedValue).toBe($value);
532
- expect(await action.addedValue.pick()).toBe(100);
533
- }
534
- });
535
- });
536
-
537
- describe("update action", () => {
538
- it("should update $lastAction with update action when update is called", async () => {
539
- const $map = map<string, FlowStateAsync<number>>();
540
- $map.add("key1", stateAsync(Promise.resolve(1)));
541
- const $newValue = stateAsync(Promise.resolve(2));
542
- $map.update("key1", $newValue);
543
- const action = await $map.$lastAction.pick();
544
- expect(action).toEqual({
545
- type: "update",
546
- key: "key1",
547
- setValue: $newValue,
548
- clearedValue: expect.any(Object),
549
- });
550
- });
551
-
552
- it("should have correct structure for update action (type + key + FlowStateAsync setValue + clearedValue)", async () => {
553
- const $map = map<string, FlowStateAsync<number>>();
554
- $map.add("key1", stateAsync(Promise.resolve(1)));
555
- const $newValue = stateAsync(Promise.resolve(200));
556
- $map.update("key1", $newValue);
557
- const action = await $map.$lastAction.pick();
558
- expect(action).toHaveProperty("type", "update");
559
- if (action.type === "update") {
560
- expect(action).toHaveProperty("key");
561
- expect(action).toHaveProperty("setValue");
562
- expect(action).toHaveProperty("clearedValue");
563
- expect(action.key).toBe("key1");
564
- expect(action.setValue).toBe($newValue);
565
- expect(await action.setValue.pick()).toBe(200);
566
- }
567
- });
568
- });
569
-
570
- describe("delete action", () => {
571
- it("should update $lastAction with delete action when delete is called", async () => {
572
- const $map = map<string, FlowStateAsync<number>>();
573
- const $value = stateAsync(Promise.resolve(1));
574
- $map.add("key1", $value);
575
- $map.delete("key1");
576
- const action = await $map.$lastAction.pick();
577
- expect(action).toEqual({
578
- type: "delete",
579
- key: "key1",
580
- removedValue: $value,
581
- });
582
- });
583
-
584
- it("should have correct structure for delete action (type + key + FlowStateAsync removedValue)", async () => {
585
- const $map = map<string, FlowStateAsync<number>>();
586
- const $value = stateAsync(Promise.resolve(300));
587
- $map.add("key1", $value);
588
- $map.delete("key1");
589
- const action = await $map.$lastAction.pick();
590
- expect(action).toHaveProperty("type", "delete");
591
- if (action.type === "delete") {
592
- expect(action).toHaveProperty("key");
593
- expect(action).toHaveProperty("removedValue");
594
- expect(action.key).toBe("key1");
595
- expect(action.removedValue).toBe($value);
596
- }
597
- });
598
- });
599
-
600
- describe("clear action", () => {
601
- it("should update $lastAction with clear action when clear is called", async () => {
602
- const $map = map<string, FlowStateAsync<number>>({
603
- key1: stateAsync(Promise.resolve(1)),
604
- key2: stateAsync(Promise.resolve(2)),
605
- });
606
- $map.clear();
607
- const action = await $map.$lastAction.pick();
608
- expect(action.type).toBe("clear");
609
- if (action.type === "clear") {
610
- expect(action.clearedMap).toBeInstanceOf(Map);
611
- expect(action.clearedMap.size).toBe(2);
612
- }
613
- });
614
-
615
- it("should have correct structure for clear action (type + clearedMap)", async () => {
616
- const $map = map<string, FlowStateAsync<number>>({
617
- key1: stateAsync(Promise.resolve(1)),
618
- });
619
- $map.clear();
620
- const action = await $map.$lastAction.pick();
621
- expect(action).toHaveProperty("type", "clear");
622
- if (action.type === "clear") {
623
- expect(action).toHaveProperty("clearedMap");
624
- expect(action.clearedMap).toBeInstanceOf(Map);
625
- }
626
- });
627
- });
628
- });
629
-
630
- describe("edge cases", () => {
631
- it("should handle multiple rapid operations", async () => {
632
- const $map = map<string, FlowStateAsync<number>>();
633
- $map.add("key1", stateAsync(Promise.resolve(1)));
634
- $map.add("key2", stateAsync(Promise.resolve(2)));
635
- $map.add("key3", stateAsync(Promise.resolve(3)));
636
- $map.update("key1", stateAsync(Promise.resolve(10)));
637
- $map.delete("key2");
638
- $map.add("key4", stateAsync(Promise.resolve(4)));
639
- $map.clear();
640
- $map.add("key5", stateAsync(Promise.resolve(5)));
641
-
642
- expect((await $map.pick()).size).toBe(1);
643
- expect(await (await $map.pick()).get("key5")?.pick()).toBe(5);
644
- });
645
-
646
- it("should handle empty map operations", async () => {
647
- const $map = map<string, FlowStateAsync<number>>();
648
- expect((await $map.pick()).size).toBe(0);
649
-
650
- $map.add("key1", stateAsync(Promise.resolve(1)));
651
- expect((await $map.pick()).size).toBe(1);
652
-
653
- $map.delete("key1");
654
- expect((await $map.pick()).size).toBe(0);
655
- });
656
-
657
- it("should handle map with single entry", async () => {
658
- const $map = map<string, FlowStateAsync<number>>({
659
- key1: stateAsync(Promise.resolve(1)),
660
- });
661
- expect((await $map.pick()).size).toBe(1);
662
-
663
- $map.update("key1", stateAsync(Promise.resolve(2)));
664
- expect(await (await $map.pick()).get("key1")?.pick()).toBe(2);
665
-
666
- $map.delete("key1");
667
- expect((await $map.pick()).size).toBe(0);
668
- });
669
-
670
- it("should handle map with many entries", async () => {
671
- const $map = map<string, FlowStateAsync<number>>();
672
- for (let i = 0; i < 100; i++) {
673
- $map.add(`key${i}`, stateAsync(Promise.resolve(i)));
674
- }
675
- expect((await $map.pick()).size).toBe(100);
676
-
677
- for (let i = 0; i < 50; i++) {
678
- $map.delete(`key${i}`);
679
- }
680
- expect((await $map.pick()).size).toBe(50);
681
- });
682
-
683
- it("should handle disposed values in map", async () => {
684
- const $value = stateAsync(Promise.resolve(1));
685
- const $map = map(new Map([["key1", $value]]));
686
-
687
- $map.delete("key1");
688
-
689
- // In flow2, values are NOT automatically disposed
690
- expect($value.disposed).toBe(false);
691
- expect((await $map.pick()).size).toBe(0);
692
- });
693
-
694
- it("should handle effects on FlowStateAsync values in map", async () => {
695
- const $value = stateAsync(Promise.resolve(1));
696
- const $map = map(new Map([["key1", $value]]));
697
- const onData = vi.fn();
698
-
699
- subscribe(
700
- (t) => $map.get(t).get("key1")?.get(t),
701
- (data) => {
702
- if (data) onData(data);
703
- },
704
- );
705
-
706
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
707
- expect(onData).toHaveBeenLastCalledWith(1);
708
-
709
- $value.set(Promise.resolve(2));
710
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
711
- expect(onData).toHaveBeenLastCalledWith(2);
712
- });
713
- });
714
- });
715
-
716
- describe("integration", () => {
717
- describe("with effects", () => {
718
- describe("reactivity", () => {
719
- it("should call effect when initialized", async () => {
720
- const $map = map<string, FlowStateAsync<number>>({
721
- key1: stateAsync(Promise.resolve(1)),
722
- key2: stateAsync(Promise.resolve(2)),
723
- });
724
- const onData = vi.fn();
725
- subscribe((t) => $map.get(t), onData);
726
-
727
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
728
- expect(onData).toHaveBeenLastCalledWith(
729
- expect.objectContaining({
730
- size: 2,
731
- }),
732
- );
733
- });
734
-
735
- it("should call effect when updated with set", async () => {
736
- const $map = map<string, FlowStateAsync<number>>({
737
- key1: stateAsync(Promise.resolve(1)),
738
- });
739
- const onData = vi.fn();
740
- subscribe((t) => $map.get(t), onData);
741
-
742
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
743
-
744
- const $newValue = stateAsync(Promise.resolve(2));
745
- $map.set(new Map([["key2", $newValue]]));
746
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
747
- expect(onData).toHaveBeenLastCalledWith(
748
- expect.objectContaining({
749
- size: 1,
750
- }),
751
- );
752
- });
753
-
754
- it("should call effect when updated with add", async () => {
755
- const $map = map<string, FlowStateAsync<number>>();
756
- const onData = vi.fn();
757
- subscribe((t) => $map.get(t), onData);
758
-
759
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
760
- expect(onData).toHaveBeenLastCalledWith(new Map());
761
-
762
- const $key1Value = stateAsync(Promise.resolve(1));
763
- $map.add("key1", $key1Value);
764
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
765
- expect(onData).toHaveBeenLastCalledWith(new Map([["key1", $key1Value]]));
766
-
767
- const $key2Value = stateAsync(Promise.resolve(2));
768
- $map.add("key2", $key2Value);
769
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
770
- expect(onData).toHaveBeenLastCalledWith(
771
- new Map([
772
- ["key1", $key1Value],
773
- ["key2", $key2Value],
774
- ]),
775
- );
776
- });
777
-
778
- it("should call effect when updated with update", async () => {
779
- const $map = map<string, FlowStateAsync<number>>();
780
- $map.add("key1", stateAsync(Promise.resolve(1)));
781
- const onData = vi.fn();
782
- subscribe((t) => $map.get(t), onData);
783
-
784
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
785
-
786
- const $newValue = stateAsync(Promise.resolve(2));
787
- $map.update("key1", $newValue);
788
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
789
- expect(onData).toHaveBeenLastCalledWith(new Map([["key1", $newValue]]));
790
- });
791
-
792
- it("should call effect when updated with delete", async () => {
793
- const $map = map<string, FlowStateAsync<number>>();
794
- $map.add("key1", stateAsync(Promise.resolve(1)));
795
- $map.add("key2", stateAsync(Promise.resolve(2)));
796
- const onData = vi.fn();
797
- subscribe((t) => $map.get(t), onData);
798
-
799
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
800
-
801
- $map.delete("key1");
802
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
803
- const mapAfterDelete = onData.mock.calls[1][0];
804
- expect(mapAfterDelete.size).toBe(1);
805
- expect(mapAfterDelete.has("key2")).toBe(true);
806
- });
807
-
808
- it("should call effect when updated with clear", async () => {
809
- const $map = map<string, FlowStateAsync<number>>({
810
- key1: stateAsync(Promise.resolve(1)),
811
- key2: stateAsync(Promise.resolve(2)),
812
- });
813
- const onData = vi.fn();
814
- subscribe((t) => $map.get(t), onData);
815
-
816
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
817
-
818
- $map.clear();
819
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
820
- expect(onData).toHaveBeenLastCalledWith(new Map());
821
- });
822
-
823
- it("should call effect when FlowStateAsync values in map are updated", async () => {
824
- const $value = stateAsync(Promise.resolve(1));
825
- const $map = map(new Map([["key1", $value]]));
826
- const onData = vi.fn();
827
-
828
- subscribe(
829
- (t) => $map.get(t).get("key1")?.get(t),
830
- (data) => {
831
- if (data) onData(data);
832
- },
833
- );
834
-
835
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
836
- expect(onData).toHaveBeenLastCalledWith(1);
837
-
838
- $value.set(Promise.resolve(2));
839
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
840
- expect(onData).toHaveBeenLastCalledWith(2);
841
- });
842
- });
843
-
844
- describe("$lastAction", () => {
845
- it("should call effect when $lastAction is updated with set action", async () => {
846
- const $map = map<string, FlowStateAsync<number>>({
847
- key1: stateAsync(Promise.resolve(1)),
848
- });
849
- const onData = vi.fn();
850
- subscribe((t) => $map.$lastAction.get(t), onData);
851
-
852
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
853
-
854
- const $newValue = stateAsync(Promise.resolve(2));
855
- $map.set(new Map([["key2", $newValue]]));
856
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
857
- expect(onData).toHaveBeenLastCalledWith({
858
- type: "set",
859
- setMap: expect.any(Map),
860
- clearedMap: expect.any(Map),
861
- });
862
- });
863
-
864
- it("should call effect when $lastAction is updated with add action", async () => {
865
- const $map = map<string, FlowStateAsync<number>>();
866
- const onData = vi.fn();
867
- subscribe((t) => $map.$lastAction.get(t), onData);
868
-
869
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
870
-
871
- const $value = stateAsync(Promise.resolve(1));
872
- $map.add("key1", $value);
873
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
874
- expect(onData).toHaveBeenLastCalledWith({
875
- type: "add",
876
- key: "key1",
877
- addedValue: $value,
878
- });
879
- });
880
-
881
- it("should call effect when $lastAction is updated with update action", async () => {
882
- const $map = map<string, FlowStateAsync<number>>();
883
- $map.add("key1", stateAsync(Promise.resolve(1)));
884
- const onData = vi.fn();
885
- subscribe((t) => $map.$lastAction.get(t), onData);
886
-
887
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
888
-
889
- const $newValue = stateAsync(Promise.resolve(2));
890
- $map.update("key1", $newValue);
891
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
892
- expect(onData).toHaveBeenLastCalledWith({
893
- type: "update",
894
- key: "key1",
895
- setValue: $newValue,
896
- clearedValue: expect.any(Object),
897
- });
898
- });
899
-
900
- it("should call effect when $lastAction is updated with delete action", async () => {
901
- const $map = map<string, FlowStateAsync<number>>();
902
- const $value = stateAsync(Promise.resolve(1));
903
- $map.add("key1", $value);
904
- const onData = vi.fn();
905
- subscribe((t) => $map.$lastAction.get(t), onData);
906
-
907
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
908
-
909
- $map.delete("key1");
910
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
911
- expect(onData).toHaveBeenLastCalledWith({
912
- type: "delete",
913
- key: "key1",
914
- removedValue: $value,
915
- });
916
- });
917
-
918
- it("should call effect when $lastAction is updated with clear action", async () => {
919
- const $map = map<string, FlowStateAsync<number>>({
920
- key1: stateAsync(Promise.resolve(1)),
921
- key2: stateAsync(Promise.resolve(2)),
922
- });
923
- const onData = vi.fn();
924
- subscribe((t) => $map.$lastAction.get(t), onData);
925
-
926
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
927
-
928
- $map.clear();
929
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
930
- expect(onData).toHaveBeenLastCalledWith({
931
- type: "clear",
932
- clearedMap: expect.any(Map),
933
- });
934
- });
935
-
936
- it("should call effect when FlowStateAsync value in $lastAction is updated", async () => {
937
- const $map = map<string, FlowStateAsync<number>>();
938
- const $value = stateAsync(Promise.resolve(1));
939
- $map.add("key1", $value);
940
- const onData = vi.fn();
941
-
942
- subscribe((t) => {
943
- const action = $map.$lastAction.get(t);
944
- if (action.type === "add") {
945
- return action.addedValue.get(t);
946
- }
947
- return undefined;
948
- }, onData);
949
-
950
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
951
- expect(onData).toHaveBeenLastCalledWith(1);
952
-
953
- $value.set(Promise.resolve(2));
954
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
955
- expect(onData).toHaveBeenLastCalledWith(2);
956
- });
957
- });
958
- });
959
- });
960
- });