@ersbeth/picoflow 1.0.1 → 1.1.1

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 (183) 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 +1155 -582
  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 +2 -4
  54. package/dist/types/index.d.ts.map +1 -1
  55. package/dist/types/solid/converters.d.ts +34 -45
  56. package/dist/types/solid/converters.d.ts.map +1 -1
  57. package/dist/types/solid/index.d.ts +2 -2
  58. package/dist/types/solid/index.d.ts.map +1 -1
  59. package/dist/types/solid/primitives.d.ts +1 -0
  60. package/dist/types/solid/primitives.d.ts.map +1 -1
  61. package/docs/.vitepress/config.mts +1 -1
  62. package/docs/api/typedoc-sidebar.json +81 -1
  63. package/package.json +60 -58
  64. package/src/flow/base/flowDisposable.ts +71 -0
  65. package/src/flow/base/flowEffect.ts +171 -0
  66. package/src/flow/base/flowGraph.ts +288 -0
  67. package/src/flow/base/flowSignal.ts +207 -0
  68. package/src/flow/base/flowTracker.ts +17 -0
  69. package/src/flow/base/index.ts +6 -0
  70. package/src/flow/base/utils.ts +19 -0
  71. package/src/flow/collections/flowArray.ts +409 -0
  72. package/src/flow/collections/flowMap.ts +398 -0
  73. package/src/flow/collections/index.ts +2 -0
  74. package/src/flow/index.ts +3 -0
  75. package/src/flow/nodes/async/flowConstantAsync.ts +142 -0
  76. package/src/flow/nodes/async/flowDerivationAsync.ts +143 -0
  77. package/src/flow/nodes/async/flowNodeAsync.ts +474 -0
  78. package/src/flow/nodes/async/flowReadonlyAsync.ts +81 -0
  79. package/src/flow/nodes/async/flowStateAsync.ts +116 -0
  80. package/src/flow/nodes/async/index.ts +5 -0
  81. package/src/flow/nodes/await/advanced/index.ts +5 -0
  82. package/src/{advanced → flow/nodes/await/advanced}/resource.ts +37 -3
  83. package/src/{advanced → flow/nodes/await/advanced}/resourceAsync.ts +35 -3
  84. package/src/{advanced → flow/nodes/await/advanced}/stream.ts +40 -2
  85. package/src/{advanced → flow/nodes/await/advanced}/streamAsync.ts +38 -3
  86. package/src/flow/nodes/await/flowConstantAwait.ts +154 -0
  87. package/src/flow/nodes/await/flowDerivationAwait.ts +154 -0
  88. package/src/flow/nodes/await/flowNodeAwait.ts +508 -0
  89. package/src/flow/nodes/await/flowReadonlyAwait.ts +89 -0
  90. package/src/flow/nodes/await/flowStateAwait.ts +130 -0
  91. package/src/flow/nodes/await/index.ts +5 -0
  92. package/src/flow/nodes/index.ts +3 -0
  93. package/src/flow/nodes/sync/flowConstant.ts +111 -0
  94. package/src/flow/nodes/sync/flowDerivation.ts +105 -0
  95. package/src/flow/nodes/sync/flowNode.ts +439 -0
  96. package/src/flow/nodes/sync/flowReadonly.ts +57 -0
  97. package/src/flow/nodes/sync/flowState.ts +101 -0
  98. package/src/flow/nodes/sync/index.ts +5 -0
  99. package/src/index.ts +2 -47
  100. package/src/solid/converters.ts +60 -199
  101. package/src/solid/index.ts +2 -8
  102. package/src/solid/primitives.ts +4 -0
  103. package/test/base/flowEffect.test.ts +108 -0
  104. package/test/base/flowGraph.test.ts +485 -0
  105. package/test/base/flowSignal.test.ts +372 -0
  106. package/test/collections/flowArray.asyncStates.test.ts +1553 -0
  107. package/test/collections/flowArray.scalars.test.ts +1129 -0
  108. package/test/collections/flowArray.states.test.ts +1365 -0
  109. package/test/collections/flowMap.asyncStates.test.ts +1105 -0
  110. package/test/collections/flowMap.scalars.test.ts +877 -0
  111. package/test/collections/flowMap.states.test.ts +1097 -0
  112. package/test/nodes/async/flowConstantAsync.test.ts +860 -0
  113. package/test/nodes/async/flowDerivationAsync.test.ts +1517 -0
  114. package/test/nodes/async/flowStateAsync.test.ts +1387 -0
  115. package/test/{resource.test.ts → nodes/await/advanced/resource.test.ts} +21 -19
  116. package/test/{resourceAsync.test.ts → nodes/await/advanced/resourceAsync.test.ts} +3 -1
  117. package/test/{stream.test.ts → nodes/await/advanced/stream.test.ts} +30 -28
  118. package/test/{streamAsync.test.ts → nodes/await/advanced/streamAsync.test.ts} +16 -14
  119. package/test/nodes/await/flowConstantAwait.test.ts +643 -0
  120. package/test/nodes/await/flowDerivationAwait.test.ts +1583 -0
  121. package/test/nodes/await/flowStateAwait.test.ts +999 -0
  122. package/test/nodes/mixed/derivation.test.ts +1527 -0
  123. package/test/nodes/sync/flowConstant.test.ts +620 -0
  124. package/test/nodes/sync/flowDerivation.test.ts +1373 -0
  125. package/test/nodes/sync/flowState.test.ts +945 -0
  126. package/test/solid/converters.test.ts +721 -0
  127. package/test/solid/primitives.test.ts +1031 -0
  128. package/tsconfig.json +2 -1
  129. package/vitest.config.ts +7 -1
  130. package/IMPLEMENTATION_GUIDE.md +0 -1578
  131. package/dist/types/advanced/array.d.ts.map +0 -1
  132. package/dist/types/advanced/index.d.ts +0 -9
  133. package/dist/types/advanced/index.d.ts.map +0 -1
  134. package/dist/types/advanced/map.d.ts +0 -166
  135. package/dist/types/advanced/map.d.ts.map +0 -1
  136. package/dist/types/advanced/resource.d.ts +0 -78
  137. package/dist/types/advanced/resource.d.ts.map +0 -1
  138. package/dist/types/advanced/resourceAsync.d.ts +0 -56
  139. package/dist/types/advanced/resourceAsync.d.ts.map +0 -1
  140. package/dist/types/advanced/stream.d.ts +0 -117
  141. package/dist/types/advanced/stream.d.ts.map +0 -1
  142. package/dist/types/advanced/streamAsync.d.ts +0 -97
  143. package/dist/types/advanced/streamAsync.d.ts.map +0 -1
  144. package/dist/types/basic/constant.d.ts +0 -60
  145. package/dist/types/basic/constant.d.ts.map +0 -1
  146. package/dist/types/basic/derivation.d.ts +0 -89
  147. package/dist/types/basic/derivation.d.ts.map +0 -1
  148. package/dist/types/basic/disposable.d.ts +0 -82
  149. package/dist/types/basic/disposable.d.ts.map +0 -1
  150. package/dist/types/basic/effect.d.ts +0 -67
  151. package/dist/types/basic/effect.d.ts.map +0 -1
  152. package/dist/types/basic/index.d.ts +0 -10
  153. package/dist/types/basic/index.d.ts.map +0 -1
  154. package/dist/types/basic/observable.d.ts +0 -83
  155. package/dist/types/basic/observable.d.ts.map +0 -1
  156. package/dist/types/basic/signal.d.ts +0 -69
  157. package/dist/types/basic/signal.d.ts.map +0 -1
  158. package/dist/types/basic/state.d.ts +0 -47
  159. package/dist/types/basic/state.d.ts.map +0 -1
  160. package/dist/types/basic/trackingContext.d.ts +0 -33
  161. package/dist/types/basic/trackingContext.d.ts.map +0 -1
  162. package/dist/types/creators.d.ts +0 -340
  163. package/dist/types/creators.d.ts.map +0 -1
  164. package/src/advanced/array.ts +0 -222
  165. package/src/advanced/index.ts +0 -12
  166. package/src/advanced/map.ts +0 -193
  167. package/src/basic/constant.ts +0 -97
  168. package/src/basic/derivation.ts +0 -147
  169. package/src/basic/disposable.ts +0 -86
  170. package/src/basic/effect.ts +0 -104
  171. package/src/basic/index.ts +0 -9
  172. package/src/basic/observable.ts +0 -109
  173. package/src/basic/signal.ts +0 -145
  174. package/src/basic/state.ts +0 -60
  175. package/src/basic/trackingContext.ts +0 -45
  176. package/src/creators.ts +0 -395
  177. package/test/array.test.ts +0 -600
  178. package/test/constant.test.ts +0 -44
  179. package/test/derivation.test.ts +0 -539
  180. package/test/effect.test.ts +0 -29
  181. package/test/map.test.ts +0 -240
  182. package/test/signal.test.ts +0 -72
  183. package/test/state.test.ts +0 -212
@@ -0,0 +1,1553 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { array, effect, type FlowStateAsync, stateAsync } from "#package";
3
+
4
+ describe("FlowArray", () => {
5
+ describe("unit", () => {
6
+ describe("initialization", () => {
7
+ it("should initialize with provided FlowStateAsync values", async () => {
8
+ const $array = array([
9
+ stateAsync(Promise.resolve(1)),
10
+ stateAsync(Promise.resolve(2)),
11
+ stateAsync(Promise.resolve(3)),
12
+ ]);
13
+
14
+ expect(
15
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
16
+ ).toEqual([1, 2, 3]);
17
+ });
18
+
19
+ it("should initialize with empty array when no value provided", async () => {
20
+ const $array = array<FlowStateAsync<number>>();
21
+
22
+ expect(await $array.pick()).toEqual([]);
23
+ });
24
+ });
25
+
26
+ describe("disposal", () => {
27
+ it("should have disposed property set to false initially and true after disposal", () => {
28
+ const $array = array([
29
+ stateAsync(Promise.resolve(1)),
30
+ stateAsync(Promise.resolve(2)),
31
+ stateAsync(Promise.resolve(3)),
32
+ ]);
33
+ expect($array.disposed).toBe(false);
34
+ $array.dispose();
35
+ expect($array.disposed).toBe(true);
36
+ });
37
+
38
+ it("should throw error when disposed twice", () => {
39
+ const $array = array([
40
+ stateAsync(Promise.resolve(1)),
41
+ stateAsync(Promise.resolve(2)),
42
+ stateAsync(Promise.resolve(3)),
43
+ ]);
44
+ $array.dispose();
45
+ expect(() => $array.dispose()).toThrow(
46
+ "[PicoFlow] Primitive is disposed",
47
+ );
48
+ });
49
+
50
+ it("should throw when pick is called after disposal", async () => {
51
+ const $array = array([
52
+ stateAsync(Promise.resolve(1)),
53
+ stateAsync(Promise.resolve(2)),
54
+ stateAsync(Promise.resolve(3)),
55
+ ]);
56
+ $array.dispose();
57
+ await expect($array.pick()).rejects.toThrow(
58
+ "[PicoFlow] Primitive is disposed",
59
+ );
60
+ });
61
+
62
+ it("should throw when set is called after disposal", async () => {
63
+ const $array = array([
64
+ stateAsync(Promise.resolve(1)),
65
+ stateAsync(Promise.resolve(2)),
66
+ stateAsync(Promise.resolve(3)),
67
+ ]);
68
+ $array.dispose();
69
+ await expect(
70
+ $array.set([stateAsync(Promise.resolve(1))]),
71
+ ).rejects.toThrow("[PicoFlow] Primitive is disposed");
72
+ });
73
+
74
+ it("should throw when setItem is called after disposal", async () => {
75
+ const $array = array([
76
+ stateAsync(Promise.resolve(1)),
77
+ stateAsync(Promise.resolve(2)),
78
+ stateAsync(Promise.resolve(3)),
79
+ ]);
80
+ $array.dispose();
81
+ await expect(
82
+ $array.update(0, stateAsync(Promise.resolve(3))),
83
+ ).rejects.toThrow("[PicoFlow] Primitive is disposed");
84
+ });
85
+
86
+ it("should throw when push is called after disposal", async () => {
87
+ const $array = array([
88
+ stateAsync(Promise.resolve(1)),
89
+ stateAsync(Promise.resolve(2)),
90
+ stateAsync(Promise.resolve(3)),
91
+ ]);
92
+ $array.dispose();
93
+ await expect(
94
+ $array.push(stateAsync(Promise.resolve(3))),
95
+ ).rejects.toThrow("[PicoFlow] Primitive is disposed");
96
+ });
97
+
98
+ it("should throw when pop is called after disposal", async () => {
99
+ const $array = array([
100
+ stateAsync(Promise.resolve(1)),
101
+ stateAsync(Promise.resolve(2)),
102
+ stateAsync(Promise.resolve(3)),
103
+ ]);
104
+ $array.dispose();
105
+ await expect($array.pop()).rejects.toThrow(
106
+ "[PicoFlow] Primitive is disposed",
107
+ );
108
+ });
109
+
110
+ it("should throw when unshift is called after disposal", async () => {
111
+ const $array = array([
112
+ stateAsync(Promise.resolve(1)),
113
+ stateAsync(Promise.resolve(2)),
114
+ stateAsync(Promise.resolve(3)),
115
+ ]);
116
+ $array.dispose();
117
+ await expect(
118
+ $array.unshift(stateAsync(Promise.resolve(3))),
119
+ ).rejects.toThrow("[PicoFlow] Primitive is disposed");
120
+ });
121
+
122
+ it("should throw when shift is called after disposal", async () => {
123
+ const $array = array([
124
+ stateAsync(Promise.resolve(1)),
125
+ stateAsync(Promise.resolve(2)),
126
+ stateAsync(Promise.resolve(3)),
127
+ ]);
128
+ $array.dispose();
129
+ await expect($array.shift()).rejects.toThrow(
130
+ "[PicoFlow] Primitive is disposed",
131
+ );
132
+ });
133
+
134
+ it("should throw when splice is called after disposal", async () => {
135
+ const $array = array<FlowStateAsync<number>>([
136
+ stateAsync(Promise.resolve(0)),
137
+ stateAsync(Promise.resolve(1)),
138
+ stateAsync(Promise.resolve(2)),
139
+ stateAsync(Promise.resolve(3)),
140
+ stateAsync(Promise.resolve(4)),
141
+ ]);
142
+ $array.dispose();
143
+ await expect($array.splice(1, 2)).rejects.toThrow(
144
+ "[PicoFlow] Primitive is disposed",
145
+ );
146
+ });
147
+
148
+ it("should throw when clear is called after disposal", async () => {
149
+ const $array = array<FlowStateAsync<number>>([
150
+ stateAsync(Promise.resolve(0)),
151
+ stateAsync(Promise.resolve(1)),
152
+ stateAsync(Promise.resolve(2)),
153
+ stateAsync(Promise.resolve(3)),
154
+ stateAsync(Promise.resolve(4)),
155
+ ]);
156
+ $array.dispose();
157
+ await expect($array.clear()).rejects.toThrow(
158
+ "[PicoFlow] Primitive is disposed",
159
+ );
160
+ });
161
+
162
+ it("should dispose items when FlowArray is disposed", () => {
163
+ const $item1 = stateAsync(Promise.resolve(1));
164
+ const $item2 = stateAsync(Promise.resolve(2));
165
+ const $item3 = stateAsync(Promise.resolve(3));
166
+ const $array = array([$item1, $item2, $item3]);
167
+
168
+ expect($item1.disposed).toBe(false);
169
+ expect($item2.disposed).toBe(false);
170
+ expect($item3.disposed).toBe(false);
171
+
172
+ $array.dispose();
173
+
174
+ expect($item1.disposed).toBe(true);
175
+ expect($item2.disposed).toBe(true);
176
+ expect($item3.disposed).toBe(true);
177
+ });
178
+
179
+ it("should dispose items with provided options when FlowArray is disposed", async () => {
180
+ const $item1 = stateAsync(Promise.resolve(1));
181
+ const $item2 = stateAsync(Promise.resolve(2));
182
+ const $array = array([$item1, $item2]);
183
+
184
+ const $effect = effect(async (t) => {
185
+ await $item1.get(t);
186
+ });
187
+
188
+ expect($effect.disposed).toBe(false);
189
+ await $effect.settled;
190
+
191
+ $array.dispose({ self: true });
192
+
193
+ expect($item1.disposed).toBe(true);
194
+ expect($item2.disposed).toBe(true);
195
+ expect($effect.disposed).toBe(false);
196
+ });
197
+ });
198
+
199
+ describe("length", () => {
200
+ it("should return correct length", () => {
201
+ const $array = array([
202
+ stateAsync(Promise.resolve(1)),
203
+ stateAsync(Promise.resolve(2)),
204
+ stateAsync(Promise.resolve(3)),
205
+ ]);
206
+ expect($array.length).toBe(3);
207
+ });
208
+
209
+ it("should return zero for empty array", () => {
210
+ const $array = array<FlowStateAsync<number>>([]);
211
+ expect($array.length).toBe(0);
212
+ });
213
+
214
+ it("should throw when length is accessed after disposal", () => {
215
+ const $array = array([
216
+ stateAsync(Promise.resolve(1)),
217
+ stateAsync(Promise.resolve(2)),
218
+ stateAsync(Promise.resolve(3)),
219
+ ]);
220
+ $array.dispose();
221
+ expect(() => $array.length).toThrow("[PicoFlow] Primitive is disposed");
222
+ });
223
+
224
+ it("should update length when array is modified", async () => {
225
+ const $array = array<FlowStateAsync<number>>([]);
226
+ expect($array.length).toBe(0);
227
+
228
+ $array.push(stateAsync(Promise.resolve(1)));
229
+ expect($array.length).toBe(1);
230
+
231
+ $array.push(stateAsync(Promise.resolve(2)));
232
+ expect($array.length).toBe(2);
233
+
234
+ $array.pop();
235
+ expect($array.length).toBe(1);
236
+
237
+ $array.unshift(stateAsync(Promise.resolve(0)));
238
+ expect($array.length).toBe(2);
239
+
240
+ $array.shift();
241
+ expect($array.length).toBe(1);
242
+
243
+ $array.clear();
244
+ expect($array.length).toBe(0);
245
+ });
246
+ });
247
+
248
+ describe("set", () => {
249
+ it("should update array when set is called with FlowStateAsync", async () => {
250
+ const $array = array([
251
+ stateAsync(Promise.resolve(0)),
252
+ stateAsync(Promise.resolve(1)),
253
+ stateAsync(Promise.resolve(2)),
254
+ ]);
255
+ expect(
256
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
257
+ ).toEqual([0, 1, 2]);
258
+
259
+ await $array.set([
260
+ stateAsync(Promise.resolve(1)),
261
+ stateAsync(Promise.resolve(2)),
262
+ stateAsync(Promise.resolve(3)),
263
+ ]);
264
+ expect(
265
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
266
+ ).toEqual([1, 2, 3]);
267
+ });
268
+
269
+ it("should dispose items when set replaces array", () => {
270
+ const $oldItem1 = stateAsync(Promise.resolve(1));
271
+ const $oldItem2 = stateAsync(Promise.resolve(2));
272
+ const $array = array([$oldItem1, $oldItem2]);
273
+
274
+ expect($oldItem1.disposed).toBe(false);
275
+ expect($oldItem2.disposed).toBe(false);
276
+
277
+ $array.set([
278
+ stateAsync(Promise.resolve(3)),
279
+ stateAsync(Promise.resolve(4)),
280
+ ]);
281
+
282
+ expect($oldItem1.disposed).toBe(true);
283
+ expect($oldItem2.disposed).toBe(true);
284
+ });
285
+
286
+ it("should dispose items with self option when set replaces array", async () => {
287
+ const $oldItem = stateAsync(Promise.resolve(1));
288
+ const $array = array([$oldItem]);
289
+
290
+ const $effect = effect(async (t) => {
291
+ await $oldItem.get(t);
292
+ });
293
+
294
+ expect($effect.disposed).toBe(false);
295
+
296
+ await $array.set([stateAsync(Promise.resolve(2))]);
297
+
298
+ expect($oldItem.disposed).toBe(true);
299
+ expect($effect.disposed).toBe(false);
300
+ });
301
+ });
302
+
303
+ describe("setItem", () => {
304
+ it("should update item at specific index", async () => {
305
+ const $array = array([
306
+ stateAsync(Promise.resolve(0)),
307
+ stateAsync(Promise.resolve(1)),
308
+ stateAsync(Promise.resolve(2)),
309
+ ]);
310
+ expect(
311
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
312
+ ).toEqual([0, 1, 2]);
313
+
314
+ $array.update(0, stateAsync(Promise.resolve(1)));
315
+ expect(
316
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
317
+ ).toEqual([1, 1, 2]);
318
+
319
+ $array.update(1, stateAsync(Promise.resolve(2)));
320
+ expect(
321
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
322
+ ).toEqual([1, 2, 2]);
323
+
324
+ $array.update(2, stateAsync(Promise.resolve(3)));
325
+ expect(
326
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
327
+ ).toEqual([1, 2, 3]);
328
+ });
329
+
330
+ it("should throw when setItem is called with negative index", async () => {
331
+ const $array = array([
332
+ stateAsync(Promise.resolve(1)),
333
+ stateAsync(Promise.resolve(2)),
334
+ stateAsync(Promise.resolve(3)),
335
+ ]);
336
+ await expect(
337
+ $array.update(-1, stateAsync(Promise.resolve(0))),
338
+ ).rejects.toThrow("[PicoFlow] Index out of bounds");
339
+ });
340
+
341
+ it("should throw when setItem is called with index >= length", async () => {
342
+ const $array = array([
343
+ stateAsync(Promise.resolve(1)),
344
+ stateAsync(Promise.resolve(2)),
345
+ stateAsync(Promise.resolve(3)),
346
+ ]);
347
+ await expect(
348
+ $array.update(3, stateAsync(Promise.resolve(0))),
349
+ ).rejects.toThrow("[PicoFlow] Index out of bounds");
350
+ await expect(
351
+ $array.update(10, stateAsync(Promise.resolve(0))),
352
+ ).rejects.toThrow("[PicoFlow] Index out of bounds");
353
+ });
354
+
355
+ it("should handle boundary indices correctly", async () => {
356
+ const $array = array([
357
+ stateAsync(Promise.resolve(0)),
358
+ stateAsync(Promise.resolve(1)),
359
+ stateAsync(Promise.resolve(2)),
360
+ ]);
361
+ $array.update(0, stateAsync(Promise.resolve(10)));
362
+ expect(
363
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
364
+ ).toEqual([10, 1, 2]);
365
+
366
+ $array.update(2, stateAsync(Promise.resolve(20)));
367
+ expect(
368
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
369
+ ).toEqual([10, 1, 20]);
370
+ });
371
+ });
372
+
373
+ describe("push", () => {
374
+ it("should append FlowStateAsync to end of array", async () => {
375
+ const $array = array<FlowStateAsync<number>>([]);
376
+ expect(await $array.pick()).toEqual([]);
377
+
378
+ $array.push(stateAsync(Promise.resolve(0)));
379
+ expect(
380
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
381
+ ).toEqual([0]);
382
+
383
+ $array.push(stateAsync(Promise.resolve(1)));
384
+ expect(
385
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
386
+ ).toEqual([0, 1]);
387
+
388
+ $array.push(stateAsync(Promise.resolve(2)));
389
+ expect(
390
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
391
+ ).toEqual([0, 1, 2]);
392
+ });
393
+
394
+ it("should handle operations on empty array", () => {
395
+ const $array = array<FlowStateAsync<number>>([]);
396
+ expect($array.length).toBe(0);
397
+ $array.push(stateAsync(Promise.resolve(1)));
398
+ expect($array.length).toBe(1);
399
+ });
400
+ });
401
+
402
+ describe("pop", () => {
403
+ it("should remove last FlowStateAsync from array", async () => {
404
+ const $array = array([
405
+ stateAsync(Promise.resolve(0)),
406
+ stateAsync(Promise.resolve(1)),
407
+ stateAsync(Promise.resolve(2)),
408
+ ]);
409
+ expect(
410
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
411
+ ).toEqual([0, 1, 2]);
412
+
413
+ $array.pop();
414
+ expect(
415
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
416
+ ).toEqual([0, 1]);
417
+
418
+ $array.pop();
419
+ expect(
420
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
421
+ ).toEqual([0]);
422
+
423
+ $array.pop();
424
+ expect(
425
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
426
+ ).toEqual([]);
427
+ });
428
+
429
+ it("should dispose item when pop removes it", () => {
430
+ const $item = stateAsync(Promise.resolve(1));
431
+ const $array = array([$item]);
432
+
433
+ expect($item.disposed).toBe(false);
434
+
435
+ $array.pop();
436
+
437
+ expect($item.disposed).toBe(true);
438
+ });
439
+
440
+ it("should dispose item with self option when pop removes it", async () => {
441
+ const $item = stateAsync(Promise.resolve(1));
442
+ const $array = array([$item]);
443
+
444
+ const $effect = effect(async (t) => {
445
+ await $item.get(t);
446
+ });
447
+
448
+ expect($effect.disposed).toBe(false);
449
+
450
+ await $array.pop();
451
+
452
+ expect($item.disposed).toBe(true);
453
+ expect($effect.disposed).toBe(false);
454
+ });
455
+
456
+ it("should handle operations on empty array", () => {
457
+ const $array = array<FlowStateAsync<number>>([]);
458
+ expect($array.length).toBe(0);
459
+ $array.pop();
460
+ expect($array.length).toBe(0);
461
+ });
462
+ });
463
+
464
+ describe("unshift", () => {
465
+ it("should insert FlowStateAsync at beginning of array", async () => {
466
+ const $array = array<FlowStateAsync<number>>([]);
467
+ expect(await $array.pick()).toEqual([]);
468
+
469
+ $array.unshift(stateAsync(Promise.resolve(0)));
470
+ expect(
471
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
472
+ ).toEqual([0]);
473
+
474
+ $array.unshift(stateAsync(Promise.resolve(1)));
475
+ expect(
476
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
477
+ ).toEqual([1, 0]);
478
+
479
+ $array.unshift(stateAsync(Promise.resolve(2)));
480
+ expect(
481
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
482
+ ).toEqual([2, 1, 0]);
483
+ });
484
+ });
485
+
486
+ describe("shift", () => {
487
+ it("should remove first FlowStateAsync from array", async () => {
488
+ const $array = array([
489
+ stateAsync(Promise.resolve(0)),
490
+ stateAsync(Promise.resolve(1)),
491
+ stateAsync(Promise.resolve(2)),
492
+ ]);
493
+ expect(
494
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
495
+ ).toEqual([0, 1, 2]);
496
+
497
+ $array.shift();
498
+ expect(
499
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
500
+ ).toEqual([1, 2]);
501
+
502
+ $array.shift();
503
+ expect(
504
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
505
+ ).toEqual([2]);
506
+
507
+ $array.shift();
508
+ expect(
509
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
510
+ ).toEqual([]);
511
+ });
512
+
513
+ it("should dispose item when shift removes it", () => {
514
+ const $item = stateAsync(Promise.resolve(1));
515
+ const $array = array([$item, stateAsync(Promise.resolve(2))]);
516
+
517
+ expect($item.disposed).toBe(false);
518
+
519
+ $array.shift();
520
+
521
+ expect($item.disposed).toBe(true);
522
+ });
523
+
524
+ it("should dispose item with self option when shift removes it", async () => {
525
+ const $item = stateAsync(Promise.resolve(1));
526
+ const $array = array([$item, stateAsync(Promise.resolve(2))]);
527
+
528
+ const $effect = effect(async (t) => {
529
+ await $item.get(t);
530
+ });
531
+
532
+ expect($effect.disposed).toBe(false);
533
+
534
+ await $array.shift();
535
+
536
+ expect($item.disposed).toBe(true);
537
+ expect($effect.disposed).toBe(false);
538
+ });
539
+
540
+ it("should handle operations on empty array", () => {
541
+ const $array = array<FlowStateAsync<number>>([]);
542
+ expect($array.length).toBe(0);
543
+ $array.shift();
544
+ expect($array.length).toBe(0);
545
+ });
546
+ });
547
+
548
+ describe("splice", () => {
549
+ it("should remove FlowStateAsync from array", async () => {
550
+ const $array = array<FlowStateAsync<number>>([
551
+ stateAsync(Promise.resolve(0)),
552
+ stateAsync(Promise.resolve(1)),
553
+ stateAsync(Promise.resolve(2)),
554
+ stateAsync(Promise.resolve(3)),
555
+ stateAsync(Promise.resolve(4)),
556
+ ]);
557
+ expect(
558
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
559
+ ).toEqual([0, 1, 2, 3, 4]);
560
+
561
+ $array.splice(1, 2);
562
+ expect(
563
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
564
+ ).toEqual([0, 3, 4]);
565
+ });
566
+
567
+ it("should insert FlowStateAsync into array", async () => {
568
+ const $array = array<FlowStateAsync<number>>([
569
+ stateAsync(Promise.resolve(0)),
570
+ stateAsync(Promise.resolve(1)),
571
+ stateAsync(Promise.resolve(2)),
572
+ stateAsync(Promise.resolve(3)),
573
+ stateAsync(Promise.resolve(4)),
574
+ ]);
575
+ $array.splice(
576
+ 1,
577
+ 0,
578
+ stateAsync(Promise.resolve(10)),
579
+ stateAsync(Promise.resolve(11)),
580
+ );
581
+ expect(
582
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
583
+ ).toEqual([0, 10, 11, 1, 2, 3, 4]);
584
+ });
585
+
586
+ it("should replace FlowStateAsync in array", async () => {
587
+ const $array = array<FlowStateAsync<number>>([
588
+ stateAsync(Promise.resolve(0)),
589
+ stateAsync(Promise.resolve(1)),
590
+ stateAsync(Promise.resolve(2)),
591
+ stateAsync(Promise.resolve(3)),
592
+ stateAsync(Promise.resolve(4)),
593
+ ]);
594
+ $array.splice(
595
+ 1,
596
+ 2,
597
+ stateAsync(Promise.resolve(10)),
598
+ stateAsync(Promise.resolve(11)),
599
+ );
600
+ expect(
601
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
602
+ ).toEqual([0, 10, 11, 3, 4]);
603
+ });
604
+
605
+ it("should dispose items when splice removes them", () => {
606
+ const $item1 = stateAsync(Promise.resolve(1));
607
+ const $item2 = stateAsync(Promise.resolve(2));
608
+ const $array = array([
609
+ stateAsync(Promise.resolve(0)),
610
+ $item1,
611
+ $item2,
612
+ stateAsync(Promise.resolve(3)),
613
+ ]);
614
+
615
+ expect($item1.disposed).toBe(false);
616
+ expect($item2.disposed).toBe(false);
617
+
618
+ $array.splice(1, 2);
619
+
620
+ expect($item1.disposed).toBe(true);
621
+ expect($item2.disposed).toBe(true);
622
+ });
623
+
624
+ it("should dispose items with self option when splice removes them", async () => {
625
+ const $item = stateAsync(Promise.resolve(1));
626
+ const $array = array([
627
+ stateAsync(Promise.resolve(0)),
628
+ $item,
629
+ stateAsync(Promise.resolve(2)),
630
+ ]);
631
+
632
+ const $effect = effect(async (t) => {
633
+ await $item.get(t);
634
+ });
635
+
636
+ expect($effect.disposed).toBe(false);
637
+
638
+ await $array.splice(1, 1);
639
+
640
+ expect($item.disposed).toBe(true);
641
+ expect($effect.disposed).toBe(false);
642
+ });
643
+ });
644
+
645
+ describe("clear", () => {
646
+ it("should remove all FlowStateAsync from array", async () => {
647
+ const $array = array<FlowStateAsync<number>>([
648
+ stateAsync(Promise.resolve(0)),
649
+ stateAsync(Promise.resolve(1)),
650
+ stateAsync(Promise.resolve(2)),
651
+ stateAsync(Promise.resolve(3)),
652
+ stateAsync(Promise.resolve(4)),
653
+ ]);
654
+ expect(
655
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
656
+ ).toEqual([0, 1, 2, 3, 4]);
657
+
658
+ $array.clear();
659
+ expect(
660
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
661
+ ).toEqual([]);
662
+ });
663
+
664
+ it("should dispose all items when clear is called", () => {
665
+ const $item1 = stateAsync(Promise.resolve(1));
666
+ const $item2 = stateAsync(Promise.resolve(2));
667
+ const $item3 = stateAsync(Promise.resolve(3));
668
+ const $array = array([$item1, $item2, $item3]);
669
+
670
+ expect($item1.disposed).toBe(false);
671
+ expect($item2.disposed).toBe(false);
672
+ expect($item3.disposed).toBe(false);
673
+
674
+ $array.clear();
675
+
676
+ expect($item1.disposed).toBe(true);
677
+ expect($item2.disposed).toBe(true);
678
+ expect($item3.disposed).toBe(true);
679
+ });
680
+
681
+ it("should dispose items with self option when clear is called", async () => {
682
+ const $item = stateAsync(Promise.resolve(1));
683
+ const $array = array([$item, stateAsync(Promise.resolve(2))]);
684
+
685
+ const $effect = effect(async (t) => {
686
+ await $item.get(t);
687
+ });
688
+
689
+ expect($effect.disposed).toBe(false);
690
+
691
+ await $array.clear();
692
+
693
+ expect($item.disposed).toBe(true);
694
+ expect($effect.disposed).toBe(false);
695
+ });
696
+ });
697
+
698
+ describe("$lastAction", () => {
699
+ describe("set action", () => {
700
+ it("should initialize $lastAction with set action on creation", async () => {
701
+ const $array = array([
702
+ stateAsync(Promise.resolve(1)),
703
+ stateAsync(Promise.resolve(2)),
704
+ stateAsync(Promise.resolve(3)),
705
+ ]);
706
+ const action = await $array.$lastAction.pick();
707
+ expect(action.type).toBe("set");
708
+ if (action.type === "set") {
709
+ expect(action.items).toHaveLength(3);
710
+ expect(action.items[0]).toBeInstanceOf(Object);
711
+ expect(action.items[1]).toBeInstanceOf(Object);
712
+ expect(action.items[2]).toBeInstanceOf(Object);
713
+ }
714
+ });
715
+
716
+ it("should update $lastAction with set action when set is called", async () => {
717
+ const $array = array([
718
+ stateAsync(Promise.resolve(0)),
719
+ stateAsync(Promise.resolve(1)),
720
+ stateAsync(Promise.resolve(2)),
721
+ ]);
722
+ await $array.set([
723
+ stateAsync(Promise.resolve(1)),
724
+ stateAsync(Promise.resolve(2)),
725
+ stateAsync(Promise.resolve(3)),
726
+ ]);
727
+ const action = await $array.$lastAction.pick();
728
+ expect(action.type).toBe("set");
729
+ if (action.type === "set") {
730
+ expect(action.items).toHaveLength(3);
731
+ }
732
+ });
733
+
734
+ it("should have correct structure for set action (items array)", async () => {
735
+ const $array = array([
736
+ stateAsync(Promise.resolve(1)),
737
+ stateAsync(Promise.resolve(2)),
738
+ stateAsync(Promise.resolve(3)),
739
+ ]);
740
+ const action = await $array.$lastAction.pick();
741
+ expect(action).toHaveProperty("type", "set");
742
+ if (action.type === "set") {
743
+ expect(action).toHaveProperty("items");
744
+ expect(Array.isArray(action.items)).toBe(true);
745
+ expect(action.items).toHaveLength(3);
746
+ }
747
+ });
748
+ });
749
+
750
+ describe("update action", () => {
751
+ it("should update $lastAction with setItem action when setItem is called", async () => {
752
+ const $array = array([
753
+ stateAsync(Promise.resolve(0)),
754
+ stateAsync(Promise.resolve(1)),
755
+ stateAsync(Promise.resolve(2)),
756
+ ]);
757
+ const $newItem = stateAsync(Promise.resolve(10));
758
+ $array.update(1, $newItem);
759
+ const action = await $array.$lastAction.pick();
760
+ expect(action.type).toBe("update");
761
+ if (action.type === "update") {
762
+ expect(action.index).toBe(1);
763
+ expect(action.item).toBe($newItem);
764
+ }
765
+ });
766
+
767
+ it("should have correct structure for update action (index and item)", async () => {
768
+ const $array = array([
769
+ stateAsync(Promise.resolve(1)),
770
+ stateAsync(Promise.resolve(2)),
771
+ stateAsync(Promise.resolve(3)),
772
+ ]);
773
+ const $newItem = stateAsync(Promise.resolve(10));
774
+ $array.update(0, $newItem);
775
+ const action = await $array.$lastAction.pick();
776
+ expect(action).toHaveProperty("type", "update");
777
+ if (action.type === "update") {
778
+ expect(action).toHaveProperty("index");
779
+ expect(action).toHaveProperty("item");
780
+ expect(action.index).toBe(0);
781
+ expect(action.item).toBe($newItem);
782
+ }
783
+ });
784
+ });
785
+
786
+ describe("push action", () => {
787
+ it("should update $lastAction with push action when push is called", async () => {
788
+ const $array = array<FlowStateAsync<number>>([]);
789
+ const $newItem = stateAsync(Promise.resolve(42));
790
+ $array.push($newItem);
791
+ const action = await $array.$lastAction.pick();
792
+ expect(action.type).toBe("push");
793
+ if (action.type === "push") {
794
+ expect(action.item).toBe($newItem);
795
+ }
796
+ });
797
+
798
+ it("should have correct structure for push action (item)", async () => {
799
+ const $array = array<FlowStateAsync<number>>([]);
800
+ const $newItem = stateAsync(Promise.resolve(100));
801
+ $array.push($newItem);
802
+ const action = await $array.$lastAction.pick();
803
+ expect(action).toHaveProperty("type", "push");
804
+ if (action.type === "push") {
805
+ expect(action).toHaveProperty("item");
806
+ expect(action.item).toBe($newItem);
807
+ }
808
+ });
809
+ });
810
+
811
+ describe("pop action", () => {
812
+ it("should update $lastAction with pop action when pop is called", async () => {
813
+ const $array = array([
814
+ stateAsync(Promise.resolve(1)),
815
+ stateAsync(Promise.resolve(2)),
816
+ stateAsync(Promise.resolve(3)),
817
+ ]);
818
+ $array.pop();
819
+ const action = await $array.$lastAction.pick();
820
+ expect(action).toEqual({
821
+ type: "pop",
822
+ });
823
+ });
824
+
825
+ it("should have correct structure for pop action (no additional fields)", async () => {
826
+ const $array = array([
827
+ stateAsync(Promise.resolve(1)),
828
+ stateAsync(Promise.resolve(2)),
829
+ stateAsync(Promise.resolve(3)),
830
+ ]);
831
+ $array.pop();
832
+ const action = await $array.$lastAction.pick();
833
+ expect(action).toHaveProperty("type", "pop");
834
+ if (action.type === "pop") {
835
+ expect(action).not.toHaveProperty("item");
836
+ expect(action).not.toHaveProperty("index");
837
+ expect(action).not.toHaveProperty("items");
838
+ }
839
+ });
840
+ });
841
+
842
+ describe("unshift action", () => {
843
+ it("should update $lastAction with unshift action when unshift is called", async () => {
844
+ const $array = array<FlowStateAsync<number>>([]);
845
+ const $newItem = stateAsync(Promise.resolve(42));
846
+ $array.unshift($newItem);
847
+ const action = await $array.$lastAction.pick();
848
+ expect(action.type).toBe("unshift");
849
+ if (action.type === "unshift") {
850
+ expect(action.item).toBe($newItem);
851
+ }
852
+ });
853
+
854
+ it("should have correct structure for unshift action (item)", async () => {
855
+ const $array = array<FlowStateAsync<number>>([]);
856
+ const $newItem = stateAsync(Promise.resolve(200));
857
+ $array.unshift($newItem);
858
+ const action = await $array.$lastAction.pick();
859
+ expect(action).toHaveProperty("type", "unshift");
860
+ if (action.type === "unshift") {
861
+ expect(action).toHaveProperty("item");
862
+ expect(action.item).toBe($newItem);
863
+ }
864
+ });
865
+ });
866
+
867
+ describe("shift action", () => {
868
+ it("should update $lastAction with shift action when shift is called", async () => {
869
+ const $array = array([
870
+ stateAsync(Promise.resolve(1)),
871
+ stateAsync(Promise.resolve(2)),
872
+ stateAsync(Promise.resolve(3)),
873
+ ]);
874
+ $array.shift();
875
+ const action = await $array.$lastAction.pick();
876
+ expect(action).toEqual({
877
+ type: "shift",
878
+ });
879
+ });
880
+
881
+ it("should have correct structure for shift action (no additional fields)", async () => {
882
+ const $array = array([
883
+ stateAsync(Promise.resolve(1)),
884
+ stateAsync(Promise.resolve(2)),
885
+ stateAsync(Promise.resolve(3)),
886
+ ]);
887
+ $array.shift();
888
+ const action = await $array.$lastAction.pick();
889
+ expect(action).toHaveProperty("type", "shift");
890
+ if (action.type === "shift") {
891
+ expect(action).not.toHaveProperty("item");
892
+ expect(action).not.toHaveProperty("index");
893
+ expect(action).not.toHaveProperty("items");
894
+ }
895
+ });
896
+ });
897
+
898
+ describe("splice action", () => {
899
+ it("should update $lastAction with splice action when splice is called", async () => {
900
+ const $array = array<FlowStateAsync<number>>([
901
+ stateAsync(Promise.resolve(0)),
902
+ stateAsync(Promise.resolve(1)),
903
+ stateAsync(Promise.resolve(2)),
904
+ stateAsync(Promise.resolve(3)),
905
+ stateAsync(Promise.resolve(4)),
906
+ ]);
907
+ const $newItem1 = stateAsync(Promise.resolve(10));
908
+ const $newItem2 = stateAsync(Promise.resolve(11));
909
+ $array.splice(1, 2, $newItem1, $newItem2);
910
+ const action = await $array.$lastAction.pick();
911
+ expect(action.type).toBe("splice");
912
+ if (action.type === "splice") {
913
+ expect(action.start).toBe(1);
914
+ expect(action.deleteCount).toBe(2);
915
+ expect(action.items).toHaveLength(2);
916
+ expect(action.items[0]).toBe($newItem1);
917
+ expect(action.items[1]).toBe($newItem2);
918
+ }
919
+ });
920
+
921
+ it("should have correct structure for splice action (start, deleteCount, items)", async () => {
922
+ const $array = array<FlowStateAsync<number>>([
923
+ stateAsync(Promise.resolve(0)),
924
+ stateAsync(Promise.resolve(1)),
925
+ stateAsync(Promise.resolve(2)),
926
+ ]);
927
+ const $newItem = stateAsync(Promise.resolve(100));
928
+ $array.splice(1, 1, $newItem);
929
+ const action = await $array.$lastAction.pick();
930
+ expect(action).toHaveProperty("type", "splice");
931
+ if (action.type === "splice") {
932
+ expect(action).toHaveProperty("start");
933
+ expect(action).toHaveProperty("deleteCount");
934
+ expect(action).toHaveProperty("items");
935
+ expect(action.start).toBe(1);
936
+ expect(action.deleteCount).toBe(1);
937
+ expect(Array.isArray(action.items)).toBe(true);
938
+ expect(action.items).toHaveLength(1);
939
+ expect(action.items[0]).toBe($newItem);
940
+ }
941
+ });
942
+ });
943
+
944
+ describe("clear action", () => {
945
+ it("should update $lastAction with clear action when clear is called", async () => {
946
+ const $array = array<FlowStateAsync<number>>([
947
+ stateAsync(Promise.resolve(0)),
948
+ stateAsync(Promise.resolve(1)),
949
+ stateAsync(Promise.resolve(2)),
950
+ stateAsync(Promise.resolve(3)),
951
+ stateAsync(Promise.resolve(4)),
952
+ ]);
953
+ $array.clear();
954
+ const action = await $array.$lastAction.pick();
955
+ expect(action).toEqual({
956
+ type: "clear",
957
+ });
958
+ });
959
+
960
+ it("should have correct structure for clear action (no additional fields)", async () => {
961
+ const $array = array<FlowStateAsync<number>>([
962
+ stateAsync(Promise.resolve(1)),
963
+ stateAsync(Promise.resolve(2)),
964
+ stateAsync(Promise.resolve(3)),
965
+ ]);
966
+ $array.clear();
967
+ const action = await $array.$lastAction.pick();
968
+ expect(action).toHaveProperty("type", "clear");
969
+ if (action.type === "clear") {
970
+ expect(action).not.toHaveProperty("item");
971
+ expect(action).not.toHaveProperty("index");
972
+ expect(action).not.toHaveProperty("items");
973
+ }
974
+ });
975
+ });
976
+ });
977
+
978
+ describe("edge cases", () => {
979
+ it("should handle multiple rapid operations", async () => {
980
+ const $array = array<FlowStateAsync<number>>([]);
981
+ $array.push(stateAsync(Promise.resolve(1)));
982
+ $array.push(stateAsync(Promise.resolve(2)));
983
+ $array.push(stateAsync(Promise.resolve(3)));
984
+ $array.pop();
985
+ $array.shift();
986
+ $array.unshift(stateAsync(Promise.resolve(0)));
987
+ expect(
988
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
989
+ ).toEqual([0, 2]);
990
+ });
991
+
992
+ it("should handle boundary indices correctly", async () => {
993
+ const $array = array([
994
+ stateAsync(Promise.resolve(0)),
995
+ stateAsync(Promise.resolve(1)),
996
+ stateAsync(Promise.resolve(2)),
997
+ ]);
998
+ $array.update(0, stateAsync(Promise.resolve(10)));
999
+ expect(
1000
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
1001
+ ).toEqual([10, 1, 2]);
1002
+
1003
+ $array.update(2, stateAsync(Promise.resolve(20)));
1004
+ expect(
1005
+ await Promise.all((await $array.pick()).map((state) => state.pick())),
1006
+ ).toEqual([10, 1, 20]);
1007
+ });
1008
+ });
1009
+ });
1010
+
1011
+ describe("integration", () => {
1012
+ describe("with effects", () => {
1013
+ describe("reactivity", () => {
1014
+ it("should call effect when initialized", async () => {
1015
+ const $array = array([
1016
+ stateAsync(Promise.resolve(1)),
1017
+ stateAsync(Promise.resolve(2)),
1018
+ stateAsync(Promise.resolve(3)),
1019
+ ]);
1020
+ const effectFn = vi.fn();
1021
+ effect(async (t) =>
1022
+ effectFn(
1023
+ await Promise.all(
1024
+ (await $array.get(t)).map(async (state) => await state.get(t)),
1025
+ ),
1026
+ ),
1027
+ );
1028
+
1029
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1030
+ expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
1031
+ });
1032
+
1033
+ it("should call effect when updated with set", async () => {
1034
+ const $array = array<FlowStateAsync<number>>([
1035
+ stateAsync(Promise.resolve(0)),
1036
+ stateAsync(Promise.resolve(1)),
1037
+ stateAsync(Promise.resolve(2)),
1038
+ ]);
1039
+ const effectFn = vi.fn();
1040
+ effect(async (t) =>
1041
+ effectFn(
1042
+ await Promise.all(
1043
+ (await $array.get(t)).map(async (state) => await state.get(t)),
1044
+ ),
1045
+ ),
1046
+ );
1047
+
1048
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1049
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
1050
+
1051
+ await $array.set([
1052
+ stateAsync(Promise.resolve(1)),
1053
+ stateAsync(Promise.resolve(2)),
1054
+ stateAsync(Promise.resolve(3)),
1055
+ ]);
1056
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1057
+ expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
1058
+ });
1059
+
1060
+ it("should call effect when updated with push/pop", async () => {
1061
+ const $array = array<FlowStateAsync<number>>();
1062
+ const effectFn = vi.fn();
1063
+ effect(async (t) =>
1064
+ effectFn(
1065
+ await Promise.all(
1066
+ (await $array.get(t)).map(async (state) => await state.get(t)),
1067
+ ),
1068
+ ),
1069
+ );
1070
+
1071
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1072
+ expect(effectFn).toHaveBeenLastCalledWith([]);
1073
+
1074
+ $array.push(stateAsync(Promise.resolve(0)));
1075
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1076
+ expect(effectFn).toHaveBeenLastCalledWith([0]);
1077
+
1078
+ $array.push(stateAsync(Promise.resolve(1)));
1079
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1080
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1]);
1081
+
1082
+ $array.push(stateAsync(Promise.resolve(2)));
1083
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
1084
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
1085
+
1086
+ $array.pop();
1087
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(5));
1088
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1]);
1089
+
1090
+ $array.pop();
1091
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(6));
1092
+ expect(effectFn).toHaveBeenLastCalledWith([0]);
1093
+
1094
+ $array.pop();
1095
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(7));
1096
+ expect(effectFn).toHaveBeenLastCalledWith([]);
1097
+ });
1098
+
1099
+ it("should call effect when updated with unshift/shift", async () => {
1100
+ const $array = array<FlowStateAsync<number>>();
1101
+ const effectFn = vi.fn();
1102
+ effect(async (t) =>
1103
+ effectFn(
1104
+ await Promise.all(
1105
+ (await $array.get(t)).map(async (state) => await state.get(t)),
1106
+ ),
1107
+ ),
1108
+ );
1109
+
1110
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1111
+ expect(effectFn).toHaveBeenLastCalledWith([]);
1112
+
1113
+ $array.unshift(stateAsync(Promise.resolve(0)));
1114
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1115
+ expect(effectFn).toHaveBeenLastCalledWith([0]);
1116
+
1117
+ $array.unshift(stateAsync(Promise.resolve(1)));
1118
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1119
+ expect(effectFn).toHaveBeenLastCalledWith([1, 0]);
1120
+
1121
+ $array.unshift(stateAsync(Promise.resolve(2)));
1122
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
1123
+ expect(effectFn).toHaveBeenLastCalledWith([2, 1, 0]);
1124
+
1125
+ $array.shift();
1126
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(5));
1127
+ expect(effectFn).toHaveBeenLastCalledWith([1, 0]);
1128
+
1129
+ $array.shift();
1130
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(6));
1131
+ expect(effectFn).toHaveBeenLastCalledWith([0]);
1132
+
1133
+ $array.shift();
1134
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(7));
1135
+ expect(effectFn).toHaveBeenLastCalledWith([]);
1136
+ });
1137
+
1138
+ it("should call effect when updated with splice", async () => {
1139
+ const $array = array<FlowStateAsync<number>>([
1140
+ stateAsync(Promise.resolve(0)),
1141
+ stateAsync(Promise.resolve(1)),
1142
+ stateAsync(Promise.resolve(2)),
1143
+ stateAsync(Promise.resolve(3)),
1144
+ stateAsync(Promise.resolve(4)),
1145
+ ]);
1146
+ const effectFn = vi.fn();
1147
+ effect(async (t) =>
1148
+ effectFn(
1149
+ await Promise.all(
1150
+ $array.get(t).map(async (state) => await state.get(t)),
1151
+ ),
1152
+ ),
1153
+ );
1154
+
1155
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1156
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
1157
+
1158
+ $array.splice(1, 2);
1159
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1160
+ expect(effectFn).toHaveBeenLastCalledWith([0, 3, 4]);
1161
+
1162
+ $array.splice(
1163
+ 1,
1164
+ 0,
1165
+ stateAsync(Promise.resolve(1)),
1166
+ stateAsync(Promise.resolve(2)),
1167
+ );
1168
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1169
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
1170
+ });
1171
+
1172
+ it("should call effect when updated with clear", async () => {
1173
+ const $array = array<FlowStateAsync<number>>([
1174
+ stateAsync(Promise.resolve(0)),
1175
+ stateAsync(Promise.resolve(1)),
1176
+ stateAsync(Promise.resolve(2)),
1177
+ stateAsync(Promise.resolve(3)),
1178
+ stateAsync(Promise.resolve(4)),
1179
+ ]);
1180
+ const effectFn = vi.fn();
1181
+ effect(async (t) =>
1182
+ effectFn(
1183
+ await Promise.all(
1184
+ $array.get(t).map(async (state) => await state.get(t)),
1185
+ ),
1186
+ ),
1187
+ );
1188
+
1189
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1190
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
1191
+
1192
+ $array.clear();
1193
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1194
+ expect(effectFn).toHaveBeenLastCalledWith([]);
1195
+ });
1196
+ });
1197
+
1198
+ describe("item reactivity", () => {
1199
+ it("should call effect when item is updated", async () => {
1200
+ const $array = array<FlowStateAsync<number>>([
1201
+ stateAsync(Promise.resolve(0)),
1202
+ stateAsync(Promise.resolve(1)),
1203
+ stateAsync(Promise.resolve(2)),
1204
+ ]);
1205
+ const effectFn = vi.fn();
1206
+ const effect0Fn = vi.fn();
1207
+ const effect1Fn = vi.fn();
1208
+ const effect2Fn = vi.fn();
1209
+
1210
+ effect(async (t) =>
1211
+ effectFn(
1212
+ await Promise.all(
1213
+ $array.get(t).map(async (state) => await state.get(t)),
1214
+ ),
1215
+ ),
1216
+ );
1217
+ effect(async (t) => effect0Fn(await (await $array.get(t))[0].get(t)));
1218
+ effect(async (t) => effect1Fn(await (await $array.get(t))[1].get(t)));
1219
+ effect(async (t) => effect2Fn(await (await $array.get(t))[2].get(t)));
1220
+
1221
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1222
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
1223
+ await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(1));
1224
+ expect(effect0Fn).toHaveBeenLastCalledWith(0);
1225
+ await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(1));
1226
+ expect(effect1Fn).toHaveBeenLastCalledWith(1);
1227
+ await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(1));
1228
+ expect(effect2Fn).toHaveBeenLastCalledWith(2);
1229
+
1230
+ await (await $array.pick())[0].set(Promise.resolve(1));
1231
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1232
+ expect(effectFn).toHaveBeenLastCalledWith([1, 1, 2]);
1233
+ await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(2));
1234
+ expect(effect0Fn).toHaveBeenLastCalledWith(1);
1235
+ await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(1));
1236
+ expect(effect1Fn).toHaveBeenLastCalledWith(1);
1237
+ await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(1));
1238
+ expect(effect2Fn).toHaveBeenLastCalledWith(2);
1239
+
1240
+ await (await $array.pick())[1].set(Promise.resolve(2));
1241
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1242
+ expect(effectFn).toHaveBeenLastCalledWith([1, 2, 2]);
1243
+ await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(2));
1244
+ expect(effect0Fn).toHaveBeenLastCalledWith(1);
1245
+ await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(2));
1246
+ expect(effect1Fn).toHaveBeenLastCalledWith(2);
1247
+ await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(1));
1248
+ expect(effect2Fn).toHaveBeenLastCalledWith(2);
1249
+
1250
+ await (await $array.pick())[2].set(Promise.resolve(3));
1251
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
1252
+ expect(effectFn).toHaveBeenLastCalledWith([1, 2, 3]);
1253
+ await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(2));
1254
+ expect(effect0Fn).toHaveBeenLastCalledWith(1);
1255
+ await vi.waitFor(() => expect(effect1Fn).toHaveBeenCalledTimes(2));
1256
+ expect(effect1Fn).toHaveBeenLastCalledWith(2);
1257
+ await vi.waitFor(() => expect(effect2Fn).toHaveBeenCalledTimes(2));
1258
+ expect(effect2Fn).toHaveBeenLastCalledWith(3);
1259
+ });
1260
+
1261
+ it("should call effect when item is replaced", async () => {
1262
+ const $array = array<FlowStateAsync<number>>([
1263
+ stateAsync(Promise.resolve(0)),
1264
+ stateAsync(Promise.resolve(1)),
1265
+ stateAsync(Promise.resolve(2)),
1266
+ ]);
1267
+ const effectFn = vi.fn();
1268
+ const effect0Fn = vi.fn();
1269
+
1270
+ effect(async (t) =>
1271
+ effectFn(
1272
+ await Promise.all(
1273
+ $array.get(t).map(async (state) => await state.get(t)),
1274
+ ),
1275
+ ),
1276
+ );
1277
+ effect(async (t) => effect0Fn(await $array.get(t)[0].get(t)));
1278
+
1279
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1280
+ expect(effectFn).toHaveBeenLastCalledWith([0, 1, 2]);
1281
+ await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(1));
1282
+ expect(effect0Fn).toHaveBeenLastCalledWith(0);
1283
+
1284
+ $array.update(0, stateAsync(Promise.resolve(3)));
1285
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1286
+ expect(effectFn).toHaveBeenLastCalledWith([3, 1, 2]);
1287
+ await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(2));
1288
+ expect(effect0Fn).toHaveBeenLastCalledWith(3);
1289
+
1290
+ $array.shift();
1291
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1292
+ expect(effectFn).toHaveBeenLastCalledWith([1, 2]);
1293
+ await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(3));
1294
+ expect(effect0Fn).toHaveBeenLastCalledWith(1);
1295
+
1296
+ $array.unshift(stateAsync(Promise.resolve(4)));
1297
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(4));
1298
+ expect(effectFn).toHaveBeenLastCalledWith([4, 1, 2]);
1299
+ await vi.waitFor(() => expect(effect0Fn).toHaveBeenCalledTimes(4));
1300
+ expect(effect0Fn).toHaveBeenLastCalledWith(4);
1301
+ });
1302
+ });
1303
+
1304
+ describe("lastAction reactivity", () => {
1305
+ describe("set action", () => {
1306
+ it("should call effect when $lastAction is updated with set action", async () => {
1307
+ const $array = array([
1308
+ stateAsync(Promise.resolve(0)),
1309
+ stateAsync(Promise.resolve(1)),
1310
+ stateAsync(Promise.resolve(2)),
1311
+ ]);
1312
+ const effectFn = vi.fn();
1313
+ effect(async (t) => effectFn(await $array.$lastAction.get(t)));
1314
+
1315
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1316
+ expect(effectFn).toHaveBeenLastCalledWith(
1317
+ expect.objectContaining({
1318
+ type: "set",
1319
+ }),
1320
+ );
1321
+
1322
+ await $array.set([
1323
+ stateAsync(Promise.resolve(1)),
1324
+ stateAsync(Promise.resolve(2)),
1325
+ stateAsync(Promise.resolve(3)),
1326
+ ]);
1327
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1328
+ expect(effectFn).toHaveBeenLastCalledWith(
1329
+ expect.objectContaining({
1330
+ type: "set",
1331
+ }),
1332
+ );
1333
+ });
1334
+ });
1335
+
1336
+ describe("setItem action", () => {
1337
+ it("should call effect when $lastAction is updated with setItem action", async () => {
1338
+ const $array = array([
1339
+ stateAsync(Promise.resolve(0)),
1340
+ stateAsync(Promise.resolve(1)),
1341
+ stateAsync(Promise.resolve(2)),
1342
+ ]);
1343
+ const effectFn = vi.fn();
1344
+ effect(async (t) => effectFn(await $array.$lastAction.get(t)));
1345
+
1346
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1347
+
1348
+ const $newItem = stateAsync(Promise.resolve(10));
1349
+ $array.update(1, $newItem);
1350
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1351
+ expect(effectFn).toHaveBeenLastCalledWith(
1352
+ expect.objectContaining({
1353
+ type: "update",
1354
+ index: 1,
1355
+ }),
1356
+ );
1357
+ });
1358
+ });
1359
+
1360
+ describe("push action", () => {
1361
+ it("should call effect when $lastAction is updated with push action", async () => {
1362
+ const $array = array<FlowStateAsync<number>>();
1363
+ const effectFn = vi.fn();
1364
+ effect(async (t) => effectFn(await $array.$lastAction.get(t)));
1365
+
1366
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1367
+ expect(effectFn).toHaveBeenLastCalledWith(
1368
+ expect.objectContaining({
1369
+ type: "set",
1370
+ }),
1371
+ );
1372
+
1373
+ const $newItem = stateAsync(Promise.resolve(0));
1374
+ $array.push($newItem);
1375
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1376
+ expect(effectFn).toHaveBeenLastCalledWith(
1377
+ expect.objectContaining({
1378
+ type: "push",
1379
+ }),
1380
+ );
1381
+
1382
+ const $newItem2 = stateAsync(Promise.resolve(1));
1383
+ $array.push($newItem2);
1384
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1385
+ expect(effectFn).toHaveBeenLastCalledWith(
1386
+ expect.objectContaining({
1387
+ type: "push",
1388
+ }),
1389
+ );
1390
+ });
1391
+ });
1392
+
1393
+ describe("pop action", () => {
1394
+ it("should call effect when $lastAction is updated with pop action", async () => {
1395
+ const $array = array<FlowStateAsync<number>>([
1396
+ stateAsync(Promise.resolve(0)),
1397
+ stateAsync(Promise.resolve(1)),
1398
+ stateAsync(Promise.resolve(2)),
1399
+ ]);
1400
+ const effectFn = vi.fn();
1401
+ effect(async (t) => effectFn(await $array.$lastAction.get(t)));
1402
+
1403
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1404
+
1405
+ $array.pop();
1406
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1407
+ expect(effectFn).toHaveBeenLastCalledWith({
1408
+ type: "pop",
1409
+ });
1410
+
1411
+ $array.pop();
1412
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1413
+ expect(effectFn).toHaveBeenLastCalledWith({
1414
+ type: "pop",
1415
+ });
1416
+ });
1417
+ });
1418
+
1419
+ describe("unshift action", () => {
1420
+ it("should call effect when $lastAction is updated with unshift action", async () => {
1421
+ const $array = array<FlowStateAsync<number>>();
1422
+ const effectFn = vi.fn();
1423
+ effect(async (t) => effectFn(await $array.$lastAction.get(t)));
1424
+
1425
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1426
+ expect(effectFn).toHaveBeenLastCalledWith(
1427
+ expect.objectContaining({
1428
+ type: "set",
1429
+ }),
1430
+ );
1431
+
1432
+ const $newItem = stateAsync(Promise.resolve(0));
1433
+ $array.unshift($newItem);
1434
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1435
+ expect(effectFn).toHaveBeenLastCalledWith(
1436
+ expect.objectContaining({
1437
+ type: "unshift",
1438
+ }),
1439
+ );
1440
+
1441
+ const $newItem2 = stateAsync(Promise.resolve(1));
1442
+ $array.unshift($newItem2);
1443
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1444
+ expect(effectFn).toHaveBeenLastCalledWith(
1445
+ expect.objectContaining({
1446
+ type: "unshift",
1447
+ }),
1448
+ );
1449
+ });
1450
+ });
1451
+
1452
+ describe("shift action", () => {
1453
+ it("should call effect when $lastAction is updated with shift action", async () => {
1454
+ const $array = array<FlowStateAsync<number>>([
1455
+ stateAsync(Promise.resolve(0)),
1456
+ stateAsync(Promise.resolve(1)),
1457
+ stateAsync(Promise.resolve(2)),
1458
+ ]);
1459
+ const effectFn = vi.fn();
1460
+ effect(async (t) => effectFn(await $array.$lastAction.get(t)));
1461
+
1462
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1463
+
1464
+ $array.shift();
1465
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1466
+ expect(effectFn).toHaveBeenLastCalledWith({
1467
+ type: "shift",
1468
+ });
1469
+
1470
+ $array.shift();
1471
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1472
+ expect(effectFn).toHaveBeenLastCalledWith({
1473
+ type: "shift",
1474
+ });
1475
+ });
1476
+ });
1477
+
1478
+ describe("splice action", () => {
1479
+ it("should call effect when $lastAction is updated with splice action", async () => {
1480
+ const $array = array<FlowStateAsync<number>>([
1481
+ stateAsync(Promise.resolve(0)),
1482
+ stateAsync(Promise.resolve(1)),
1483
+ stateAsync(Promise.resolve(2)),
1484
+ stateAsync(Promise.resolve(3)),
1485
+ stateAsync(Promise.resolve(4)),
1486
+ ]);
1487
+ const effectFn = vi.fn();
1488
+ effect(async (t) => effectFn(await $array.$lastAction.get(t)));
1489
+
1490
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1491
+ expect(effectFn).toHaveBeenLastCalledWith(
1492
+ expect.objectContaining({
1493
+ type: "set",
1494
+ }),
1495
+ );
1496
+
1497
+ $array.splice(1, 2);
1498
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1499
+ expect(effectFn).toHaveBeenLastCalledWith(
1500
+ expect.objectContaining({
1501
+ type: "splice",
1502
+ start: 1,
1503
+ deleteCount: 2,
1504
+ }),
1505
+ );
1506
+
1507
+ $array.splice(
1508
+ 1,
1509
+ 0,
1510
+ stateAsync(Promise.resolve(1)),
1511
+ stateAsync(Promise.resolve(2)),
1512
+ );
1513
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(3));
1514
+ expect(effectFn).toHaveBeenLastCalledWith(
1515
+ expect.objectContaining({
1516
+ type: "splice",
1517
+ start: 1,
1518
+ deleteCount: 0,
1519
+ }),
1520
+ );
1521
+ });
1522
+ });
1523
+
1524
+ describe("clear action", () => {
1525
+ it("should call effect when $lastAction is updated with clear action", async () => {
1526
+ const $array = array<FlowStateAsync<number>>([
1527
+ stateAsync(Promise.resolve(0)),
1528
+ stateAsync(Promise.resolve(1)),
1529
+ stateAsync(Promise.resolve(2)),
1530
+ stateAsync(Promise.resolve(3)),
1531
+ stateAsync(Promise.resolve(4)),
1532
+ ]);
1533
+ const effectFn = vi.fn();
1534
+ effect(async (t) => effectFn(await $array.$lastAction.get(t)));
1535
+
1536
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(1));
1537
+ expect(effectFn).toHaveBeenLastCalledWith(
1538
+ expect.objectContaining({
1539
+ type: "set",
1540
+ }),
1541
+ );
1542
+
1543
+ $array.clear();
1544
+ await vi.waitFor(() => expect(effectFn).toHaveBeenCalledTimes(2));
1545
+ expect(effectFn).toHaveBeenLastCalledWith({
1546
+ type: "clear",
1547
+ });
1548
+ });
1549
+ });
1550
+ });
1551
+ });
1552
+ });
1553
+ });