@alwatr/signal 9.26.0 → 9.30.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/README.md +197 -603
- package/dist/core/channel-signal.d.ts +12 -53
- package/dist/core/channel-signal.d.ts.map +1 -1
- package/dist/core/computed-signal.d.ts +19 -33
- package/dist/core/computed-signal.d.ts.map +1 -1
- package/dist/core/derived-signal.d.ts +71 -0
- package/dist/core/derived-signal.d.ts.map +1 -0
- package/dist/core/effect-signal.d.ts +15 -1
- package/dist/core/effect-signal.d.ts.map +1 -1
- package/dist/core/event-signal.d.ts +11 -4
- package/dist/core/event-signal.d.ts.map +1 -1
- package/dist/core/persistent-state-signal.d.ts +21 -2
- package/dist/core/persistent-state-signal.d.ts.map +1 -1
- package/dist/core/session-state-signal.d.ts +19 -2
- package/dist/core/session-state-signal.d.ts.map +1 -1
- package/dist/core/signal-base.d.ts +58 -38
- package/dist/core/signal-base.d.ts.map +1 -1
- package/dist/core/state-signal.d.ts +33 -14
- package/dist/core/state-signal.d.ts.map +1 -1
- package/dist/creators/channel.d.ts +1 -1
- package/dist/creators/channel.d.ts.map +1 -1
- package/dist/creators/derived.d.ts +31 -0
- package/dist/creators/derived.d.ts.map +1 -0
- package/dist/main.d.ts +2 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +3 -3
- package/dist/main.js.map +16 -15
- package/dist/operators/debounce.d.ts +2 -3
- package/dist/operators/debounce.d.ts.map +1 -1
- package/dist/operators/filter.d.ts +14 -13
- package/dist/operators/filter.d.ts.map +1 -1
- package/dist/type.d.ts +68 -3
- package/dist/type.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/core/channel-signal.ts +25 -68
- package/src/core/computed-signal.ts +50 -74
- package/src/core/derived-signal.ts +166 -0
- package/src/core/effect-signal.ts +23 -11
- package/src/core/event-signal.ts +14 -9
- package/src/core/persistent-state-signal.ts +21 -4
- package/src/core/session-state-signal.ts +19 -4
- package/src/core/signal-base.ts +98 -61
- package/src/core/state-signal.ts +48 -29
- package/src/creators/channel.ts +1 -2
- package/src/creators/derived.ts +34 -0
- package/src/main.ts +2 -1
- package/src/operators/debounce.ts +13 -23
- package/src/operators/filter.ts +20 -26
- package/src/type.ts +71 -3
- package/dist/operators/map.d.ts +0 -36
- package/dist/operators/map.d.ts.map +0 -1
- package/src/operators/map.ts +0 -48
|
@@ -1,58 +1,6 @@
|
|
|
1
|
-
import type { Awaitable } from '@alwatr/type-helper';
|
|
2
1
|
import { type AlwatrLogger } from '@alwatr/logger';
|
|
3
2
|
import { SignalBase } from './signal-base.js';
|
|
4
|
-
import type {
|
|
5
|
-
/**
|
|
6
|
-
* Determines whether the payload argument for a given channel message is
|
|
7
|
-
* required or optional, based solely on the declared type in `TMap`.
|
|
8
|
-
*
|
|
9
|
-
* - `void | undefined` → payload is optional (second arg may be omitted).
|
|
10
|
-
* - anything else → payload is **required** (omitting it is a compile error).
|
|
11
|
-
*
|
|
12
|
-
* This is used to build the rest-parameter tuple for `dispatch()` so that
|
|
13
|
-
* TypeScript enforces the correct call signature at every dispatch site.
|
|
14
|
-
*
|
|
15
|
-
* @template TMap A record mapping message names to their payload types.
|
|
16
|
-
* @template K The specific message name key.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* // ActionRecord: { 'logout': void; 'add-to-cart': {productId: number} }
|
|
21
|
-
* type A = DispatchArgs<ActionRecord, 'logout'>; // [name: 'logout', payload?: void]
|
|
22
|
-
* type B = DispatchArgs<ActionRecord, 'add-to-cart'>; // [name: 'add-to-cart', payload: {productId: number}]
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export type DispatchArgs<TMap extends object, K extends keyof TMap> = TMap[K] extends void | undefined ? [name: K, payload?: TMap[K]] : [name: K, payload: TMap[K]];
|
|
26
|
-
/**
|
|
27
|
-
* A single message dispatched through a `ChannelSignal`.
|
|
28
|
-
*
|
|
29
|
-
* `name` identifies the message type (e.g. `'open-drawer'`, `'add-to-cart'`).
|
|
30
|
-
* `payload` carries the associated data, whose type is determined by the generic `TMap` based on the `name`.
|
|
31
|
-
*
|
|
32
|
-
* @template TMap A record mapping message names to their payload types.
|
|
33
|
-
* @template K The specific message name key (inferred, not set manually).
|
|
34
|
-
*/
|
|
35
|
-
export type ChannelMessage<TMap extends object, K extends keyof TMap = keyof TMap> = {
|
|
36
|
-
name: K;
|
|
37
|
-
payload: TMap[K];
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* A typed handler for a specific named message on a `ChannelSignal`.
|
|
41
|
-
* Receives only the `payload` — the name is already known at subscription time.
|
|
42
|
-
*
|
|
43
|
-
* The payload type mirrors `DispatchArgs`: it is `TMap[K] | undefined` only
|
|
44
|
-
* when the declared type is `void | undefined`; otherwise it is exactly `TMap[K]`
|
|
45
|
-
* (non-optional) so handlers do not need unnecessary null-guards.
|
|
46
|
-
*
|
|
47
|
-
* @template TMap A record mapping message names to their payload types.
|
|
48
|
-
* @template K The specific message name key.
|
|
49
|
-
*/
|
|
50
|
-
export type ChannelHandler<TMap extends object, K extends keyof TMap = keyof TMap> = (payload: TMap[K]) => Awaitable<void>;
|
|
51
|
-
/**
|
|
52
|
-
* Configuration for creating a `ChannelSignal`.
|
|
53
|
-
*/
|
|
54
|
-
export interface ChannelSignalConfig extends SignalConfig {
|
|
55
|
-
}
|
|
3
|
+
import type { SubscribeOptions, SubscribeResult, ListenerCallback, ChannelHandler, ChannelMessage, ChannelSignalConfig, DispatchArgs } from '../type.js';
|
|
56
4
|
/**
|
|
57
5
|
* A stateless multi-channel signal that acts as a typed O(1) message bus.
|
|
58
6
|
*
|
|
@@ -100,6 +48,7 @@ export interface ChannelSignalConfig extends SignalConfig {
|
|
|
100
48
|
export declare class ChannelSignal<TMap extends object> extends SignalBase<ChannelMessage<TMap>> {
|
|
101
49
|
/**
|
|
102
50
|
* The logger instance for this signal.
|
|
51
|
+
*
|
|
103
52
|
* @protected
|
|
104
53
|
*/
|
|
105
54
|
protected logger_: AlwatrLogger;
|
|
@@ -117,6 +66,11 @@ export declare class ChannelSignal<TMap extends object> extends SignalBase<Chann
|
|
|
117
66
|
* @private
|
|
118
67
|
*/
|
|
119
68
|
private readonly namedHandlers__;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a new ChannelSignal instance.
|
|
71
|
+
*
|
|
72
|
+
* @param config Configuration options including the unique channel name and cleanup hook.
|
|
73
|
+
*/
|
|
120
74
|
constructor(config: ChannelSignalConfig);
|
|
121
75
|
/**
|
|
122
76
|
* Dispatches a named message to:
|
|
@@ -141,6 +95,7 @@ export declare class ChannelSignal<TMap extends object> extends SignalBase<Chann
|
|
|
141
95
|
* channel.dispatch('logout', undefined); // ✅ also fine
|
|
142
96
|
* ```
|
|
143
97
|
*
|
|
98
|
+
* @template K The specific message name key.
|
|
144
99
|
* @param args Tuple of `[name, payload]` — payload optionality is enforced
|
|
145
100
|
* by `DispatchArgs<TMap, K>` based on the declared type.
|
|
146
101
|
*/
|
|
@@ -155,6 +110,7 @@ export declare class ChannelSignal<TMap extends object> extends SignalBase<Chann
|
|
|
155
110
|
* envelope) — since the name is already known at subscription time, passing
|
|
156
111
|
* it again would be redundant.
|
|
157
112
|
*
|
|
113
|
+
* @template K The specific message name key.
|
|
158
114
|
* @param name The message name to listen for.
|
|
159
115
|
* @param handler Callback invoked with the payload each time the named message
|
|
160
116
|
* is dispatched.
|
|
@@ -200,6 +156,9 @@ export declare class ChannelSignal<TMap extends object> extends SignalBase<Chann
|
|
|
200
156
|
* 2. Invokes each handler, removing `once` entries after their first call.
|
|
201
157
|
* 3. Notifies raw-stream subscribers via `SignalBase.notify_()`.
|
|
202
158
|
*
|
|
159
|
+
* @template K The specific message name key.
|
|
160
|
+
* @param name The message name to route.
|
|
161
|
+
* @param payload The payload associated with the message name.
|
|
203
162
|
* @private
|
|
204
163
|
*/
|
|
205
164
|
private route__;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel-signal.d.ts","sourceRoot":"","sources":["../../src/core/channel-signal.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"channel-signal.d.ts","sourceRoot":"","sources":["../../src/core/channel-signal.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,KAAK,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAC5C,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,cAAc,EACd,cAAc,EACd,mBAAmB,EACnB,YAAY,EACb,MAAM,YAAY,CAAC;AAiBpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,qBAAa,aAAa,CAAC,IAAI,SAAS,MAAM,CAAE,SAAQ,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IACtF;;;;OAIG;IACH,SAAS,CAAC,OAAO,EAAE,YAAY,CAAC;IAEhC;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAyE;IAEzG;;;;OAIG;gBACS,MAAM,EAAE,mBAAmB;IAMvC;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACI,QAAQ,CAAC,CAAC,SAAS,MAAM,IAAI,EAAE,GAAG,IAAI,EAAE,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,IAAI;IAO3E;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACI,EAAE,CAAC,CAAC,SAAS,MAAM,IAAI,EAC5B,IAAI,EAAE,CAAC,EACP,OAAO,EAAE,cAAc,CAAC,IAAI,EAAE,CAAC,CAAC,EAChC,OAAO,CAAC,EAAE,IAAI,CAAC,gBAAgB,EAAE,MAAM,CAAC,GACvC,eAAe;IAyBlB;;;;;;;;;;;;;;;;;;OAkBG;IACa,SAAS,CACvB,QAAQ,EAAE,gBAAgB,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAChD,OAAO,CAAC,EAAE,gBAAgB,GACzB,eAAe;IAKlB;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,OAAO;IAsBf;;OAEG;IACa,OAAO,IAAI,IAAI;CAIhC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type AlwatrLogger } from '@alwatr/logger';
|
|
2
2
|
import { StateSignal } from './state-signal.js';
|
|
3
|
-
import type { ComputedSignalConfig, IReadonlySignal, SubscribeResult,
|
|
3
|
+
import type { ComputedSignalConfig, IReadonlySignal, SubscribeOptions, SubscribeResult, ListenerCallback } from '../type.js';
|
|
4
4
|
/**
|
|
5
5
|
* A read-only signal that derives its value from a set of dependency signals.
|
|
6
6
|
*
|
|
@@ -12,32 +12,6 @@ import type { ComputedSignalConfig, IReadonlySignal, SubscribeResult, SubscribeO
|
|
|
12
12
|
* needed to prevent memory leaks from its subscriptions to dependency signals.
|
|
13
13
|
*
|
|
14
14
|
* @template T The type of the computed value.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* // --- Create dependency signals ---
|
|
18
|
-
* const firstName = new StateSignal({ name: 'firstName', initialValue: 'John' });
|
|
19
|
-
* const lastName = new StateSignal({ name: 'lastName', initialValue: 'Doe' });
|
|
20
|
-
*
|
|
21
|
-
* // --- Create a computed signal ---
|
|
22
|
-
* const fullName = new ComputedSignal({
|
|
23
|
-
* name: 'fullName',
|
|
24
|
-
* deps: [firstName, lastName],
|
|
25
|
-
* get: () => `${firstName.get()} ${lastName.get()}`,
|
|
26
|
-
* });
|
|
27
|
-
*
|
|
28
|
-
* console.log(fullName.get()); // Outputs: "John Doe"
|
|
29
|
-
*
|
|
30
|
-
* // --- Subscribe to the computed value ---
|
|
31
|
-
* fullName.subscribe(newFullName => {
|
|
32
|
-
* console.log(`Name changed to: ${newFullName}`);
|
|
33
|
-
* });
|
|
34
|
-
*
|
|
35
|
-
* // --- Update a dependency ---
|
|
36
|
-
* lastName.set('Smith'); // Recalculates and logs: "Name changed to: John Smith"
|
|
37
|
-
* console.log(fullName.get()); // Outputs: "John Smith"
|
|
38
|
-
*
|
|
39
|
-
* // --- IMPORTANT: Clean up when done ---
|
|
40
|
-
* fullName.destroy();
|
|
41
15
|
*/
|
|
42
16
|
export declare class ComputedSignal<T> implements IReadonlySignal<T> {
|
|
43
17
|
protected config_: ComputedSignalConfig<T>;
|
|
@@ -47,25 +21,38 @@ export declare class ComputedSignal<T> implements IReadonlySignal<T> {
|
|
|
47
21
|
readonly name: string;
|
|
48
22
|
/**
|
|
49
23
|
* The logger instance for this signal.
|
|
24
|
+
*
|
|
50
25
|
* @protected
|
|
51
26
|
*/
|
|
52
27
|
protected readonly logger_: AlwatrLogger;
|
|
53
28
|
/**
|
|
54
29
|
* The internal `StateSignal` that holds the computed value.
|
|
55
30
|
* This is how the computed signal provides `.get()` and `.subscribe()` methods.
|
|
31
|
+
* Enforces COMPOSITION over inheritance.
|
|
32
|
+
*
|
|
56
33
|
* @protected
|
|
57
34
|
*/
|
|
58
35
|
protected readonly internalSignal_: StateSignal<T>;
|
|
59
36
|
/**
|
|
60
37
|
* A list of subscriptions to dependency signals.
|
|
38
|
+
* Used to unsubscribe from dependencies when this signal is destroyed.
|
|
39
|
+
*
|
|
61
40
|
* @private
|
|
62
41
|
*/
|
|
63
42
|
private readonly dependencySubscriptions__;
|
|
64
43
|
/**
|
|
65
44
|
* A flag to prevent concurrent recalculations.
|
|
45
|
+
* Avoids queuing multiple updates in the event loop.
|
|
46
|
+
*
|
|
66
47
|
* @private
|
|
67
48
|
*/
|
|
68
49
|
private isRecalculating__;
|
|
50
|
+
/**
|
|
51
|
+
* Creates a new ComputedSignal instance.
|
|
52
|
+
* Subscribes to all dependency signals to trigger recalculations.
|
|
53
|
+
*
|
|
54
|
+
* @param config_ Configuration options including dependencies, evaluation getter, and cleanup hook.
|
|
55
|
+
*/
|
|
69
56
|
constructor(config_: ComputedSignalConfig<T>);
|
|
70
57
|
/**
|
|
71
58
|
* The current value of the computed signal.
|
|
@@ -78,6 +65,7 @@ export declare class ComputedSignal<T> implements IReadonlySignal<T> {
|
|
|
78
65
|
/**
|
|
79
66
|
* Indicates whether the computed signal has been destroyed.
|
|
80
67
|
* A destroyed signal cannot be used and will throw an error if interacted with.
|
|
68
|
+
*
|
|
81
69
|
* @returns `true` if the signal is destroyed, `false` otherwise.
|
|
82
70
|
*/
|
|
83
71
|
get isDestroyed(): boolean;
|
|
@@ -89,7 +77,7 @@ export declare class ComputedSignal<T> implements IReadonlySignal<T> {
|
|
|
89
77
|
* @param options Subscription options.
|
|
90
78
|
* @returns A `SubscribeResult` object with an `unsubscribe` method.
|
|
91
79
|
*/
|
|
92
|
-
subscribe(callback:
|
|
80
|
+
subscribe(callback: ListenerCallback<T>, options?: SubscribeOptions): SubscribeResult;
|
|
93
81
|
/**
|
|
94
82
|
* Returns a Promise that resolves with the next computed value.
|
|
95
83
|
*
|
|
@@ -107,13 +95,11 @@ export declare class ComputedSignal<T> implements IReadonlySignal<T> {
|
|
|
107
95
|
*/
|
|
108
96
|
destroy(): void;
|
|
109
97
|
/**
|
|
110
|
-
*
|
|
98
|
+
* Recalculates the derived value.
|
|
99
|
+
* Centralized microtask batcher coordination avoids internal loop crashes.
|
|
111
100
|
*
|
|
112
|
-
* This method batches updates using a macrotask (`delay.nextMacrotask`) to ensure the
|
|
113
|
-
* `get` function runs only once per event loop tick, even if multiple dependencies
|
|
114
|
-
* change in the same synchronous block of code.
|
|
115
101
|
* @protected
|
|
116
102
|
*/
|
|
117
|
-
protected recalculate_():
|
|
103
|
+
protected recalculate_(): void;
|
|
118
104
|
}
|
|
119
105
|
//# sourceMappingURL=computed-signal.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computed-signal.d.ts","sourceRoot":"","sources":["../../src/core/computed-signal.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"computed-signal.d.ts","sourceRoot":"","sources":["../../src/core/computed-signal.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,KAAK,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAC,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAC9C,OAAO,KAAK,EACV,oBAAoB,EACpB,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EACjB,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;;;GAWG;AACH,qBAAa,cAAc,CAAC,CAAC,CAAE,YAAW,eAAe,CAAC,CAAC,CAAC;IA4C9C,SAAS,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC,CAAC;IA3CtD;;OAEG;IACH,SAAgB,IAAI,EAAE,MAAM,CAAC;IAE7B;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;IAEzC;;;;;;OAMG;IACH,SAAS,CAAC,QAAQ,CAAC,eAAe,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IAEnD;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAAyB;IAEnE;;;;;OAKG;IACH,OAAO,CAAC,iBAAiB,CAAS;IAElC;;;;;OAKG;gBACmB,OAAO,EAAE,oBAAoB,CAAC,CAAC,CAAC;IAoBtD;;;;;;OAMG;IACI,GAAG,IAAI,CAAC;IAIf;;;;;OAKG;IACH,IAAW,WAAW,IAAI,OAAO,CAEhC;IAED;;;;;;;OAOG;IACI,SAAS,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,eAAe;IAI5F;;;;OAIG;IACI,SAAS,IAAI,OAAO,CAAC,CAAC,CAAC;IAI9B;;;;;;;;OAQG;IACI,OAAO,IAAI,IAAI;IAiBtB;;;;;OAKG;IACH,SAAS,CAAC,YAAY,IAAI,IAAI;CA8B/B"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type AlwatrLogger } from '@alwatr/logger';
|
|
2
|
+
import { StateSignal } from './state-signal.js';
|
|
3
|
+
import type { IReadonlySignal, DerivedSignalConfig, ListenerCallback, SubscribeOptions, SubscribeResult } from '../type.js';
|
|
4
|
+
/**
|
|
5
|
+
* Ultra-performance read-only signal mapping exactly 1-to-1 over a single upstream source.
|
|
6
|
+
*
|
|
7
|
+
* COMPOSITION DESIGN PATTERN (HAS-A):
|
|
8
|
+
* Instead of extending the heavyweight Base class and duplicating tracking structures, it wraps
|
|
9
|
+
* an internal StateSignal instance. It features a "Cold Awakening Lifecycle": it consumes exactly
|
|
10
|
+
* ZERO stream overhead from the source until it receives its own first consumer subscription.
|
|
11
|
+
* If all consumers disconnect, it goes back to sleep (hibernation phase) to save performance.
|
|
12
|
+
*
|
|
13
|
+
* @template S The type of the source signal state.
|
|
14
|
+
* @template T The type of the derived/projected signal state.
|
|
15
|
+
*/
|
|
16
|
+
export declare class DerivedSignal<S, T> implements IReadonlySignal<T> {
|
|
17
|
+
protected config_: DerivedSignalConfig<S, T>;
|
|
18
|
+
/** The unique identifier for this signal instance, useful for debugging and tracing. */
|
|
19
|
+
readonly name: string;
|
|
20
|
+
/** Scoped logger for tracking derived operations. */
|
|
21
|
+
protected readonly logger_: AlwatrLogger;
|
|
22
|
+
/** Wrapped internal state carrier - Enforcing COMPOSITION over inheritance, allocated lazily */
|
|
23
|
+
protected internalSignal_?: StateSignal<T> | null;
|
|
24
|
+
/** Subscription handle to the upstream source signal, active only when awake. */
|
|
25
|
+
private sourceSubscription__?;
|
|
26
|
+
/** Number of active standard/priority listeners currently subscribed to this signal. */
|
|
27
|
+
private activeConsumerCount__;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new DerivedSignal instance.
|
|
30
|
+
*
|
|
31
|
+
* @param config_ Configuration options including name, source, and projector.
|
|
32
|
+
*/
|
|
33
|
+
constructor(config_: DerivedSignalConfig<S, T>);
|
|
34
|
+
untilNext(): Promise<T>;
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves the current value of the derived signal.
|
|
37
|
+
*
|
|
38
|
+
* If there are no active subscribers (cold state), it re-computes dynamically
|
|
39
|
+
* on demand to ensure strict data freshness.
|
|
40
|
+
*
|
|
41
|
+
* @returns The current projected value.
|
|
42
|
+
*/
|
|
43
|
+
get(): T;
|
|
44
|
+
/**
|
|
45
|
+
* Indicates whether the signal has been destroyed.
|
|
46
|
+
*/
|
|
47
|
+
get isDestroyed(): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Subscribes a listener to updates of this derived signal.
|
|
50
|
+
*
|
|
51
|
+
* In case of first subscription, it triggers the "Cold Awakening Lifecycle"
|
|
52
|
+
* to subscribe to the source signal.
|
|
53
|
+
*
|
|
54
|
+
* @param callback Subscription callback function.
|
|
55
|
+
* @param options Subscription configurations.
|
|
56
|
+
* @returns Unsubscribe handle object.
|
|
57
|
+
*/
|
|
58
|
+
subscribe(callback: ListenerCallback<T>, options?: SubscribeOptions): SubscribeResult;
|
|
59
|
+
/**
|
|
60
|
+
* Destroys the derived signal and unsubscribes from the source signal if currently awake.
|
|
61
|
+
*/
|
|
62
|
+
destroy(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Checks if the signal has been destroyed.
|
|
65
|
+
*
|
|
66
|
+
* @private
|
|
67
|
+
* @throws {Error} If destroyed.
|
|
68
|
+
*/
|
|
69
|
+
private checkDestroyed__;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=derived-signal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"derived-signal.d.ts","sourceRoot":"","sources":["../../src/core/derived-signal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,KAAK,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAC,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAC9C,OAAO,KAAK,EACV,eAAe,EACf,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EAChB,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;;;GAWG;AACH,qBAAa,aAAa,CAAC,CAAC,EAAE,CAAC,CAAE,YAAW,eAAe,CAAC,CAAC,CAAC;IAqBhD,SAAS,CAAC,OAAO,EAAE,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC;IApBxD,wFAAwF;IACxF,SAAgB,IAAI,EAAE,MAAM,CAAC;IAE7B,qDAAqD;IACrD,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;IAEzC,gGAAgG;IAChG,SAAS,CAAC,eAAe,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IAElD,iFAAiF;IACjF,OAAO,CAAC,oBAAoB,CAAC,CAAkB;IAE/C,wFAAwF;IACxF,OAAO,CAAC,qBAAqB,CAAK;IAElC;;;;OAIG;gBACmB,OAAO,EAAE,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC;IAKxD,SAAS,IAAI,OAAO,CAAC,CAAC,CAAC;IAavB;;;;;;;OAOG;IACI,GAAG,IAAI,CAAC;IASf;;OAEG;IACH,IAAW,WAAW,IAAI,OAAO,CAEhC;IAED;;;;;;;;;OASG;IACI,SAAS,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,eAAe;IAyC5F;;OAEG;IACI,OAAO,IAAI,IAAI;IActB;;;;;OAKG;IACH,OAAO,CAAC,gBAAgB;CAMzB"}
|
|
@@ -47,21 +47,27 @@ export declare class EffectSignal implements IEffectSignal {
|
|
|
47
47
|
readonly name: string;
|
|
48
48
|
/**
|
|
49
49
|
* The logger instance for this signal.
|
|
50
|
+
*
|
|
50
51
|
* @protected
|
|
51
52
|
*/
|
|
52
53
|
protected readonly logger_: AlwatrLogger;
|
|
53
54
|
/**
|
|
54
55
|
* A list of subscriptions to dependency signals.
|
|
56
|
+
* Used to unsubscribe from dependencies when this signal is destroyed.
|
|
57
|
+
*
|
|
55
58
|
* @private
|
|
56
59
|
*/
|
|
57
60
|
private readonly dependencySubscriptions__;
|
|
58
61
|
/**
|
|
59
62
|
* A flag to prevent concurrent executions of the effect.
|
|
63
|
+
* Avoids scheduling multiple runs within the same event loop.
|
|
64
|
+
*
|
|
60
65
|
* @private
|
|
61
66
|
*/
|
|
62
67
|
private isRunning__;
|
|
63
68
|
/**
|
|
64
69
|
* A flag indicating whether the effect has been destroyed.
|
|
70
|
+
*
|
|
65
71
|
* @private
|
|
66
72
|
*/
|
|
67
73
|
private isDestroyed__;
|
|
@@ -72,14 +78,22 @@ export declare class EffectSignal implements IEffectSignal {
|
|
|
72
78
|
* @returns `true` if the signal is destroyed, `false` otherwise.
|
|
73
79
|
*/
|
|
74
80
|
get isDestroyed(): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a new EffectSignal instance.
|
|
83
|
+
* Subscribes to all dependency signals to listen for updates.
|
|
84
|
+
*
|
|
85
|
+
* @param config_ Configuration options including dependencies, side-effect runner callback, and immediate execution flag.
|
|
86
|
+
*/
|
|
75
87
|
constructor(config_: EffectSignalConfig);
|
|
76
88
|
/**
|
|
77
89
|
* Schedules the execution of the effect's `run` function.
|
|
78
90
|
*
|
|
79
|
-
* This method batches updates using a macrotask (`delay.
|
|
91
|
+
* This method batches updates using a macrotask (`delay.nextMicrotask`) to ensure the
|
|
80
92
|
* `run` function executes only once per event loop tick, even if multiple
|
|
81
93
|
* dependencies change simultaneously.
|
|
94
|
+
*
|
|
82
95
|
* @protected
|
|
96
|
+
* @returns A promise that resolves when the execution schedules or runs.
|
|
83
97
|
*/
|
|
84
98
|
protected scheduleExecution_(): Promise<void>;
|
|
85
99
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"effect-signal.d.ts","sourceRoot":"","sources":["../../src/core/effect-signal.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"effect-signal.d.ts","sourceRoot":"","sources":["../../src/core/effect-signal.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,KAAK,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAC/D,OAAO,KAAK,EAAC,kBAAkB,EAAE,aAAa,EAAkB,MAAM,YAAY,CAAC;AAEnF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,qBAAa,YAAa,YAAW,aAAa;IAoDpC,SAAS,CAAC,OAAO,EAAE,kBAAkB;IAnDjD;;OAEG;IACH,SAAgB,IAAI,EAAE,MAAM,CAAC;IAE7B;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;IAEzC;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAAyB;IAEnE;;;;;OAKG;IACH,OAAO,CAAC,WAAW,CAAS;IAE5B;;;;OAIG;IACH,OAAO,CAAC,aAAa,CAAS;IAE9B;;;;;OAKG;IACH,IAAW,WAAW,IAAI,OAAO,CAEhC;IAED;;;;;OAKG;gBACmB,OAAO,EAAE,kBAAkB;IAsBjD;;;;;;;;;OASG;cACa,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;IAkCnD;;;;;;OAMG;IACI,OAAO,IAAI,IAAI;CAmBvB"}
|
|
@@ -31,16 +31,23 @@ import type { IBaseSignal, SignalConfig } from '../type.js';
|
|
|
31
31
|
export declare class EventSignal<T = void> extends SignalBase<T> implements IBaseSignal<T> {
|
|
32
32
|
/**
|
|
33
33
|
* The logger instance for this signal.
|
|
34
|
+
*
|
|
34
35
|
* @protected
|
|
35
36
|
*/
|
|
36
37
|
protected logger_: AlwatrLogger;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new EventSignal instance.
|
|
40
|
+
*
|
|
41
|
+
* @param config Configuration options including the unique event name and custom cleanup hooks.
|
|
42
|
+
*/
|
|
37
43
|
constructor(config: SignalConfig);
|
|
38
44
|
/**
|
|
39
|
-
* Dispatches an event with
|
|
40
|
-
*
|
|
41
|
-
*
|
|
45
|
+
* Dispatches an event with the specified payload to all active listeners.
|
|
46
|
+
*
|
|
47
|
+
* To prevent blocking of the main thread and ensure consistent execution order,
|
|
48
|
+
* the notification execution is scheduled as a microtask using `queueMicrotask`.
|
|
42
49
|
*
|
|
43
|
-
* @param payload The data to send with the event.
|
|
50
|
+
* @param payload The data payload to send with the event.
|
|
44
51
|
*/
|
|
45
52
|
dispatch(payload: T): void;
|
|
46
53
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-signal.d.ts","sourceRoot":"","sources":["../../src/core/event-signal.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,KAAK,YAAY,EAAC,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"event-signal.d.ts","sourceRoot":"","sources":["../../src/core/event-signal.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,KAAK,YAAY,EAAC,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAC5C,OAAO,KAAK,EAAC,WAAW,EAAE,YAAY,EAAC,MAAM,YAAY,CAAC;AAE1D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,qBAAa,WAAW,CAAC,CAAC,GAAG,IAAI,CAAE,SAAQ,UAAU,CAAC,CAAC,CAAE,YAAW,WAAW,CAAC,CAAC,CAAC;IAChF;;;;OAIG;IACH,SAAS,CAAC,OAAO,EAAE,YAAY,CAAC;IAEhC;;;;OAIG;gBACS,MAAM,EAAE,YAAY;IAMhC;;;;;;;OAOG;IACI,QAAQ,CAAC,OAAO,EAAE,CAAC,GAAG,IAAI;CAMlC"}
|
|
@@ -44,11 +44,15 @@ import type { PersistentStateSignalConfig } from '../type.js';
|
|
|
44
44
|
export declare class PersistentStateSignal<T> extends StateSignal<T> {
|
|
45
45
|
/**
|
|
46
46
|
* The underlying storage provider instance.
|
|
47
|
+
* Handles read, write, and schema version management under the hood.
|
|
48
|
+
*
|
|
47
49
|
* @private
|
|
48
50
|
*/
|
|
49
51
|
private readonly storageProvider__;
|
|
50
52
|
/**
|
|
51
53
|
* Debouncer to limit how often we write to localStorage.
|
|
54
|
+
* Reduces performance overhead from excessive disk writes.
|
|
55
|
+
*
|
|
52
56
|
* @private
|
|
53
57
|
*/
|
|
54
58
|
private readonly storageDebouncer__;
|
|
@@ -56,23 +60,38 @@ export declare class PersistentStateSignal<T> extends StateSignal<T> {
|
|
|
56
60
|
* The subscription to the signal's own changes to sync with storage.
|
|
57
61
|
* We subscribe to our own signal. When the value is set from anywhere,
|
|
58
62
|
* this listener will trigger and write it to localStorage.
|
|
63
|
+
*
|
|
59
64
|
* @private
|
|
60
65
|
*/
|
|
61
66
|
private readonly storageSyncSubscription__;
|
|
62
67
|
/**
|
|
63
68
|
* Listener for the browser's pagehide events to flush pending saves.
|
|
69
|
+
* Ensures that pending changes are saved before the page unloading.
|
|
70
|
+
*
|
|
64
71
|
* @private
|
|
65
72
|
*/
|
|
66
73
|
private readonly windowPageHideListener_;
|
|
67
74
|
/**
|
|
68
75
|
* Listener for the browser's pageshow events to sync from storage when restored from BFCache.
|
|
76
|
+
* Refreshes the in-memory value if retrieved from the Back/Forward Cache.
|
|
77
|
+
*
|
|
69
78
|
* @private
|
|
70
79
|
*/
|
|
71
80
|
private readonly windowPageShowListener_;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a new PersistentStateSignal instance.
|
|
83
|
+
* Restores initial value from storage if it exists, otherwise uses default initialValue.
|
|
84
|
+
* Sets up window page visibility and BFCache listeners to guarantee write flushes.
|
|
85
|
+
*
|
|
86
|
+
* @param config Configuration options including storage keys, debounce delays, schema, parse, and stringify overrides.
|
|
87
|
+
*/
|
|
72
88
|
constructor(config: PersistentStateSignalConfig<T>);
|
|
73
89
|
/**
|
|
74
90
|
* Syncs the new value to storage.
|
|
75
|
-
*
|
|
91
|
+
* Invoked automatically by the debouncer.
|
|
92
|
+
*
|
|
93
|
+
* @param newValue The new value to write to storage.
|
|
94
|
+
* @private
|
|
76
95
|
*/
|
|
77
96
|
private syncStorage__;
|
|
78
97
|
/**
|
|
@@ -81,7 +100,7 @@ export declare class PersistentStateSignal<T> extends StateSignal<T> {
|
|
|
81
100
|
*/
|
|
82
101
|
remove(): void;
|
|
83
102
|
/**
|
|
84
|
-
* Overrides the destroy method to also clean up the storage sync subscription.
|
|
103
|
+
* Overrides the destroy method to also clean up the storage sync subscription and event listeners.
|
|
85
104
|
*/
|
|
86
105
|
destroy(): void;
|
|
87
106
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persistent-state-signal.d.ts","sourceRoot":"","sources":["../../src/core/persistent-state-signal.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"persistent-state-signal.d.ts","sourceRoot":"","sources":["../../src/core/persistent-state-signal.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAC9C,OAAO,KAAK,EAAC,2BAA2B,EAAC,MAAM,YAAY,CAAC;AAG5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,qBAAa,qBAAqB,CAAC,CAAC,CAAE,SAAQ,WAAW,CAAC,CAAC,CAAC;IAC1D;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAA0B;IAE5D;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAC;IAEpC;;;;;;OAMG;IACH,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAAC;IAE3C;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAEtC;IAEF;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAQtC;IAEF;;;;;;OAMG;gBACS,MAAM,EAAE,2BAA2B,CAAC,CAAC,CAAC;IA6ClD;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IAKrB;;;OAGG;IACI,MAAM,IAAI,IAAI;IAOrB;;OAEG;IACa,OAAO,IAAI,IAAI;CAYhC"}
|
|
@@ -38,7 +38,7 @@ import type { SessionStateSignalConfig } from '../type.js';
|
|
|
38
38
|
*
|
|
39
39
|
* // Example 2: Custom state type with parse and stringify
|
|
40
40
|
* const dateSignal = new SessionStateSignal<Date>({
|
|
41
|
-
* name: '
|
|
41
|
+
* name: 'timestamp-signal',
|
|
42
42
|
* initialValue: new Date(),
|
|
43
43
|
* parse: (str: string) => new Date(str),
|
|
44
44
|
* stringify: (date: Date) => date.toISOString(),
|
|
@@ -56,35 +56,52 @@ import type { SessionStateSignalConfig } from '../type.js';
|
|
|
56
56
|
export declare class SessionStateSignal<T> extends StateSignal<T> {
|
|
57
57
|
/**
|
|
58
58
|
* The underlying session storage provider instance.
|
|
59
|
+
*
|
|
59
60
|
* @private
|
|
60
61
|
*/
|
|
61
62
|
private readonly storageProvider__;
|
|
62
63
|
/**
|
|
63
64
|
* Debouncer to limit how often we write to sessionStorage.
|
|
65
|
+
* Helps reduce synchronous overhead from session storage updates.
|
|
66
|
+
*
|
|
64
67
|
* @private
|
|
65
68
|
*/
|
|
66
69
|
private readonly storageDebouncer__;
|
|
67
70
|
/**
|
|
68
71
|
* Subscription to the signal's own changes for sessionStorage sync.
|
|
72
|
+
* Writes the value to sessionStorage after a debounce delay.
|
|
73
|
+
*
|
|
69
74
|
* @private
|
|
70
75
|
*/
|
|
71
76
|
private readonly storageSyncSubscription__;
|
|
72
77
|
/**
|
|
73
78
|
* Listener for the browser's pagehide events to flush pending saves.
|
|
79
|
+
* Ensures pending state changes are flushed to sessionStorage before leaving the page.
|
|
80
|
+
*
|
|
74
81
|
* @private
|
|
75
82
|
*/
|
|
76
83
|
private readonly windowPageHideListener__;
|
|
77
84
|
/**
|
|
78
85
|
* Listener for the browser's pageshow events to sync from storage when restored from BFCache.
|
|
86
|
+
* Ensures in-memory sync when restored from the back-forward cache.
|
|
87
|
+
*
|
|
79
88
|
* @private
|
|
80
89
|
*/
|
|
81
90
|
private readonly windowPageShowListener__;
|
|
91
|
+
/**
|
|
92
|
+
* Creates a new SessionStateSignal instance.
|
|
93
|
+
* Loads initial value from sessionStorage if found, otherwise uses config's initialValue.
|
|
94
|
+
* Sets up page hide/show lifecycle handlers.
|
|
95
|
+
*
|
|
96
|
+
* @param config Configuration options including storageKeys, custom parse/stringify, and debounce delay.
|
|
97
|
+
*/
|
|
82
98
|
constructor(config: SessionStateSignalConfig<T>);
|
|
83
99
|
/**
|
|
84
100
|
* Syncs the new value to sessionStorage.
|
|
85
101
|
* Called automatically by the debouncer after each state change.
|
|
86
102
|
*
|
|
87
103
|
* @param newValue The new value to sync.
|
|
104
|
+
* @private
|
|
88
105
|
*/
|
|
89
106
|
private syncStorage__;
|
|
90
107
|
/**
|
|
@@ -101,7 +118,7 @@ export declare class SessionStateSignal<T> extends StateSignal<T> {
|
|
|
101
118
|
*/
|
|
102
119
|
remove(): void;
|
|
103
120
|
/**
|
|
104
|
-
* Destroys the signal, flushing pending writes and cleaning up all resources.
|
|
121
|
+
* Destroys the signal, flushing pending writes and cleaning up all resources and events.
|
|
105
122
|
*
|
|
106
123
|
* Always call this when the signal is no longer needed (e.g., on component unmount)
|
|
107
124
|
* to prevent memory leaks.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-state-signal.d.ts","sourceRoot":"","sources":["../../src/core/session-state-signal.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"session-state-signal.d.ts","sourceRoot":"","sources":["../../src/core/session-state-signal.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAC9C,OAAO,KAAK,EAAC,wBAAwB,EAAC,MAAM,YAAY,CAAC;AAGzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,qBAAa,kBAAkB,CAAC,CAAC,CAAE,SAAQ,WAAW,CAAC,CAAC,CAAC;IACvD;;;;OAIG;IACH,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAA4B;IAE9D;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAC;IAEpC;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,yBAAyB,CAAC;IAE3C;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CAEvC;IAEF;;;;;OAKG;IACH,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CAQvC;IAEF;;;;;;OAMG;gBACS,MAAM,EAAE,wBAAwB,CAAC,CAAC,CAAC;IAmC/C;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IAKrB;;;;;;;;;;;OAWG;IACI,MAAM,IAAI,IAAI;IAMrB;;;;;;;;;;;OAWG;IACa,OAAO,IAAI,IAAI;CAYhC"}
|