@ersbeth/picoflow 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.cursor/plans/unifier-flowresource-avec-flowderivation-c9506e24.plan.md +372 -0
- package/README.md +17 -1
- package/biome.json +4 -1
- package/dist/picoflow.js +1129 -661
- package/dist/types/flow/base/flowDisposable.d.ts +67 -0
- package/dist/types/flow/base/flowDisposable.d.ts.map +1 -0
- package/dist/types/flow/base/flowEffect.d.ts +127 -0
- package/dist/types/flow/base/flowEffect.d.ts.map +1 -0
- package/dist/types/flow/base/flowGraph.d.ts +97 -0
- package/dist/types/flow/base/flowGraph.d.ts.map +1 -0
- package/dist/types/flow/base/flowSignal.d.ts +134 -0
- package/dist/types/flow/base/flowSignal.d.ts.map +1 -0
- package/dist/types/flow/base/flowTracker.d.ts +15 -0
- package/dist/types/flow/base/flowTracker.d.ts.map +1 -0
- package/dist/types/flow/base/index.d.ts +7 -0
- package/dist/types/flow/base/index.d.ts.map +1 -0
- package/dist/types/flow/base/utils.d.ts +20 -0
- package/dist/types/flow/base/utils.d.ts.map +1 -0
- package/dist/types/{advanced/array.d.ts → flow/collections/flowArray.d.ts} +50 -12
- package/dist/types/flow/collections/flowArray.d.ts.map +1 -0
- package/dist/types/flow/collections/flowMap.d.ts +224 -0
- package/dist/types/flow/collections/flowMap.d.ts.map +1 -0
- package/dist/types/flow/collections/index.d.ts +3 -0
- package/dist/types/flow/collections/index.d.ts.map +1 -0
- package/dist/types/flow/index.d.ts +4 -0
- package/dist/types/flow/index.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowConstantAsync.d.ts +137 -0
- package/dist/types/flow/nodes/async/flowConstantAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts +137 -0
- package/dist/types/flow/nodes/async/flowDerivationAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowNodeAsync.d.ts +343 -0
- package/dist/types/flow/nodes/async/flowNodeAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts +81 -0
- package/dist/types/flow/nodes/async/flowReadonlyAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/flowStateAsync.d.ts +111 -0
- package/dist/types/flow/nodes/async/flowStateAsync.d.ts.map +1 -0
- package/dist/types/flow/nodes/async/index.d.ts +6 -0
- package/dist/types/flow/nodes/async/index.d.ts.map +1 -0
- package/dist/types/flow/nodes/index.d.ts +3 -0
- package/dist/types/flow/nodes/index.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowConstant.d.ts +108 -0
- package/dist/types/flow/nodes/sync/flowConstant.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowDerivation.d.ts +100 -0
- package/dist/types/flow/nodes/sync/flowDerivation.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowNode.d.ts +314 -0
- package/dist/types/flow/nodes/sync/flowNode.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowReadonly.d.ts +57 -0
- package/dist/types/flow/nodes/sync/flowReadonly.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/flowState.d.ts +96 -0
- package/dist/types/flow/nodes/sync/flowState.d.ts.map +1 -0
- package/dist/types/flow/nodes/sync/index.d.ts +6 -0
- package/dist/types/flow/nodes/sync/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/solid/converters.d.ts +34 -44
- package/dist/types/solid/converters.d.ts.map +1 -1
- package/dist/types/solid/primitives.d.ts +1 -0
- package/dist/types/solid/primitives.d.ts.map +1 -1
- package/docs/.vitepress/config.mts +1 -1
- package/docs/api/typedoc-sidebar.json +81 -1
- package/package.json +60 -58
- package/src/flow/base/flowDisposable.ts +71 -0
- package/src/flow/base/flowEffect.ts +171 -0
- package/src/flow/base/flowGraph.ts +288 -0
- package/src/flow/base/flowSignal.ts +207 -0
- package/src/flow/base/flowTracker.ts +17 -0
- package/src/flow/base/index.ts +6 -0
- package/src/flow/base/utils.ts +19 -0
- package/src/flow/collections/flowArray.ts +409 -0
- package/src/flow/collections/flowMap.ts +398 -0
- package/src/flow/collections/index.ts +2 -0
- package/src/flow/index.ts +3 -0
- package/src/flow/nodes/async/flowConstantAsync.ts +142 -0
- package/src/flow/nodes/async/flowDerivationAsync.ts +143 -0
- package/src/flow/nodes/async/flowNodeAsync.ts +474 -0
- package/src/flow/nodes/async/flowReadonlyAsync.ts +81 -0
- package/src/flow/nodes/async/flowStateAsync.ts +116 -0
- package/src/flow/nodes/async/index.ts +5 -0
- package/src/flow/nodes/await/advanced/index.ts +5 -0
- package/src/{advanced → flow/nodes/await/advanced}/resource.ts +37 -3
- package/src/{advanced → flow/nodes/await/advanced}/resourceAsync.ts +35 -3
- package/src/{advanced → flow/nodes/await/advanced}/stream.ts +40 -2
- package/src/{advanced → flow/nodes/await/advanced}/streamAsync.ts +38 -3
- package/src/flow/nodes/await/flowConstantAwait.ts +154 -0
- package/src/flow/nodes/await/flowDerivationAwait.ts +154 -0
- package/src/flow/nodes/await/flowNodeAwait.ts +508 -0
- package/src/flow/nodes/await/flowReadonlyAwait.ts +89 -0
- package/src/flow/nodes/await/flowStateAwait.ts +130 -0
- package/src/flow/nodes/await/index.ts +5 -0
- package/src/flow/nodes/index.ts +3 -0
- package/src/flow/nodes/sync/flowConstant.ts +111 -0
- package/src/flow/nodes/sync/flowDerivation.ts +105 -0
- package/src/flow/nodes/sync/flowNode.ts +439 -0
- package/src/flow/nodes/sync/flowReadonly.ts +57 -0
- package/src/flow/nodes/sync/flowState.ts +101 -0
- package/src/flow/nodes/sync/index.ts +5 -0
- package/src/index.ts +1 -47
- package/src/solid/converters.ts +59 -198
- package/src/solid/primitives.ts +4 -0
- package/test/base/flowEffect.test.ts +108 -0
- package/test/base/flowGraph.test.ts +485 -0
- package/test/base/flowSignal.test.ts +372 -0
- package/test/collections/flowArray.asyncStates.test.ts +1553 -0
- package/test/collections/flowArray.scalars.test.ts +1129 -0
- package/test/collections/flowArray.states.test.ts +1365 -0
- package/test/collections/flowMap.asyncStates.test.ts +1105 -0
- package/test/collections/flowMap.scalars.test.ts +877 -0
- package/test/collections/flowMap.states.test.ts +1097 -0
- package/test/nodes/async/flowConstantAsync.test.ts +860 -0
- package/test/nodes/async/flowDerivationAsync.test.ts +1517 -0
- package/test/nodes/async/flowStateAsync.test.ts +1387 -0
- package/test/{resource.test.ts → nodes/await/advanced/resource.test.ts} +21 -19
- package/test/{resourceAsync.test.ts → nodes/await/advanced/resourceAsync.test.ts} +3 -1
- package/test/{stream.test.ts → nodes/await/advanced/stream.test.ts} +30 -28
- package/test/{streamAsync.test.ts → nodes/await/advanced/streamAsync.test.ts} +16 -14
- package/test/nodes/await/flowConstantAwait.test.ts +643 -0
- package/test/nodes/await/flowDerivationAwait.test.ts +1583 -0
- package/test/nodes/await/flowStateAwait.test.ts +999 -0
- package/test/nodes/mixed/derivation.test.ts +1527 -0
- package/test/nodes/sync/flowConstant.test.ts +620 -0
- package/test/nodes/sync/flowDerivation.test.ts +1373 -0
- package/test/nodes/sync/flowState.test.ts +945 -0
- package/test/solid/converters.test.ts +721 -0
- package/test/solid/primitives.test.ts +1031 -0
- package/tsconfig.json +2 -1
- package/vitest.config.ts +7 -1
- package/IMPLEMENTATION_GUIDE.md +0 -1578
- package/dist/types/advanced/array.d.ts.map +0 -1
- package/dist/types/advanced/index.d.ts +0 -9
- package/dist/types/advanced/index.d.ts.map +0 -1
- package/dist/types/advanced/map.d.ts +0 -166
- package/dist/types/advanced/map.d.ts.map +0 -1
- package/dist/types/advanced/resource.d.ts +0 -78
- package/dist/types/advanced/resource.d.ts.map +0 -1
- package/dist/types/advanced/resourceAsync.d.ts +0 -56
- package/dist/types/advanced/resourceAsync.d.ts.map +0 -1
- package/dist/types/advanced/stream.d.ts +0 -117
- package/dist/types/advanced/stream.d.ts.map +0 -1
- package/dist/types/advanced/streamAsync.d.ts +0 -97
- package/dist/types/advanced/streamAsync.d.ts.map +0 -1
- package/dist/types/basic/constant.d.ts +0 -60
- package/dist/types/basic/constant.d.ts.map +0 -1
- package/dist/types/basic/derivation.d.ts +0 -89
- package/dist/types/basic/derivation.d.ts.map +0 -1
- package/dist/types/basic/disposable.d.ts +0 -82
- package/dist/types/basic/disposable.d.ts.map +0 -1
- package/dist/types/basic/effect.d.ts +0 -67
- package/dist/types/basic/effect.d.ts.map +0 -1
- package/dist/types/basic/index.d.ts +0 -10
- package/dist/types/basic/index.d.ts.map +0 -1
- package/dist/types/basic/observable.d.ts +0 -83
- package/dist/types/basic/observable.d.ts.map +0 -1
- package/dist/types/basic/signal.d.ts +0 -69
- package/dist/types/basic/signal.d.ts.map +0 -1
- package/dist/types/basic/state.d.ts +0 -47
- package/dist/types/basic/state.d.ts.map +0 -1
- package/dist/types/basic/trackingContext.d.ts +0 -33
- package/dist/types/basic/trackingContext.d.ts.map +0 -1
- package/dist/types/creators.d.ts +0 -340
- package/dist/types/creators.d.ts.map +0 -1
- package/src/advanced/array.ts +0 -222
- package/src/advanced/index.ts +0 -12
- package/src/advanced/map.ts +0 -193
- package/src/basic/constant.ts +0 -97
- package/src/basic/derivation.ts +0 -147
- package/src/basic/disposable.ts +0 -86
- package/src/basic/effect.ts +0 -104
- package/src/basic/index.ts +0 -9
- package/src/basic/observable.ts +0 -109
- package/src/basic/signal.ts +0 -145
- package/src/basic/state.ts +0 -60
- package/src/basic/trackingContext.ts +0 -45
- package/src/creators.ts +0 -395
- package/test/array.test.ts +0 -600
- package/test/constant.test.ts +0 -44
- package/test/derivation.test.ts +0 -539
- package/test/effect.test.ts +0 -29
- package/test/map.test.ts +0 -240
- package/test/signal.test.ts +0 -72
- package/test/state.test.ts +0 -212
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import { FlowSignal, FlowTracker } from '../../base';
|
|
2
|
+
/**
|
|
3
|
+
* A versatile reactive node that can operate as mutable state or computed derivation, with asynchronous values.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam T - The type of the value held by this node (not the Promise itself).
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* FlowNodeAsync is a flexible reactive primitive that adapts its behavior based on how it's initialized.
|
|
9
|
+
* It extends {@link FlowSignal} and provides reactive value access with automatic dependency tracking.
|
|
10
|
+
* Unlike its synchronous counterpart {@link FlowNode}, FlowNodeAsync always works with `Promise<T>` values,
|
|
11
|
+
* making it suitable for asynchronous computations and data fetching.
|
|
12
|
+
*
|
|
13
|
+
* **Operating Modes:**
|
|
14
|
+
*
|
|
15
|
+
* - **Mutable State Mode**: When constructed with a constant Promise, FlowNodeAsync acts as a mutable
|
|
16
|
+
* reactive state. You can update its value using `set()`, and all dependents are automatically
|
|
17
|
+
* notified when the value changes. The value is always a Promise that resolves to type T.
|
|
18
|
+
*
|
|
19
|
+
* - **Computed Derivation Mode**: When constructed with a compute function, FlowNodeAsync acts as a
|
|
20
|
+
* computed value that automatically tracks its dependencies. The compute function runs lazily
|
|
21
|
+
* (only when the value is first accessed) and recomputes when any tracked dependency changes.
|
|
22
|
+
* The compute function must return a `Promise<T>`. The computed value can be temporarily overridden
|
|
23
|
+
* using `set()`, but the override is cleared on the next recomputation (triggered by dependency changes).
|
|
24
|
+
*
|
|
25
|
+
* **Asynchronous Nature:**
|
|
26
|
+
* All values in FlowNodeAsync are Promises. When you call `get()` or `pick()`, you receive a `Promise<T>`
|
|
27
|
+
* that you must await to get the actual value. This allows for seamless integration with async/await
|
|
28
|
+
* patterns and asynchronous data sources.
|
|
29
|
+
*
|
|
30
|
+
* **Lazy Computation:**
|
|
31
|
+
* When using a compute function, the computation doesn't run immediately upon construction. It
|
|
32
|
+
* executes only when:
|
|
33
|
+
* - The value is first read via `get()` or `pick()`
|
|
34
|
+
* - The node is watched via `watch()`
|
|
35
|
+
*
|
|
36
|
+
* **Dirty Tracking:**
|
|
37
|
+
* When a tracked dependency changes, the node is marked as "dirty" but doesn't recompute
|
|
38
|
+
* immediately. Recomputation happens lazily on the next value access, preventing unnecessary
|
|
39
|
+
* computations when multiple dependencies change in quick succession. The Promise is cached until
|
|
40
|
+
* dependencies change, ensuring efficient access patterns.
|
|
41
|
+
*
|
|
42
|
+
* **Dynamic Dependencies:**
|
|
43
|
+
* Dependencies are tracked dynamically during each computation. If the compute function
|
|
44
|
+
* conditionally tracks different observables, the dependency graph updates automatically.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* // Mutable state mode
|
|
49
|
+
* const $count = new FlowNodeAsync(Promise.resolve(0));
|
|
50
|
+
* await $count.set(Promise.resolve(1)); // Updates the value
|
|
51
|
+
*
|
|
52
|
+
* // Computed derivation mode
|
|
53
|
+
* const $firstName = new FlowNodeAsync(Promise.resolve('John'));
|
|
54
|
+
* const $lastName = new FlowNodeAsync(Promise.resolve('Doe'));
|
|
55
|
+
* const $fullName = new FlowNodeAsync(async (t) => {
|
|
56
|
+
* const first = await $firstName.get(t);
|
|
57
|
+
* const last = await $lastName.get(t);
|
|
58
|
+
* return `${first} ${last}`;
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* // Compute function hasn't run yet (lazy)
|
|
62
|
+
* const name = await $fullName.pick(); // Now it computes: "John Doe"
|
|
63
|
+
*
|
|
64
|
+
* // Temporarily override computed value
|
|
65
|
+
* await $fullName.set(Promise.resolve('Override'));
|
|
66
|
+
* console.log(await $fullName.pick()); // "Override"
|
|
67
|
+
*
|
|
68
|
+
* // Trigger recomputation (clears override)
|
|
69
|
+
* await $firstName.set(Promise.resolve('Jane'));
|
|
70
|
+
* console.log(await $fullName.pick()); // "Jane Doe" (override cleared)
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @public
|
|
74
|
+
*/
|
|
75
|
+
export declare class FlowNodeAsync<T> extends FlowSignal {
|
|
76
|
+
protected _promise?: Promise<T>;
|
|
77
|
+
private _dirty;
|
|
78
|
+
private _compute?;
|
|
79
|
+
/**
|
|
80
|
+
* Creates a new FlowNodeAsync.
|
|
81
|
+
*
|
|
82
|
+
* @param compute - Either a constant Promise or a compute function that derives the value.
|
|
83
|
+
*
|
|
84
|
+
* @remarks
|
|
85
|
+
* The constructor accepts two different initialization modes:
|
|
86
|
+
*
|
|
87
|
+
* - **Constant Promise**: Creates a mutable reactive node that can be updated via `set()`.
|
|
88
|
+
* The Promise is stored immediately and can be changed at any time. The Promise resolves
|
|
89
|
+
* to the value of type T.
|
|
90
|
+
*
|
|
91
|
+
* - **Compute function**: Creates a computed reactive node that automatically tracks dependencies
|
|
92
|
+
* and recomputes when they change. The compute function receives a tracking context (`t`)
|
|
93
|
+
* that should be used to access dependencies via `.get(t)`. The function must return a
|
|
94
|
+
* `Promise<T>`. The function is not executed immediately; it runs lazily on first access.
|
|
95
|
+
* The computed value can be temporarily overridden using `set()`, but the override is cleared
|
|
96
|
+
* on the next recomputation (triggered by dependency changes).
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* // Mutable state with constant Promise
|
|
101
|
+
* const $count = new FlowNodeAsync(Promise.resolve(0));
|
|
102
|
+
* await $count.set(Promise.resolve(5));
|
|
103
|
+
*
|
|
104
|
+
* // Computed derivation with async function
|
|
105
|
+
* const $a = new FlowNodeAsync(Promise.resolve(10));
|
|
106
|
+
* const $b = new FlowNodeAsync(Promise.resolve(20));
|
|
107
|
+
* const $sum = new FlowNodeAsync(async (t) => {
|
|
108
|
+
* const a = await $a.get(t);
|
|
109
|
+
* const b = await $b.get(t);
|
|
110
|
+
* return a + b;
|
|
111
|
+
* });
|
|
112
|
+
*
|
|
113
|
+
* // Lazy evaluation - compute function hasn't run yet
|
|
114
|
+
* console.log(await $sum.pick()); // Now it computes: 30
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* @public
|
|
118
|
+
*/
|
|
119
|
+
constructor(compute: Promise<T> | ((t: FlowTracker) => Promise<T>));
|
|
120
|
+
get settled(): Promise<T> | undefined;
|
|
121
|
+
watch(tracker: FlowTracker): Promise<void>;
|
|
122
|
+
/**
|
|
123
|
+
* Gets the current value with dependency tracking.
|
|
124
|
+
*
|
|
125
|
+
* @param tracker - The tracking context for reactive tracking. This observable is registered
|
|
126
|
+
* as a dependency when accessed through this method.
|
|
127
|
+
*
|
|
128
|
+
* @returns A Promise that resolves to the current value of type T.
|
|
129
|
+
*
|
|
130
|
+
* @remarks
|
|
131
|
+
* Use `get(t)` within effects and derivations to create reactive dependencies. The tracker
|
|
132
|
+
* parameter must be provided from the reactive context (typically the `t` parameter in effect
|
|
133
|
+
* or derivation callbacks).
|
|
134
|
+
*
|
|
135
|
+
* **Important:** This method returns a `Promise<T>`, not `T`. You must await the Promise to
|
|
136
|
+
* get the actual value. The Promise is cached until dependencies change, ensuring efficient
|
|
137
|
+
* access patterns.
|
|
138
|
+
*
|
|
139
|
+
* To read a value without creating a dependency, use `pick()` instead.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* effect(async (t) => {
|
|
144
|
+
* const tracked = await $state.get(t); // Dependency registered, await the Promise
|
|
145
|
+
* const untracked = await $other.pick(); // No dependency
|
|
146
|
+
* });
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* @throws Error if the node has been disposed.
|
|
150
|
+
*
|
|
151
|
+
* @public
|
|
152
|
+
*/
|
|
153
|
+
get(tracker: FlowTracker | null): Promise<T>;
|
|
154
|
+
/**
|
|
155
|
+
* Updates the node with a new value.
|
|
156
|
+
*
|
|
157
|
+
* @param value - A new Promise or a callback function that computes a new Promise based on the current value.
|
|
158
|
+
*
|
|
159
|
+
* @returns A promise that resolves after the update is processed and all dependent effects have been notified.
|
|
160
|
+
*
|
|
161
|
+
* @remarks
|
|
162
|
+
* This method can be used in two ways:
|
|
163
|
+
*
|
|
164
|
+
* **For mutable state nodes** (constructed with a constant Promise):
|
|
165
|
+
* - Updates the stored Promise directly
|
|
166
|
+
* - All dependents are notified of the change
|
|
167
|
+
*
|
|
168
|
+
* **For computed nodes** (constructed with a compute function):
|
|
169
|
+
* - Temporarily overrides the computed value
|
|
170
|
+
* - The override persists until the next recomputation, which occurs when:
|
|
171
|
+
* - A tracked dependency changes, or
|
|
172
|
+
* - The node is refreshed
|
|
173
|
+
* - This allows temporarily overriding computed values for testing or manual control
|
|
174
|
+
*
|
|
175
|
+
* **Value Comparison:**
|
|
176
|
+
* The Promises are resolved and their values are compared. If the resolved new value is strictly
|
|
177
|
+
* equal (`===`) to the current resolved value, no update occurs and subscribers are not notified.
|
|
178
|
+
* This prevents unnecessary re-renders and effect executions.
|
|
179
|
+
*
|
|
180
|
+
* **Asynchronous Processing:**
|
|
181
|
+
* The update is processed asynchronously through the reactive graph, ensuring proper
|
|
182
|
+
* ordering of updates and effect execution. The method returns a Promise that resolves
|
|
183
|
+
* after the update is complete.
|
|
184
|
+
*
|
|
185
|
+
* @throws Error if the node has been disposed.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```typescript
|
|
189
|
+
* // Mutable state usage
|
|
190
|
+
* const $count = new FlowNodeAsync(Promise.resolve(0));
|
|
191
|
+
* await $count.set(Promise.resolve(5));
|
|
192
|
+
* await $count.set(async (current) => Promise.resolve(current + 1)); // 6
|
|
193
|
+
*
|
|
194
|
+
* // Temporary override of computed value
|
|
195
|
+
* const $source = new FlowNodeAsync(Promise.resolve(10));
|
|
196
|
+
* const $doubled = new FlowNodeAsync(async (t) => {
|
|
197
|
+
* const val = await $source.get(t);
|
|
198
|
+
* return val * 2;
|
|
199
|
+
* });
|
|
200
|
+
*
|
|
201
|
+
* console.log(await $doubled.pick()); // 20
|
|
202
|
+
*
|
|
203
|
+
* // Temporarily override
|
|
204
|
+
* await $doubled.set(Promise.resolve(50));
|
|
205
|
+
* console.log(await $doubled.pick()); // 50 (override active)
|
|
206
|
+
*
|
|
207
|
+
* // Dependency change clears override
|
|
208
|
+
* await $source.set(Promise.resolve(15));
|
|
209
|
+
* console.log(await $doubled.pick()); // 30 (recomputed, override cleared)
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* @public
|
|
213
|
+
*/
|
|
214
|
+
set(value: Promise<T> | ((current: T) => Promise<T>)): Promise<void>;
|
|
215
|
+
/**
|
|
216
|
+
* Forces recomputation of the value, even if it's not marked as dirty.
|
|
217
|
+
*
|
|
218
|
+
* @returns A promise that resolves after the recomputation is complete and all
|
|
219
|
+
* dependent effects have been notified.
|
|
220
|
+
*
|
|
221
|
+
* @remarks
|
|
222
|
+
* This method is useful when you need to force a recomputation of a computed value,
|
|
223
|
+
* for example when the computation depends on external data that has changed outside
|
|
224
|
+
* the reactive system.
|
|
225
|
+
*
|
|
226
|
+
* **Behavior:**
|
|
227
|
+
* - For nodes with a compute function: Forces the compute function to run again,
|
|
228
|
+
* even if no dependencies have changed. This effectively clears any temporary
|
|
229
|
+
* override that was set using `set()`.
|
|
230
|
+
* - For nodes without a compute function (mutable state): Recomputes the current
|
|
231
|
+
* value (which is just the stored value), useful for consistency but typically
|
|
232
|
+
* not necessary.
|
|
233
|
+
*
|
|
234
|
+
* The recomputation happens asynchronously through the reactive graph, ensuring
|
|
235
|
+
* proper ordering of updates and effect execution.
|
|
236
|
+
*
|
|
237
|
+
* @throws Error if the node has been disposed.
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* const $externalData = new FlowNode(() => fetchExternalData());
|
|
242
|
+
*
|
|
243
|
+
* // Some external event occurs that changes the data source
|
|
244
|
+
* externalDataChanged();
|
|
245
|
+
*
|
|
246
|
+
* // Force recomputation to get the new value
|
|
247
|
+
* await $externalData.refresh();
|
|
248
|
+
*
|
|
249
|
+
* // For computed nodes with temporary overrides
|
|
250
|
+
* const $computed = new FlowNode((t) => $source.get(t) * 2);
|
|
251
|
+
* $computed.set(100); // Temporary override
|
|
252
|
+
* await $computed.refresh(); // Clears override, recomputes from source
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
* @public
|
|
256
|
+
*/
|
|
257
|
+
refresh(): Promise<void>;
|
|
258
|
+
/**
|
|
259
|
+
* Gets the current value without any dependency tracking.
|
|
260
|
+
*
|
|
261
|
+
* @returns A promise that resolves with the current value of type T.
|
|
262
|
+
*
|
|
263
|
+
* @remarks
|
|
264
|
+
* This method reads the value asynchronously through the reactive graph, ensuring proper ordering of
|
|
265
|
+
* read operations. Unlike `get(t)`, this method does not create a reactive dependency.
|
|
266
|
+
*
|
|
267
|
+
* **Important:** This is an async method that returns `Promise<T>`. Always use `await` when calling it.
|
|
268
|
+
*
|
|
269
|
+
* Use `pick()` when you want to read a snapshot of the current value without creating a reactive
|
|
270
|
+
* dependency. This is useful for:
|
|
271
|
+
* - Reading initial values outside reactive contexts
|
|
272
|
+
* - Accessing configuration that shouldn't trigger updates
|
|
273
|
+
* - Mixing tracked and untracked reads in the same effect
|
|
274
|
+
*
|
|
275
|
+
* @example
|
|
276
|
+
* ```typescript
|
|
277
|
+
* // Read a snapshot outside reactive context
|
|
278
|
+
* const currentValue = await $state.pick();
|
|
279
|
+
*
|
|
280
|
+
* // Mix tracked and untracked reads
|
|
281
|
+
* effect(async (t) => {
|
|
282
|
+
* const tracked = await $reactive.get(t); // Triggers re-runs
|
|
283
|
+
* const snapshot = await $config.pick(); // Doesn't trigger re-runs
|
|
284
|
+
* processData(tracked, snapshot);
|
|
285
|
+
* });
|
|
286
|
+
* ```
|
|
287
|
+
*
|
|
288
|
+
* @throws Error if the node has been disposed.
|
|
289
|
+
*
|
|
290
|
+
* @public
|
|
291
|
+
*/
|
|
292
|
+
pick(): Promise<T>;
|
|
293
|
+
/**
|
|
294
|
+
* Subscribes a listener function to changes of this node.
|
|
295
|
+
*
|
|
296
|
+
* @param listener - A callback function that receives the new value whenever it changes.
|
|
297
|
+
*
|
|
298
|
+
* @returns A disposer function that cancels the subscription when called.
|
|
299
|
+
*
|
|
300
|
+
* @remarks
|
|
301
|
+
* This method creates a reactive subscription that automatically tracks this node as a dependency.
|
|
302
|
+
* The listener is executed:
|
|
303
|
+
* - Immediately with the current value when the subscription is created
|
|
304
|
+
* - Automatically whenever the value changes
|
|
305
|
+
*
|
|
306
|
+
* **Important:** The listener receives a `Promise<T>`, not `T`. You must await the Promise
|
|
307
|
+
* within the listener to access the actual value. The Promise is the same one returned by `get()`,
|
|
308
|
+
* so it's cached until dependencies change.
|
|
309
|
+
*
|
|
310
|
+
* The subscription uses a {@link FlowEffect} internally to manage the reactive tracking and
|
|
311
|
+
* automatic re-execution. When the value changes, the listener is called with the new Promise
|
|
312
|
+
* after the update is processed through the reactive graph.
|
|
313
|
+
*
|
|
314
|
+
* **Cleanup:**
|
|
315
|
+
* Always call the returned disposer function when you no longer need the subscription to
|
|
316
|
+
* prevent memory leaks and unnecessary computations.
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* ```typescript
|
|
320
|
+
* const $count = new FlowNodeAsync(Promise.resolve(0));
|
|
321
|
+
*
|
|
322
|
+
* // Subscribe to changes
|
|
323
|
+
* const unsubscribe = $count.subscribe(async (valuePromise) => {
|
|
324
|
+
* const value = await valuePromise;
|
|
325
|
+
* console.log(`Count is now: ${value}`);
|
|
326
|
+
* });
|
|
327
|
+
* // Logs immediately: "Count is now: 0"
|
|
328
|
+
*
|
|
329
|
+
* await $count.set(Promise.resolve(5));
|
|
330
|
+
* // Logs: "Count is now: 5"
|
|
331
|
+
*
|
|
332
|
+
* // Clean up when done
|
|
333
|
+
* unsubscribe();
|
|
334
|
+
* ```
|
|
335
|
+
*
|
|
336
|
+
* @throws Error if the node has been disposed.
|
|
337
|
+
*
|
|
338
|
+
* @public
|
|
339
|
+
*/
|
|
340
|
+
subscribe(listener: (value: Promise<T>) => void): () => void;
|
|
341
|
+
private _computeValue;
|
|
342
|
+
}
|
|
343
|
+
//# sourceMappingURL=flowNodeAsync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flowNodeAsync.d.ts","sourceRoot":"","sources":["../../../../../src/flow/nodes/async/flowNodeAsync.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,UAAU,EACV,KAAK,WAAW,EAChB,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwEG;AACH,qBAAa,aAAa,CAAC,CAAC,CAAE,SAAQ,UAAU;IAC/C,SAAS,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IAChC,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,QAAQ,CAAC,CAAiC;IAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuCG;gBACS,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;IAUlE,IAAI,OAAO,2BAEV;IAEc,KAAK,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAOzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACG,GAAG,CAAC,OAAO,EAAE,WAAW,GAAG,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC;IAOlD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2DG;IACG,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IA4B1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAyCG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAwB9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACG,IAAI,IAAI,OAAO,CAAC,CAAC,CAAC;IAMxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8CG;IACH,SAAS,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,IAAI,GAAG,MAAM,IAAI;YAkB9C,aAAa;CA8B3B"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { FlowNodeAsync } from './flowNodeAsync';
|
|
2
|
+
/**
|
|
3
|
+
* A generic utility type for creating read-only views of reactive nodes, with asynchronous values.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam T - The type of the value held by the reactive node (not the Promise itself).
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* `FlowReadonlyAsync` is a type alias based on {@link FlowNodeAsync} with the `set()` and `refresh()`
|
|
9
|
+
* methods removed, making it a read-only view. It provides all the reactive capabilities of `FlowNodeAsync`
|
|
10
|
+
* (dependency tracking, lazy computation, caching, reading values) but prevents mutation.
|
|
11
|
+
*
|
|
12
|
+
* **Asynchronous Nature:**
|
|
13
|
+
* All values in FlowReadonlyAsync are Promises. When you call `get()` or `pick()`, you receive a
|
|
14
|
+
* `Promise<T>` that you must await to get the actual value. This allows for seamless integration
|
|
15
|
+
* with async/await patterns and asynchronous data sources.
|
|
16
|
+
*
|
|
17
|
+
* **Difference from FlowConstantAsync and FlowDerivationAsync:**
|
|
18
|
+
* - {@link FlowConstantAsync}: Specialized type for immutable constants that compute once and never change
|
|
19
|
+
* - {@link FlowDerivationAsync}: Specialized type for computed values that automatically recompute when dependencies change
|
|
20
|
+
* - `FlowReadonlyAsync`: Generic utility type that can be used with any `FlowNodeAsync` to create a read-only view
|
|
21
|
+
*
|
|
22
|
+
* **Use Cases:**
|
|
23
|
+
* - **Type Safety**: Ensure functions don't accidentally mutate state by accepting `FlowReadonlyAsync` instead of `FlowStateAsync`
|
|
24
|
+
* - **API Design**: Expose read-only views of internal state to prevent external mutation
|
|
25
|
+
* - **Function Parameters**: Accept any reactive node (state, derivation, constant) but prevent mutation
|
|
26
|
+
* - **Composition**: Create read-only wrappers around mutable states for safer sharing
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* // Type safety: Function that only reads state
|
|
31
|
+
* function displayCount(count: FlowReadonlyAsync<number>) {
|
|
32
|
+
* // count.set(Promise.resolve(100)); // TypeScript error: Property 'set' does not exist
|
|
33
|
+
* effect(async (t) => {
|
|
34
|
+
* const value = await count.get(t); // OK: Reading is allowed
|
|
35
|
+
* console.log(value);
|
|
36
|
+
* });
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* const $count = stateAsync(Promise.resolve(0));
|
|
40
|
+
* displayCount($count); // OK: FlowStateAsync is assignable to FlowReadonlyAsync
|
|
41
|
+
*
|
|
42
|
+
* // API design: Expose read-only view
|
|
43
|
+
* class Counter {
|
|
44
|
+
* private _count = stateAsync(Promise.resolve(0));
|
|
45
|
+
*
|
|
46
|
+
* get count(): FlowReadonlyAsync<number> {
|
|
47
|
+
* return this._count; // FlowStateAsync is assignable to FlowReadonlyAsync
|
|
48
|
+
* }
|
|
49
|
+
*
|
|
50
|
+
* async increment() {
|
|
51
|
+
* await this._count.set(async (current) => Promise.resolve(current + 1)); // Internal mutation allowed
|
|
52
|
+
* }
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* const counter = new Counter();
|
|
56
|
+
* // counter.count.set(Promise.resolve(100)); // TypeScript error: Cannot mutate read-only view
|
|
57
|
+
* const current = await counter.count.pick(); // OK: Reading is allowed
|
|
58
|
+
*
|
|
59
|
+
* // Works with derivations too
|
|
60
|
+
* const $firstName = stateAsync(Promise.resolve('John'));
|
|
61
|
+
* const $lastName = stateAsync(Promise.resolve('Doe'));
|
|
62
|
+
* const $fullName = derivationAsync(async (t) => {
|
|
63
|
+
* const first = await $firstName.get(t);
|
|
64
|
+
* const last = await $lastName.get(t);
|
|
65
|
+
* return `${first} ${last}`;
|
|
66
|
+
* });
|
|
67
|
+
*
|
|
68
|
+
* function displayName(name: FlowReadonlyAsync<string>) {
|
|
69
|
+
* effect(async (t) => {
|
|
70
|
+
* const fullName = await name.get(t);
|
|
71
|
+
* console.log(fullName);
|
|
72
|
+
* });
|
|
73
|
+
* }
|
|
74
|
+
*
|
|
75
|
+
* displayName($fullName); // OK: FlowDerivationAsync is assignable to FlowReadonlyAsync
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
export type FlowReadonlyAsync<T> = Omit<FlowNodeAsync<T>, "set" | "refresh">;
|
|
81
|
+
//# sourceMappingURL=flowReadonlyAsync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flowReadonlyAsync.d.ts","sourceRoot":"","sources":["../../../../../src/flow/nodes/async/flowReadonlyAsync.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6EG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC,CAAC"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { FlowNodeAsync } from './flowNodeAsync';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a reactive state that holds a mutable value, with asynchronous values.
|
|
4
|
+
*
|
|
5
|
+
* @typeParam T - The type of the state value (not the Promise itself).
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* `FlowStateAsync` is a type alias of {@link FlowNodeAsync}, providing a mutable reactive state that can be
|
|
9
|
+
* updated using the `set()` method. It provides all the reactive capabilities of `FlowNodeAsync`
|
|
10
|
+
* (lazy computation, caching) with the ability to mutate the value.
|
|
11
|
+
*
|
|
12
|
+
* Unlike {@link FlowConstantAsync}, which is immutable and computes once, states can be updated at any
|
|
13
|
+
* time. Unlike {@link FlowDerivationAsync}, which recomputes when dependencies change, states are
|
|
14
|
+
* updated explicitly via `set()`.
|
|
15
|
+
*
|
|
16
|
+
* **Asynchronous Nature:**
|
|
17
|
+
* All values in FlowStateAsync are Promises. When you call `get()` or `pick()`, you receive a
|
|
18
|
+
* `Promise<T>` that you must await to get the actual value. The `set()` method returns `Promise<void>`
|
|
19
|
+
* and must be awaited. This allows for seamless integration with async/await patterns and asynchronous
|
|
20
|
+
* data sources.
|
|
21
|
+
*
|
|
22
|
+
* **Reading Values:**
|
|
23
|
+
* You can read the state value using `get(t)` for tracked access (creates reactive dependency)
|
|
24
|
+
* or `pick()` for untracked access (reads without creating dependency). Both methods return `Promise<T>`
|
|
25
|
+
* that must be awaited.
|
|
26
|
+
*
|
|
27
|
+
* **Updating Values:**
|
|
28
|
+
* When the state is updated with a new value that differs from the current value (strict equality),
|
|
29
|
+
* all dependent effects and derivations are automatically notified and re-executed. If the new value
|
|
30
|
+
* is strictly equal to the current value, no notification occurs. The `set()` method accepts either
|
|
31
|
+
* a `Promise<T>` or a function `(current: T) => Promise<T>` and returns `Promise<void>`.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const $count = stateAsync(Promise.resolve(0));
|
|
36
|
+
*
|
|
37
|
+
* // Read with tracking
|
|
38
|
+
* effect(async (t) => {
|
|
39
|
+
* const value = await $count.get(t); // Effect re-runs when $count changes
|
|
40
|
+
* console.log(value);
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // Read without tracking
|
|
44
|
+
* const snapshot = await $count.pick();
|
|
45
|
+
*
|
|
46
|
+
* // Update the value
|
|
47
|
+
* await $count.set(Promise.resolve(1));
|
|
48
|
+
* await $count.set(async (current) => Promise.resolve(current + 1));
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
53
|
+
export type FlowStateAsync<T> = Omit<FlowNodeAsync<T>, "refresh">;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a new reactive state holding a mutable value.
|
|
56
|
+
*
|
|
57
|
+
* @typeParam T - The type of the state value (not the Promise itself).
|
|
58
|
+
*
|
|
59
|
+
* @param value - The initial value for the state, or a function that returns the initial value.
|
|
60
|
+
* If a function is provided, it is called lazily only when the value is first accessed via
|
|
61
|
+
* `get()` or `pick()`. If a direct Promise is provided, it is stored immediately.
|
|
62
|
+
*
|
|
63
|
+
* @returns A new instance of {@link FlowStateAsync} that provides reactive access to the mutable value.
|
|
64
|
+
*
|
|
65
|
+
* @remarks
|
|
66
|
+
* State is the most common reactive primitive - a mutable container that notifies dependents
|
|
67
|
+
* when its value changes. Use `set()` to update the value (returns `Promise<void>`) and `get(t)`
|
|
68
|
+
* or `pick()` to read it (both return `Promise<T>`).
|
|
69
|
+
*
|
|
70
|
+
* **Asynchronous Nature:**
|
|
71
|
+
* All values are Promises. When you access the state via `get(t)` or `pick()`, you receive a
|
|
72
|
+
* `Promise<T>` that you must await to get the actual value. The `set()` method also returns
|
|
73
|
+
* `Promise<void>` and must be awaited. This allows for seamless integration with async/await
|
|
74
|
+
* patterns and asynchronous data sources.
|
|
75
|
+
*
|
|
76
|
+
* **Lazy Initialization:**
|
|
77
|
+
* When a function is provided, the initialization is lazy - the function is not called immediately
|
|
78
|
+
* upon creation. It executes only when:
|
|
79
|
+
* - The value is first read via `get(t)` or `pick()`
|
|
80
|
+
* - The state is watched via `watch()`
|
|
81
|
+
*
|
|
82
|
+
* This is useful for expensive async initializations that may not be needed immediately.
|
|
83
|
+
*
|
|
84
|
+
* **When to Use States:**
|
|
85
|
+
* - Use `stateAsync()` for mutable values that can be updated directly
|
|
86
|
+
* - Use `constantAsync()` for values that compute once and never change
|
|
87
|
+
* - Use `derivationAsync()` for values that recompute when dependencies change
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* // Direct Promise - initialized immediately
|
|
92
|
+
* const $count = stateAsync(Promise.resolve(0));
|
|
93
|
+
*
|
|
94
|
+
* // Lazy initialization - computed on first access
|
|
95
|
+
* const $config = stateAsync(async () => {
|
|
96
|
+
* return await loadConfiguration();
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* effect(async (t) => {
|
|
100
|
+
* const value = await $count.get(t);
|
|
101
|
+
* console.log(value);
|
|
102
|
+
* });
|
|
103
|
+
*
|
|
104
|
+
* await $count.set(Promise.resolve(1)); // Logs: 1
|
|
105
|
+
* await $count.set(async (current) => Promise.resolve(current + 1)); // Logs: 2
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @public
|
|
109
|
+
*/
|
|
110
|
+
export declare function stateAsync<T>(value: Promise<T> | (() => Promise<T>)): FlowStateAsync<T>;
|
|
111
|
+
//# sourceMappingURL=flowStateAsync.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flowStateAsync.d.ts","sourceRoot":"","sources":["../../../../../src/flow/nodes/async/flowStateAsync.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;AAElE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAC3B,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC,GACpC,cAAc,CAAC,CAAC,CAAC,CAEnB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/flow/nodes/async/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/flow/nodes/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { NotPromise } from '../../base';
|
|
2
|
+
import { FlowNode } from './flowNode';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a constant value that can be computed lazily upon first access.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam T - The type of the constant value.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* `FlowConstant` is a type alias based on {@link FlowNode} with the `set()` method removed,
|
|
10
|
+
* making it immutable. It provides all the reactive capabilities of `FlowNode` (
|
|
11
|
+
* lazy computation, caching) but prevents value mutation after initialization.
|
|
12
|
+
*
|
|
13
|
+
* Unlike {@link FlowState}, which is mutable via the `set()` method, a constant's value never
|
|
14
|
+
* changes after it's computed. This makes it ideal for values that should remain stable throughout
|
|
15
|
+
* the application lifecycle.
|
|
16
|
+
*
|
|
17
|
+
* **Lazy Computation:**
|
|
18
|
+
* Constants are created using the `constant()` factory function, which accepts a compute function.
|
|
19
|
+
* The computation doesn't run immediately upon creation. It executes only when:
|
|
20
|
+
* - The value is first read via `get()` or `pick()`
|
|
21
|
+
* - The constant is watched via `watch()`
|
|
22
|
+
* This allows you to defer expensive computations until they're actually needed.
|
|
23
|
+
*
|
|
24
|
+
* **Caching:**
|
|
25
|
+
* Once computed, the value is cached permanently. All subsequent accesses return the cached value
|
|
26
|
+
* without re-computation, ensuring the value remains constant and the computation runs only once.
|
|
27
|
+
*
|
|
28
|
+
* **Use Cases:**
|
|
29
|
+
* - Configuration values that don't change
|
|
30
|
+
* - Expensive computations that should only run once
|
|
31
|
+
* - Initialization values for other reactive primitives
|
|
32
|
+
* - Values derived from external sources that shouldn't be modified
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* // Lazy initialization - computed on first access
|
|
37
|
+
* const $expensiveValue = constant(() => {
|
|
38
|
+
* return performExpensiveCalculation();
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* // First access triggers computation
|
|
42
|
+
* const value1 = await $expensiveValue.pick();
|
|
43
|
+
*
|
|
44
|
+
* // Subsequent accesses return cached value
|
|
45
|
+
* const value2 = await $expensiveValue.pick();
|
|
46
|
+
*
|
|
47
|
+
* // Value cannot be changed (set() method is not available)
|
|
48
|
+
* // $expensiveValue.set(100); // TypeScript error: Property 'set' does not exist
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
53
|
+
export type FlowConstant<T> = Omit<FlowNode<T>, "set" | "refresh">;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a new reactive constant with lazy initialization.
|
|
56
|
+
*
|
|
57
|
+
* @typeParam T - The type of the constant value.
|
|
58
|
+
*
|
|
59
|
+
* @param value - A function that computes the constant value. This function is called lazily
|
|
60
|
+
* only when the value is first accessed via `get()` or `pick()`.
|
|
61
|
+
*
|
|
62
|
+
* @returns A new instance of {@link FlowConstant} that provides reactive access to the computed value.
|
|
63
|
+
*
|
|
64
|
+
* @remarks
|
|
65
|
+
* Constants are immutable reactive values that are computed once and cached permanently. Unlike
|
|
66
|
+
* states (created with `state()`), constants cannot be modified after initialization - they don't
|
|
67
|
+
* have a `set()` method.
|
|
68
|
+
*
|
|
69
|
+
* **Lazy Computation:**
|
|
70
|
+
* The compute function is not executed immediately upon creation. It runs only when:
|
|
71
|
+
* - The value is first read via `get(t)` or `pick()`
|
|
72
|
+
* - The constant is watched via `watch()`
|
|
73
|
+
*
|
|
74
|
+
* This lazy computation is useful for:
|
|
75
|
+
* - Expensive computations that may not be needed immediately
|
|
76
|
+
* - Values that depend on resources not available at construction time
|
|
77
|
+
* - Deferring computation until the value is actually needed
|
|
78
|
+
*
|
|
79
|
+
* **When to Use Constants:**
|
|
80
|
+
* - Use `constant()` for values that should never change after initialization
|
|
81
|
+
* - Use `state()` for mutable values that can be updated
|
|
82
|
+
* - Use `derivation()` for values that recompute when dependencies change
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* // Expensive computation that runs only once
|
|
87
|
+
* const $config = constant(() => {
|
|
88
|
+
* return loadConfigurationFromFile();
|
|
89
|
+
* });
|
|
90
|
+
*
|
|
91
|
+
* // Computation hasn't run yet
|
|
92
|
+
* // First access triggers it
|
|
93
|
+
* const config = await $config.pick();
|
|
94
|
+
*
|
|
95
|
+
* // Subsequent accesses return cached value
|
|
96
|
+
* const config2 = await $config.pick();
|
|
97
|
+
*
|
|
98
|
+
* // Use in reactive context
|
|
99
|
+
* effect((t) => {
|
|
100
|
+
* const cfg = $config.get(t);
|
|
101
|
+
* console.log(`API URL: ${cfg.apiUrl}`);
|
|
102
|
+
* });
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @public
|
|
106
|
+
*/
|
|
107
|
+
export declare function constant<T>(value: () => NotPromise<T>): FlowConstant<T>;
|
|
108
|
+
//# sourceMappingURL=flowConstant.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flowConstant.d.ts","sourceRoot":"","sources":["../../../../../src/flow/nodes/sync/flowConstant.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC,CAAC;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAEvE"}
|