@ersbeth/picoflow 2.0.2 → 2.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 (244) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/SKILL.md +106 -0
  4. package/dist/picoflow.js +204 -60
  5. package/dist/types/api/base/flowObservable.d.ts +3 -3
  6. package/dist/types/api/base/flowObservable.d.ts.map +1 -1
  7. package/dist/types/api/base/flowSubscribable.d.ts +2 -2
  8. package/dist/types/api/base/flowSubscribable.d.ts.map +1 -1
  9. package/dist/types/api/base/flowTracker.d.ts +1 -1
  10. package/dist/types/api/base/flowTracker.d.ts.map +1 -1
  11. package/dist/types/api/base/index.d.ts +4 -4
  12. package/dist/types/api/base/index.d.ts.map +1 -1
  13. package/dist/types/api/index.d.ts +2 -2
  14. package/dist/types/api/index.d.ts.map +1 -1
  15. package/dist/types/api/nodes/async/flowConstantAsync.d.ts +1 -1
  16. package/dist/types/api/nodes/async/flowConstantAsync.d.ts.map +1 -1
  17. package/dist/types/api/nodes/async/flowDerivationAsync.d.ts +3 -3
  18. package/dist/types/api/nodes/async/flowDerivationAsync.d.ts.map +1 -1
  19. package/dist/types/api/nodes/async/flowStateAsync.d.ts +3 -3
  20. package/dist/types/api/nodes/async/flowStateAsync.d.ts.map +1 -1
  21. package/dist/types/api/nodes/async/flowWritableDerivationAsync.d.ts +2 -2
  22. package/dist/types/api/nodes/async/flowWritableDerivationAsync.d.ts.map +1 -1
  23. package/dist/types/api/nodes/async/index.d.ts +4 -4
  24. package/dist/types/api/nodes/async/index.d.ts.map +1 -1
  25. package/dist/types/api/nodes/collections/flowArray.d.ts +7 -4
  26. package/dist/types/api/nodes/collections/flowArray.d.ts.map +1 -1
  27. package/dist/types/api/nodes/collections/flowMap.d.ts +3 -2
  28. package/dist/types/api/nodes/collections/flowMap.d.ts.map +1 -1
  29. package/dist/types/api/nodes/collections/index.d.ts +2 -2
  30. package/dist/types/api/nodes/collections/index.d.ts.map +1 -1
  31. package/dist/types/api/nodes/flowEffect.d.ts +2 -2
  32. package/dist/types/api/nodes/flowEffect.d.ts.map +1 -1
  33. package/dist/types/api/nodes/flowSignal.d.ts +1 -1
  34. package/dist/types/api/nodes/flowSignal.d.ts.map +1 -1
  35. package/dist/types/api/nodes/flowValue.d.ts +2 -2
  36. package/dist/types/api/nodes/flowValue.d.ts.map +1 -1
  37. package/dist/types/api/nodes/index.d.ts +7 -7
  38. package/dist/types/api/nodes/index.d.ts.map +1 -1
  39. package/dist/types/api/nodes/sync/flowConstant.d.ts +2 -2
  40. package/dist/types/api/nodes/sync/flowConstant.d.ts.map +1 -1
  41. package/dist/types/api/nodes/sync/flowDerivation.d.ts +3 -3
  42. package/dist/types/api/nodes/sync/flowDerivation.d.ts.map +1 -1
  43. package/dist/types/api/nodes/sync/flowState.d.ts +3 -3
  44. package/dist/types/api/nodes/sync/flowState.d.ts.map +1 -1
  45. package/dist/types/api/nodes/sync/flowWritableDerivation.d.ts +2 -2
  46. package/dist/types/api/nodes/sync/flowWritableDerivation.d.ts.map +1 -1
  47. package/dist/types/api/nodes/sync/index.d.ts +4 -4
  48. package/dist/types/api/nodes/sync/index.d.ts.map +1 -1
  49. package/dist/types/api/nodes/utils.d.ts +1 -1
  50. package/dist/types/api/nodes/utils.d.ts.map +1 -1
  51. package/dist/types/base/disposable.d.ts +1 -1
  52. package/dist/types/base/disposable.d.ts.map +1 -1
  53. package/dist/types/base/executionStack.d.ts +22 -1
  54. package/dist/types/base/executionStack.d.ts.map +1 -1
  55. package/dist/types/base/index.d.ts +5 -5
  56. package/dist/types/base/index.d.ts.map +1 -1
  57. package/dist/types/base/node.d.ts +4 -4
  58. package/dist/types/base/node.d.ts.map +1 -1
  59. package/dist/types/base/observable.d.ts +3 -3
  60. package/dist/types/base/observable.d.ts.map +1 -1
  61. package/dist/types/base/observer.d.ts +2 -2
  62. package/dist/types/base/observer.d.ts.map +1 -1
  63. package/dist/types/converters/index.d.ts +1 -1
  64. package/dist/types/converters/index.d.ts.map +1 -1
  65. package/dist/types/converters/solid.d.ts +12 -8
  66. package/dist/types/converters/solid.d.ts.map +1 -1
  67. package/dist/types/index.d.ts +2 -2
  68. package/dist/types/index.d.ts.map +1 -1
  69. package/dist/types/nodes/actionNode.d.ts +23 -0
  70. package/dist/types/nodes/actionNode.d.ts.map +1 -0
  71. package/dist/types/nodes/arrayNode.d.ts +4 -3
  72. package/dist/types/nodes/arrayNode.d.ts.map +1 -1
  73. package/dist/types/nodes/effectNode.d.ts +18 -2
  74. package/dist/types/nodes/effectNode.d.ts.map +1 -1
  75. package/dist/types/nodes/index.d.ts +8 -7
  76. package/dist/types/nodes/index.d.ts.map +1 -1
  77. package/dist/types/nodes/mapNode.d.ts +4 -3
  78. package/dist/types/nodes/mapNode.d.ts.map +1 -1
  79. package/dist/types/nodes/signalNode.d.ts +2 -2
  80. package/dist/types/nodes/signalNode.d.ts.map +1 -1
  81. package/dist/types/nodes/valueAsyncNode.d.ts +3 -3
  82. package/dist/types/nodes/valueAsyncNode.d.ts.map +1 -1
  83. package/dist/types/nodes/valueNode.d.ts +3 -3
  84. package/dist/types/nodes/valueNode.d.ts.map +1 -1
  85. package/dist/types/nodes/valueSyncNode.d.ts +3 -3
  86. package/dist/types/nodes/valueSyncNode.d.ts.map +1 -1
  87. package/dist/types/schedulers/asyncScheduler.d.ts +2 -1
  88. package/dist/types/schedulers/asyncScheduler.d.ts.map +1 -1
  89. package/dist/types/schedulers/index.d.ts +4 -4
  90. package/dist/types/schedulers/index.d.ts.map +1 -1
  91. package/dist/types/schedulers/syncResolver.d.ts +1 -2
  92. package/dist/types/schedulers/syncResolver.d.ts.map +1 -1
  93. package/dist/types/schedulers/syncScheduler.d.ts +1 -1
  94. package/dist/types/schedulers/syncScheduler.d.ts.map +1 -1
  95. package/package.json +24 -1
  96. package/.gitlab-ci.yml +0 -24
  97. package/.vscode/settings.json +0 -5
  98. package/CHANGELOG.md +0 -94
  99. package/biome.json +0 -47
  100. package/docs/.vitepress/config.mts +0 -145
  101. package/docs/api/functions/array.md +0 -35
  102. package/docs/api/functions/constant.md +0 -33
  103. package/docs/api/functions/constantAsync.md +0 -69
  104. package/docs/api/functions/derivation.md +0 -34
  105. package/docs/api/functions/derivationAsync.md +0 -34
  106. package/docs/api/functions/from.md +0 -129
  107. package/docs/api/functions/isDisposable.md +0 -27
  108. package/docs/api/functions/map.md +0 -36
  109. package/docs/api/functions/signal.md +0 -21
  110. package/docs/api/functions/state.md +0 -67
  111. package/docs/api/functions/stateAsync.md +0 -69
  112. package/docs/api/functions/subscribe.md +0 -40
  113. package/docs/api/functions/writableDerivation.md +0 -33
  114. package/docs/api/functions/writableDerivationAsync.md +0 -34
  115. package/docs/api/index.md +0 -61
  116. package/docs/api/interfaces/FlowArray.md +0 -439
  117. package/docs/api/interfaces/FlowConstant.md +0 -220
  118. package/docs/api/interfaces/FlowConstantAsync.md +0 -221
  119. package/docs/api/interfaces/FlowDerivation.md +0 -241
  120. package/docs/api/interfaces/FlowDerivationAsync.md +0 -242
  121. package/docs/api/interfaces/FlowDisposable.md +0 -59
  122. package/docs/api/interfaces/FlowEffect.md +0 -64
  123. package/docs/api/interfaces/FlowMap.md +0 -374
  124. package/docs/api/interfaces/FlowObservable.md +0 -155
  125. package/docs/api/interfaces/FlowSignal.md +0 -156
  126. package/docs/api/interfaces/FlowState.md +0 -269
  127. package/docs/api/interfaces/FlowStateAsync.md +0 -268
  128. package/docs/api/interfaces/FlowSubscribable.md +0 -55
  129. package/docs/api/interfaces/FlowTracker.md +0 -61
  130. package/docs/api/interfaces/FlowValue.md +0 -222
  131. package/docs/api/interfaces/FlowWritableDerivation.md +0 -292
  132. package/docs/api/interfaces/FlowWritableDerivationAsync.md +0 -293
  133. package/docs/api/type-aliases/DerivationFunction.md +0 -28
  134. package/docs/api/type-aliases/DerivationFunctionAsync.md +0 -28
  135. package/docs/api/type-aliases/FlowArrayAction.md +0 -60
  136. package/docs/api/type-aliases/FlowDataTracker.md +0 -33
  137. package/docs/api/type-aliases/FlowMapAction.md +0 -48
  138. package/docs/api/type-aliases/FlowOnDataListener.md +0 -33
  139. package/docs/api/type-aliases/FlowOnErrorListener.md +0 -27
  140. package/docs/api/type-aliases/FlowOnPendingListener.md +0 -21
  141. package/docs/api/type-aliases/FlowReadonly.md +0 -22
  142. package/docs/api/type-aliases/InitFunction.md +0 -21
  143. package/docs/api/type-aliases/InitFunctionAsync.md +0 -21
  144. package/docs/api/type-aliases/NotPromise.md +0 -21
  145. package/docs/api/type-aliases/UpdateFunction.md +0 -27
  146. package/docs/api/type-aliases/UpdateFunctionAsync.md +0 -27
  147. package/docs/api/typedoc-sidebar.json +0 -65
  148. package/docs/examples/examples.md +0 -2311
  149. package/docs/examples/patterns.md +0 -649
  150. package/docs/guide/advanced/architecture.md +0 -1234
  151. package/docs/guide/advanced/disposal.md +0 -426
  152. package/docs/guide/advanced/migration-v1.md +0 -464
  153. package/docs/guide/advanced/migration-v2.md +0 -204
  154. package/docs/guide/advanced/solidjs.md +0 -135
  155. package/docs/guide/introduction/concepts.md +0 -57
  156. package/docs/guide/introduction/conventions.md +0 -30
  157. package/docs/guide/introduction/getting-started.md +0 -139
  158. package/docs/guide/introduction/lifecycle.md +0 -368
  159. package/docs/guide/primitives/array.md +0 -286
  160. package/docs/guide/primitives/constant.md +0 -207
  161. package/docs/guide/primitives/derivations.md +0 -281
  162. package/docs/guide/primitives/effects.md +0 -372
  163. package/docs/guide/primitives/map.md +0 -265
  164. package/docs/guide/primitives/overview.md +0 -92
  165. package/docs/guide/primitives/signal.md +0 -222
  166. package/docs/guide/primitives/state.md +0 -272
  167. package/docs/index.md +0 -47
  168. package/docs/public/logo.svg +0 -1
  169. package/src/api/base/flowDisposable.ts +0 -44
  170. package/src/api/base/flowObservable.ts +0 -28
  171. package/src/api/base/flowSubscribable.ts +0 -87
  172. package/src/api/base/flowTracker.ts +0 -7
  173. package/src/api/base/index.ts +0 -4
  174. package/src/api/index.ts +0 -2
  175. package/src/api/nodes/async/flowConstantAsync.ts +0 -36
  176. package/src/api/nodes/async/flowDerivationAsync.ts +0 -42
  177. package/src/api/nodes/async/flowStateAsync.ts +0 -47
  178. package/src/api/nodes/async/flowWritableDerivationAsync.ts +0 -33
  179. package/src/api/nodes/async/index.ts +0 -4
  180. package/src/api/nodes/collections/flowArray.ts +0 -155
  181. package/src/api/nodes/collections/flowMap.ts +0 -115
  182. package/src/api/nodes/collections/index.ts +0 -2
  183. package/src/api/nodes/flowEffect.ts +0 -42
  184. package/src/api/nodes/flowSignal.ts +0 -28
  185. package/src/api/nodes/flowValue.ts +0 -37
  186. package/src/api/nodes/index.ts +0 -7
  187. package/src/api/nodes/sync/flowConstant.ts +0 -33
  188. package/src/api/nodes/sync/flowDerivation.ts +0 -41
  189. package/src/api/nodes/sync/flowState.ts +0 -45
  190. package/src/api/nodes/sync/flowWritableDerivation.ts +0 -31
  191. package/src/api/nodes/sync/index.ts +0 -4
  192. package/src/api/nodes/utils.ts +0 -24
  193. package/src/base/disposable.ts +0 -18
  194. package/src/base/executionStack.ts +0 -42
  195. package/src/base/index.ts +0 -5
  196. package/src/base/node.ts +0 -98
  197. package/src/base/observable.ts +0 -92
  198. package/src/base/observer.ts +0 -51
  199. package/src/converters/index.ts +0 -1
  200. package/src/converters/solid.ts +0 -109
  201. package/src/index.ts +0 -2
  202. package/src/nodes/arrayNode.ts +0 -180
  203. package/src/nodes/effectNode.ts +0 -58
  204. package/src/nodes/index.ts +0 -7
  205. package/src/nodes/mapNode.ts +0 -125
  206. package/src/nodes/signalNode.ts +0 -19
  207. package/src/nodes/valueAsyncNode.ts +0 -85
  208. package/src/nodes/valueNode.ts +0 -148
  209. package/src/nodes/valueSyncNode.ts +0 -125
  210. package/src/schedulers/asyncResolver.ts +0 -78
  211. package/src/schedulers/asyncScheduler.ts +0 -66
  212. package/src/schedulers/index.ts +0 -4
  213. package/src/schedulers/pendingError.ts +0 -13
  214. package/src/schedulers/scheduler.ts +0 -9
  215. package/src/schedulers/syncResolver.ts +0 -69
  216. package/src/schedulers/syncScheduler.ts +0 -55
  217. package/test/base/pendingError.test.ts +0 -67
  218. package/test/converters/solid.derivation.browser.test.tsx +0 -69
  219. package/test/converters/solid.node.test.ts +0 -654
  220. package/test/converters/solid.state.browser.test.tsx +0 -1592
  221. package/test/reactivity/flowSignal.test.ts +0 -226
  222. package/test/reactivity/nodes/async/asyncScheduler/asyncResolver.test.ts +0 -593
  223. package/test/reactivity/nodes/async/asyncScheduler/asyncScheduler.test.ts +0 -317
  224. package/test/reactivity/nodes/async/flowConstantAsync.test.ts +0 -652
  225. package/test/reactivity/nodes/async/flowDerivation.test.ts +0 -898
  226. package/test/reactivity/nodes/async/flowDerivationAsync.test.ts +0 -1716
  227. package/test/reactivity/nodes/async/flowStateAsync.test.ts +0 -708
  228. package/test/reactivity/nodes/async/flowWritableDerivationAsync.test.ts +0 -614
  229. package/test/reactivity/nodes/collections/flowArray.asyncStates.test.ts +0 -1289
  230. package/test/reactivity/nodes/collections/flowArray.scalars.test.ts +0 -961
  231. package/test/reactivity/nodes/collections/flowArray.states.test.ts +0 -1035
  232. package/test/reactivity/nodes/collections/flowMap.asyncStates.test.ts +0 -960
  233. package/test/reactivity/nodes/collections/flowMap.scalars.test.ts +0 -775
  234. package/test/reactivity/nodes/collections/flowMap.states.test.ts +0 -958
  235. package/test/reactivity/nodes/sync/flowConstant.test.ts +0 -377
  236. package/test/reactivity/nodes/sync/flowDerivation.test.ts +0 -896
  237. package/test/reactivity/nodes/sync/flowState.test.ts +0 -341
  238. package/test/reactivity/nodes/sync/flowWritableDerivation.test.ts +0 -603
  239. package/test/vitest.d.ts +0 -10
  240. package/tsconfig.json +0 -37
  241. package/typedoc.json +0 -37
  242. package/vite.config.ts +0 -31
  243. package/vitest.browser.config.ts +0 -21
  244. package/vitest.config.ts +0 -17
@@ -1,1035 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import { array, type FlowState, state, subscribe } from "~";
3
-
4
- describe("FlowArray", () => {
5
- describe("unit", () => {
6
- describe("initialization", () => {
7
- it("should initialize with provided FlowState values", async () => {
8
- const $array = array([state(1), state(2), state(3)]);
9
-
10
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([1, 2, 3]);
11
- });
12
-
13
- it("should initialize with empty array when no value provided", async () => {
14
- const $array = array<FlowState<number>>();
15
-
16
- expect(await $array.pick()).toEqual([]);
17
- });
18
- });
19
-
20
- describe("disposal", () => {
21
- it("should have disposed property set to false initially and true after disposal", () => {
22
- const $array = array([state(1), state(2), state(3)]);
23
- expect($array.disposed).toBe(false);
24
- $array.dispose();
25
- expect($array.disposed).toBe(true);
26
- });
27
-
28
- it("should throw error when disposed twice", () => {
29
- const $array = array([state(1), state(2), state(3)]);
30
- $array.dispose();
31
- expect(() => $array.dispose()).toThrow("[PicoFlow] Primitive is disposed");
32
- });
33
-
34
- it("should throw when pick is called after disposal", async () => {
35
- const $array = array([state(1), state(2), state(3)]);
36
- $array.dispose();
37
- await expect($array.pick()).rejects.toThrow("[PicoFlow] Primitive is disposed");
38
- });
39
-
40
- it("should throw when set is called after disposal", () => {
41
- const $array = array([state(1), state(2), state(3)]);
42
- $array.dispose();
43
- expect(() => $array.set([state(1)])).toThrow("[PicoFlow] Primitive is disposed");
44
- });
45
-
46
- it("should throw when update is called after disposal", () => {
47
- const $array = array([state(1), state(2), state(3)]);
48
- $array.dispose();
49
- expect(() => $array.update(0, state(3))).toThrow("[PicoFlow] Primitive is disposed");
50
- });
51
-
52
- it("should throw when push is called after disposal", () => {
53
- const $array = array([state(1), state(2), state(3)]);
54
- $array.dispose();
55
- expect(() => $array.push(state(3))).toThrow("[PicoFlow] Primitive is disposed");
56
- });
57
-
58
- it("should throw when pop is called after disposal", () => {
59
- const $array = array([state(1), state(2), state(3)]);
60
- $array.dispose();
61
- expect(() => $array.pop()).toThrow("[PicoFlow] Primitive is disposed");
62
- });
63
-
64
- it("should throw when unshift is called after disposal", () => {
65
- const $array = array([state(1), state(2), state(3)]);
66
- $array.dispose();
67
- expect(() => $array.unshift(state(3))).toThrow("[PicoFlow] Primitive is disposed");
68
- });
69
-
70
- it("should throw when shift is called after disposal", () => {
71
- const $array = array([state(1), state(2), state(3)]);
72
- $array.dispose();
73
- expect(() => $array.shift()).toThrow("[PicoFlow] Primitive is disposed");
74
- });
75
-
76
- it("should throw when splice is called after disposal", () => {
77
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
78
- $array.dispose();
79
- expect(() => $array.splice(1, 2)).toThrow("[PicoFlow] Primitive is disposed");
80
- });
81
-
82
- it("should throw when clear is called after disposal", () => {
83
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
84
- $array.dispose();
85
- expect(() => $array.clear()).toThrow("[PicoFlow] Primitive is disposed");
86
- });
87
- });
88
-
89
- describe("length", () => {
90
- it("should return correct length", () => {
91
- const $array = array([state(1), state(2), state(3)]);
92
- expect($array.length).toBe(3);
93
- });
94
-
95
- it("should return zero for empty array", () => {
96
- const $array = array<FlowState<number>>([]);
97
- expect($array.length).toBe(0);
98
- });
99
-
100
- it("should throw when length is accessed after disposal", () => {
101
- const $array = array([state(1), state(2), state(3)]);
102
- $array.dispose();
103
- expect(() => $array.length).toThrow("[PicoFlow] Primitive is disposed");
104
- });
105
-
106
- it("should update length when array is modified", () => {
107
- const $array = array<FlowState<number>>([]);
108
- expect($array.length).toBe(0);
109
-
110
- $array.push(state(1));
111
- expect($array.length).toBe(1);
112
-
113
- $array.push(state(2));
114
- expect($array.length).toBe(2);
115
-
116
- $array.pop();
117
- expect($array.length).toBe(1);
118
-
119
- $array.unshift(state(0));
120
- expect($array.length).toBe(2);
121
-
122
- $array.shift();
123
- expect($array.length).toBe(1);
124
-
125
- $array.clear();
126
- expect($array.length).toBe(0);
127
- });
128
- });
129
-
130
- describe("set", () => {
131
- it("should update array when set is called with FlowStates", async () => {
132
- const $array = array([state(0), state(1), state(2)]);
133
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0, 1, 2]);
134
-
135
- $array.set([state(1), state(2), state(3)]);
136
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([1, 2, 3]);
137
- });
138
- });
139
-
140
- describe("update", () => {
141
- it("should update item at specific index", async () => {
142
- const $array = array([state(0), state(1), state(2)]);
143
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0, 1, 2]);
144
-
145
- $array.update(0, state(1));
146
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([1, 1, 2]);
147
-
148
- $array.update(1, state(2));
149
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([1, 2, 2]);
150
-
151
- $array.update(2, state(3));
152
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([1, 2, 3]);
153
- });
154
-
155
- it("should throw when update is called with negative index", () => {
156
- const $array = array([state(1), state(2), state(3)]);
157
- expect(() => $array.update(-1, state(0))).toThrow("[PicoFlow] Index out of bounds");
158
- });
159
-
160
- it("should throw when update is called with index >= length", () => {
161
- const $array = array([state(1), state(2), state(3)]);
162
- expect(() => $array.update(3, state(0))).toThrow("[PicoFlow] Index out of bounds");
163
- expect(() => $array.update(10, state(0))).toThrow("[PicoFlow] Index out of bounds");
164
- });
165
-
166
- it("should handle boundary indices correctly", async () => {
167
- const $array = array([state(0), state(1), state(2)]);
168
- $array.update(0, state(10));
169
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([10, 1, 2]);
170
-
171
- $array.update(2, state(20));
172
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([10, 1, 20]);
173
- });
174
- });
175
-
176
- describe("push", () => {
177
- it("should append FlowState to end of array", async () => {
178
- const $array = array<FlowState<number>>([]);
179
- expect(await $array.pick()).toEqual([]);
180
-
181
- $array.push(state(0));
182
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0]);
183
-
184
- $array.push(state(1));
185
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0, 1]);
186
-
187
- $array.push(state(2));
188
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0, 1, 2]);
189
- });
190
-
191
- it("should handle operations on empty array", () => {
192
- const $array = array<FlowState<number>>([]);
193
- expect($array.length).toBe(0);
194
- $array.push(state(1));
195
- expect($array.length).toBe(1);
196
- });
197
- });
198
-
199
- describe("pop", () => {
200
- it("should remove last FlowState from array", async () => {
201
- const $array = array([state(0), state(1), state(2)]);
202
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0, 1, 2]);
203
-
204
- $array.pop();
205
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0, 1]);
206
-
207
- $array.pop();
208
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0]);
209
-
210
- $array.pop();
211
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([]);
212
- });
213
-
214
- it("should handle operations on empty array", () => {
215
- const $array = array<FlowState<number>>([]);
216
- expect($array.length).toBe(0);
217
- $array.pop();
218
- expect($array.length).toBe(0);
219
- });
220
- });
221
-
222
- describe("unshift", () => {
223
- it("should insert FlowState at beginning of array", async () => {
224
- const $array = array<FlowState<number>>([]);
225
- expect(await $array.pick()).toEqual([]);
226
-
227
- $array.unshift(state(0));
228
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0]);
229
-
230
- $array.unshift(state(1));
231
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([1, 0]);
232
-
233
- $array.unshift(state(2));
234
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([2, 1, 0]);
235
- });
236
- });
237
-
238
- describe("shift", () => {
239
- it("should remove first FlowState from array", async () => {
240
- const $array = array([state(0), state(1), state(2)]);
241
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0, 1, 2]);
242
-
243
- $array.shift();
244
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([1, 2]);
245
-
246
- $array.shift();
247
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([2]);
248
-
249
- $array.shift();
250
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([]);
251
- });
252
-
253
- it("should handle operations on empty array", () => {
254
- const $array = array<FlowState<number>>([]);
255
- expect($array.length).toBe(0);
256
- $array.shift();
257
- expect($array.length).toBe(0);
258
- });
259
- });
260
-
261
- describe("splice", () => {
262
- it("should remove FlowStates from array", async () => {
263
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
264
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0, 1, 2, 3, 4]);
265
-
266
- $array.splice(1, 2);
267
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0, 3, 4]);
268
- });
269
-
270
- it("should insert FlowStates into array", async () => {
271
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
272
- $array.splice(1, 0, state(10), state(11));
273
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([
274
- 0, 10, 11, 1, 2, 3, 4,
275
- ]);
276
- });
277
-
278
- it("should replace FlowStates in array", async () => {
279
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
280
- $array.splice(1, 2, state(10), state(11));
281
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([
282
- 0, 10, 11, 3, 4,
283
- ]);
284
- });
285
- });
286
-
287
- describe("clear", () => {
288
- it("should remove all FlowStates from array", async () => {
289
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
290
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0, 1, 2, 3, 4]);
291
-
292
- $array.clear();
293
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([]);
294
- });
295
- });
296
-
297
- describe("$lastAction", () => {
298
- describe("set action", () => {
299
- it("should initialize $lastAction with set action on creation", async () => {
300
- const $array = array([state(1), state(2), state(3)]);
301
- const action = await $array.$lastAction.pick();
302
- expect(action.type).toBe("set");
303
- if (action.type === "set") {
304
- expect(action.setItems).toHaveLength(3);
305
- expect(action.setItems[0]).toBeInstanceOf(Object);
306
- expect(action.setItems[1]).toBeInstanceOf(Object);
307
- expect(action.setItems[2]).toBeInstanceOf(Object);
308
- expect(action.clearedItems).toEqual([]);
309
- }
310
- });
311
-
312
- it("should update $lastAction with set action when set is called", async () => {
313
- const $array = array([state(0), state(1), state(2)]);
314
- $array.set([state(1), state(2), state(3)]);
315
- const action = await $array.$lastAction.pick();
316
- expect(action.type).toBe("set");
317
- if (action.type === "set") {
318
- expect(action.setItems).toHaveLength(3);
319
- expect(action.clearedItems).toHaveLength(3);
320
- }
321
- });
322
-
323
- it("should have correct structure for set action (setItems and clearedItems)", async () => {
324
- const $array = array([state(1), state(2), state(3)]);
325
- const action = await $array.$lastAction.pick();
326
- expect(action).toHaveProperty("type", "set");
327
- if (action.type === "set") {
328
- expect(action).toHaveProperty("setItems");
329
- expect(action).toHaveProperty("clearedItems");
330
- expect(Array.isArray(action.setItems)).toBe(true);
331
- expect(Array.isArray(action.clearedItems)).toBe(true);
332
- expect(action.setItems).toHaveLength(3);
333
- expect(action.clearedItems).toEqual([]);
334
- }
335
- });
336
- });
337
-
338
- describe("update action", () => {
339
- it("should update $lastAction with update action when update is called", async () => {
340
- const $array = array([state(0), state(1), state(2)]);
341
- const $newItem = state(10);
342
- $array.update(1, $newItem);
343
- const action = await $array.$lastAction.pick();
344
- expect(action.type).toBe("update");
345
- if (action.type === "update") {
346
- expect(action.index).toBe(1);
347
- expect(action.setItem).toBe($newItem);
348
- expect(action.clearedItem).toBeInstanceOf(Object);
349
- }
350
- });
351
-
352
- it("should have correct structure for update action (index, setItem, clearedItem)", async () => {
353
- const $array = array([state(1), state(2), state(3)]);
354
- const $newItem = state(10);
355
- $array.update(0, $newItem);
356
- const action = await $array.$lastAction.pick();
357
- expect(action).toHaveProperty("type", "update");
358
- if (action.type === "update") {
359
- expect(action).toHaveProperty("index");
360
- expect(action).toHaveProperty("setItem");
361
- expect(action).toHaveProperty("clearedItem");
362
- expect(action.index).toBe(0);
363
- expect(action.setItem).toBe($newItem);
364
- expect(action.clearedItem).toBeInstanceOf(Object);
365
- }
366
- });
367
- });
368
-
369
- describe("push action", () => {
370
- it("should update $lastAction with push action when push is called", async () => {
371
- const $array = array<FlowState<number>>([]);
372
- const $newItem = state(42);
373
- $array.push($newItem);
374
- const action = await $array.$lastAction.pick();
375
- expect(action.type).toBe("push");
376
- if (action.type === "push") {
377
- expect(action.addedItem).toBe($newItem);
378
- }
379
- });
380
-
381
- it("should have correct structure for push action (addedItem)", async () => {
382
- const $array = array<FlowState<number>>([]);
383
- const $newItem = state(100);
384
- $array.push($newItem);
385
- const action = await $array.$lastAction.pick();
386
- expect(action).toHaveProperty("type", "push");
387
- if (action.type === "push") {
388
- expect(action).toHaveProperty("addedItem");
389
- expect(action.addedItem).toBe($newItem);
390
- }
391
- });
392
- });
393
-
394
- describe("pop action", () => {
395
- it("should update $lastAction with pop action when pop is called", async () => {
396
- const $array = array([state(1), state(2), state(3)]);
397
- $array.pop();
398
- const action = await $array.$lastAction.pick();
399
- expect(action).toEqual({
400
- type: "pop",
401
- removedItem: expect.any(Object),
402
- });
403
- });
404
-
405
- it("should have correct structure for pop action (removedItem)", async () => {
406
- const $array = array([state(1), state(2), state(3)]);
407
- $array.pop();
408
- const action = await $array.$lastAction.pick();
409
- expect(action).toHaveProperty("type", "pop");
410
- if (action.type === "pop") {
411
- expect(action).toHaveProperty("removedItem");
412
- expect(action.removedItem).toBeInstanceOf(Object);
413
- }
414
- });
415
-
416
- it("should have removedItem undefined when pop is called on empty array", async () => {
417
- const $array = array<FlowState<number>>([]);
418
- $array.pop();
419
- const action = await $array.$lastAction.pick();
420
- expect(action).toHaveProperty("type", "pop");
421
- if (action.type === "pop") {
422
- expect(action).toHaveProperty("removedItem");
423
- expect(action.removedItem).toBeUndefined();
424
- }
425
- });
426
- });
427
-
428
- describe("unshift action", () => {
429
- it("should update $lastAction with unshift action when unshift is called", async () => {
430
- const $array = array<FlowState<number>>([]);
431
- const $newItem = state(42);
432
- $array.unshift($newItem);
433
- const action = await $array.$lastAction.pick();
434
- expect(action.type).toBe("unshift");
435
- if (action.type === "unshift") {
436
- expect(action.addedItem).toBe($newItem);
437
- }
438
- });
439
-
440
- it("should have correct structure for unshift action (addedItem)", async () => {
441
- const $array = array<FlowState<number>>([]);
442
- const $newItem = state(200);
443
- $array.unshift($newItem);
444
- const action = await $array.$lastAction.pick();
445
- expect(action).toHaveProperty("type", "unshift");
446
- if (action.type === "unshift") {
447
- expect(action).toHaveProperty("addedItem");
448
- expect(action.addedItem).toBe($newItem);
449
- }
450
- });
451
- });
452
-
453
- describe("shift action", () => {
454
- it("should update $lastAction with shift action when shift is called", async () => {
455
- const $array = array([state(1), state(2), state(3)]);
456
- $array.shift();
457
- const action = await $array.$lastAction.pick();
458
- expect(action).toEqual({
459
- type: "shift",
460
- removedItem: expect.any(Object),
461
- });
462
- });
463
-
464
- it("should have correct structure for shift action (removedItem)", async () => {
465
- const $array = array([state(1), state(2), state(3)]);
466
- $array.shift();
467
- const action = await $array.$lastAction.pick();
468
- expect(action).toHaveProperty("type", "shift");
469
- if (action.type === "shift") {
470
- expect(action).toHaveProperty("removedItem");
471
- expect(action.removedItem).toBeInstanceOf(Object);
472
- }
473
- });
474
-
475
- it("should have removedItem undefined when shift is called on empty array", async () => {
476
- const $array = array<FlowState<number>>([]);
477
- $array.shift();
478
- const action = await $array.$lastAction.pick();
479
- expect(action).toHaveProperty("type", "shift");
480
- if (action.type === "shift") {
481
- expect(action).toHaveProperty("removedItem");
482
- expect(action.removedItem).toBeUndefined();
483
- }
484
- });
485
- });
486
-
487
- describe("splice action", () => {
488
- it("should update $lastAction with splice action when splice is called", async () => {
489
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
490
- const $newItem1 = state(10);
491
- const $newItem2 = state(11);
492
- $array.splice(1, 2, $newItem1, $newItem2);
493
- const action = await $array.$lastAction.pick();
494
- expect(action.type).toBe("splice");
495
- if (action.type === "splice") {
496
- expect(action.start).toBe(1);
497
- expect(action.deleteCount).toBe(2);
498
- expect(action.addedItems).toHaveLength(2);
499
- expect(action.addedItems[0]).toBe($newItem1);
500
- expect(action.addedItems[1]).toBe($newItem2);
501
- expect(action.removedItems).toHaveLength(2);
502
- }
503
- });
504
-
505
- it("should have correct structure for splice action (start, deleteCount, addedItems, removedItems)", async () => {
506
- const $array = array<FlowState<number>>([state(0), state(1), state(2)]);
507
- const $newItem = state(100);
508
- $array.splice(1, 1, $newItem);
509
- const action = await $array.$lastAction.pick();
510
- expect(action).toHaveProperty("type", "splice");
511
- if (action.type === "splice") {
512
- expect(action).toHaveProperty("start");
513
- expect(action).toHaveProperty("deleteCount");
514
- expect(action).toHaveProperty("addedItems");
515
- expect(action).toHaveProperty("removedItems");
516
- expect(action.start).toBe(1);
517
- expect(action.deleteCount).toBe(1);
518
- expect(Array.isArray(action.addedItems)).toBe(true);
519
- expect(Array.isArray(action.removedItems)).toBe(true);
520
- expect(action.addedItems).toHaveLength(1);
521
- expect(action.addedItems[0]).toBe($newItem);
522
- expect(action.removedItems).toHaveLength(1);
523
- }
524
- });
525
- });
526
-
527
- describe("clear action", () => {
528
- it("should update $lastAction with clear action when clear is called", async () => {
529
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
530
- $array.clear();
531
- const action = await $array.$lastAction.pick();
532
- expect(action).toEqual({
533
- type: "clear",
534
- clearedItems: expect.any(Array),
535
- });
536
- if (action.type === "clear") {
537
- expect(action.clearedItems).toHaveLength(5);
538
- }
539
- });
540
-
541
- it("should have correct structure for clear action (clearedItems)", async () => {
542
- const $array = array<FlowState<number>>([state(1), state(2), state(3)]);
543
- $array.clear();
544
- const action = await $array.$lastAction.pick();
545
- expect(action).toHaveProperty("type", "clear");
546
- if (action.type === "clear") {
547
- expect(action).toHaveProperty("clearedItems");
548
- expect(Array.isArray(action.clearedItems)).toBe(true);
549
- expect(action.clearedItems).toHaveLength(3);
550
- }
551
- });
552
- });
553
- });
554
-
555
- describe("edge cases", () => {
556
- it("should handle multiple rapid operations", async () => {
557
- const $array = array<FlowState<number>>([]);
558
- $array.push(state(1));
559
- $array.push(state(2));
560
- $array.push(state(3));
561
- $array.pop();
562
- $array.shift();
563
- $array.unshift(state(0));
564
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([0, 2]);
565
- });
566
-
567
- it("should handle boundary indices correctly", async () => {
568
- const $array = array([state(0), state(1), state(2)]);
569
- $array.update(0, state(10));
570
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([10, 1, 2]);
571
-
572
- $array.update(2, state(20));
573
- expect(await Promise.all((await $array.pick()).map((state) => state.pick()))).toEqual([10, 1, 20]);
574
- });
575
- });
576
- });
577
-
578
- describe("integration", () => {
579
- describe("with effects", () => {
580
- describe("reactivity", () => {
581
- it("should call effect when initialized", async () => {
582
- const $array = array([state(1), state(2), state(3)]);
583
- const onData = vi.fn();
584
- subscribe((t) => $array.get(t).map((state) => state.get(t)), onData);
585
-
586
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
587
- expect(onData).toHaveBeenLastCalledWith([1, 2, 3]);
588
- });
589
-
590
- it("should call effect when updated with set", async () => {
591
- const $array = array<FlowState<number>>([state(0), state(1), state(2)]);
592
- const onData = vi.fn();
593
- subscribe((t) => $array.get(t).map((state) => state.get(t)), onData);
594
-
595
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
596
- expect(onData).toHaveBeenLastCalledWith([0, 1, 2]);
597
-
598
- $array.set([state(1), state(2), state(3)]);
599
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
600
- expect(onData).toHaveBeenLastCalledWith([1, 2, 3]);
601
- });
602
-
603
- it("should call effect when updated with push/pop", async () => {
604
- const $array = array<FlowState<number>>();
605
- const onData = vi.fn();
606
- subscribe((t) => $array.get(t).map((state) => state.get(t)), onData);
607
-
608
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
609
- expect(onData).toHaveBeenLastCalledWith([]);
610
-
611
- $array.push(state(0));
612
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
613
- expect(onData).toHaveBeenLastCalledWith([0]);
614
-
615
- $array.push(state(1));
616
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
617
- expect(onData).toHaveBeenLastCalledWith([0, 1]);
618
-
619
- $array.push(state(2));
620
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(4));
621
- expect(onData).toHaveBeenLastCalledWith([0, 1, 2]);
622
-
623
- $array.pop();
624
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(5));
625
- expect(onData).toHaveBeenLastCalledWith([0, 1]);
626
-
627
- $array.pop();
628
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(6));
629
- expect(onData).toHaveBeenLastCalledWith([0]);
630
-
631
- $array.pop();
632
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(7));
633
- expect(onData).toHaveBeenLastCalledWith([]);
634
- });
635
-
636
- it("should call effect when updated with unshift/shift", async () => {
637
- const $array = array<FlowState<number>>();
638
- const onData = vi.fn();
639
- subscribe((t) => $array.get(t).map((state) => state.get(t)), onData);
640
-
641
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
642
- expect(onData).toHaveBeenLastCalledWith([]);
643
-
644
- $array.unshift(state(0));
645
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
646
- expect(onData).toHaveBeenLastCalledWith([0]);
647
-
648
- $array.unshift(state(1));
649
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
650
- expect(onData).toHaveBeenLastCalledWith([1, 0]);
651
-
652
- $array.unshift(state(2));
653
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(4));
654
- expect(onData).toHaveBeenLastCalledWith([2, 1, 0]);
655
-
656
- $array.shift();
657
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(5));
658
- expect(onData).toHaveBeenLastCalledWith([1, 0]);
659
-
660
- $array.shift();
661
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(6));
662
- expect(onData).toHaveBeenLastCalledWith([0]);
663
-
664
- $array.shift();
665
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(7));
666
- expect(onData).toHaveBeenLastCalledWith([]);
667
- });
668
-
669
- it("should call effect when updated with splice", async () => {
670
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
671
- const onData = vi.fn();
672
- subscribe((t) => $array.get(t).map((state) => state.get(t)), onData);
673
-
674
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
675
- expect(onData).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
676
-
677
- $array.splice(1, 2);
678
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
679
- expect(onData).toHaveBeenLastCalledWith([0, 3, 4]);
680
-
681
- $array.splice(1, 0, state(1), state(2));
682
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
683
- expect(onData).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
684
- });
685
-
686
- it("should call effect when updated with clear", async () => {
687
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
688
- const onData = vi.fn();
689
- subscribe((t) => $array.get(t).map((state) => state.get(t)), onData);
690
-
691
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
692
- expect(onData).toHaveBeenLastCalledWith([0, 1, 2, 3, 4]);
693
-
694
- $array.clear();
695
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
696
- expect(onData).toHaveBeenLastCalledWith([]);
697
- });
698
- });
699
-
700
- describe("item reactivity", () => {
701
- it("should call effect when item is updated", async () => {
702
- const $array = array<FlowState<number>>([state(0), state(1), state(2)]);
703
- const onData = vi.fn();
704
- const onData0 = vi.fn();
705
- const onData1 = vi.fn();
706
- const onData2 = vi.fn();
707
-
708
- subscribe((t) => $array.get(t).map((state) => state.get(t)), onData);
709
- subscribe((t) => $array.get(t)[0].get(t), onData0);
710
- subscribe((t) => $array.get(t)[1].get(t), onData1);
711
- subscribe((t) => $array.get(t)[2].get(t), onData2);
712
-
713
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
714
- expect(onData).toHaveBeenLastCalledWith([0, 1, 2]);
715
- await vi.waitFor(() => expect(onData0).toHaveBeenCalledTimes(1));
716
- expect(onData0).toHaveBeenLastCalledWith(0);
717
- await vi.waitFor(() => expect(onData1).toHaveBeenCalledTimes(1));
718
- expect(onData1).toHaveBeenLastCalledWith(1);
719
- await vi.waitFor(() => expect(onData2).toHaveBeenCalledTimes(1));
720
- expect(onData2).toHaveBeenLastCalledWith(2);
721
-
722
- (await $array.pick())[0].set(1);
723
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
724
- expect(onData).toHaveBeenLastCalledWith([1, 1, 2]);
725
- await vi.waitFor(() => expect(onData0).toHaveBeenCalledTimes(2));
726
- expect(onData0).toHaveBeenLastCalledWith(1);
727
- await vi.waitFor(() => expect(onData1).toHaveBeenCalledTimes(1));
728
- expect(onData1).toHaveBeenLastCalledWith(1);
729
- await vi.waitFor(() => expect(onData2).toHaveBeenCalledTimes(1));
730
- expect(onData2).toHaveBeenLastCalledWith(2);
731
-
732
- (await $array.pick())[1].set(2);
733
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
734
- expect(onData).toHaveBeenLastCalledWith([1, 2, 2]);
735
- await vi.waitFor(() => expect(onData0).toHaveBeenCalledTimes(2));
736
- expect(onData0).toHaveBeenLastCalledWith(1);
737
- await vi.waitFor(() => expect(onData1).toHaveBeenCalledTimes(2));
738
- expect(onData1).toHaveBeenLastCalledWith(2);
739
- await vi.waitFor(() => expect(onData2).toHaveBeenCalledTimes(1));
740
- expect(onData2).toHaveBeenLastCalledWith(2);
741
-
742
- (await $array.pick())[2].set(3);
743
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(4));
744
- expect(onData).toHaveBeenLastCalledWith([1, 2, 3]);
745
- await vi.waitFor(() => expect(onData0).toHaveBeenCalledTimes(2));
746
- expect(onData0).toHaveBeenLastCalledWith(1);
747
- await vi.waitFor(() => expect(onData1).toHaveBeenCalledTimes(2));
748
- expect(onData1).toHaveBeenLastCalledWith(2);
749
- await vi.waitFor(() => expect(onData2).toHaveBeenCalledTimes(2));
750
- expect(onData2).toHaveBeenLastCalledWith(3);
751
- });
752
-
753
- it("should call effect when item is replaced", async () => {
754
- const $array = array<FlowState<number>>([state(0), state(1), state(2)]);
755
- const onData = vi.fn();
756
- const onData0 = vi.fn();
757
-
758
- subscribe((t) => $array.get(t).map((state) => state.get(t)), onData);
759
- subscribe((t) => $array.get(t)[0].get(t), onData0);
760
-
761
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
762
- expect(onData).toHaveBeenLastCalledWith([0, 1, 2]);
763
- await vi.waitFor(() => expect(onData0).toHaveBeenCalledTimes(1));
764
- expect(onData0).toHaveBeenLastCalledWith(0);
765
-
766
- $array.update(0, state(3));
767
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
768
- expect(onData).toHaveBeenLastCalledWith([3, 1, 2]);
769
- await vi.waitFor(() => expect(onData0).toHaveBeenCalledTimes(2));
770
- expect(onData0).toHaveBeenLastCalledWith(3);
771
-
772
- $array.shift();
773
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
774
- expect(onData).toHaveBeenLastCalledWith([1, 2]);
775
- await vi.waitFor(() => expect(onData0).toHaveBeenCalledTimes(3));
776
- expect(onData0).toHaveBeenLastCalledWith(1);
777
-
778
- $array.unshift(state(4));
779
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(4));
780
- expect(onData).toHaveBeenLastCalledWith([4, 1, 2]);
781
- await vi.waitFor(() => expect(onData0).toHaveBeenCalledTimes(4));
782
- expect(onData0).toHaveBeenLastCalledWith(4);
783
- });
784
- });
785
-
786
- describe("lastAction reactivity", () => {
787
- describe("set action", () => {
788
- it("should call effect when $lastAction is updated with set action", async () => {
789
- const $array = array([state(0), state(1), state(2)]);
790
- const onData = vi.fn();
791
- subscribe((t) => $array.$lastAction.get(t), onData);
792
-
793
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
794
- expect(onData).toHaveBeenLastCalledWith(
795
- expect.objectContaining({
796
- type: "set",
797
- setItems: expect.any(Array),
798
- clearedItems: expect.any(Array),
799
- }),
800
- );
801
-
802
- $array.set([state(1), state(2), state(3)]);
803
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
804
- expect(onData).toHaveBeenLastCalledWith(
805
- expect.objectContaining({
806
- type: "set",
807
- setItems: expect.any(Array),
808
- clearedItems: expect.any(Array),
809
- }),
810
- );
811
- });
812
- });
813
-
814
- describe("update action", () => {
815
- it("should call effect when $lastAction is updated with update action", async () => {
816
- const $array = array([state(0), state(1), state(2)]);
817
- const onData = vi.fn();
818
- subscribe((t) => $array.$lastAction.get(t), onData);
819
-
820
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
821
-
822
- const $newItem = state(10);
823
- $array.update(1, $newItem);
824
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
825
- expect(onData).toHaveBeenLastCalledWith(
826
- expect.objectContaining({
827
- type: "update",
828
- index: 1,
829
- setItem: $newItem,
830
- clearedItem: expect.any(Object),
831
- }),
832
- );
833
- });
834
- });
835
-
836
- describe("push action", () => {
837
- it("should call effect when $lastAction is updated with push action", async () => {
838
- const $array = array<FlowState<number>>();
839
- const onData = vi.fn();
840
- subscribe((t) => $array.$lastAction.get(t), onData);
841
-
842
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
843
- expect(onData).toHaveBeenLastCalledWith(
844
- expect.objectContaining({
845
- type: "set",
846
- setItems: expect.any(Array),
847
- clearedItems: expect.any(Array),
848
- }),
849
- );
850
-
851
- const $newItem = state(0);
852
- $array.push($newItem);
853
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
854
- expect(onData).toHaveBeenLastCalledWith(
855
- expect.objectContaining({
856
- type: "push",
857
- addedItem: $newItem,
858
- }),
859
- );
860
-
861
- const $newItem2 = state(1);
862
- $array.push($newItem2);
863
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
864
- expect(onData).toHaveBeenLastCalledWith(
865
- expect.objectContaining({
866
- type: "push",
867
- addedItem: $newItem2,
868
- }),
869
- );
870
- });
871
- });
872
-
873
- describe("pop action", () => {
874
- it("should call effect when $lastAction is updated with pop action", async () => {
875
- const $array = array<FlowState<number>>([state(0), state(1), state(2)]);
876
- const onData = vi.fn();
877
- subscribe((t) => $array.$lastAction.get(t), onData);
878
-
879
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
880
-
881
- $array.pop();
882
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
883
- expect(onData).toHaveBeenLastCalledWith(
884
- expect.objectContaining({
885
- type: "pop",
886
- removedItem: expect.any(Object),
887
- }),
888
- );
889
-
890
- $array.pop();
891
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
892
- expect(onData).toHaveBeenLastCalledWith(
893
- expect.objectContaining({
894
- type: "pop",
895
- removedItem: expect.any(Object),
896
- }),
897
- );
898
- });
899
- });
900
-
901
- describe("unshift action", () => {
902
- it("should call effect when $lastAction is updated with unshift action", async () => {
903
- const $array = array<FlowState<number>>();
904
- const onData = vi.fn();
905
- subscribe((t) => $array.$lastAction.get(t), onData);
906
-
907
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
908
- expect(onData).toHaveBeenLastCalledWith(
909
- expect.objectContaining({
910
- type: "set",
911
- setItems: expect.any(Array),
912
- clearedItems: expect.any(Array),
913
- }),
914
- );
915
-
916
- const $newItem = state(0);
917
- $array.unshift($newItem);
918
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
919
- expect(onData).toHaveBeenLastCalledWith(
920
- expect.objectContaining({
921
- type: "unshift",
922
- addedItem: $newItem,
923
- }),
924
- );
925
-
926
- const $newItem2 = state(1);
927
- $array.unshift($newItem2);
928
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
929
- expect(onData).toHaveBeenLastCalledWith(
930
- expect.objectContaining({
931
- type: "unshift",
932
- addedItem: $newItem2,
933
- }),
934
- );
935
- });
936
- });
937
-
938
- describe("shift action", () => {
939
- it("should call effect when $lastAction is updated with shift action", async () => {
940
- const $array = array<FlowState<number>>([state(0), state(1), state(2)]);
941
- const onData = vi.fn();
942
- subscribe((t) => $array.$lastAction.get(t), onData);
943
-
944
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
945
-
946
- $array.shift();
947
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
948
- expect(onData).toHaveBeenLastCalledWith(
949
- expect.objectContaining({
950
- type: "shift",
951
- removedItem: expect.any(Object),
952
- }),
953
- );
954
-
955
- $array.shift();
956
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
957
- expect(onData).toHaveBeenLastCalledWith(
958
- expect.objectContaining({
959
- type: "shift",
960
- removedItem: expect.any(Object),
961
- }),
962
- );
963
- });
964
- });
965
-
966
- describe("splice action", () => {
967
- it("should call effect when $lastAction is updated with splice action", async () => {
968
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
969
- const onData = vi.fn();
970
- subscribe((t) => $array.$lastAction.get(t), onData);
971
-
972
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
973
- expect(onData).toHaveBeenLastCalledWith(
974
- expect.objectContaining({
975
- type: "set",
976
- setItems: expect.any(Array),
977
- clearedItems: expect.any(Array),
978
- }),
979
- );
980
-
981
- $array.splice(1, 2);
982
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
983
- expect(onData).toHaveBeenLastCalledWith(
984
- expect.objectContaining({
985
- type: "splice",
986
- start: 1,
987
- deleteCount: 2,
988
- addedItems: expect.any(Array),
989
- removedItems: expect.any(Array),
990
- }),
991
- );
992
-
993
- $array.splice(1, 0, state(1), state(2));
994
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
995
- expect(onData).toHaveBeenLastCalledWith(
996
- expect.objectContaining({
997
- type: "splice",
998
- start: 1,
999
- deleteCount: 0,
1000
- addedItems: expect.any(Array),
1001
- removedItems: expect.any(Array),
1002
- }),
1003
- );
1004
- });
1005
- });
1006
-
1007
- describe("clear action", () => {
1008
- it("should call effect when $lastAction is updated with clear action", async () => {
1009
- const $array = array<FlowState<number>>([state(0), state(1), state(2), state(3), state(4)]);
1010
- const onData = vi.fn();
1011
- subscribe((t) => $array.$lastAction.get(t), onData);
1012
-
1013
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
1014
- expect(onData).toHaveBeenLastCalledWith(
1015
- expect.objectContaining({
1016
- type: "set",
1017
- setItems: expect.any(Array),
1018
- clearedItems: expect.any(Array),
1019
- }),
1020
- );
1021
-
1022
- $array.clear();
1023
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
1024
- expect(onData).toHaveBeenLastCalledWith(
1025
- expect.objectContaining({
1026
- type: "clear",
1027
- clearedItems: expect.any(Array),
1028
- }),
1029
- );
1030
- });
1031
- });
1032
- });
1033
- });
1034
- });
1035
- });