@ersbeth/picoflow 1.0.1 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +1155 -582
- 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 +2 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/solid/converters.d.ts +34 -45
- package/dist/types/solid/converters.d.ts.map +1 -1
- package/dist/types/solid/index.d.ts +2 -2
- package/dist/types/solid/index.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 +2 -47
- package/src/solid/converters.ts +60 -199
- package/src/solid/index.ts +2 -8
- 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
package/src/creators.ts
DELETED
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
FlowMap,
|
|
3
|
-
FlowResource,
|
|
4
|
-
FlowResourceAsync,
|
|
5
|
-
FlowStream,
|
|
6
|
-
FlowStreamAsync,
|
|
7
|
-
} from "./advanced/";
|
|
8
|
-
import { FlowArray } from "./advanced/array";
|
|
9
|
-
import type { TrackingContext } from "./basic/";
|
|
10
|
-
import {
|
|
11
|
-
FlowConstant,
|
|
12
|
-
FlowDerivation,
|
|
13
|
-
FlowEffect,
|
|
14
|
-
FlowSignal,
|
|
15
|
-
FlowState,
|
|
16
|
-
} from "./basic/";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Creates a new reactive signal.
|
|
20
|
-
*
|
|
21
|
-
* @returns A new instance of {@link FlowSignal}.
|
|
22
|
-
*
|
|
23
|
-
* @remarks
|
|
24
|
-
* A signal is the simplest reactive primitive - it doesn't hold a value, but can be triggered
|
|
25
|
-
* to notify dependent effects and derivations. Use signals when you need event-like notifications
|
|
26
|
-
* without associated data.
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* const $refresh = signal();
|
|
31
|
-
*
|
|
32
|
-
* effect((t) => {
|
|
33
|
-
* $refresh.watch(t);
|
|
34
|
-
* console.log('Refreshing...');
|
|
35
|
-
* });
|
|
36
|
-
*
|
|
37
|
-
* $refresh.trigger(); // Logs: "Refreshing..."
|
|
38
|
-
* ```
|
|
39
|
-
*
|
|
40
|
-
* @public
|
|
41
|
-
*/
|
|
42
|
-
export function signal(): FlowSignal {
|
|
43
|
-
return new FlowSignal();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Creates a new reactive constant.
|
|
48
|
-
*
|
|
49
|
-
* @typeParam T - The type of the constant value.
|
|
50
|
-
* @param value - The value or a function that returns the value (for lazy initialization).
|
|
51
|
-
* @returns A new instance of {@link FlowConstant}.
|
|
52
|
-
*
|
|
53
|
-
* @remarks
|
|
54
|
-
* A constant is an immutable reactive value that never changes after initialization.
|
|
55
|
-
* It can be initialized eagerly (direct value) or lazily (function). Use constants for
|
|
56
|
-
* configuration, computed-once values, or expensive initialization that should only run once.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* // Eager initialization
|
|
61
|
-
* const $config = constant({ apiUrl: 'https://api.example.com' });
|
|
62
|
-
*
|
|
63
|
-
* // Lazy initialization
|
|
64
|
-
* const $computed = constant(() => expensiveCalculation());
|
|
65
|
-
* ```
|
|
66
|
-
*
|
|
67
|
-
* @public
|
|
68
|
-
*/
|
|
69
|
-
export function constant<T>(value: T | (() => T)): FlowConstant<T> {
|
|
70
|
-
return new FlowConstant<T>(value);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Creates a new reactive state holding a mutable value.
|
|
75
|
-
*
|
|
76
|
-
* @typeParam T - The type of the state value.
|
|
77
|
-
* @param value - The initial value for the state, or a function returning the initial value.
|
|
78
|
-
* @returns A new instance of {@link FlowState}.
|
|
79
|
-
*
|
|
80
|
-
* @remarks
|
|
81
|
-
* State is the most common reactive primitive - a mutable container that notifies dependents
|
|
82
|
-
* when its value changes. Use `set()` to update the value and `get(t)` or `pick()` to read it.
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```typescript
|
|
86
|
-
* const $count = state(0);
|
|
87
|
-
*
|
|
88
|
-
* effect((t) => {
|
|
89
|
-
* console.log($count.get(t));
|
|
90
|
-
* });
|
|
91
|
-
*
|
|
92
|
-
* $count.set(1); // Logs: 1
|
|
93
|
-
* $count.set(n => n + 1); // Logs: 2
|
|
94
|
-
* ```
|
|
95
|
-
*
|
|
96
|
-
* @public
|
|
97
|
-
*/
|
|
98
|
-
export function state<T>(value: T | (() => T)): FlowState<T> {
|
|
99
|
-
return new FlowState(value);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Creates a new reactive resource that asynchronously fetches its value, returning `T | undefined`.
|
|
104
|
-
*
|
|
105
|
-
* @typeParam T - The type of the resource value.
|
|
106
|
-
* @param fn - An asynchronous function that fetches the resource value.
|
|
107
|
-
* @returns A new instance of {@link FlowResource}.
|
|
108
|
-
*
|
|
109
|
-
* @remarks
|
|
110
|
-
* A resource manages async data fetching with reactive updates. Unlike {@link resourceAsync},
|
|
111
|
-
* this returns the resolved value directly (or `undefined` if not fetched yet), making it
|
|
112
|
-
* easier to work with in synchronous contexts. Call `fetch()` to trigger the async operation.
|
|
113
|
-
*
|
|
114
|
-
* @example
|
|
115
|
-
* ```typescript
|
|
116
|
-
* const $user = resource(() => fetch('/api/user').then(r => r.json()));
|
|
117
|
-
*
|
|
118
|
-
* // Trigger fetch
|
|
119
|
-
* await $user.fetch();
|
|
120
|
-
*
|
|
121
|
-
* // Use in effect
|
|
122
|
-
* effect((t) => {
|
|
123
|
-
* const user = $user.get(t);
|
|
124
|
-
* if (user) {
|
|
125
|
-
* console.log(user.name);
|
|
126
|
-
* }
|
|
127
|
-
* });
|
|
128
|
-
* ```
|
|
129
|
-
*
|
|
130
|
-
* @public
|
|
131
|
-
*/
|
|
132
|
-
export function resource<T>(fn: () => Promise<T>): FlowResource<T> {
|
|
133
|
-
return new FlowResource(fn);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Creates a new reactive asynchronous resource that always returns a Promise.
|
|
138
|
-
*
|
|
139
|
-
* @typeParam T - The type of the resource value.
|
|
140
|
-
* @param fn - An asynchronous function that fetches the resource value.
|
|
141
|
-
* @returns A new instance of {@link FlowResourceAsync}.
|
|
142
|
-
*
|
|
143
|
-
* @remarks
|
|
144
|
-
* An async resource manages async data fetching and always returns a Promise, making it
|
|
145
|
-
* ideal for async/await patterns. Unlike {@link resource}, the Promise is created lazily
|
|
146
|
-
* on first access and cached. Call `fetch()` to create a new Promise and trigger a refetch.
|
|
147
|
-
*
|
|
148
|
-
* @example
|
|
149
|
-
* ```typescript
|
|
150
|
-
* const $data = resourceAsync(() => fetch('/api/data').then(r => r.json()));
|
|
151
|
-
*
|
|
152
|
-
* // Use with async/await
|
|
153
|
-
* effect(async (t) => {
|
|
154
|
-
* const data = await $data.get(t);
|
|
155
|
-
* console.log(data);
|
|
156
|
-
* });
|
|
157
|
-
*
|
|
158
|
-
* // Refetch
|
|
159
|
-
* await $data.fetch();
|
|
160
|
-
* ```
|
|
161
|
-
*
|
|
162
|
-
* @public
|
|
163
|
-
*/
|
|
164
|
-
export function resourceAsync<T>(fn: () => Promise<T>): FlowResourceAsync<T> {
|
|
165
|
-
return new FlowResourceAsync(fn);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Creates a new reactive stream that bridges external event sources with PicoFlow's reactive system.
|
|
170
|
-
*
|
|
171
|
-
* @typeParam T - The type of the stream value.
|
|
172
|
-
* @param updater - A function that receives a setter to update the stream's value.
|
|
173
|
-
* It should return a disposer function to clean up resources.
|
|
174
|
-
* @returns A new instance of {@link FlowStream}.
|
|
175
|
-
*
|
|
176
|
-
* @remarks
|
|
177
|
-
* Streams are ideal for integrating push-based data sources like WebSockets, DOM events,
|
|
178
|
-
* timers, or any event emitter. The updater sets up subscriptions and calls the setter
|
|
179
|
-
* when new data arrives. The returned disposer is called on cleanup.
|
|
180
|
-
*
|
|
181
|
-
* @example
|
|
182
|
-
* ```typescript
|
|
183
|
-
* // WebSocket stream
|
|
184
|
-
* const $messages = stream<string>((set) => {
|
|
185
|
-
* const ws = new WebSocket('ws://example.com');
|
|
186
|
-
* ws.onmessage = (e) => set(e.data);
|
|
187
|
-
* return () => ws.close();
|
|
188
|
-
* });
|
|
189
|
-
*
|
|
190
|
-
* // Timer stream
|
|
191
|
-
* const $tick = stream<number>((set) => {
|
|
192
|
-
* let count = 0;
|
|
193
|
-
* const id = setInterval(() => set(count++), 1000);
|
|
194
|
-
* return () => clearInterval(id);
|
|
195
|
-
* });
|
|
196
|
-
* ```
|
|
197
|
-
*
|
|
198
|
-
* @public
|
|
199
|
-
*/
|
|
200
|
-
export function stream<T>(
|
|
201
|
-
updater: (set: (value: T) => void) => () => void,
|
|
202
|
-
): FlowStream<T> {
|
|
203
|
-
return new FlowStream(updater);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Creates a new reactive asynchronous stream that always returns a Promise.
|
|
208
|
-
*
|
|
209
|
-
* @typeParam T - The type of the stream value.
|
|
210
|
-
* @param updater - A function that receives a setter to update the stream's value.
|
|
211
|
-
* It should return a disposer function to clean up resources.
|
|
212
|
-
* @returns A new instance of {@link FlowStreamAsync}.
|
|
213
|
-
*
|
|
214
|
-
* @remarks
|
|
215
|
-
* Async streams are ideal for push-based async data sources where you want to use async/await.
|
|
216
|
-
* Unlike {@link stream}, this always returns a Promise that resolves to the value. The initial
|
|
217
|
-
* Promise is created on construction and resolves when the setter is first called.
|
|
218
|
-
*
|
|
219
|
-
* @example
|
|
220
|
-
* ```typescript
|
|
221
|
-
* const $asyncMessages = streamAsync<string>((set) => {
|
|
222
|
-
* const ws = new WebSocket('ws://example.com');
|
|
223
|
-
* ws.onmessage = (e) => set(e.data);
|
|
224
|
-
* return () => ws.close();
|
|
225
|
-
* });
|
|
226
|
-
*
|
|
227
|
-
* effect(async (t) => {
|
|
228
|
-
* const message = await $asyncMessages.get(t);
|
|
229
|
-
* console.log('Received:', message);
|
|
230
|
-
* });
|
|
231
|
-
* ```
|
|
232
|
-
*
|
|
233
|
-
* @public
|
|
234
|
-
*/
|
|
235
|
-
export function streamAsync<T>(
|
|
236
|
-
updater: (set: (value: T) => void) => () => void,
|
|
237
|
-
): FlowStreamAsync<T> {
|
|
238
|
-
return new FlowStreamAsync(updater);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Creates a new reactive derivation whose value is computed based on other reactive signals.
|
|
243
|
-
*
|
|
244
|
-
* @typeParam T - The type of the derived value.
|
|
245
|
-
* @param fn - A function that computes the derived value using a tracking context.
|
|
246
|
-
* @returns A new instance of {@link FlowDerivation}.
|
|
247
|
-
*
|
|
248
|
-
* @remarks
|
|
249
|
-
* A derivation is a computed reactive value that automatically tracks its dependencies and
|
|
250
|
-
* recomputes when they change. The computation is lazy - it runs only when the value is
|
|
251
|
-
* accessed, not on construction. Use derivations to create derived state without manual
|
|
252
|
-
* dependency management.
|
|
253
|
-
*
|
|
254
|
-
* @example
|
|
255
|
-
* ```typescript
|
|
256
|
-
* const $firstName = state('John');
|
|
257
|
-
* const $lastName = state('Doe');
|
|
258
|
-
*
|
|
259
|
-
* const $fullName = derivation((t) => {
|
|
260
|
-
* return `${$firstName.get(t)} ${$lastName.get(t)}`;
|
|
261
|
-
* });
|
|
262
|
-
*
|
|
263
|
-
* effect((t) => {
|
|
264
|
-
* console.log($fullName.get(t)); // Logs: "John Doe"
|
|
265
|
-
* });
|
|
266
|
-
*
|
|
267
|
-
* $firstName.set('Jane'); // Logs: "Jane Doe"
|
|
268
|
-
* ```
|
|
269
|
-
*
|
|
270
|
-
* @public
|
|
271
|
-
*/
|
|
272
|
-
export function derivation<T>(
|
|
273
|
-
fn: (t: TrackingContext) => T,
|
|
274
|
-
): FlowDerivation<T> {
|
|
275
|
-
return new FlowDerivation(fn);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Creates a new reactive effect that executes a side-effect function based on its dependencies.
|
|
280
|
-
*
|
|
281
|
-
* @param fn - A function that performs side effects using a tracking context.
|
|
282
|
-
* @returns A new instance of {@link FlowEffect}.
|
|
283
|
-
*
|
|
284
|
-
* @remarks
|
|
285
|
-
* An effect is a reactive computation that runs immediately and re-runs whenever its tracked
|
|
286
|
-
* dependencies change. Use effects for side effects like logging, DOM updates, network requests,
|
|
287
|
-
* or any operation that should respond to reactive state changes.
|
|
288
|
-
*
|
|
289
|
-
* The effect executes immediately upon creation and provides a tracking context (`t`) that you
|
|
290
|
-
* use to explicitly mark dependencies via `observable.get(t)`. Use `observable.pick()` for
|
|
291
|
-
* reads that shouldn't trigger re-runs.
|
|
292
|
-
*
|
|
293
|
-
* @example
|
|
294
|
-
* ```typescript
|
|
295
|
-
* const $count = state(0);
|
|
296
|
-
*
|
|
297
|
-
* const fx = effect((t) => {
|
|
298
|
-
* console.log('Count is:', $count.get(t));
|
|
299
|
-
* });
|
|
300
|
-
*
|
|
301
|
-
* $count.set(1); // Logs: "Count is: 1"
|
|
302
|
-
* $count.set(2); // Logs: "Count is: 2"
|
|
303
|
-
*
|
|
304
|
-
* // Clean up when done
|
|
305
|
-
* fx.dispose();
|
|
306
|
-
* ```
|
|
307
|
-
*
|
|
308
|
-
* @public
|
|
309
|
-
*/
|
|
310
|
-
export function effect(fn: (t: TrackingContext) => void): FlowEffect {
|
|
311
|
-
return new FlowEffect(fn);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* Creates a new reactive map state with fine-grained tracking of operations.
|
|
316
|
-
*
|
|
317
|
-
* @typeParam K - The type of the keys.
|
|
318
|
-
* @typeParam V - The type of the values.
|
|
319
|
-
* @param initial - An optional record of key-value pairs to initialize the map.
|
|
320
|
-
* @returns A new instance of {@link FlowMap}.
|
|
321
|
-
*
|
|
322
|
-
* @remarks
|
|
323
|
-
* A reactive map wraps a native JavaScript Map and provides multiple levels of reactivity:
|
|
324
|
-
* tracking the entire map, tracking individual set operations, and tracking individual
|
|
325
|
-
* delete operations. The initial record (if provided) is converted to a native Map.
|
|
326
|
-
*
|
|
327
|
-
* @example
|
|
328
|
-
* ```typescript
|
|
329
|
-
* const $users = map<string, User>({
|
|
330
|
-
* 'user1': { name: 'John', age: 30 }
|
|
331
|
-
* });
|
|
332
|
-
*
|
|
333
|
-
* // Track the whole map
|
|
334
|
-
* effect((t) => {
|
|
335
|
-
* console.log('Users:', $users.get(t).size);
|
|
336
|
-
* });
|
|
337
|
-
*
|
|
338
|
-
* // Track additions
|
|
339
|
-
* effect((t) => {
|
|
340
|
-
* const { key, value } = $users.$lastSet.get(t);
|
|
341
|
-
* if (key) console.log(`Added: ${key}`);
|
|
342
|
-
* });
|
|
343
|
-
*
|
|
344
|
-
* $users.setAt('user2', { name: 'Jane', age: 25 });
|
|
345
|
-
* ```
|
|
346
|
-
*
|
|
347
|
-
* @public
|
|
348
|
-
*/
|
|
349
|
-
export function map<K extends string | number | symbol, V>(
|
|
350
|
-
initial?: Record<K, V>,
|
|
351
|
-
): FlowMap<K, V> {
|
|
352
|
-
return new FlowMap<K, V>(
|
|
353
|
-
new Map<K, V>(initial ? (Object.entries(initial) as [K, V][]) : []),
|
|
354
|
-
);
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* Creates a new reactive array with mutation methods and fine-grained action tracking.
|
|
359
|
-
*
|
|
360
|
-
* @typeParam T - The type of the array elements.
|
|
361
|
-
* @param initial - An optional array of initial values.
|
|
362
|
-
* @returns A new instance of {@link FlowArray}.
|
|
363
|
-
*
|
|
364
|
-
* @remarks
|
|
365
|
-
* A reactive array provides array-like mutation methods (push, pop, shift, unshift, splice)
|
|
366
|
-
* and tracks the last operation performed via `$lastAction`. This enables both whole-array
|
|
367
|
-
* reactivity and fine-grained tracking of specific mutations.
|
|
368
|
-
*
|
|
369
|
-
* The array automatically disposes disposable items when they are removed (if they implement
|
|
370
|
-
* the FlowDisposable interface).
|
|
371
|
-
*
|
|
372
|
-
* @example
|
|
373
|
-
* ```typescript
|
|
374
|
-
* const $items = array([1, 2, 3]);
|
|
375
|
-
*
|
|
376
|
-
* // Track the whole array
|
|
377
|
-
* effect((t) => {
|
|
378
|
-
* console.log('Items:', $items.get(t));
|
|
379
|
-
* });
|
|
380
|
-
*
|
|
381
|
-
* // Track the last action
|
|
382
|
-
* effect((t) => {
|
|
383
|
-
* const action = $items.$lastAction.get(t);
|
|
384
|
-
* console.log('Action:', action.type);
|
|
385
|
-
* });
|
|
386
|
-
*
|
|
387
|
-
* $items.push(4); // Logs action: "push"
|
|
388
|
-
* $items.pop(); // Logs action: "pop"
|
|
389
|
-
* ```
|
|
390
|
-
*
|
|
391
|
-
* @public
|
|
392
|
-
*/
|
|
393
|
-
export function array<T>(initial?: T[]): FlowArray<T> {
|
|
394
|
-
return new FlowArray<T>(initial);
|
|
395
|
-
}
|