@ersbeth/picoflow 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. package/.cursor/plans/unifier-flowresource-avec-flowderivation-c9506e24.plan.md +372 -0
  2. package/README.md +17 -1
  3. package/biome.json +4 -1
  4. package/dist/picoflow.js +1129 -661
  5. package/dist/types/flow/base/flowDisposable.d.ts +67 -0
  6. package/dist/types/flow/base/flowDisposable.d.ts.map +1 -0
  7. package/dist/types/flow/base/flowEffect.d.ts +127 -0
  8. package/dist/types/flow/base/flowEffect.d.ts.map +1 -0
  9. package/dist/types/flow/base/flowGraph.d.ts +97 -0
  10. package/dist/types/flow/base/flowGraph.d.ts.map +1 -0
  11. package/dist/types/flow/base/flowSignal.d.ts +134 -0
  12. package/dist/types/flow/base/flowSignal.d.ts.map +1 -0
  13. package/dist/types/flow/base/flowTracker.d.ts +15 -0
  14. package/dist/types/flow/base/flowTracker.d.ts.map +1 -0
  15. package/dist/types/flow/base/index.d.ts +7 -0
  16. package/dist/types/flow/base/index.d.ts.map +1 -0
  17. package/dist/types/flow/base/utils.d.ts +20 -0
  18. package/dist/types/flow/base/utils.d.ts.map +1 -0
  19. package/dist/types/{advanced/array.d.ts → flow/collections/flowArray.d.ts} +50 -12
  20. package/dist/types/flow/collections/flowArray.d.ts.map +1 -0
  21. package/dist/types/flow/collections/flowMap.d.ts +224 -0
  22. package/dist/types/flow/collections/flowMap.d.ts.map +1 -0
  23. package/dist/types/flow/collections/index.d.ts +3 -0
  24. package/dist/types/flow/collections/index.d.ts.map +1 -0
  25. package/dist/types/flow/index.d.ts +4 -0
  26. package/dist/types/flow/index.d.ts.map +1 -0
  27. package/dist/types/flow/nodes/async/flowConstantAsync.d.ts +137 -0
  28. package/dist/types/flow/nodes/async/flowConstantAsync.d.ts.map +1 -0
  29. package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts +137 -0
  30. package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts.map +1 -0
  31. package/dist/types/flow/nodes/async/flowNodeAsync.d.ts +343 -0
  32. package/dist/types/flow/nodes/async/flowNodeAsync.d.ts.map +1 -0
  33. package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts +81 -0
  34. package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts.map +1 -0
  35. package/dist/types/flow/nodes/async/flowStateAsync.d.ts +111 -0
  36. package/dist/types/flow/nodes/async/flowStateAsync.d.ts.map +1 -0
  37. package/dist/types/flow/nodes/async/index.d.ts +6 -0
  38. package/dist/types/flow/nodes/async/index.d.ts.map +1 -0
  39. package/dist/types/flow/nodes/index.d.ts +3 -0
  40. package/dist/types/flow/nodes/index.d.ts.map +1 -0
  41. package/dist/types/flow/nodes/sync/flowConstant.d.ts +108 -0
  42. package/dist/types/flow/nodes/sync/flowConstant.d.ts.map +1 -0
  43. package/dist/types/flow/nodes/sync/flowDerivation.d.ts +100 -0
  44. package/dist/types/flow/nodes/sync/flowDerivation.d.ts.map +1 -0
  45. package/dist/types/flow/nodes/sync/flowNode.d.ts +314 -0
  46. package/dist/types/flow/nodes/sync/flowNode.d.ts.map +1 -0
  47. package/dist/types/flow/nodes/sync/flowReadonly.d.ts +57 -0
  48. package/dist/types/flow/nodes/sync/flowReadonly.d.ts.map +1 -0
  49. package/dist/types/flow/nodes/sync/flowState.d.ts +96 -0
  50. package/dist/types/flow/nodes/sync/flowState.d.ts.map +1 -0
  51. package/dist/types/flow/nodes/sync/index.d.ts +6 -0
  52. package/dist/types/flow/nodes/sync/index.d.ts.map +1 -0
  53. package/dist/types/index.d.ts +1 -4
  54. package/dist/types/index.d.ts.map +1 -1
  55. package/dist/types/solid/converters.d.ts +34 -44
  56. package/dist/types/solid/converters.d.ts.map +1 -1
  57. package/dist/types/solid/primitives.d.ts +1 -0
  58. package/dist/types/solid/primitives.d.ts.map +1 -1
  59. package/docs/.vitepress/config.mts +1 -1
  60. package/docs/api/typedoc-sidebar.json +81 -1
  61. package/package.json +60 -58
  62. package/src/flow/base/flowDisposable.ts +71 -0
  63. package/src/flow/base/flowEffect.ts +171 -0
  64. package/src/flow/base/flowGraph.ts +288 -0
  65. package/src/flow/base/flowSignal.ts +207 -0
  66. package/src/flow/base/flowTracker.ts +17 -0
  67. package/src/flow/base/index.ts +6 -0
  68. package/src/flow/base/utils.ts +19 -0
  69. package/src/flow/collections/flowArray.ts +409 -0
  70. package/src/flow/collections/flowMap.ts +398 -0
  71. package/src/flow/collections/index.ts +2 -0
  72. package/src/flow/index.ts +3 -0
  73. package/src/flow/nodes/async/flowConstantAsync.ts +142 -0
  74. package/src/flow/nodes/async/flowDerivationAsync.ts +143 -0
  75. package/src/flow/nodes/async/flowNodeAsync.ts +474 -0
  76. package/src/flow/nodes/async/flowReadonlyAsync.ts +81 -0
  77. package/src/flow/nodes/async/flowStateAsync.ts +116 -0
  78. package/src/flow/nodes/async/index.ts +5 -0
  79. package/src/flow/nodes/await/advanced/index.ts +5 -0
  80. package/src/{advanced → flow/nodes/await/advanced}/resource.ts +37 -3
  81. package/src/{advanced → flow/nodes/await/advanced}/resourceAsync.ts +35 -3
  82. package/src/{advanced → flow/nodes/await/advanced}/stream.ts +40 -2
  83. package/src/{advanced → flow/nodes/await/advanced}/streamAsync.ts +38 -3
  84. package/src/flow/nodes/await/flowConstantAwait.ts +154 -0
  85. package/src/flow/nodes/await/flowDerivationAwait.ts +154 -0
  86. package/src/flow/nodes/await/flowNodeAwait.ts +508 -0
  87. package/src/flow/nodes/await/flowReadonlyAwait.ts +89 -0
  88. package/src/flow/nodes/await/flowStateAwait.ts +130 -0
  89. package/src/flow/nodes/await/index.ts +5 -0
  90. package/src/flow/nodes/index.ts +3 -0
  91. package/src/flow/nodes/sync/flowConstant.ts +111 -0
  92. package/src/flow/nodes/sync/flowDerivation.ts +105 -0
  93. package/src/flow/nodes/sync/flowNode.ts +439 -0
  94. package/src/flow/nodes/sync/flowReadonly.ts +57 -0
  95. package/src/flow/nodes/sync/flowState.ts +101 -0
  96. package/src/flow/nodes/sync/index.ts +5 -0
  97. package/src/index.ts +1 -47
  98. package/src/solid/converters.ts +59 -198
  99. package/src/solid/primitives.ts +4 -0
  100. package/test/base/flowEffect.test.ts +108 -0
  101. package/test/base/flowGraph.test.ts +485 -0
  102. package/test/base/flowSignal.test.ts +372 -0
  103. package/test/collections/flowArray.asyncStates.test.ts +1553 -0
  104. package/test/collections/flowArray.scalars.test.ts +1129 -0
  105. package/test/collections/flowArray.states.test.ts +1365 -0
  106. package/test/collections/flowMap.asyncStates.test.ts +1105 -0
  107. package/test/collections/flowMap.scalars.test.ts +877 -0
  108. package/test/collections/flowMap.states.test.ts +1097 -0
  109. package/test/nodes/async/flowConstantAsync.test.ts +860 -0
  110. package/test/nodes/async/flowDerivationAsync.test.ts +1517 -0
  111. package/test/nodes/async/flowStateAsync.test.ts +1387 -0
  112. package/test/{resource.test.ts → nodes/await/advanced/resource.test.ts} +21 -19
  113. package/test/{resourceAsync.test.ts → nodes/await/advanced/resourceAsync.test.ts} +3 -1
  114. package/test/{stream.test.ts → nodes/await/advanced/stream.test.ts} +30 -28
  115. package/test/{streamAsync.test.ts → nodes/await/advanced/streamAsync.test.ts} +16 -14
  116. package/test/nodes/await/flowConstantAwait.test.ts +643 -0
  117. package/test/nodes/await/flowDerivationAwait.test.ts +1583 -0
  118. package/test/nodes/await/flowStateAwait.test.ts +999 -0
  119. package/test/nodes/mixed/derivation.test.ts +1527 -0
  120. package/test/nodes/sync/flowConstant.test.ts +620 -0
  121. package/test/nodes/sync/flowDerivation.test.ts +1373 -0
  122. package/test/nodes/sync/flowState.test.ts +945 -0
  123. package/test/solid/converters.test.ts +721 -0
  124. package/test/solid/primitives.test.ts +1031 -0
  125. package/tsconfig.json +2 -1
  126. package/vitest.config.ts +7 -1
  127. package/IMPLEMENTATION_GUIDE.md +0 -1578
  128. package/dist/types/advanced/array.d.ts.map +0 -1
  129. package/dist/types/advanced/index.d.ts +0 -9
  130. package/dist/types/advanced/index.d.ts.map +0 -1
  131. package/dist/types/advanced/map.d.ts +0 -166
  132. package/dist/types/advanced/map.d.ts.map +0 -1
  133. package/dist/types/advanced/resource.d.ts +0 -78
  134. package/dist/types/advanced/resource.d.ts.map +0 -1
  135. package/dist/types/advanced/resourceAsync.d.ts +0 -56
  136. package/dist/types/advanced/resourceAsync.d.ts.map +0 -1
  137. package/dist/types/advanced/stream.d.ts +0 -117
  138. package/dist/types/advanced/stream.d.ts.map +0 -1
  139. package/dist/types/advanced/streamAsync.d.ts +0 -97
  140. package/dist/types/advanced/streamAsync.d.ts.map +0 -1
  141. package/dist/types/basic/constant.d.ts +0 -60
  142. package/dist/types/basic/constant.d.ts.map +0 -1
  143. package/dist/types/basic/derivation.d.ts +0 -89
  144. package/dist/types/basic/derivation.d.ts.map +0 -1
  145. package/dist/types/basic/disposable.d.ts +0 -82
  146. package/dist/types/basic/disposable.d.ts.map +0 -1
  147. package/dist/types/basic/effect.d.ts +0 -67
  148. package/dist/types/basic/effect.d.ts.map +0 -1
  149. package/dist/types/basic/index.d.ts +0 -10
  150. package/dist/types/basic/index.d.ts.map +0 -1
  151. package/dist/types/basic/observable.d.ts +0 -83
  152. package/dist/types/basic/observable.d.ts.map +0 -1
  153. package/dist/types/basic/signal.d.ts +0 -69
  154. package/dist/types/basic/signal.d.ts.map +0 -1
  155. package/dist/types/basic/state.d.ts +0 -47
  156. package/dist/types/basic/state.d.ts.map +0 -1
  157. package/dist/types/basic/trackingContext.d.ts +0 -33
  158. package/dist/types/basic/trackingContext.d.ts.map +0 -1
  159. package/dist/types/creators.d.ts +0 -340
  160. package/dist/types/creators.d.ts.map +0 -1
  161. package/src/advanced/array.ts +0 -222
  162. package/src/advanced/index.ts +0 -12
  163. package/src/advanced/map.ts +0 -193
  164. package/src/basic/constant.ts +0 -97
  165. package/src/basic/derivation.ts +0 -147
  166. package/src/basic/disposable.ts +0 -86
  167. package/src/basic/effect.ts +0 -104
  168. package/src/basic/index.ts +0 -9
  169. package/src/basic/observable.ts +0 -109
  170. package/src/basic/signal.ts +0 -145
  171. package/src/basic/state.ts +0 -60
  172. package/src/basic/trackingContext.ts +0 -45
  173. package/src/creators.ts +0 -395
  174. package/test/array.test.ts +0 -600
  175. package/test/constant.test.ts +0 -44
  176. package/test/derivation.test.ts +0 -539
  177. package/test/effect.test.ts +0 -29
  178. package/test/map.test.ts +0 -240
  179. package/test/signal.test.ts +0 -72
  180. package/test/state.test.ts +0 -212
@@ -0,0 +1,1097 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { effect, FlowGraph, type FlowState, map, state } from "#package";
3
+
4
+ describe("FlowMap", () => {
5
+ describe("unit", () => {
6
+ describe("initialization", () => {
7
+ it("should initialize with provided Record of FlowState values", async () => {
8
+ const $map = map({
9
+ key1: state(1),
10
+ key2: state(2),
11
+ key3: state(3),
12
+ });
13
+
14
+ const values = await Promise.all(
15
+ Array.from((await $map.pick()).entries()).map(([_, value]) =>
16
+ value.pick(),
17
+ ),
18
+ );
19
+
20
+ expect(values).toEqual([1, 2, 3]);
21
+ });
22
+
23
+ it("should initialize with provided Map of FlowState values", async () => {
24
+ const $value1 = state(1);
25
+ const $value2 = state(2);
26
+ const initialMap = new Map([
27
+ ["key1", $value1],
28
+ ["key2", $value2],
29
+ ]);
30
+ const $map = map(initialMap);
31
+
32
+ expect(Array.from((await $map.pick()).entries())).toEqual([
33
+ ["key1", $value1],
34
+ ["key2", $value2],
35
+ ]);
36
+ });
37
+
38
+ it("should initialize with empty map when no value provided", async () => {
39
+ const $map = map<string, FlowState<number>>();
40
+
41
+ expect(Array.from((await $map.pick()).entries())).toEqual([]);
42
+ expect((await $map.pick()).size).toBe(0);
43
+ });
44
+
45
+ it("should initialize $lastAction with set action on creation", async () => {
46
+ const $value1 = state(1);
47
+ const $value2 = state(2);
48
+ const $map = map({
49
+ key1: $value1,
50
+ key2: $value2,
51
+ });
52
+ const action = await $map.$lastAction.pick();
53
+ expect(action.type).toBe("set");
54
+ if (action.type === "set") {
55
+ expect(action.map.size).toBe(2);
56
+ expect(action.map.get("key1")).toBe($value1);
57
+ expect(action.map.get("key2")).toBe($value2);
58
+ }
59
+ });
60
+
61
+ it("should initialize $lastAction with empty map when no value provided", async () => {
62
+ const $map = map<string, FlowState<number>>();
63
+ const action = await $map.$lastAction.pick();
64
+ expect(action.type).toBe("set");
65
+ if (action.type === "set") {
66
+ expect(action.map.size).toBe(0);
67
+ }
68
+ });
69
+ });
70
+
71
+ describe("disposal", () => {
72
+ it("should have disposed property set to false initially and true after disposal", () => {
73
+ const $map = map({
74
+ key1: state(1),
75
+ key2: state(2),
76
+ });
77
+ expect($map.disposed).toBe(false);
78
+ $map.dispose();
79
+ expect($map.disposed).toBe(true);
80
+ });
81
+
82
+ it("should throw error when disposed twice", () => {
83
+ const $map = map({
84
+ key1: state(1),
85
+ key2: state(2),
86
+ });
87
+ $map.dispose();
88
+ expect(() => $map.dispose()).toThrow(
89
+ "[PicoFlow] Primitive is disposed",
90
+ );
91
+ });
92
+
93
+ it("should throw when pick is called after disposal", async () => {
94
+ const $map = map<string, FlowState<number>>({
95
+ key1: state(1),
96
+ key2: state(2),
97
+ key3: state(3),
98
+ });
99
+ $map.dispose();
100
+ await expect($map.pick()).rejects.toThrow(
101
+ "[PicoFlow] Primitive is disposed",
102
+ );
103
+ });
104
+
105
+ it("should throw when set is called after disposal", async () => {
106
+ const $map = map<string, FlowState<number>>({
107
+ key1: state(1),
108
+ });
109
+ $map.dispose();
110
+ await expect($map.set(new Map([["key2", state(2)]]))).rejects.toThrow(
111
+ "[PicoFlow] Primitive is disposed",
112
+ );
113
+ });
114
+
115
+ it("should throw when add is called after disposal", async () => {
116
+ const $map = map<string, FlowState<number>>();
117
+ $map.dispose();
118
+ await expect($map.add("key1", state(2))).rejects.toThrow(
119
+ "[PicoFlow] Primitive is disposed",
120
+ );
121
+ });
122
+
123
+ it("should throw when update is called after disposal", async () => {
124
+ const $map = map<string, FlowState<number>>();
125
+ await $map.add("key1", state(1));
126
+ $map.dispose();
127
+ await expect($map.update("key1", state(2))).rejects.toThrow(
128
+ "[PicoFlow] Primitive is disposed",
129
+ );
130
+ });
131
+
132
+ it("should throw when delete is called after disposal", async () => {
133
+ const $map = map<string, FlowState<number>>({
134
+ key1: state(1),
135
+ key2: state(2),
136
+ key3: state(3),
137
+ });
138
+ $map.dispose();
139
+ await expect($map.delete("key1")).rejects.toThrow(
140
+ "[PicoFlow] Primitive is disposed",
141
+ );
142
+ });
143
+
144
+ it("should throw when clear is called after disposal", async () => {
145
+ const $map = map<string, FlowState<number>>({
146
+ key1: state(1),
147
+ key2: state(2),
148
+ });
149
+ $map.dispose();
150
+ await expect($map.clear()).rejects.toThrow(
151
+ "[PicoFlow] Primitive is disposed",
152
+ );
153
+ });
154
+
155
+ it("should dispose values when FlowMap is disposed", () => {
156
+ const $map = map<string, FlowState<number>>();
157
+ const $value1 = state(1);
158
+ const $value2 = state(2);
159
+ const $value3 = state(3);
160
+
161
+ $map.add("key1", $value1);
162
+ $map.add("key2", $value2);
163
+ $map.add("key3", $value3);
164
+
165
+ expect($value1.disposed).toBe(false);
166
+ expect($value2.disposed).toBe(false);
167
+ expect($value3.disposed).toBe(false);
168
+
169
+ $map.dispose();
170
+
171
+ expect($value1.disposed).toBe(true);
172
+ expect($value2.disposed).toBe(true);
173
+ expect($value3.disposed).toBe(true);
174
+ });
175
+
176
+ it("should dispose values with provided options when FlowMap is disposed", () => {
177
+ const $map = map<string, FlowState<number>>();
178
+ const $value1 = state(1);
179
+ const $value2 = state(2);
180
+
181
+ $map.add("key1", $value1);
182
+ $map.add("key2", $value2);
183
+
184
+ const $effect = effect((t) => {
185
+ $value1.get(t);
186
+ });
187
+
188
+ expect($effect.disposed).toBe(false);
189
+
190
+ $map.dispose({ self: true });
191
+
192
+ expect($value1.disposed).toBe(true);
193
+ expect($value2.disposed).toBe(true);
194
+ expect($effect.disposed).toBe(false);
195
+
196
+ $effect.dispose();
197
+ });
198
+
199
+ it("should handle non-disposable values gracefully", async () => {
200
+ const $map = map<string, FlowState<number>>();
201
+ await $map.add("key1", state(1));
202
+ await $map.add("key2", state(2));
203
+ await $map.add("key3", state(3));
204
+
205
+ expect((await $map.pick()).size).toBe(3);
206
+
207
+ expect(() => $map.dispose()).not.toThrow();
208
+ });
209
+ });
210
+
211
+ describe("set", () => {
212
+ it("should replace entire map when set is called", async () => {
213
+ const $map = map<string, FlowState<number>>({
214
+ key1: state(1),
215
+ key2: state(2),
216
+ });
217
+ const $newValue1 = state(3);
218
+ const $newValue2 = state(4);
219
+ const newMap = new Map([
220
+ ["key3", $newValue1],
221
+ ["key4", $newValue2],
222
+ ]);
223
+
224
+ await $map.set(newMap);
225
+ expect(Array.from((await $map.pick()).entries())).toEqual([
226
+ ["key3", $newValue1],
227
+ ["key4", $newValue2],
228
+ ]);
229
+ });
230
+
231
+ it("should dispose values when set replaces map", async () => {
232
+ const $oldValue1 = state(1);
233
+ const $oldValue2 = state(2);
234
+ const $map = map(
235
+ new Map([
236
+ ["key1", $oldValue1],
237
+ ["key2", $oldValue2],
238
+ ]),
239
+ );
240
+
241
+ expect($oldValue1.disposed).toBe(false);
242
+ expect($oldValue2.disposed).toBe(false);
243
+
244
+ await $map.set(
245
+ new Map([
246
+ ["key3", state(3)],
247
+ ["key4", state(4)],
248
+ ]),
249
+ );
250
+
251
+ expect($oldValue1.disposed).toBe(true);
252
+ expect($oldValue2.disposed).toBe(true);
253
+ });
254
+
255
+ it("should dispose values with self option when set replaces map", async () => {
256
+ const $oldValue = state(1);
257
+ const $map = map(new Map([["key1", $oldValue]]));
258
+
259
+ const $effect = effect((t) => {
260
+ $oldValue.get(t);
261
+ });
262
+
263
+ expect($effect.disposed).toBe(false);
264
+
265
+ await $map.set(new Map([["key2", state(2)]]));
266
+
267
+ expect($oldValue.disposed).toBe(true);
268
+ expect($effect.disposed).toBe(false);
269
+
270
+ $effect.dispose();
271
+ });
272
+
273
+ it("should update $lastAction with set action when set is called", async () => {
274
+ const $map = map<string, FlowState<number>>({
275
+ key1: state(1),
276
+ key2: state(2),
277
+ });
278
+ const $newValue1 = state(3);
279
+ const $newValue2 = state(4);
280
+ const newMap = new Map([
281
+ ["key3", $newValue1],
282
+ ["key4", $newValue2],
283
+ ]);
284
+
285
+ await $map.set(newMap);
286
+ const action = await $map.$lastAction.pick();
287
+ expect(action).toEqual({
288
+ type: "set",
289
+ map: newMap,
290
+ });
291
+ });
292
+ });
293
+
294
+ describe("add", () => {
295
+ it("should add key-value pair to map", async () => {
296
+ const $map = map<string, FlowState<number>>();
297
+
298
+ await $map.add("key1", state(1));
299
+ expect(await (await $map.pick()).get("key1")?.pick()).toBe(1);
300
+ expect((await $map.pick()).size).toBe(1);
301
+ });
302
+
303
+ it("should add multiple keys successively", async () => {
304
+ const $map = map<string, FlowState<number>>();
305
+
306
+ await $map.add("key1", state(1));
307
+ await $map.add("key2", state(2));
308
+ await $map.add("key3", state(3));
309
+
310
+ expect((await $map.pick()).size).toBe(3);
311
+ expect(await (await $map.pick()).get("key1")?.pick()).toBe(1);
312
+ expect(await (await $map.pick()).get("key2")?.pick()).toBe(2);
313
+ expect(await (await $map.pick()).get("key3")?.pick()).toBe(3);
314
+ });
315
+
316
+ it("should throw when key already exists", async () => {
317
+ const $map = map<string, FlowState<number>>();
318
+ await $map.add("key1", state(1));
319
+ await expect($map.add("key1", state(2))).rejects.toThrow(
320
+ "[PicoFlow] Key already exists",
321
+ );
322
+ });
323
+
324
+ it("should update $lastAction with add action when add is called", async () => {
325
+ const $map = map<string, FlowState<number>>();
326
+ const $value = state(1);
327
+ await $map.add("key1", $value);
328
+ const action = await $map.$lastAction.pick();
329
+ expect(action).toEqual({
330
+ type: "add",
331
+ key: "key1",
332
+ value: $value,
333
+ });
334
+ });
335
+
336
+ it("should have FlowState value in $lastAction when add is called", async () => {
337
+ const $map = map<string, FlowState<number>>();
338
+ const $value = state(100);
339
+ await $map.add("key1", $value);
340
+ const action = await $map.$lastAction.pick();
341
+ expect(action.type).toBe("add");
342
+ if (action.type === "add") {
343
+ expect(action.value).toBe($value);
344
+ expect(await action.value.pick()).toBe(100);
345
+ }
346
+ });
347
+
348
+ it("should trigger reactivity when add is called", async () => {
349
+ const $map = map<string, FlowState<number>>();
350
+ const effectFn = vi.fn();
351
+ effect((t) => effectFn($map.get(t)));
352
+
353
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
354
+
355
+ await $map.add("key1", state(1));
356
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
357
+ });
358
+ });
359
+
360
+ describe("update", () => {
361
+ it("should update existing key-value pair", async () => {
362
+ const $map = map<string, FlowState<number>>();
363
+ await $map.add("key1", state(1));
364
+
365
+ const $newValue = state(2);
366
+ await $map.update("key1", $newValue);
367
+ expect(await (await $map.pick()).get("key1")?.pick()).toBe(2);
368
+ });
369
+
370
+ it("should update multiple keys successively", async () => {
371
+ const $map = map<string, FlowState<number>>();
372
+ await $map.add("key1", state(1));
373
+ await $map.add("key2", state(2));
374
+
375
+ await $map.update("key1", state(10));
376
+ await $map.update("key2", state(20));
377
+
378
+ expect(await (await $map.pick()).get("key1")?.pick()).toBe(10);
379
+ expect(await (await $map.pick()).get("key2")?.pick()).toBe(20);
380
+ });
381
+
382
+ it("should throw when key does not exist", async () => {
383
+ const $map = map<string, FlowState<number>>();
384
+ await expect($map.update("key1", state(1))).rejects.toThrow(
385
+ "[PicoFlow] Key does not exist",
386
+ );
387
+ });
388
+
389
+ it("should update $lastAction with update action when update is called", async () => {
390
+ const $map = map<string, FlowState<number>>();
391
+ await $map.add("key1", state(1));
392
+ const $newValue = state(2);
393
+ await $map.update("key1", $newValue);
394
+ const action = await $map.$lastAction.pick();
395
+ expect(action).toEqual({
396
+ type: "update",
397
+ key: "key1",
398
+ value: $newValue,
399
+ });
400
+ });
401
+
402
+ it("should have FlowState value in $lastAction when update is called", async () => {
403
+ const $map = map<string, FlowState<number>>();
404
+ await $map.add("key1", state(1));
405
+ const $newValue = state(200);
406
+ await $map.update("key1", $newValue);
407
+ const action = await $map.$lastAction.pick();
408
+ expect(action.type).toBe("update");
409
+ if (action.type === "update") {
410
+ expect(action.value).toBe($newValue);
411
+ expect(await action.value.pick()).toBe(200);
412
+ }
413
+ });
414
+
415
+ it("should dispose old value when update is called", async () => {
416
+ const $map = map<string, FlowState<number>>();
417
+ const $oldValue = state(1);
418
+ await $map.add("key1", $oldValue);
419
+
420
+ const $newValue = state(2);
421
+ await $map.update("key1", $newValue);
422
+
423
+ expect($oldValue.disposed).toBe(true);
424
+ expect($newValue.disposed).toBe(false);
425
+ });
426
+
427
+ it("should trigger reactivity when update is called", async () => {
428
+ const $map = map<string, FlowState<number>>();
429
+ await $map.add("key1", state(1));
430
+ const effectFn = vi.fn();
431
+ effect((t) => effectFn($map.get(t)));
432
+
433
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
434
+
435
+ await $map.update("key1", state(2));
436
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
437
+ });
438
+ });
439
+
440
+ describe("delete", () => {
441
+ it("should delete key from map", async () => {
442
+ const $map = map<string, FlowState<number>>();
443
+ await $map.add("key1", state(1));
444
+ await $map.add("key2", state(2));
445
+
446
+ await $map.delete("key1");
447
+ expect(await (await $map.pick()).get("key1")?.pick()).toBe(undefined);
448
+ expect(await (await $map.pick()).get("key2")?.pick()).toBe(2);
449
+ expect((await $map.pick()).size).toBe(1);
450
+ });
451
+
452
+ it("should delete multiple keys successively", async () => {
453
+ const $map = map<string, FlowState<number>>();
454
+ await $map.add("key1", state(1));
455
+ await $map.add("key2", state(2));
456
+ await $map.add("key3", state(3));
457
+
458
+ await $map.delete("key1");
459
+ await $map.delete("key2");
460
+
461
+ expect((await $map.pick()).size).toBe(1);
462
+ expect(await (await $map.pick()).get("key3")?.pick()).toBe(3);
463
+ });
464
+
465
+ it("should throw when key does not exist", async () => {
466
+ const $map = map<string, FlowState<number>>();
467
+ await expect($map.delete("key1")).rejects.toThrow(
468
+ "[PicoFlow] Key does not exist",
469
+ );
470
+ });
471
+
472
+ it("should update $lastAction with delete action when delete is called", async () => {
473
+ const $map = map<string, FlowState<number>>();
474
+ const $value = state(1);
475
+ await $map.add("key1", $value);
476
+ await $map.delete("key1");
477
+ const action = await $map.$lastAction.pick();
478
+ expect(action.type).toBe("delete");
479
+ if (action.type === "delete") {
480
+ expect(action.key).toBe("key1");
481
+ expect(action.value).toBe($value);
482
+ }
483
+ });
484
+
485
+ it("should have disposed FlowState value in $lastAction when delete is called", async () => {
486
+ const $map = map<string, FlowState<number>>();
487
+ const $value = state(300);
488
+ await $map.add("key1", $value);
489
+ await $map.delete("key1");
490
+ const action = await $map.$lastAction.pick();
491
+ expect(action.type).toBe("delete");
492
+ if (action.type === "delete") {
493
+ expect(action.value).toBe($value);
494
+ expect(action.value.disposed).toBe(true);
495
+ }
496
+ });
497
+
498
+ it("should dispose value with self option when delete is called", async () => {
499
+ const $value = state(1);
500
+ const $map = map(new Map([["key1", $value]]));
501
+
502
+ const $effect = effect((t) => {
503
+ $value.get(t);
504
+ });
505
+
506
+ expect($effect.disposed).toBe(false);
507
+
508
+ await $map.delete("key1");
509
+
510
+ expect($value.disposed).toBe(true);
511
+ expect($effect.disposed).toBe(false);
512
+
513
+ $effect.dispose();
514
+ });
515
+
516
+ it("should trigger reactivity when delete is called", async () => {
517
+ const $map = map<string, FlowState<number>>();
518
+ await $map.add("key1", state(1));
519
+ const effectFn = vi.fn();
520
+ effect((t) => effectFn($map.get(t)));
521
+
522
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
523
+
524
+ await $map.delete("key1");
525
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
526
+ });
527
+ });
528
+
529
+ describe("clear", () => {
530
+ it("should remove all entries from map", async () => {
531
+ const $map = map<string, FlowState<number>>({
532
+ key1: state(1),
533
+ key2: state(2),
534
+ key3: state(3),
535
+ });
536
+ expect((await $map.pick()).size).toBe(3);
537
+
538
+ await $map.clear();
539
+ expect((await $map.pick()).size).toBe(0);
540
+ });
541
+
542
+ it("should dispose all values when clear is called", async () => {
543
+ const $value1 = state(1);
544
+ const $value2 = state(2);
545
+ const $value3 = state(3);
546
+ const $map = map(
547
+ new Map([
548
+ ["key1", $value1],
549
+ ["key2", $value2],
550
+ ["key3", $value3],
551
+ ]),
552
+ );
553
+
554
+ expect($value1.disposed).toBe(false);
555
+ expect($value2.disposed).toBe(false);
556
+ expect($value3.disposed).toBe(false);
557
+
558
+ await $map.clear();
559
+
560
+ expect($value1.disposed).toBe(true);
561
+ expect($value2.disposed).toBe(true);
562
+ expect($value3.disposed).toBe(true);
563
+ });
564
+
565
+ it("should dispose values with self option when clear is called", async () => {
566
+ const $value = state(1);
567
+ const $map = map(
568
+ new Map([
569
+ ["key1", $value],
570
+ ["key2", state(2)],
571
+ ]),
572
+ );
573
+
574
+ const $effect = effect((t) => {
575
+ $value.get(t);
576
+ });
577
+
578
+ expect($effect.disposed).toBe(false);
579
+
580
+ await $map.clear();
581
+
582
+ expect($value.disposed).toBe(true);
583
+ expect($effect.disposed).toBe(false);
584
+
585
+ $effect.dispose();
586
+ });
587
+
588
+ it("should update $lastAction with clear action when clear is called", async () => {
589
+ const $map = map<string, FlowState<number>>({
590
+ key1: state(1),
591
+ key2: state(2),
592
+ });
593
+ await $map.clear();
594
+ const action = await $map.$lastAction.pick();
595
+ expect(action).toEqual({ type: "clear" });
596
+ });
597
+ });
598
+
599
+ describe("$lastAction", () => {
600
+ describe("set action", () => {
601
+ it("should initialize $lastAction with set action on creation", async () => {
602
+ const $value1 = state(1);
603
+ const $value2 = state(2);
604
+ const $map = map({
605
+ key1: $value1,
606
+ key2: $value2,
607
+ });
608
+ const action = await $map.$lastAction.pick();
609
+ expect(action).toEqual({
610
+ type: "set",
611
+ map: expect.any(Map),
612
+ });
613
+ if (action.type === "set") {
614
+ expect(action.map.get("key1")).toBe($value1);
615
+ expect(action.map.get("key2")).toBe($value2);
616
+ }
617
+ });
618
+
619
+ it("should update $lastAction with set action when set is called", async () => {
620
+ const $map = map<string, FlowState<number>>({
621
+ key1: state(1),
622
+ });
623
+ const $newValue = state(2);
624
+ const newMap = new Map([["key2", $newValue]]);
625
+ await $map.set(newMap);
626
+ const action = await $map.$lastAction.pick();
627
+ expect(action).toEqual({
628
+ type: "set",
629
+ map: newMap,
630
+ });
631
+ });
632
+
633
+ it("should have correct structure for set action (type + map with FlowState)", async () => {
634
+ const $value = state(1);
635
+ const $map = map({ key1: $value });
636
+ const action = await $map.$lastAction.pick();
637
+ expect(action).toHaveProperty("type", "set");
638
+ if (action.type === "set") {
639
+ expect(action).toHaveProperty("map");
640
+ expect(action.map).toBeInstanceOf(Map);
641
+ expect(action.map.get("key1")).toBe($value);
642
+ }
643
+ });
644
+ });
645
+
646
+ describe("add action", () => {
647
+ it("should update $lastAction with add action when add is called", async () => {
648
+ const $map = map<string, FlowState<number>>();
649
+ const $value = state(1);
650
+ await $map.add("key1", $value);
651
+ const action = await $map.$lastAction.pick();
652
+ expect(action).toEqual({
653
+ type: "add",
654
+ key: "key1",
655
+ value: $value,
656
+ });
657
+ });
658
+
659
+ it("should have correct structure for add action (type + key + FlowState value)", async () => {
660
+ const $map = map<string, FlowState<number>>();
661
+ const $value = state(100);
662
+ await $map.add("key1", $value);
663
+ const action = await $map.$lastAction.pick();
664
+ expect(action).toHaveProperty("type", "add");
665
+ if (action.type === "add") {
666
+ expect(action).toHaveProperty("key");
667
+ expect(action).toHaveProperty("value");
668
+ expect(action.key).toBe("key1");
669
+ expect(action.value).toBe($value);
670
+ expect(await action.value.pick()).toBe(100);
671
+ }
672
+ });
673
+ });
674
+
675
+ describe("update action", () => {
676
+ it("should update $lastAction with update action when update is called", async () => {
677
+ const $map = map<string, FlowState<number>>();
678
+ await $map.add("key1", state(1));
679
+ const $newValue = state(2);
680
+ await $map.update("key1", $newValue);
681
+ const action = await $map.$lastAction.pick();
682
+ expect(action).toEqual({
683
+ type: "update",
684
+ key: "key1",
685
+ value: $newValue,
686
+ });
687
+ });
688
+
689
+ it("should have correct structure for update action (type + key + FlowState value)", async () => {
690
+ const $map = map<string, FlowState<number>>();
691
+ await $map.add("key1", state(1));
692
+ const $newValue = state(200);
693
+ await $map.update("key1", $newValue);
694
+ const action = await $map.$lastAction.pick();
695
+ expect(action).toHaveProperty("type", "update");
696
+ if (action.type === "update") {
697
+ expect(action).toHaveProperty("key");
698
+ expect(action).toHaveProperty("value");
699
+ expect(action.key).toBe("key1");
700
+ expect(action.value).toBe($newValue);
701
+ expect(await action.value.pick()).toBe(200);
702
+ }
703
+ });
704
+ });
705
+
706
+ describe("delete action", () => {
707
+ it("should update $lastAction with delete action when delete is called", async () => {
708
+ const $map = map<string, FlowState<number>>();
709
+ const $value = state(1);
710
+ await $map.add("key1", $value);
711
+ await $map.delete("key1");
712
+ const action = await $map.$lastAction.pick();
713
+ expect(action).toEqual({
714
+ type: "delete",
715
+ key: "key1",
716
+ value: $value,
717
+ });
718
+ });
719
+
720
+ it("should have correct structure for delete action (type + key + disposed FlowState value)", async () => {
721
+ const $map = map<string, FlowState<number>>();
722
+ const $value = state(300);
723
+ await $map.add("key1", $value);
724
+ await $map.delete("key1");
725
+ const action = await $map.$lastAction.pick();
726
+ expect(action).toHaveProperty("type", "delete");
727
+ if (action.type === "delete") {
728
+ expect(action).toHaveProperty("key");
729
+ expect(action).toHaveProperty("value");
730
+ expect(action.key).toBe("key1");
731
+ expect(action.value).toBe($value);
732
+ expect(action.value.disposed).toBe(true);
733
+ }
734
+ });
735
+ });
736
+
737
+ describe("clear action", () => {
738
+ it("should update $lastAction with clear action when clear is called", async () => {
739
+ const $map = map<string, FlowState<number>>({
740
+ key1: state(1),
741
+ key2: state(2),
742
+ });
743
+ await $map.clear();
744
+ const action = await $map.$lastAction.pick();
745
+ expect(action).toEqual({
746
+ type: "clear",
747
+ });
748
+ });
749
+
750
+ it("should have correct structure for clear action (type only)", async () => {
751
+ const $map = map<string, FlowState<number>>({
752
+ key1: state(1),
753
+ });
754
+ await $map.clear();
755
+ const action = await $map.$lastAction.pick();
756
+ expect(action).toHaveProperty("type", "clear");
757
+ if (action.type === "clear") {
758
+ expect(action).not.toHaveProperty("key");
759
+ expect(action).not.toHaveProperty("value");
760
+ expect(action).not.toHaveProperty("map");
761
+ }
762
+ });
763
+ });
764
+ });
765
+
766
+ describe("edge cases", () => {
767
+ it("should handle multiple rapid operations", async () => {
768
+ const $map = map<string, FlowState<number>>();
769
+ await $map.add("key1", state(1));
770
+ await $map.add("key2", state(2));
771
+ await $map.add("key3", state(3));
772
+ await $map.update("key1", state(10));
773
+ await $map.delete("key2");
774
+ await $map.add("key4", state(4));
775
+ await $map.clear();
776
+ await $map.add("key5", state(5));
777
+
778
+ expect((await $map.pick()).size).toBe(1);
779
+ expect(await (await $map.pick()).get("key5")?.pick()).toBe(5);
780
+ });
781
+
782
+ it("should handle empty map operations", async () => {
783
+ const $map = map<string, FlowState<number>>();
784
+ expect((await $map.pick()).size).toBe(0);
785
+
786
+ await $map.add("key1", state(1));
787
+ expect((await $map.pick()).size).toBe(1);
788
+
789
+ await $map.delete("key1");
790
+ expect((await $map.pick()).size).toBe(0);
791
+ });
792
+
793
+ it("should handle map with single entry", async () => {
794
+ const $map = map<string, FlowState<number>>({
795
+ key1: state(1),
796
+ });
797
+ expect((await $map.pick()).size).toBe(1);
798
+
799
+ await $map.update("key1", state(2));
800
+ expect(await (await $map.pick()).get("key1")?.pick()).toBe(2);
801
+
802
+ await $map.delete("key1");
803
+ expect((await $map.pick()).size).toBe(0);
804
+ });
805
+
806
+ it("should handle map with many entries", async () => {
807
+ const $map = map<string, FlowState<number>>();
808
+ for (let i = 0; i < 100; i++) {
809
+ await $map.add(`key${i}`, state(i));
810
+ }
811
+ expect((await $map.pick()).size).toBe(100);
812
+
813
+ for (let i = 0; i < 50; i++) {
814
+ await $map.delete(`key${i}`);
815
+ }
816
+ expect((await $map.pick()).size).toBe(50);
817
+ });
818
+
819
+ it("should handle disposed values in map", async () => {
820
+ const $value = state(1);
821
+ const $map = map(new Map([["key1", $value]]));
822
+
823
+ await $map.delete("key1");
824
+
825
+ expect($value.disposed).toBe(true);
826
+ expect((await $map.pick()).size).toBe(0);
827
+ });
828
+
829
+ it("should handle effects on FlowState values in map", async () => {
830
+ const $value = state(1);
831
+ const $map = map(new Map([["key1", $value]]));
832
+ const effectFn = vi.fn();
833
+
834
+ effect((t) => {
835
+ const mapValue = $map.get(t).get("key1");
836
+ if (mapValue) {
837
+ effectFn(mapValue.get(t));
838
+ }
839
+ });
840
+
841
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
842
+ expect(effectFn).toHaveBeenLastCalledWith(1);
843
+
844
+ await $value.set(2);
845
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
846
+ expect(effectFn).toHaveBeenLastCalledWith(2);
847
+ });
848
+ });
849
+ });
850
+
851
+ describe("integration", () => {
852
+ beforeEach(() => {
853
+ FlowGraph.clear();
854
+ });
855
+
856
+ describe("with effects", () => {
857
+ describe("reactivity", () => {
858
+ it("should call effect when initialized", async () => {
859
+ const $map = map<string, FlowState<number>>({
860
+ key1: state(1),
861
+ key2: state(2),
862
+ });
863
+ const effectFn = vi.fn();
864
+ effect((t) => effectFn($map.get(t)));
865
+
866
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
867
+ expect(effectFn).toHaveBeenLastCalledWith(
868
+ expect.objectContaining({
869
+ size: 2,
870
+ }),
871
+ );
872
+ });
873
+
874
+ it("should call effect when updated with set", async () => {
875
+ const $map = map<string, FlowState<number>>({
876
+ key1: state(1),
877
+ });
878
+ const effectFn = vi.fn();
879
+ effect((t) => effectFn($map.get(t)));
880
+
881
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
882
+
883
+ const $newValue = state(2);
884
+ await $map.set(new Map([["key2", $newValue]]));
885
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
886
+ expect(effectFn).toHaveBeenLastCalledWith(
887
+ expect.objectContaining({
888
+ size: 1,
889
+ }),
890
+ );
891
+ });
892
+
893
+ it("should call effect when updated with add", async () => {
894
+ const $map = map<string, FlowState<number>>();
895
+ const effectFn = vi.fn();
896
+ effect((t) => effectFn($map.get(t)));
897
+
898
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
899
+ expect(effectFn).toHaveBeenLastCalledWith(new Map());
900
+
901
+ const $key1Value = state(1);
902
+ await $map.add("key1", $key1Value);
903
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
904
+ expect(effectFn).toHaveBeenLastCalledWith(
905
+ new Map([["key1", $key1Value]]),
906
+ );
907
+
908
+ const $key2Value = state(2);
909
+ await $map.add("key2", $key2Value);
910
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
911
+ expect(effectFn).toHaveBeenLastCalledWith(
912
+ new Map([
913
+ ["key1", $key1Value],
914
+ ["key2", $key2Value],
915
+ ]),
916
+ );
917
+ });
918
+
919
+ it("should call effect when updated with update", async () => {
920
+ const $map = map<string, FlowState<number>>();
921
+ await $map.add("key1", state(1));
922
+ const effectFn = vi.fn();
923
+ effect((t) => effectFn($map.get(t)));
924
+
925
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
926
+
927
+ const $newValue = state(2);
928
+ await $map.update("key1", $newValue);
929
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
930
+ expect(effectFn).toHaveBeenLastCalledWith(
931
+ new Map([["key1", $newValue]]),
932
+ );
933
+ });
934
+
935
+ it("should call effect when updated with delete", async () => {
936
+ const $map = map<string, FlowState<number>>();
937
+ await $map.add("key1", state(1));
938
+ await $map.add("key2", state(2));
939
+ const effectFn = vi.fn();
940
+ effect((t) => effectFn($map.get(t)));
941
+
942
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
943
+
944
+ await $map.delete("key1");
945
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
946
+ const mapAfterDelete = effectFn.mock.calls[1][0];
947
+ expect(mapAfterDelete.size).toBe(1);
948
+ expect(mapAfterDelete.has("key2")).toBe(true);
949
+ });
950
+
951
+ it("should call effect when updated with clear", async () => {
952
+ const $map = map<string, FlowState<number>>({
953
+ key1: state(1),
954
+ key2: state(2),
955
+ });
956
+ const effectFn = vi.fn();
957
+ effect((t) => effectFn($map.get(t)));
958
+
959
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
960
+
961
+ await $map.clear();
962
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
963
+ expect(effectFn).toHaveBeenLastCalledWith(new Map());
964
+ });
965
+
966
+ it("should call effect when FlowState values in map are updated", async () => {
967
+ const $value = state(1);
968
+ const $map = map(new Map([["key1", $value]]));
969
+ const effectFn = vi.fn();
970
+
971
+ effect((t) => {
972
+ const mapValue = $map.get(t).get("key1");
973
+ if (mapValue) {
974
+ effectFn(mapValue.get(t));
975
+ }
976
+ });
977
+
978
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
979
+ expect(effectFn).toHaveBeenLastCalledWith(1);
980
+
981
+ await $value.set(2);
982
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
983
+ expect(effectFn).toHaveBeenLastCalledWith(2);
984
+ });
985
+ });
986
+
987
+ describe("$lastAction", () => {
988
+ it("should call effect when $lastAction is updated with set action", async () => {
989
+ const $map = map<string, FlowState<number>>({
990
+ key1: state(1),
991
+ });
992
+ const effectFn = vi.fn();
993
+ effect((t) => effectFn($map.$lastAction.get(t)));
994
+
995
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
996
+
997
+ const $newValue = state(2);
998
+ await $map.set(new Map([["key2", $newValue]]));
999
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1000
+ expect(effectFn).toHaveBeenLastCalledWith({
1001
+ type: "set",
1002
+ map: expect.any(Map),
1003
+ });
1004
+ });
1005
+
1006
+ it("should call effect when $lastAction is updated with add action", async () => {
1007
+ const $map = map<string, FlowState<number>>();
1008
+ const effectFn = vi.fn();
1009
+ effect((t) => effectFn($map.$lastAction.get(t)));
1010
+
1011
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1012
+
1013
+ const $value = state(1);
1014
+ await $map.add("key1", $value);
1015
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1016
+ expect(effectFn).toHaveBeenLastCalledWith({
1017
+ type: "add",
1018
+ key: "key1",
1019
+ value: $value,
1020
+ });
1021
+ });
1022
+
1023
+ it("should call effect when $lastAction is updated with update action", async () => {
1024
+ const $map = map<string, FlowState<number>>();
1025
+ await $map.add("key1", state(1));
1026
+ const effectFn = vi.fn();
1027
+ effect((t) => effectFn($map.$lastAction.get(t)));
1028
+
1029
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1030
+
1031
+ const $newValue = state(2);
1032
+ await $map.update("key1", $newValue);
1033
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1034
+ expect(effectFn).toHaveBeenLastCalledWith({
1035
+ type: "update",
1036
+ key: "key1",
1037
+ value: $newValue,
1038
+ });
1039
+ });
1040
+
1041
+ it("should call effect when $lastAction is updated with delete action", async () => {
1042
+ const $map = map<string, FlowState<number>>();
1043
+ const $value = state(1);
1044
+ await $map.add("key1", $value);
1045
+ const effectFn = vi.fn();
1046
+ effect((t) => effectFn($map.$lastAction.get(t)));
1047
+
1048
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1049
+
1050
+ await $map.delete("key1");
1051
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1052
+ expect(effectFn).toHaveBeenLastCalledWith({
1053
+ type: "delete",
1054
+ key: "key1",
1055
+ value: $value,
1056
+ });
1057
+ });
1058
+
1059
+ it("should call effect when $lastAction is updated with clear action", async () => {
1060
+ const $map = map<string, FlowState<number>>({
1061
+ key1: state(1),
1062
+ key2: state(2),
1063
+ });
1064
+ const effectFn = vi.fn();
1065
+ effect((t) => effectFn($map.$lastAction.get(t)));
1066
+
1067
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1068
+
1069
+ await $map.clear();
1070
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1071
+ expect(effectFn).toHaveBeenLastCalledWith({ type: "clear" });
1072
+ });
1073
+
1074
+ it("should call effect when FlowState value in $lastAction is updated", async () => {
1075
+ const $map = map<string, FlowState<number>>();
1076
+ const $value = state(1);
1077
+ await $map.add("key1", $value);
1078
+ const effectFn = vi.fn();
1079
+
1080
+ effect((t) => {
1081
+ const action = $map.$lastAction.get(t);
1082
+ if (action.type === "add") {
1083
+ effectFn(action.value.get(t));
1084
+ }
1085
+ });
1086
+
1087
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1088
+ expect(effectFn).toHaveBeenLastCalledWith(1);
1089
+
1090
+ await $value.set(2);
1091
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1092
+ expect(effectFn).toHaveBeenLastCalledWith(2);
1093
+ });
1094
+ });
1095
+ });
1096
+ });
1097
+ });