@ersbeth/picoflow 0.0.1 → 0.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/api/doc/index.md +1 -1
- package/api/doc/picoflow.constant.md +55 -0
- package/api/doc/picoflow.derivation.md +1 -1
- package/api/doc/picoflow.effect.md +1 -1
- package/api/doc/picoflow.flowconstant._constructor_.md +49 -0
- package/api/doc/picoflow.flowconstant.get.md +25 -0
- package/api/doc/picoflow.flowconstant.md +88 -0
- package/api/doc/picoflow.flowderivation._constructor_.md +2 -2
- package/api/doc/picoflow.flowderivation.get.md +2 -2
- package/api/doc/picoflow.flowderivation.md +2 -2
- package/api/doc/picoflow.floweffect._constructor_.md +7 -2
- package/api/doc/picoflow.floweffect.dispose.md +3 -3
- package/api/doc/picoflow.floweffect.disposed.md +1 -1
- package/api/doc/picoflow.floweffect.md +4 -4
- package/api/doc/picoflow.flowgetter.md +2 -2
- package/api/doc/picoflow.flowmap._lastdeleted.md +1 -1
- package/api/doc/picoflow.flowmap._lastset.md +1 -1
- package/api/doc/picoflow.flowmap.delete.md +6 -2
- package/api/doc/picoflow.flowmap.md +5 -7
- package/api/doc/picoflow.flowmap.setat.md +6 -2
- package/api/doc/picoflow.flowobservable.get.md +3 -3
- package/api/doc/picoflow.flowobservable.md +18 -4
- package/api/doc/picoflow.flowobservable.subscribe.md +55 -0
- package/api/doc/picoflow.flowresource._constructor_.md +2 -18
- package/api/doc/picoflow.flowresource.fetch.md +1 -1
- package/api/doc/picoflow.flowresource.get.md +4 -4
- package/api/doc/picoflow.flowresource.md +4 -4
- package/api/doc/picoflow.flowresourceasync._constructor_.md +49 -0
- package/api/doc/picoflow.flowresourceasync.fetch.md +27 -0
- package/api/doc/picoflow.flowresourceasync.get.md +23 -0
- package/api/doc/picoflow.flowresourceasync.md +100 -0
- package/api/doc/picoflow.flowsignal.dispose.md +3 -7
- package/api/doc/picoflow.flowsignal.disposed.md +2 -2
- package/api/doc/picoflow.flowsignal.md +5 -5
- package/api/doc/picoflow.flowsignal.trigger.md +3 -7
- package/api/doc/picoflow.flowstate.md +4 -52
- package/api/doc/picoflow.flowstate.set.md +5 -5
- package/api/doc/picoflow.flowstream._constructor_.md +3 -19
- package/api/doc/picoflow.flowstream.dispose.md +1 -1
- package/api/doc/picoflow.flowstream.get.md +4 -4
- package/api/doc/picoflow.flowstream.md +5 -5
- package/api/doc/picoflow.flowstreamasync._constructor_.md +54 -0
- package/api/doc/picoflow.flowstreamasync.dispose.md +21 -0
- package/api/doc/picoflow.flowstreamasync.get.md +23 -0
- package/api/doc/picoflow.flowstreamasync.md +100 -0
- package/api/doc/picoflow.flowstreamdisposer.md +13 -0
- package/api/doc/picoflow.flowstreamsetter.md +13 -0
- package/api/doc/picoflow.flowstreamupdater.md +19 -0
- package/api/doc/picoflow.flowwatcher.md +1 -1
- package/api/doc/picoflow.map.md +1 -1
- package/api/doc/picoflow.md +80 -14
- package/api/doc/picoflow.resource.md +2 -18
- package/api/doc/picoflow.resourceasync.md +55 -0
- package/api/doc/picoflow.signal.md +1 -1
- package/api/doc/picoflow.state.md +3 -3
- package/api/doc/picoflow.stream.md +2 -18
- package/api/doc/picoflow.streamasync.md +55 -0
- package/api/picoflow.public.api.md +131 -0
- package/api-extractor.json +2 -1
- package/dist/picoflow.js +326 -302
- package/dist/types/advanced/index.d.ts +7 -0
- package/dist/types/advanced/index.d.ts.map +1 -0
- package/dist/types/{map.d.ts → advanced/map.d.ts} +12 -12
- package/dist/types/advanced/map.d.ts.map +1 -0
- package/dist/types/advanced/resource.d.ts +39 -0
- package/dist/types/advanced/resource.d.ts.map +1 -0
- package/dist/types/{resource.d.ts → advanced/resourceAsync.d.ts} +6 -11
- package/dist/types/advanced/resourceAsync.d.ts.map +1 -0
- package/dist/types/advanced/stream.d.ts +59 -0
- package/dist/types/advanced/stream.d.ts.map +1 -0
- package/dist/types/advanced/streamAsync.d.ts +43 -0
- package/dist/types/advanced/streamAsync.d.ts.map +1 -0
- package/dist/types/basic/constant.d.ts +32 -0
- package/dist/types/basic/constant.d.ts.map +1 -0
- package/dist/types/basic/derivation.d.ts +40 -0
- package/dist/types/basic/derivation.d.ts.map +1 -0
- package/dist/types/basic/effect.d.ts +56 -0
- package/dist/types/basic/effect.d.ts.map +1 -0
- package/dist/types/basic/index.d.ts +9 -0
- package/dist/types/basic/index.d.ts.map +1 -0
- package/dist/types/basic/observable.d.ts +34 -0
- package/dist/types/basic/observable.d.ts.map +1 -0
- package/dist/types/basic/signal.d.ts +37 -0
- package/dist/types/basic/signal.d.ts.map +1 -0
- package/dist/types/basic/state.d.ts +26 -0
- package/dist/types/basic/state.d.ts.map +1 -0
- package/dist/types/creators.d.ts +29 -13
- package/dist/types/creators.d.ts.map +1 -1
- package/dist/types/index.d.ts +3 -9
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/advanced/index.ts +10 -0
- package/src/{map.ts → advanced/map.ts} +14 -14
- package/src/advanced/resource.ts +56 -0
- package/src/{resource.ts → advanced/resourceAsync.ts} +9 -16
- package/src/advanced/stream.ts +87 -0
- package/src/advanced/streamAsync.ts +82 -0
- package/src/basic/constant.ts +64 -0
- package/src/basic/derivation.ts +86 -0
- package/src/basic/effect.ts +96 -0
- package/src/basic/index.ts +8 -0
- package/src/basic/observable.ts +51 -0
- package/src/basic/signal.ts +105 -0
- package/src/basic/state.ts +39 -0
- package/src/creators.ts +54 -15
- package/src/index.ts +21 -11
- package/test/constant.test.ts +46 -0
- package/test/derivation.test.ts +30 -6
- package/test/effect.test.ts +29 -0
- package/test/map.test.ts +38 -0
- package/test/resource.test.ts +18 -16
- package/test/resourceAsync.test.ts +108 -0
- package/test/signal.test.ts +18 -1
- package/test/state.test.ts +107 -2
- package/test/stream.test.ts +38 -13
- package/test/streamAsync.test.ts +194 -0
- package/tsconfig.json +3 -1
- package/api/doc/picoflow.flowdisposer.md +0 -13
- package/api/doc/picoflow.flowsetter.md +0 -13
- package/api/doc/picoflow.flowstate._constructor_.md +0 -49
- package/api/doc/picoflow.flowstate.get.md +0 -23
- package/api/doc/picoflow.flowupdater.md +0 -19
- package/api/picoflow.api.md +0 -145
- package/dist/types/derivation.d.ts +0 -58
- package/dist/types/derivation.d.ts.map +0 -1
- package/dist/types/effect.d.ts +0 -108
- package/dist/types/effect.d.ts.map +0 -1
- package/dist/types/map.d.ts.map +0 -1
- package/dist/types/observable.d.ts +0 -40
- package/dist/types/observable.d.ts.map +0 -1
- package/dist/types/resource.d.ts.map +0 -1
- package/dist/types/signal.d.ts +0 -111
- package/dist/types/signal.d.ts.map +0 -1
- package/dist/types/state.d.ts +0 -39
- package/dist/types/state.d.ts.map +0 -1
- package/dist/types/stream.d.ts +0 -71
- package/dist/types/stream.d.ts.map +0 -1
- package/src/derivation.ts +0 -96
- package/src/effect.ts +0 -152
- package/src/observable.ts +0 -50
- package/src/signal.ts +0 -166
- package/src/state.ts +0 -52
- package/src/stream.ts +0 -99
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACH,MAAM,EACN,KAAK,EACL,QAAQ,EACR,QAAQ,EACR,MAAM,EACN,UAAU,EACV,MAAM,EACN,GAAG,EACH,WAAW,EACX,aAAa,GAChB,MAAM,YAAY,CAAC;AACpB,YAAY,EACR,cAAc,EACd,UAAU,EACV,UAAU,EACV,cAAc,EACd,UAAU,EACV,WAAW,EACX,SAAS,EACT,YAAY,GACf,MAAM,UAAU,CAAC;AAClB,YAAY,EACR,YAAY,EACZ,OAAO,EACP,iBAAiB,EACjB,eAAe,EACf,UAAU,EACV,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,GACpB,MAAM,aAAa,CAAC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { FlowMap } from "./map";
|
|
2
|
+
export { FlowStream } from "./stream";
|
|
3
|
+
export { FlowStreamAsync } from "./streamAsync";
|
|
4
|
+
export { FlowResource } from "./resource";
|
|
5
|
+
export { FlowResourceAsync } from "./resourceAsync";
|
|
6
|
+
export type {
|
|
7
|
+
FlowStreamUpdater,
|
|
8
|
+
FlowStreamDisposer,
|
|
9
|
+
FlowStreamSetter,
|
|
10
|
+
} from "./stream";
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { FlowState } from "
|
|
1
|
+
import { FlowState } from "../basic/";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Represents a reactive map of states
|
|
4
|
+
* Represents a reactive map of states extending {@link FlowState} for a Map of key/value pairs.
|
|
5
5
|
*
|
|
6
6
|
* @remarks
|
|
7
7
|
* FlowMap wraps a native Map and provides reactive signals for fine-grained tracking
|
|
8
8
|
* of updates to the map. In addition to the reactive capabilities inherited from FlowState,
|
|
9
|
-
* it exposes two
|
|
9
|
+
* it exposes two reactive signals:
|
|
10
10
|
*
|
|
11
|
-
*
|
|
11
|
+
* • $lastSet: Holds the most recent key-value pair that was set.
|
|
12
|
+
* • $lastDeleted: Holds the most recent key-value pair that was deleted.
|
|
12
13
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* Use {@link FlowMap.setAt} to set a key-value pair and {@link FlowMap.delete} to remove a key.
|
|
14
|
+
* Use {@link FlowMap.setAt} to add or update a key-value pair and {@link FlowMap.delete} to remove a key.
|
|
16
15
|
*
|
|
17
16
|
* @typeparam K - The type of the map keys.
|
|
18
17
|
* @typeparam V - The type of the map values.
|
|
@@ -47,15 +46,16 @@ export class FlowMap<K, V> extends FlowState<Map<K, V>> {
|
|
|
47
46
|
*
|
|
48
47
|
* @param key - The key at which to set the value.
|
|
49
48
|
* @param value - The value to set.
|
|
49
|
+
* @throws If the FlowMap instance is disposed.
|
|
50
50
|
*
|
|
51
51
|
* @remarks
|
|
52
|
-
*
|
|
53
|
-
*
|
|
52
|
+
* Updates the internal map, emits the key-value pair via {@link FlowMap.$lastSet},
|
|
53
|
+
* and notifies all subscribers of the change.
|
|
54
54
|
*
|
|
55
55
|
* @public
|
|
56
56
|
*/
|
|
57
57
|
public setAt(key: K, value: V): void {
|
|
58
|
-
if (this._disposed) throw new Error("
|
|
58
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
59
59
|
this._value.set(key, value);
|
|
60
60
|
this.$lastSet.set({ key, value });
|
|
61
61
|
this._notify();
|
|
@@ -65,16 +65,16 @@ export class FlowMap<K, V> extends FlowState<Map<K, V>> {
|
|
|
65
65
|
* Deletes the value at the specified key from the underlying map.
|
|
66
66
|
*
|
|
67
67
|
* @param key - The key to delete.
|
|
68
|
+
* @throws If the FlowMap instance is disposed.
|
|
68
69
|
*
|
|
69
70
|
* @remarks
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* of the change.
|
|
71
|
+
* Removes the key from the internal map, emits the deleted key and its value via {@link FlowMap.$lastDeleted},
|
|
72
|
+
* and notifies all subscribers of the change.
|
|
73
73
|
*
|
|
74
74
|
* @public
|
|
75
75
|
*/
|
|
76
76
|
public delete(key: K): void {
|
|
77
|
-
if (this._disposed) throw new Error("
|
|
77
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
78
78
|
const value = this._value.get(key);
|
|
79
79
|
this._value.delete(key);
|
|
80
80
|
this.$lastDeleted.set({ key, value });
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { FlowObservable } from "../basic";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents a reactive resource that asynchronously fetches its value.
|
|
5
|
+
*
|
|
6
|
+
* @remarks A FlowResource extends FlowObservable and encapsulates an asynchronous fetch function.
|
|
7
|
+
* It is used to retrieve and update its value asynchronously. When the fetch is executed,
|
|
8
|
+
* if the new value differs from the current value, the resource is updated and its subscribers
|
|
9
|
+
* are notified.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export class FlowResource<T> extends FlowObservable<T | undefined> {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new FlowResource.
|
|
16
|
+
* @param fetch - An asynchronous function that retrieves the resource's value.
|
|
17
|
+
*
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
constructor(fetch: () => Promise<T>) {
|
|
21
|
+
super();
|
|
22
|
+
this._fetch = fetch;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Retrieves the current resource value.
|
|
27
|
+
* @returns The current value, or undefined if the resource has not been fetched yet.
|
|
28
|
+
* @throws Error if the resource is disposed.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
public get(): T | undefined {
|
|
32
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
33
|
+
return this._value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Asynchronously fetches a new value for the resource.
|
|
38
|
+
* @remarks
|
|
39
|
+
* Executes the internal fetch function. If the fetched value differs from the current one,
|
|
40
|
+
* updates the resource's value and notifies subscribers.
|
|
41
|
+
* @returns A Promise that resolves when the fetch operation is complete.
|
|
42
|
+
* @throws Error if the resource is disposed.
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
public async fetch(): Promise<void> {
|
|
46
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
47
|
+
const value = await this._fetch();
|
|
48
|
+
if (value === this._value) return;
|
|
49
|
+
this._value = value;
|
|
50
|
+
this._notify();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* INTERNAL ------------------------------------------------ */
|
|
54
|
+
|
|
55
|
+
private _fetch: () => Promise<T>;
|
|
56
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { FlowObservable } from "
|
|
1
|
+
import { FlowObservable } from "../basic/";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Represents a reactive resource that asynchronously fetches its value.
|
|
@@ -13,27 +13,26 @@ import { FlowObservable } from "./observable";
|
|
|
13
13
|
*
|
|
14
14
|
* @public
|
|
15
15
|
*/
|
|
16
|
-
export class
|
|
16
|
+
export class FlowResourceAsync<T> extends FlowObservable<Promise<T>> {
|
|
17
17
|
/**
|
|
18
18
|
* Creates a new FlowResource.
|
|
19
19
|
* @param fetch - An asynchronous function that retrieves the resource's value.
|
|
20
|
-
* @param initial - The initial value of the resource.
|
|
21
20
|
* @public
|
|
22
21
|
*/
|
|
23
|
-
constructor(fetch: () => Promise<T
|
|
22
|
+
constructor(fetch: () => Promise<T>) {
|
|
24
23
|
super();
|
|
25
|
-
this._value = initial;
|
|
26
24
|
this._fetch = fetch;
|
|
27
25
|
}
|
|
28
26
|
|
|
29
27
|
/**
|
|
30
28
|
* Retrieves the current resource value.
|
|
31
|
-
* @returns The current value.
|
|
29
|
+
* @returns The current value, or undefined if the resource has not been fetched yet.
|
|
32
30
|
* @throws Error if the resource is disposed.
|
|
33
31
|
* @public
|
|
34
32
|
*/
|
|
35
|
-
public get(): T {
|
|
36
|
-
if (this._disposed) throw new Error("
|
|
33
|
+
public get(): Promise<T> {
|
|
34
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
35
|
+
if (!this._value) this._value = this._fetch();
|
|
37
36
|
return this._value;
|
|
38
37
|
}
|
|
39
38
|
|
|
@@ -47,18 +46,12 @@ export class FlowResource<T> extends FlowObservable<T> {
|
|
|
47
46
|
* @public
|
|
48
47
|
*/
|
|
49
48
|
public async fetch(): Promise<void> {
|
|
50
|
-
if (this._disposed) throw new Error("
|
|
51
|
-
|
|
52
|
-
if (value === this._value) return;
|
|
53
|
-
this._value = value;
|
|
49
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
50
|
+
this._value = this._fetch();
|
|
54
51
|
this._notify();
|
|
55
52
|
}
|
|
56
53
|
|
|
57
54
|
/* INTERNAL ------------------------------------------------ */
|
|
58
55
|
|
|
59
|
-
/**
|
|
60
|
-
* @internal
|
|
61
|
-
* The asynchronous function used to fetch the resource value.
|
|
62
|
-
*/
|
|
63
56
|
private _fetch: () => Promise<T>;
|
|
64
57
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { FlowObservable } from "../basic/";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A function type that sets a new value for the reactive stream.
|
|
5
|
+
* @typeparam T - The type of the value.
|
|
6
|
+
* @public
|
|
7
|
+
*/
|
|
8
|
+
export type FlowStreamSetter<T> = (value: T) => void;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A function type that disposes of a resource.
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export type FlowStreamDisposer = () => void;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A function type that updates a stream and returns a disposer for cleanup.
|
|
18
|
+
* @remarks
|
|
19
|
+
* The updater receives a setter function to update the stream's value.
|
|
20
|
+
* It should return a disposer function to release any resources or subscriptions.
|
|
21
|
+
* @typeparam T - The type of the stream value.
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export type FlowStreamUpdater<T> = (
|
|
25
|
+
set: FlowStreamSetter<T>,
|
|
26
|
+
) => FlowStreamDisposer;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Represents a reactive stream that updates its value based on an updater function.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* FlowStream extends FlowObservable to encapsulate an update mechanism provided by an external updater function.
|
|
33
|
+
* The updater is invoked during construction with a setter, and it returns a disposer to be called upon disposal.
|
|
34
|
+
* Note: The stream's current value may be undefined until explicitly set.
|
|
35
|
+
*
|
|
36
|
+
* @typeparam T - The type of the stream's value.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
export class FlowStream<T> extends FlowObservable<T | undefined> {
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new FlowStream.
|
|
42
|
+
* @param updater - A function that receives a setter to update the stream's value.
|
|
43
|
+
* It should return a disposer function that will be called upon disposal.
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
constructor(updater: FlowStreamUpdater<T>) {
|
|
47
|
+
super();
|
|
48
|
+
this._disposer = updater((value: T) => {
|
|
49
|
+
this._set(value);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Retrieves the current value of the stream.
|
|
55
|
+
* @returns The current value, or undefined if no value has been set yet.
|
|
56
|
+
* @throws Error if the stream is disposed.
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
public get(): T | undefined {
|
|
60
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
61
|
+
return this._value;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Disposes the stream, releasing all resources.
|
|
66
|
+
* @remarks
|
|
67
|
+
* In addition to disposing the underlying observable, this method calls the disposer
|
|
68
|
+
* returned by the updater.
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
public override dispose(): void {
|
|
72
|
+
super.dispose();
|
|
73
|
+
this._disposer();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/* INTERNAL ------------------------------------------------------ */
|
|
77
|
+
|
|
78
|
+
private _disposer: FlowStreamDisposer;
|
|
79
|
+
|
|
80
|
+
private _set(value: T): void {
|
|
81
|
+
/* v8 ignore next */
|
|
82
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
83
|
+
if (value === this._value) return;
|
|
84
|
+
this._value = value;
|
|
85
|
+
this._notify();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { FlowObservable } from "../basic";
|
|
2
|
+
import type { FlowStreamDisposer, FlowStreamUpdater } from "./stream";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents an asynchronous reactive stream that updates its value based on an updater function.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* A FlowStreamAsync extends FlowObservable and encapsulates a mechanism for asynchronously updating its value.
|
|
9
|
+
* The updater is invoked during construction with a setter, and its returned disposer is called upon disposal.
|
|
10
|
+
* Note: The stream's current value may be undefined until it is set.
|
|
11
|
+
*
|
|
12
|
+
* @typeparam T - The type of the stream's value.
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export class FlowStreamAsync<T> extends FlowObservable<Promise<T>> {
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new asynchronous FlowStream.
|
|
18
|
+
* @param updater - A function that receives a setter to update the stream's value.
|
|
19
|
+
* It should return a disposer function that will be called upon disposal.
|
|
20
|
+
* @remarks The updater function can invoke the setter asynchronously to update the stream.
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
constructor(updater: FlowStreamUpdater<T>) {
|
|
24
|
+
super();
|
|
25
|
+
this._disposer = updater((value: T) => {
|
|
26
|
+
this._set(value);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
this._value = new Promise((resolve) => {
|
|
30
|
+
this._resolve = resolve;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Retrieves the current value of the stream as a Promise.
|
|
36
|
+
* @returns A Promise that resolves to the current value.
|
|
37
|
+
* @throws Error if the stream is disposed.
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
public get(): Promise<T> {
|
|
41
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
42
|
+
return this._value;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Disposes the stream, releasing all resources.
|
|
47
|
+
* @remarks In addition to disposing the underlying observable, this method calls the disposer
|
|
48
|
+
* returned by the updater.
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
51
|
+
public override dispose(): void {
|
|
52
|
+
super.dispose();
|
|
53
|
+
this._disposer();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/* INTERNAL ------------------------------------------------------ */
|
|
57
|
+
|
|
58
|
+
private _initialized = false;
|
|
59
|
+
private _awaitedValue?: T;
|
|
60
|
+
|
|
61
|
+
private _resolve!: (value: T) => void;
|
|
62
|
+
private _disposer: FlowStreamDisposer;
|
|
63
|
+
|
|
64
|
+
private _set(value: T): void {
|
|
65
|
+
/* v8 ignore next */
|
|
66
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
67
|
+
|
|
68
|
+
if (!this._initialized) {
|
|
69
|
+
this._resolve(value);
|
|
70
|
+
this._initialized = true;
|
|
71
|
+
this._awaitedValue = value;
|
|
72
|
+
this._notify();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (value === this._awaitedValue) return;
|
|
77
|
+
|
|
78
|
+
this._value = Promise.resolve(value);
|
|
79
|
+
this._awaitedValue = value;
|
|
80
|
+
this._notify();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { FlowObservable } from "./observable";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents a reactive and immutable constant value computed lazily upon first access.
|
|
5
|
+
*
|
|
6
|
+
* @remarks This class extends FlowObservable and supports initializing the constant using either a direct value
|
|
7
|
+
* or a lazy initializer function. Once computed, the value is cached for all subsequent accesses.
|
|
8
|
+
*
|
|
9
|
+
* @typeparam T - The type of the constant value.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export class FlowConstant<T> extends FlowObservable<T> {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new FlowConstant instance.
|
|
16
|
+
*
|
|
17
|
+
* @param value - Either a direct value of type T or a function returning a value of type T for lazy initialization.
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
constructor(value: T | (() => T)) {
|
|
21
|
+
super();
|
|
22
|
+
this._initEager(value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Retrieves the constant value, computing it lazily if needed.
|
|
27
|
+
*
|
|
28
|
+
* Accessing this method will initialize the value if it has not been computed already.
|
|
29
|
+
* Throws an error if the instance has been disposed or if lazy initialization fails.
|
|
30
|
+
*
|
|
31
|
+
* @returns The cached constant value.
|
|
32
|
+
* @throws Error if the constant is disposed or cannot be initialized.
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
get(): T {
|
|
36
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
37
|
+
this._initLazy();
|
|
38
|
+
return this._value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/* INTERNAL --------------------------------------------------------- */
|
|
42
|
+
|
|
43
|
+
/*@internal*/ protected _initialized = false;
|
|
44
|
+
/*@internal*/ protected _init?: () => T;
|
|
45
|
+
|
|
46
|
+
/*@internal*/ protected _initEager(value: T | (() => T)): void {
|
|
47
|
+
if (typeof value === "function") {
|
|
48
|
+
this._init = value as () => T;
|
|
49
|
+
} else {
|
|
50
|
+
this._value = value;
|
|
51
|
+
this._initialized = true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/*@internal*/ protected _initLazy(): void {
|
|
56
|
+
if (!this._initialized && this._init) {
|
|
57
|
+
this._value = this._init();
|
|
58
|
+
this._initialized = true;
|
|
59
|
+
}
|
|
60
|
+
/* v8 ignore next 2 */
|
|
61
|
+
if (!this._initialized)
|
|
62
|
+
throw new Error("[PicoFlow] Primitive can't be initialized");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { type FlowGetter, FlowObservable } from "./observable";
|
|
2
|
+
import type { FlowWatcher } from "./signal";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a reactive derivation whose value is computed based on other reactive signals.
|
|
6
|
+
* @remarks
|
|
7
|
+
* It tracks dependencies automatically and recomputes its value when any dependency changes.
|
|
8
|
+
* Use FlowDerivation to create derived values in a reactive manner. It lazily initializes the computed value,
|
|
9
|
+
* ensuring that computations only occur when necessary.
|
|
10
|
+
* @typeparam T - The type of the computed value.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export class FlowDerivation<T> extends FlowObservable<T> {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a new FlowDerivation.
|
|
16
|
+
* @param compute - A function that computes the derived value. It is provided with two parameters:
|
|
17
|
+
* a getter and a watcher that respect dependency tracking.
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
constructor(compute: (get: FlowGetter, watch: FlowWatcher) => T) {
|
|
21
|
+
super();
|
|
22
|
+
this._initEager(compute);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Gets the current derived value.
|
|
27
|
+
* @returns The current computed value.
|
|
28
|
+
* @remarks
|
|
29
|
+
* This method lazily initializes and updates the derivation if it is marked as dirty. It throws an error
|
|
30
|
+
* if the derivation has been disposed.
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
public get(): T {
|
|
34
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
35
|
+
this._initLazy();
|
|
36
|
+
this._compute();
|
|
37
|
+
return this._value;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/* INTERNAL --------------------------------------------------------- */
|
|
41
|
+
|
|
42
|
+
private _initialized = false;
|
|
43
|
+
private _dirty = false;
|
|
44
|
+
|
|
45
|
+
private _trackedGet: FlowGetter = (observable) => observable._getFrom(this);
|
|
46
|
+
private _trackedWatch: FlowWatcher = (signal) => signal._watchFrom(this);
|
|
47
|
+
private _untrackedGet: FlowGetter = (observable) => observable.get();
|
|
48
|
+
private _untrackedWatch: FlowWatcher = (signal) => signal._watch();
|
|
49
|
+
private _trackedCompute!: () => T;
|
|
50
|
+
private _untrackedCompute!: () => T;
|
|
51
|
+
|
|
52
|
+
private _initEager(
|
|
53
|
+
compute: (get: FlowGetter, watch: FlowWatcher) => T,
|
|
54
|
+
): void {
|
|
55
|
+
this._trackedCompute = () =>
|
|
56
|
+
compute(this._trackedGet, this._trackedWatch);
|
|
57
|
+
this._untrackedCompute = () =>
|
|
58
|
+
compute(this._untrackedGet, this._untrackedWatch);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private _initLazy(): void {
|
|
62
|
+
if (!this._initialized) {
|
|
63
|
+
this._value = this._trackedCompute();
|
|
64
|
+
this._initialized = true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/* @internal */ _compute(): void {
|
|
69
|
+
if (this._dirty) {
|
|
70
|
+
this._value = this._untrackedCompute();
|
|
71
|
+
this._dirty = false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/* @internal */ override _notify(): void {
|
|
76
|
+
this._dirty = true;
|
|
77
|
+
super._notify();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/* @internal */ override _watch(): void {
|
|
81
|
+
/* v8 ignore next 1 */
|
|
82
|
+
if (this._disposed) throw new Error("[PicoFlow] Primitive is disposed");
|
|
83
|
+
this._initLazy();
|
|
84
|
+
this._compute();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { FlowGetter } from "./observable";
|
|
2
|
+
import type { FlowSignal, FlowWatcher } from "./signal";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a reactive effect that executes side-effect functions based
|
|
6
|
+
* on its tracked dependencies.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* The FlowEffect executes an apply function that performs side effects,
|
|
10
|
+
* running initially in a tracked mode to register dependencies and then in
|
|
11
|
+
* an untracked mode to re-execute the effect on updates.
|
|
12
|
+
*
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export class FlowEffect {
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new FlowEffect.
|
|
18
|
+
*
|
|
19
|
+
* @param apply - A side-effect function that receives a getter and a watcher to
|
|
20
|
+
* access and register dependencies on reactive observables and signals.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* The provided function is executed immediately in a tracked mode to collect dependencies.
|
|
24
|
+
* On subsequent executions, it runs in an untracked mode.
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
constructor(apply: (get: FlowGetter, watch: FlowWatcher) => void) {
|
|
29
|
+
this._trackedExec = () => apply(this._trackedGet, this._trackedWatch);
|
|
30
|
+
this._untrackedExec = () =>
|
|
31
|
+
apply(this._untrackedGet, this._untrackedWatch);
|
|
32
|
+
this._exec();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Disposes the effect, unregistering all its tracked dependencies.
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* Once disposed, the effect must no longer be used. Trying to dispose an effect
|
|
40
|
+
* that is already disposed will throw an error.
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
public dispose(): void {
|
|
45
|
+
if (this._disposed) throw new Error("[PicoFlow] Effect is disposed");
|
|
46
|
+
Array.from(this._dependencies).forEach((dependency) => {
|
|
47
|
+
this._unregisterDependency(dependency);
|
|
48
|
+
});
|
|
49
|
+
this._disposed = true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Indicates whether this effect has been disposed.
|
|
54
|
+
*
|
|
55
|
+
* @returns A boolean value that is true if the effect is disposed, false otherwise.
|
|
56
|
+
*
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
59
|
+
public get disposed(): boolean {
|
|
60
|
+
return this._disposed;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* INTERNAL ------------------------------------------------------------ */
|
|
64
|
+
|
|
65
|
+
private _disposed = false;
|
|
66
|
+
private _initialized = false;
|
|
67
|
+
private _dependencies = new Set<FlowSignal>();
|
|
68
|
+
|
|
69
|
+
private _trackedGet: FlowGetter = (observable) => observable._getFrom(this);
|
|
70
|
+
private _trackedWatch: FlowWatcher = (signal) => signal._watchFrom(this);
|
|
71
|
+
private _untrackedGet: FlowGetter = (observable) => observable.get();
|
|
72
|
+
private _untrackedWatch: FlowWatcher = (signal) => signal._watch();
|
|
73
|
+
private _trackedExec: () => void;
|
|
74
|
+
private _untrackedExec: () => void;
|
|
75
|
+
|
|
76
|
+
/*@internal*/ _exec(): void {
|
|
77
|
+
if (this._disposed)
|
|
78
|
+
/* v8 ignore next 1 */
|
|
79
|
+
throw new Error("[PicoFlow] Effect is disposed");
|
|
80
|
+
if (this._initialized) this._untrackedExec();
|
|
81
|
+
else {
|
|
82
|
+
this._trackedExec();
|
|
83
|
+
this._initialized = true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/*@internal*/ _registerDependency(dependency: FlowSignal): void {
|
|
88
|
+
this._dependencies.add(dependency);
|
|
89
|
+
dependency._registerEffect(this);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/*@internal*/ _unregisterDependency(dependency: FlowSignal): void {
|
|
93
|
+
this._dependencies.delete(dependency);
|
|
94
|
+
dependency._unregisterEffect(this);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { FlowSignal } from "./signal";
|
|
2
|
+
export { FlowState } from "./state";
|
|
3
|
+
export { FlowObservable } from "./observable";
|
|
4
|
+
export { FlowDerivation } from "./derivation";
|
|
5
|
+
export { FlowEffect } from "./effect";
|
|
6
|
+
export { FlowConstant } from "./constant";
|
|
7
|
+
export type { FlowGetter } from "./observable";
|
|
8
|
+
export type { FlowWatcher } from "./signal";
|