@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,125 +0,0 @@
1
- import type { DerivationFunction, InitFunction, NotPromise, UpdateFunction } from "../api";
2
- import { SyncScheduler } from "../schedulers";
3
- import { ValueNode } from "./valueNode";
4
-
5
- /**
6
- * Function that computes a value (either initialization or derivation).
7
- * @internal
8
- */
9
- export type ComputeFunction<T> = InitFunction<T> | DerivationFunction<T>;
10
-
11
- /**
12
- * Synchronous reactive value that computes immediately without async scheduling.
13
- * @internal
14
- */
15
- export class ValueSyncNode<T extends NotPromise<unknown>> extends ValueNode<T> {
16
- protected _scheduler: SyncScheduler<T>;
17
- private _compute: () => T;
18
-
19
- constructor(valueOrCompute: NotPromise<T> | ComputeFunction<T>) {
20
- super();
21
- if (typeof valueOrCompute === "function") {
22
- // compute is a function
23
- // @ts-expect-error valueOrCompute is a function
24
- this._compute = () => valueOrCompute(this, this._value);
25
- } else {
26
- // compute is a value
27
- this._compute = () => valueOrCompute;
28
- }
29
-
30
- this.status = "dirty";
31
- this._scheduler = new SyncScheduler<T>(
32
- () => this._compute(),
33
- (value) => this._onResolve(value as NotPromise<T>),
34
- (error) => this._onReject(error),
35
- );
36
-
37
- if (typeof valueOrCompute !== "function") {
38
- this._scheduler.compute();
39
- }
40
- }
41
-
42
- private _onResolve(value: NotPromise<T>) {
43
- this.status = "resolved";
44
- this._value = value;
45
- this._error = undefined;
46
- }
47
-
48
- private _onReject(error: unknown) {
49
- this.status = "error";
50
- this._error = error;
51
- }
52
-
53
- set(value: NotPromise<T>): void;
54
- set(updater: UpdateFunction<T>): void;
55
- set(valueOrUpdater: NotPromise<T> | UpdateFunction<T>): void {
56
- if (this.disposed) throw new Error("[PicoFlow] Primitive is disposed");
57
-
58
- if (typeof valueOrUpdater === "function") {
59
- const updater = valueOrUpdater as UpdateFunction<T>;
60
- switch (this.status) {
61
- case "resolved": {
62
- const nextValue = updater(this._value);
63
- if (nextValue === this._value) return;
64
- this.status = "pending";
65
- this._scheduler.overwrite(nextValue);
66
- this.notifyDependents();
67
- return;
68
- }
69
- case "pending":
70
- case "error":
71
- case "dirty": {
72
- // TODO: For now we use latest value to compute the next value
73
- // Maybe we should throw instead ?
74
- const nextValue = updater(this._value);
75
- this.status = "pending";
76
- this._scheduler.overwrite(nextValue);
77
- this.notifyDependents();
78
- return;
79
- }
80
- }
81
- } else {
82
- const nextValue = valueOrUpdater;
83
- switch (this.status) {
84
- case "resolved": {
85
- if (nextValue === this._value) return;
86
- this.status = "pending";
87
- this._scheduler.overwrite(nextValue);
88
- this.notifyDependents();
89
- return;
90
- }
91
- case "pending":
92
- case "error":
93
- case "dirty": {
94
- this.status = "pending";
95
- this._scheduler.overwrite(nextValue);
96
- this.notifyDependents();
97
- return;
98
- }
99
- }
100
- }
101
- }
102
-
103
- refresh(): void {
104
- if (this.disposed) throw new Error("[PicoFlow] Primitive is disposed");
105
-
106
- const currentValue = this._value;
107
- const currentStatus = this.status;
108
- this.execute();
109
-
110
- switch (this.status) {
111
- case "resolved": {
112
- const nextValue = this._value;
113
- if (nextValue === currentValue && currentStatus === this.status) return;
114
- this.notifyDependents();
115
- return;
116
- }
117
- case "error":
118
- case "pending":
119
- case "dirty": {
120
- this.notifyDependents();
121
- return;
122
- }
123
- }
124
- }
125
- }
@@ -1,78 +0,0 @@
1
- import { PendingError } from "./pendingError";
2
-
3
- /**
4
- * Manages asynchronous computation lifecycle with iteration tracking to prevent race conditions.
5
- * @internal
6
- */
7
- export class AsyncResolver<T> {
8
- private readonly _compute: () => Promise<T>;
9
- private readonly _computed = Promise.withResolvers<T>();
10
- private _iteration = 0;
11
- private _aborted = false;
12
- private _finished = false;
13
-
14
- constructor(compute: () => Promise<T>) {
15
- this._compute = compute;
16
-
17
- // Prevent unhandled rejections before finally block is executed: catch all errors so they don't propagate.
18
- // Actual error handling occurs elsewhere in ComputationScheduler through proper promise chaining.
19
- this._computed.promise
20
- .catch(() => {})
21
- .finally(() => {
22
- this._finished = true;
23
- });
24
- }
25
-
26
- get computed(): Promise<T> {
27
- return this._computed.promise;
28
- }
29
-
30
- get aborted(): boolean {
31
- return this._aborted;
32
- }
33
-
34
- get finished(): boolean {
35
- return this._finished;
36
- }
37
-
38
- compute(): void {
39
- if (this._finished) throw new Error("[Picoflow] PendingResolver: Can't restart a settled resolver");
40
- if (this._aborted) throw new Error("[Picoflow] PendingResolver: Can't restart an aborted resolver");
41
-
42
- this._iteration++;
43
- const currentIteration = this._iteration;
44
- this._compute()
45
- .then((value) => {
46
- if (this._iteration === currentIteration && !this._aborted) this._computed.resolve(value);
47
- })
48
- .catch((error) => {
49
- if (this._iteration === currentIteration && !this._aborted) {
50
- if (!(error instanceof PendingError)) {
51
- this._computed.reject(error);
52
- }
53
- }
54
- });
55
- }
56
-
57
- overwrite(promise: Promise<T>): void {
58
- if (this._finished) throw new Error("[Picoflow] PendingResolver: Can't overwrite a settled resolver");
59
- if (this._aborted) throw new Error("[Picoflow] PendingResolver: Can't overwrite an aborted resolver");
60
-
61
- this._iteration++;
62
- const currentIteration = this._iteration;
63
- promise
64
- .then((value) => {
65
- if (this._iteration === currentIteration && !this._aborted) this._computed.resolve(value);
66
- })
67
- .catch((error) => {
68
- if (this._iteration === currentIteration && !this._aborted) {
69
- this._computed.reject(error);
70
- }
71
- });
72
- }
73
-
74
- abort(): void {
75
- this._iteration++;
76
- this._aborted = true;
77
- }
78
- }
@@ -1,66 +0,0 @@
1
- import { AsyncResolver } from "./asyncResolver";
2
- import type { Scheduler } from "./scheduler";
3
-
4
- /**
5
- * Scheduler that manages asynchronous computation lifecycle with resolver instances.
6
- * @internal
7
- */
8
- export class AsyncScheduler<T> implements Scheduler {
9
- private readonly _compute: () => Promise<T>;
10
- private _resolver: AsyncResolver<T>;
11
- private _onResolve: (value: T) => void;
12
- private _onReject: (error: unknown) => void;
13
- private _settled = Promise.withResolvers<void>();
14
- private _disposed = false;
15
-
16
- constructor(compute: () => Promise<T>, onResolve: (value: T) => void, onReject: (error: unknown) => void) {
17
- this._compute = compute;
18
- this._onResolve = onResolve;
19
- this._onReject = onReject;
20
- this._resolver = new AsyncResolver(compute);
21
- this._resolver.computed
22
- .then(this._onResolve)
23
- .catch(this._onReject)
24
- .finally(() => this._settled.resolve());
25
- }
26
-
27
- get settled(): Promise<void> {
28
- if (this._disposed) throw new Error("[PicoFlow] ComputationScheduler is disposed");
29
- return this._settled.promise;
30
- }
31
-
32
- get disposed(): boolean {
33
- return this._disposed;
34
- }
35
-
36
- overwrite(promise: Promise<T>): void {
37
- if (this._disposed) throw new Error("[PicoFlow] ComputationScheduler is disposed");
38
- if (this._resolver.finished) {
39
- this._resolver = new AsyncResolver(this._compute);
40
- this._settled = Promise.withResolvers<void>();
41
- this._resolver.computed
42
- .then(this._onResolve)
43
- .catch(this._onReject)
44
- .finally(() => this._settled.resolve());
45
- }
46
- this._resolver.overwrite(promise);
47
- }
48
-
49
- compute(): void {
50
- if (this._disposed) throw new Error("[PicoFlow] ComputationScheduler is disposed");
51
- if (this._resolver.finished) {
52
- this._resolver = new AsyncResolver(this._compute);
53
- this._settled = Promise.withResolvers<void>();
54
- this._resolver.computed
55
- .then(this._onResolve)
56
- .catch(this._onReject)
57
- .finally(() => this._settled.resolve());
58
- }
59
- this._resolver.compute();
60
- }
61
-
62
- dispose(): void {
63
- this._resolver.abort();
64
- this._disposed = true;
65
- }
66
- }
@@ -1,4 +0,0 @@
1
- export * from "./asyncScheduler";
2
- export * from "./pendingError";
3
- export * from "./scheduler";
4
- export * from "./syncScheduler";
@@ -1,13 +0,0 @@
1
- /**
2
- * Error thrown when an async computation is pending, encapsulating its promise for cascading dependencies.
3
- * @internal
4
- */
5
- export class PendingError extends Error {
6
- readonly pendingPromise: Promise<unknown>;
7
-
8
- constructor(promise: Promise<unknown>) {
9
- super("[PicoFlow] PendingResolver pending");
10
- this.name = "PendingError";
11
- this.pendingPromise = promise;
12
- }
13
- }
@@ -1,9 +0,0 @@
1
- /**
2
- * Contract for schedulers that coordinate reactive computations and their lifecycle.
3
- * @internal
4
- */
5
- export interface Scheduler {
6
- compute(): void;
7
- dispose(): void;
8
- settled: Promise<void>;
9
- }
@@ -1,69 +0,0 @@
1
- import { PendingError } from "./pendingError";
2
-
3
- /**
4
- * Manages synchronous computation lifecycle with iteration tracking to prevent race conditions.
5
- * @internal
6
- */
7
- export class SyncResolver<T> {
8
- private readonly _compute: () => T;
9
- private _iteration = 0;
10
- private _aborted = false;
11
- private _finished = false;
12
-
13
- private _onValue: (value: T) => void;
14
- private _onError: (error: unknown) => void;
15
-
16
- constructor(compute: () => T, onValue: (value: T) => void, onError: (error: unknown) => void) {
17
- this._compute = compute;
18
- this._onValue = onValue;
19
- this._onError = onError;
20
- }
21
-
22
- get aborted(): boolean {
23
- return this._aborted;
24
- }
25
-
26
- get finished(): boolean {
27
- return this._finished;
28
- }
29
-
30
- compute(): void {
31
- if (this._finished) throw new Error("[Picoflow] PendingResolver: Can't restart a settled resolver");
32
- if (this._aborted) throw new Error("[Picoflow] PendingResolver: Can't restart an aborted resolver");
33
-
34
- this._iteration++;
35
- const currentIteration = this._iteration;
36
- try {
37
- const value = this._compute();
38
- if (this._iteration === currentIteration && !this._aborted) {
39
- this._finished = true;
40
- this._onValue(value);
41
- }
42
- } catch (error) {
43
- if (this._iteration === currentIteration && !this._aborted) {
44
- if (!(error instanceof PendingError)) {
45
- this._finished = true;
46
- this._onError(error);
47
- }
48
- }
49
- }
50
- }
51
-
52
- overwrite(value: T): void {
53
- if (this._finished) throw new Error("[Picoflow] PendingResolver: Can't overwrite a settled resolver");
54
- if (this._aborted) throw new Error("[Picoflow] PendingResolver: Can't overwrite an aborted resolver");
55
-
56
- this._iteration++;
57
- const currentIteration = this._iteration;
58
-
59
- if (this._iteration === currentIteration && !this._aborted) {
60
- this._finished = true;
61
- this._onValue(value);
62
- }
63
- }
64
-
65
- abort(): void {
66
- this._iteration++;
67
- this._aborted = true;
68
- }
69
- }
@@ -1,55 +0,0 @@
1
- import type { Scheduler } from "./scheduler";
2
- import { SyncResolver } from "./syncResolver";
3
-
4
- /**
5
- * Scheduler that manages synchronous computation lifecycle with resolver instances.
6
- * @internal
7
- */
8
- export class SyncScheduler<T> implements Scheduler {
9
- private readonly _compute: () => T;
10
- private _resolver: SyncResolver<T>;
11
- private _onResolve: (value: T) => void;
12
- private _onReject: (error: unknown) => void;
13
- private _settled = Promise.withResolvers<void>();
14
- private _disposed = false;
15
-
16
- constructor(compute: () => T, onResolve: (value: T) => void, onReject: (error: unknown) => void) {
17
- this._compute = compute;
18
- this._onResolve = (value) => {
19
- onResolve(value);
20
- this._settled.resolve();
21
- };
22
- this._onReject = (error) => {
23
- onReject(error);
24
- this._settled.resolve();
25
- };
26
- this._resolver = new SyncResolver(compute, this._onResolve, this._onReject);
27
- }
28
-
29
- get settled(): Promise<void> {
30
- return this._settled.promise;
31
- }
32
-
33
- overwrite(value: T): void {
34
- if (this._disposed) throw new Error("[PicoFlow] ComputationScheduler is disposed");
35
- if (this._resolver.finished) {
36
- this._settled = Promise.withResolvers<void>();
37
- this._resolver = new SyncResolver(this._compute, this._onResolve, this._onReject);
38
- }
39
- this._resolver.overwrite(value);
40
- }
41
-
42
- compute(): void {
43
- if (this._disposed) throw new Error("[PicoFlow] ComputationScheduler is disposed");
44
- if (this._resolver.finished) {
45
- this._settled = Promise.withResolvers<void>();
46
- this._resolver = new SyncResolver(this._compute, this._onResolve, this._onReject);
47
- }
48
- this._resolver.compute();
49
- }
50
-
51
- dispose(): void {
52
- this._resolver.abort();
53
- this._disposed = true;
54
- }
55
- }
@@ -1,67 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { PendingError } from "../../src/schedulers";
3
-
4
- describe("PendingError", () => {
5
- describe("constructor and properties", () => {
6
- it("should create instance with pending promise", () => {
7
- const promise = Promise.resolve(42);
8
- const error = new PendingError(promise);
9
-
10
- expect(error).toBeInstanceOf(PendingError);
11
- expect(error.pendingPromise).toBe(promise);
12
- });
13
-
14
- it("should have correct name", () => {
15
- const promise = Promise.resolve(42);
16
- const error = new PendingError(promise);
17
-
18
- expect(error.name).toBe("PendingError");
19
- });
20
-
21
- it("should have correct message", () => {
22
- const promise = Promise.resolve(42);
23
- const error = new PendingError(promise);
24
-
25
- expect(error.message).toBe("[PicoFlow] PendingResolver pending");
26
- });
27
-
28
- it("should extend Error", () => {
29
- const promise = Promise.resolve(42);
30
- const error = new PendingError(promise);
31
-
32
- expect(error).toBeInstanceOf(Error);
33
- });
34
-
35
- it("should store pendingPromise property correctly", async () => {
36
- const promise = Promise.resolve("test-value");
37
- const error = new PendingError(promise);
38
-
39
- expect(error.pendingPromise).toBe(promise);
40
- expect(await error.pendingPromise).toBe("test-value");
41
- });
42
- });
43
-
44
- describe("pendingPromise behavior", () => {
45
- it("should expose the pending promise", () => {
46
- const promise = Promise.resolve(100);
47
- const error = new PendingError(promise);
48
-
49
- expect(error.pendingPromise).toBe(promise);
50
- });
51
-
52
- it("should allow awaiting the pending promise", async () => {
53
- const promise = Promise.resolve("resolved-value");
54
- const error = new PendingError(promise);
55
-
56
- const result = await error.pendingPromise;
57
- expect(result).toBe("resolved-value");
58
- });
59
-
60
- it("should allow awaiting rejected promise", async () => {
61
- const promise = Promise.reject(new Error("test-error"));
62
- const error = new PendingError(promise);
63
-
64
- await expect(error.pendingPromise).rejects.toThrow("test-error");
65
- });
66
- });
67
- });
@@ -1,69 +0,0 @@
1
- import { cleanup, render } from "@solidjs/testing-library";
2
- import { type Component, Match, Switch } from "solid-js";
3
- import { beforeEach, describe, expect, it, vi } from "vitest";
4
- import { userEvent } from "vitest/browser";
5
- import { derivation, from } from "~";
6
-
7
- describe("solid converter", () => {
8
- beforeEach(() => {
9
- cleanup();
10
- });
11
-
12
- describe("updates", () => {
13
- describe("error", () => {
14
- describe("flag", () => {
15
- it("update-error-flag: should handle error in FlowDerivation", async () => {
16
- const Comp: Component = () => {
17
- let condition = false;
18
- const $derivation = derivation(() => {
19
- if (condition) {
20
- throw new Error("Test error");
21
- }
22
- return 42;
23
- });
24
-
25
- const $$derivation = from($derivation);
26
-
27
- return (
28
- <div>
29
- <Switch>
30
- <Match when={$$derivation.error}>
31
- <div>Error</div>
32
- </Match>
33
- <Match when={$$derivation()}>
34
- <div>{$$derivation()}</div>
35
- </Match>
36
- </Switch>
37
- <button
38
- type="button"
39
- onClick={() => {
40
- condition = true;
41
- $derivation.refresh();
42
- }}
43
- >
44
- Update Error
45
- </button>
46
- <button
47
- type="button"
48
- onClick={() => {
49
- condition = false;
50
- $derivation.refresh();
51
- }}
52
- >
53
- Update Value
54
- </button>
55
- </div>
56
- );
57
- };
58
-
59
- const { getByText } = render(() => <Comp />);
60
- await vi.waitFor(() => expect.element(getByText("42")).toBeInTheDocument());
61
- await userEvent.click(getByText("Update Error"));
62
- await vi.waitFor(() => expect.element(getByText("Error")).toBeInTheDocument());
63
- await userEvent.click(getByText("Update Value"));
64
- await vi.waitFor(() => expect.element(getByText("42")).toBeInTheDocument());
65
- });
66
- });
67
- });
68
- });
69
- });