@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,896 +0,0 @@
1
- import { describe, expect, it, vi } from "vitest";
2
- import type { FlowTracker } from "~";
3
- import { derivation, signal, state, subscribe } from "~";
4
-
5
- describe("flowDerivation", () => {
6
- describe("unit", () => {
7
- describe("initialization", () => {
8
- it("should support self-derivation function without previous value", async () => {
9
- const $state = state(1);
10
- const compute = (t: FlowTracker) => {
11
- return 2 * $state.get(t);
12
- };
13
- const $derivation = derivation(compute);
14
- const onData = vi.fn();
15
- $derivation.subscribe(onData);
16
-
17
- expect(await $derivation.pick()).toBe(2);
18
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
19
- expect(onData).toHaveBeenLastCalledWith(2);
20
-
21
- $state.set(2);
22
- expect(await $derivation.pick()).toBe(4);
23
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
24
- expect(onData).toHaveBeenLastCalledWith(4);
25
- });
26
-
27
- it("should support self-derivation function with previous value", async () => {
28
- const $state = state(1);
29
- const compute = (t: FlowTracker, previous?: number) => {
30
- const current = $state.get(t);
31
- return previous !== undefined ? previous + current : current;
32
- };
33
- const $derivation = derivation(compute);
34
- const onData = vi.fn();
35
- $derivation.subscribe(onData);
36
-
37
- expect(await $derivation.pick()).toBe(1);
38
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
39
- expect(onData).toHaveBeenLastCalledWith(1);
40
-
41
- $state.set(2);
42
- expect(await $derivation.pick()).toBe(3);
43
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
44
- expect(onData).toHaveBeenLastCalledWith(3);
45
- });
46
- });
47
-
48
- describe("reactive dependencies", () => {
49
- it("should recompute when state dependency changes", async () => {
50
- const $state = state(1);
51
- const computeFn = vi.fn((t) => $state.get(t) * 2);
52
- const $derivation = derivation(computeFn);
53
-
54
- expect(await $derivation.pick()).toBe(2);
55
- expect(computeFn).toHaveBeenCalledTimes(1);
56
-
57
- $state.set(2);
58
- expect(await $derivation.pick()).toBe(4);
59
- expect(computeFn).toHaveBeenCalledTimes(2);
60
- });
61
-
62
- it("should recompute when signal dependency is triggered", async () => {
63
- const $signal = signal();
64
- const $state = state(0);
65
- const computeFn = vi.fn((t) => {
66
- $signal.watch(t);
67
- return $state.get(t);
68
- });
69
- const $derivation = derivation(computeFn);
70
-
71
- expect(await $derivation.pick()).toBe(0);
72
- expect(computeFn).toHaveBeenCalledTimes(1);
73
-
74
- $state.set(1);
75
- $signal.trigger();
76
- expect(await $derivation.pick()).toBe(1);
77
- expect(computeFn).toHaveBeenCalledTimes(2);
78
- });
79
-
80
- it("should recompute when another derivation dependency changes", async () => {
81
- const $state = state(1);
82
- const $derivation1 = derivation((t) => $state.get(t) * 2);
83
- derivation((t) => $derivation1.get(t) * 2);
84
- const computeFn = vi.fn((t) => $derivation1.get(t) * 2);
85
- const $derivation2Spy = derivation(computeFn);
86
-
87
- expect(await $derivation2Spy.pick()).toBe(4);
88
- expect(computeFn).toHaveBeenCalledTimes(1);
89
-
90
- $state.set(2);
91
- expect(await $derivation2Spy.pick()).toBe(8);
92
- expect(computeFn).toHaveBeenCalledTimes(2);
93
- });
94
-
95
- it("should mark as dirty when dependency changes", async () => {
96
- const $state = state(1);
97
- const computeFn = vi.fn((t) => $state.get(t) * 2);
98
- const $derivation = derivation(computeFn);
99
-
100
- await $derivation.pick();
101
- expect(computeFn).toHaveBeenCalledTimes(1);
102
-
103
- $state.set(2);
104
- // Should be dirty but not recomputed yet
105
- expect(computeFn).toHaveBeenCalledTimes(1);
106
-
107
- // Next access triggers recomputation
108
- await $derivation.pick();
109
- expect(computeFn).toHaveBeenCalledTimes(2);
110
- });
111
-
112
- it("should recompute if value is identical after dependency change", async () => {
113
- const $state = state(2);
114
- const computeFn = vi.fn((t) => $state.get(t) * 0);
115
- const $derivation = derivation(computeFn);
116
- const onData = vi.fn();
117
-
118
- $derivation.subscribe(onData);
119
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
120
- expect(onData).toHaveBeenLastCalledWith(0);
121
-
122
- $state.set(5);
123
- // Value is still 0 (2*0 = 0, 5*0 = 0), but derivation is marked dirty
124
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
125
- expect(onData).toHaveBeenLastCalledWith(0);
126
- });
127
- });
128
-
129
- describe("get", () => {
130
- it("should recompute when dependency changes with tracking via subscribe", async () => {
131
- const $state = state(1);
132
- const computeFn = vi.fn((t) => $state.get(t) * 2);
133
- const $derivation = derivation(computeFn);
134
- const onData = vi.fn();
135
-
136
- $derivation.subscribe((value) => {
137
- onData(value);
138
- });
139
-
140
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
141
- expect(onData).toHaveBeenLastCalledWith(2);
142
- expect(computeFn).toHaveBeenCalledTimes(1);
143
-
144
- $state.set(2);
145
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
146
- expect(onData).toHaveBeenLastCalledWith(4);
147
- expect(computeFn).toHaveBeenCalledTimes(2);
148
- });
149
-
150
- it("should register dependencies automatically via tracker in subscribe", async () => {
151
- const $state1 = state(1);
152
- const $state2 = state(2);
153
- const computeFn = vi.fn((t) => {
154
- return $state1.get(t) + $state2.get(t);
155
- });
156
- const $derivation = derivation(computeFn);
157
- const onData = vi.fn();
158
-
159
- $derivation.subscribe((value) => {
160
- onData(value);
161
- });
162
-
163
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
164
- expect(onData).toHaveBeenLastCalledWith(3);
165
-
166
- $state1.set(3);
167
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
168
- expect(onData).toHaveBeenLastCalledWith(5);
169
-
170
- $state2.set(4);
171
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
172
- expect(onData).toHaveBeenLastCalledWith(7);
173
- });
174
-
175
- it("should propagate errors in compute function with dependencies", async () => {
176
- const $state = state(1);
177
- const error = new Error("Compute error");
178
- const $derivation = derivation((t) => {
179
- const value = $state.get(t);
180
- if (value > 1) {
181
- throw error;
182
- }
183
- return value;
184
- });
185
-
186
- await $derivation.pick();
187
-
188
- $state.set(2);
189
- await expect($derivation.pick()).rejects.toThrow(error);
190
- });
191
- });
192
-
193
- describe("watch", () => {
194
- it("should register dependency when watch is called in effect context", async () => {
195
- const $state = state(1);
196
- const $derivation = derivation((t) => $state.get(t) * 2);
197
- const onData = vi.fn();
198
-
199
- subscribe((t) => $derivation.watch(t), onData);
200
-
201
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
202
- });
203
-
204
- it("should recompute when dependency changes after watch", async () => {
205
- const $state = state(1);
206
- const computeFn = vi.fn((t) => $state.get(t) * 2);
207
- const $derivation = derivation(computeFn);
208
- const onData = vi.fn();
209
-
210
- subscribe((t) => $derivation.watch(t), onData);
211
-
212
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
213
- expect(computeFn).toHaveBeenCalledTimes(1);
214
-
215
- $state.set(2);
216
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
217
- expect(computeFn).toHaveBeenCalledTimes(2);
218
- });
219
- });
220
-
221
- describe("pick", () => {
222
- it("should recompute when dependency changes without tracking", async () => {
223
- const $state = state(1);
224
- const computeFn = vi.fn((t) => $state.get(t) * 2);
225
- const $derivation = derivation(computeFn);
226
-
227
- expect(await $derivation.pick()).toBe(2);
228
- expect(computeFn).toHaveBeenCalledTimes(1);
229
-
230
- $state.set(2);
231
- expect(await $derivation.pick()).toBe(4);
232
- expect(computeFn).toHaveBeenCalledTimes(2);
233
- });
234
- });
235
-
236
- describe("refresh", () => {
237
- it("should force recomputation even if not dirty", async () => {
238
- const $state = state(1);
239
- const computeFn = vi.fn((t) => $state.get(t) * 2);
240
- const $derivation = derivation(computeFn);
241
-
242
- await $derivation.pick();
243
- expect(computeFn).toHaveBeenCalledTimes(1);
244
-
245
- $derivation.refresh();
246
- await new Promise((resolve) => setTimeout(resolve, 10));
247
- expect(computeFn).toHaveBeenCalledTimes(2);
248
- });
249
-
250
- it("should notify only if value changes after refresh", async () => {
251
- let multiplier = 2;
252
- const $state = state(5);
253
- const $derivation = derivation((t) => $state.get(t) * multiplier);
254
- const onData = vi.fn();
255
-
256
- $derivation.subscribe(onData);
257
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
258
- expect(onData).toHaveBeenLastCalledWith(10);
259
-
260
- multiplier = 3;
261
- $derivation.refresh();
262
- // Refresh triggers notification asynchronously
263
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2), {
264
- timeout: 2000,
265
- });
266
- expect(onData).toHaveBeenLastCalledWith(15);
267
- });
268
-
269
- it("should not notify if value is identical after refresh", async () => {
270
- const $state = state(5);
271
- const $derivation = derivation((t) => $state.get(t) * 2);
272
- const onData = vi.fn();
273
-
274
- $derivation.subscribe(onData);
275
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
276
-
277
- $derivation.refresh();
278
- await new Promise((resolve) => setTimeout(resolve, 50));
279
- expect(onData).toHaveBeenCalledTimes(1);
280
- });
281
-
282
- it("should force recomputation even if dependencies have not changed", async () => {
283
- const $state = state(1);
284
- const computeFn = vi.fn((t) => $state.get(t) * 2);
285
- const $derivation = derivation(computeFn);
286
-
287
- await $derivation.pick();
288
- expect(computeFn).toHaveBeenCalledTimes(1);
289
-
290
- $derivation.refresh();
291
- await new Promise((resolve) => setTimeout(resolve, 10));
292
- expect(computeFn).toHaveBeenCalledTimes(2);
293
- });
294
- });
295
-
296
- describe("dynamic dependencies", () => {
297
- it("should handle dependencies that change between computations", async () => {
298
- const $state1 = state(1);
299
- const $state2 = state(10);
300
- const $cond = state(true);
301
- const computeFn = vi.fn((t) => {
302
- if ($cond.get(t)) {
303
- return $state1.get(t) * 2;
304
- }
305
- return $state2.get(t) * 2;
306
- });
307
- const $derivation = derivation(computeFn);
308
-
309
- expect(await $derivation.pick()).toBe(2);
310
- expect(computeFn).toHaveBeenCalledTimes(1);
311
-
312
- $state1.set(2);
313
- expect(await $derivation.pick()).toBe(4);
314
- expect(computeFn).toHaveBeenCalledTimes(2);
315
-
316
- $cond.set(false);
317
- expect(await $derivation.pick()).toBe(20);
318
- expect(computeFn).toHaveBeenCalledTimes(3);
319
-
320
- $state2.set(20);
321
- expect(await $derivation.pick()).toBe(40);
322
- expect(computeFn).toHaveBeenCalledTimes(4);
323
-
324
- $state1.set(100);
325
- expect(await $derivation.pick()).toBe(40);
326
- expect(computeFn).toHaveBeenCalledTimes(4);
327
- });
328
-
329
- it("should unregister dependencies that are no longer used", async () => {
330
- const $state1 = state(1);
331
- const $state2 = state(10);
332
- const $cond = state(true);
333
- const $derivation = derivation((t) => {
334
- if ($cond.get(t)) {
335
- return $state1.get(t);
336
- }
337
- return $state2.get(t);
338
- });
339
- const onData = vi.fn();
340
-
341
- $derivation.subscribe(onData);
342
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
343
-
344
- $cond.set(false);
345
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
346
-
347
- $state1.set(100);
348
- await new Promise((resolve) => setTimeout(resolve, 50));
349
- expect(onData).toHaveBeenCalledTimes(2);
350
- });
351
-
352
- it("should handle dependencies that appear and disappear", async () => {
353
- const $state1 = state(1);
354
- const $state2 = state(2);
355
- const $useState1 = state(true);
356
- const $derivation = derivation((t) => {
357
- if ($useState1.get(t)) {
358
- return $state1.get(t);
359
- }
360
- return $state2.get(t);
361
- });
362
- const onData = vi.fn();
363
-
364
- $derivation.subscribe(onData);
365
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
366
- expect(onData).toHaveBeenLastCalledWith(1);
367
-
368
- $useState1.set(false);
369
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
370
- expect(onData).toHaveBeenLastCalledWith(2);
371
-
372
- $useState1.set(true);
373
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
374
- expect(onData).toHaveBeenLastCalledWith(1);
375
- });
376
- });
377
-
378
- describe("nested derivations", () => {
379
- it("should handle derivation depending on another derivation", async () => {
380
- const $state = state(1);
381
- const $derivation1 = derivation((t) => $state.get(t) * 2);
382
- const $derivation2 = derivation((t) => $derivation1.get(t) * 2);
383
-
384
- expect(await $derivation2.pick()).toBe(4);
385
-
386
- $state.set(2);
387
- expect(await $derivation2.pick()).toBe(8);
388
- });
389
-
390
- it("should handle chain of derivations", async () => {
391
- const $state = state(1);
392
- const $derivationA = derivation((t) => $state.get(t) * 2);
393
- const $derivationB = derivation((t) => $derivationA.get(t) * 2);
394
- const $derivationC = derivation((t) => $derivationB.get(t) * 2);
395
-
396
- expect(await $derivationC.pick()).toBe(8);
397
-
398
- $state.set(2);
399
- expect(await $derivationC.pick()).toBe(16);
400
- });
401
-
402
- it("should recompute in cascade when dependency changes", async () => {
403
- const $state = state(1);
404
- const computeFnA = vi.fn((t) => $state.get(t) * 2);
405
- const computeFnB = vi.fn((t) => {
406
- const $a = derivation(computeFnA);
407
- return $a.get(t) * 2;
408
- });
409
- derivation(computeFnA);
410
- const $derivationB = derivation(computeFnB);
411
-
412
- await $derivationB.pick();
413
- expect(computeFnA).toHaveBeenCalled();
414
- expect(computeFnB).toHaveBeenCalled();
415
-
416
- $state.set(2);
417
- await $derivationB.pick();
418
- expect(computeFnA).toHaveBeenCalledTimes(2);
419
- expect(computeFnB).toHaveBeenCalledTimes(2);
420
- });
421
- });
422
-
423
- describe("error handling", () => {
424
- it("should propagate errors in compute function with dependencies", async () => {
425
- const $state = state(1);
426
- const error = new Error("Compute error");
427
- const $derivation = derivation((t) => {
428
- const value = $state.get(t);
429
- if (value > 1) {
430
- throw error;
431
- }
432
- return value;
433
- });
434
-
435
- expect(await $derivation.pick()).toBe(1);
436
-
437
- $state.set(2);
438
- await expect($derivation.pick()).rejects.toThrow("Compute error");
439
- });
440
- });
441
- });
442
-
443
- describe("with effect", () => {
444
- describe("reactive dependencies", () => {
445
- it("should re-execute effect when derivation dependency changes", async () => {
446
- const $state = state(1);
447
- const $derivation = derivation((t) => $state.get(t) * 2);
448
- const onData = vi.fn();
449
-
450
- subscribe((t) => $derivation.get(t), onData);
451
-
452
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
453
-
454
- $state.set(2);
455
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
456
- });
457
-
458
- it("should re-execute effect if derivation value is identical", async () => {
459
- const $state = state(2);
460
- const $derivation = derivation((t) => $state.get(t) * 0);
461
- const onData = vi.fn();
462
-
463
- subscribe((t) => $derivation.get(t), onData);
464
-
465
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
466
-
467
- $state.set(5);
468
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
469
- });
470
-
471
- it("should work with derivation depending on state", async () => {
472
- const $state = state(1);
473
- const $derivation = derivation((t) => $state.get(t) * 2);
474
- const onData = vi.fn();
475
-
476
- subscribe((t) => $derivation.get(t), onData);
477
-
478
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
479
- expect(onData).toHaveBeenLastCalledWith(2);
480
-
481
- $state.set(2);
482
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
483
- expect(onData).toHaveBeenLastCalledWith(4);
484
- });
485
-
486
- it("should work with derivation depending on signal", async () => {
487
- const $signal = signal();
488
- const $state = state(0);
489
- const $derivation = derivation((t) => {
490
- $signal.watch(t);
491
- return $state.get(t);
492
- });
493
- const onData = vi.fn();
494
-
495
- subscribe((t) => $derivation.get(t), onData);
496
-
497
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
498
- expect(onData).toHaveBeenLastCalledWith(0);
499
-
500
- $state.set(1);
501
- $signal.trigger();
502
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
503
- expect(onData).toHaveBeenLastCalledWith(1);
504
- });
505
-
506
- it("should work with derivation depending on another derivation", async () => {
507
- const $state = state(1);
508
- const $derivation1 = derivation((t) => $state.get(t) * 2);
509
- const $derivation2 = derivation((t) => $derivation1.get(t) * 2);
510
- const onData = vi.fn();
511
-
512
- subscribe((t) => $derivation2.get(t), onData);
513
-
514
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
515
- expect(onData).toHaveBeenLastCalledWith(4);
516
-
517
- $state.set(2);
518
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
519
- expect(onData).toHaveBeenLastCalledWith(8);
520
- });
521
- });
522
-
523
- describe("multiple dependencies", () => {
524
- it("should work with derivation depending on multiple states", async () => {
525
- const $stateA = state(5);
526
- const $stateB = state(10);
527
- const $derivation = derivation((t) => $stateA.get(t) + $stateB.get(t));
528
- const onData = vi.fn();
529
-
530
- subscribe((t) => $derivation.get(t), onData);
531
-
532
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
533
- expect(onData).toHaveBeenLastCalledWith(15);
534
-
535
- $stateA.set(6);
536
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
537
- expect(onData).toHaveBeenLastCalledWith(16);
538
-
539
- $stateB.set(11);
540
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
541
- expect(onData).toHaveBeenLastCalledWith(17);
542
- });
543
-
544
- it("should work with derivation depending on multiple signals", async () => {
545
- const $signal1 = signal();
546
- const $signal2 = signal();
547
- const $derivation = derivation((t) => {
548
- $signal1.watch(t);
549
- $signal2.watch(t);
550
- return 42;
551
- });
552
- const onData = vi.fn();
553
-
554
- subscribe((t) => $derivation.watch(t), onData);
555
-
556
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
557
-
558
- $signal1.trigger();
559
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
560
-
561
- $signal2.trigger();
562
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
563
- });
564
-
565
- it("should work with derivation depending on state and signal", async () => {
566
- const $signal = signal();
567
- const $state = state(1);
568
- const $derivation = derivation((t) => {
569
- $signal.watch(t);
570
- return $state.get(t) * 2;
571
- });
572
- const onData = vi.fn();
573
-
574
- subscribe((t) => $derivation.get(t), onData);
575
-
576
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
577
- expect(onData).toHaveBeenLastCalledWith(2);
578
-
579
- $signal.trigger();
580
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
581
- expect(onData).toHaveBeenLastCalledWith(2);
582
-
583
- $state.set(2);
584
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
585
- expect(onData).toHaveBeenLastCalledWith(4);
586
- });
587
-
588
- it("should work with derivation depending on state and another derivation", async () => {
589
- const $state = state(1);
590
- const $derivation1 = derivation((t) => $state.get(t) * 2);
591
- const $derivation2 = derivation((t) => $state.get(t) + $derivation1.get(t));
592
- const onData = vi.fn();
593
-
594
- subscribe((t) => $derivation2.get(t), onData);
595
-
596
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
597
- expect(onData).toHaveBeenLastCalledWith(3);
598
-
599
- $state.set(2);
600
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
601
- expect(onData).toHaveBeenLastCalledWith(6);
602
- });
603
- });
604
-
605
- describe("nested derivations", () => {
606
- it("should work with chained derivations", async () => {
607
- const $state = state(1);
608
- const $derivation1 = derivation((t) => $state.get(t) * 2);
609
- const $derivation2 = derivation((t) => $derivation1.get(t) * 2);
610
- const $derivation3 = derivation((t) => $derivation2.get(t) * 2);
611
- const onData = vi.fn();
612
-
613
- subscribe((t) => $derivation3.get(t), onData);
614
-
615
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
616
- expect(onData).toHaveBeenLastCalledWith(8);
617
-
618
- $state.set(2);
619
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
620
- expect(onData).toHaveBeenLastCalledWith(16);
621
- });
622
-
623
- it("should re-execute effect when distant dependency changes", async () => {
624
- const $state = state(1);
625
- const $derivation1 = derivation((t) => $state.get(t) * 2);
626
- const $derivation2 = derivation((t) => $derivation1.get(t) * 2);
627
- const onData = vi.fn();
628
-
629
- subscribe((t) => $derivation2.get(t), onData);
630
-
631
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
632
- expect(onData).toHaveBeenLastCalledWith(4);
633
-
634
- $state.set(2);
635
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
636
- expect(onData).toHaveBeenLastCalledWith(8);
637
- });
638
-
639
- it("should propagate changes correctly through the chain", async () => {
640
- const $state = state(1);
641
- const $derivation1 = derivation((t) => $state.get(t) * 2);
642
- const $derivation2 = derivation((t) => $derivation1.get(t) * 2);
643
- const onData1 = vi.fn();
644
- const onData2 = vi.fn();
645
-
646
- subscribe((t) => $derivation1.get(t), onData1);
647
- subscribe((t) => $derivation2.get(t), onData2);
648
-
649
- await vi.waitFor(() => {
650
- expect(onData1).toHaveBeenCalledTimes(1);
651
- expect(onData2).toHaveBeenCalledTimes(1);
652
- });
653
-
654
- $state.set(2);
655
- await vi.waitFor(() => {
656
- expect(onData1).toHaveBeenCalledTimes(2);
657
- expect(onData2).toHaveBeenCalledTimes(2);
658
- });
659
- });
660
- });
661
-
662
- describe("dynamic dependencies", () => {
663
- it("should work with derivation with conditional dependencies", async () => {
664
- const $state1 = state(1);
665
- const $state2 = state(10);
666
- const $cond = state(true);
667
- const $derivation = derivation((t) => {
668
- if ($cond.get(t)) {
669
- return $state1.get(t) * 2;
670
- }
671
- return $state2.get(t) * 2;
672
- });
673
- const onData = vi.fn();
674
-
675
- subscribe((t) => $derivation.get(t), onData);
676
-
677
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
678
- expect(onData).toHaveBeenLastCalledWith(2);
679
-
680
- $state1.set(2);
681
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
682
- expect(onData).toHaveBeenLastCalledWith(4);
683
-
684
- $cond.set(false);
685
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
686
- expect(onData).toHaveBeenLastCalledWith(20);
687
-
688
- $state2.set(20);
689
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(4));
690
- expect(onData).toHaveBeenLastCalledWith(40);
691
- });
692
-
693
- it("should re-execute effect when dependencies change dynamically", async () => {
694
- const $state1 = state(1);
695
- const $state2 = state(2);
696
- const $useState1 = state(true);
697
- const $derivation = derivation((t) => {
698
- if ($useState1.get(t)) {
699
- return $state1.get(t);
700
- }
701
- return $state2.get(t);
702
- });
703
- const onData = vi.fn();
704
-
705
- subscribe((t) => $derivation.get(t), onData);
706
-
707
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
708
- expect(onData).toHaveBeenLastCalledWith(1);
709
-
710
- $useState1.set(false);
711
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
712
- expect(onData).toHaveBeenLastCalledWith(2);
713
-
714
- $state1.set(10);
715
- await new Promise((resolve) => setTimeout(resolve, 50));
716
- expect(onData).toHaveBeenCalledTimes(2);
717
-
718
- $state2.set(20);
719
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
720
- expect(onData).toHaveBeenLastCalledWith(20);
721
- });
722
-
723
- it("should unregister old dependencies correctly", async () => {
724
- const $state1 = state(1);
725
- const $state2 = state(10);
726
- const $cond = state(true);
727
- const $derivation = derivation((t) => {
728
- if ($cond.get(t)) {
729
- return $state1.get(t);
730
- }
731
- return $state2.get(t);
732
- });
733
- const onData = vi.fn();
734
-
735
- subscribe((t) => $derivation.get(t), onData);
736
-
737
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
738
-
739
- $cond.set(false);
740
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
741
-
742
- $state1.set(100);
743
- await new Promise((resolve) => setTimeout(resolve, 50));
744
- expect(onData).toHaveBeenCalledTimes(2);
745
- });
746
- });
747
-
748
- describe("complex patterns", () => {
749
- it("should handle diamond pattern", async () => {
750
- const $stateA = state(1);
751
- const $aMult3 = derivation((t) => $stateA.get(t) * 3);
752
- const $aMult2 = derivation((t) => $stateA.get(t) * 2);
753
- const $addAmult3Amult2 = derivation((t) => $aMult3.get(t) + $aMult2.get(t));
754
- const onData = vi.fn();
755
-
756
- subscribe((t) => $addAmult3Amult2.get(t), onData);
757
-
758
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
759
- expect(onData).toHaveBeenLastCalledWith(5);
760
-
761
- $stateA.set(2);
762
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
763
- expect(onData).toHaveBeenLastCalledWith(10);
764
-
765
- $stateA.set(3);
766
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
767
- expect(onData).toHaveBeenLastCalledWith(15);
768
- });
769
-
770
- it("should handle multi-diamond pattern", async () => {
771
- const $stateA = state(1);
772
- const $stateB = state(2);
773
- const $addAB = derivation((t) => {
774
- const a = $stateA.get(t);
775
- const b = $stateB.get(t);
776
- return a + b;
777
- });
778
- const $multiplyAB = derivation((t) => {
779
- const a = $stateA.get(t);
780
- const b = $stateB.get(t);
781
- return a * b;
782
- });
783
- const $addAndMultiply = derivation((t) => {
784
- const add = $addAB.get(t);
785
- const multiply = $multiplyAB.get(t);
786
- return add * multiply;
787
- });
788
- const onData = vi.fn();
789
-
790
- subscribe((t) => $addAndMultiply.get(t), onData);
791
-
792
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
793
- expect(onData).toHaveBeenLastCalledWith(6);
794
-
795
- $stateA.set(2);
796
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
797
- expect(onData).toHaveBeenLastCalledWith(16);
798
-
799
- $stateB.set(3);
800
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(3));
801
- expect(onData).toHaveBeenLastCalledWith(30);
802
- });
803
- });
804
-
805
- describe("with refresh", () => {
806
- it("should re-execute effect when refresh forces recomputation", async () => {
807
- let multiplier = 2;
808
- const $state = state(5);
809
- const $derivation = derivation((t) => $state.get(t) * multiplier);
810
- const onData = vi.fn();
811
-
812
- subscribe((t) => $derivation.get(t), onData);
813
-
814
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
815
- expect(onData).toHaveBeenLastCalledWith(10);
816
-
817
- multiplier = 3;
818
- $derivation.refresh();
819
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(2));
820
- expect(onData).toHaveBeenLastCalledWith(15);
821
- });
822
-
823
- it("should not re-execute effect if refresh returns same value", async () => {
824
- const $state = state(1);
825
- const computeFn = vi.fn((t) => $state.get(t) * 2);
826
- const $derivation = derivation(computeFn);
827
- const onData = vi.fn();
828
-
829
- subscribe((t) => $derivation.get(t), onData);
830
-
831
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
832
- expect(computeFn).toHaveBeenCalledTimes(1);
833
-
834
- $derivation.refresh();
835
- await new Promise((resolve) => setTimeout(resolve, 50));
836
- expect(onData).toHaveBeenCalledTimes(1);
837
- expect(computeFn).toHaveBeenCalledTimes(2);
838
- });
839
-
840
- it("should force recomputation even if dependencies have not changed", async () => {
841
- const $state = state(1);
842
- const computeFn = vi.fn((t) => $state.get(t) * 2);
843
- const $derivation = derivation(computeFn);
844
- const onData = vi.fn();
845
-
846
- subscribe((t) => $derivation.get(t), onData);
847
-
848
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
849
- expect(computeFn).toHaveBeenCalledTimes(1);
850
-
851
- $derivation.refresh();
852
- await new Promise((resolve) => setTimeout(resolve, 50));
853
- expect(computeFn).toHaveBeenCalledTimes(2);
854
- });
855
- });
856
-
857
- describe("error handling", () => {
858
- it("should propagate errors in compute function with dependencies in effect", async () => {
859
- const $state = state(1);
860
- const error = new Error("Effect compute error");
861
- const $derivation = derivation((t) => {
862
- const value = $state.get(t);
863
- if (value > 1) {
864
- throw error;
865
- }
866
- return value;
867
- });
868
-
869
- const onData = vi.fn();
870
- const onError = vi.fn();
871
- subscribe((t) => $derivation.get(t), onData, onError);
872
-
873
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
874
-
875
- $state.set(2);
876
- await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(1));
877
- expect(onError).toHaveBeenLastCalledWith(error);
878
- });
879
-
880
- it("should handle errors when dependency is disposed during computation", async () => {
881
- const $state = state(1);
882
- const $derivation = derivation((t) => $state.get(t) * 2);
883
- const onData = vi.fn();
884
-
885
- subscribe((t) => $derivation.get(t), onData);
886
-
887
- await vi.waitFor(() => expect(onData).toHaveBeenCalledTimes(1));
888
-
889
- $state.dispose();
890
-
891
- await new Promise((resolve) => setTimeout(resolve, 50));
892
- expect(onData).toHaveBeenCalledTimes(1);
893
- });
894
- });
895
- });
896
- });