@xaendar/signals 0.0.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/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "@xaendar/signals",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "author": "Kaitenjo",
6
+ "license": "MIT",
7
+ "description": "A library for managing signals in web applications",
8
+ "peerDependencies": {
9
+ "@xaendar/common": "^1.0.0",
10
+ "vite": "^8.0.4"
11
+ }
12
+ }
@@ -0,0 +1,180 @@
1
+ declare global {
2
+ namespace Signal {
3
+ /**
4
+ * A mutable Signal that holds a value and notifies dependents when it changes.
5
+ *
6
+ * @template T The type of the value held by this Signal.
7
+ */
8
+ class State <T = any> {
9
+ /**
10
+ * Creates a new `State` signal.
11
+ *
12
+ * @param initialValue - The initial value of the signal.
13
+ * @param options - Optional configuration:
14
+ * - `equals` — custom equality function; defaults to `Object.is`.
15
+ * - `watched` — called when the signal gains its first sink.
16
+ * - `unwatched` — called when the signal loses its last sink.
17
+ */
18
+ constructor(value: T, options?: { equals?: (a: T, b: T) => boolean, watched?: () => void, unwatched?: () => void });
19
+ /**
20
+ * Returns the current value of the signal, registering this Signal as a
21
+ * source of the innermost `Computed` currently being evaluated (if any).
22
+ *
23
+ * @throws If `frozen` is `true` — reads are forbidden while a protected
24
+ * callback (`notify`, `watched`, `unwatched`) is executing.
25
+ */
26
+ get(): T;
27
+ /**
28
+ * Updates the signal's value and propagates changes to all dependent
29
+ * sinks.
30
+ *
31
+ * If `equals(currentValue, newValue)` returns `true` the call is a no-op
32
+ * and no propagation occurs. Otherwise the value is updated, all direct
33
+ * `Computed` sinks are marked dirty, indirect ones checked, and
34
+ * each reachable `Watcher` has its `notify` callback invoked synchronously
35
+ * (with `frozen = true`).
36
+ *
37
+ * @param newValue - The new value to set.
38
+ * @throws If `frozen` is `true` — writes are forbidden while a protected
39
+ * callback is executing.
40
+ */
41
+ set(newValue: T): void;
42
+ }
43
+
44
+ /**
45
+ * A read-only Signal whose value is derived lazily from other Signals.
46
+ *
47
+ * The value is recomputed only when explicitly read and only if one or more
48
+ * of its (recursive) dependencies have changed since the last evaluation.
49
+ * The result is cached and reused until the Signal becomes stale again.
50
+ *
51
+ * @template T The type of the computed value.
52
+ */
53
+ class Computed<T = any> {
54
+ /**
55
+ * Creates a new `Computed` signal.
56
+ *
57
+ * The Signal starts in the dirty state with an uninitialised value, so
58
+ * the callback will be invoked on the first `get()`.
59
+ *
60
+ * @param computeFn - Pure function evaluated lazily to produce the value.
61
+ * @param options - Optional configuration:
62
+ * - `equals` — custom equality function; defaults to `Object.is`.
63
+ */
64
+ constructor(computeFn: () => T, options?: { equals?: (a: T, b: T) => boolean, watched?: () => void, unwatched?: () => void });
65
+ /**
66
+ * Returns the current value of this Signal, re-evaluating the callback if
67
+ * the cached value may be stale.
68
+ *
69
+ * Registers this Signal as a source of any outer `Computed` currently
70
+ * being evaluated (automatic dependency tracking).
71
+ *
72
+ * @returns The current computed value, or a boxed error object if the last
73
+ * evaluation threw.
74
+ * @throws If `frozen` is `true`.
75
+ * @throws If the Signal is in the computing state (cyclic dependency).
76
+ */
77
+ get(): T | { isError: true, value: Error };
78
+ }
79
+
80
+ namespace subtle {
81
+ /**
82
+ * Executes a function without tracking any dependencies.
83
+ * @param fn - The function to execute without tracking.
84
+ * @returns The result of the function execution.
85
+ */
86
+ function untrack<T>(fn: () => T): T;
87
+ /**
88
+ * Returns the currently active `Computed` instance being evaluated, or `null`.
89
+ * @returns The currently active `Computed` instance, or `null` if none is being evaluated.
90
+ */
91
+ function currentComputed(): Computed | null;
92
+ /**
93
+ * Returns the ordered list of all Signals which the given `Computed` or
94
+ * `Watcher` referenced during its last evaluation.
95
+ *
96
+ * - For a `Computed`, these are the Signals read inside its callback.
97
+ * - For a `Watcher`, these are the Signals it is currently watching.
98
+ *
99
+ * @param s - The `Computed` or `Watcher` to introspect.
100
+ * @returns An array of `State` and `Computed` instances.
101
+ */
102
+ function introspectSources(s: Computed | Watcher): (State | Computed)[];
103
+ /**
104
+ * Returns the direct dependents of the given Signal — Watchers that contain
105
+ * it, plus any `Computed` Signals which read it during their last evaluation
106
+ * (if that `Computed` is recursively watched).
107
+ *
108
+ * @param signal - The `State` or `Computed` Signal to introspect.
109
+ * @returns An array of `Computed` and `Watcher` instances.
110
+ */
111
+ function introspectSinks(signal: State | Computed): (Computed | Watcher)[];
112
+ /**
113
+ * Returns `true` if the given Signal is 'live' — i.e. it is watched by a
114
+ * `Watcher`, or it is read by a `Computed` Signal which is (recursively)
115
+ * live.
116
+ *
117
+ * @param signal - The `State` or `Computed` Signal to check.
118
+ * @returns `true` if the Signal has at least one sink.
119
+ */
120
+ function hasSinks(signal: State | Computed): boolean;
121
+ /**
122
+ * Returns `true` if the given node is 'reactive' — i.e. it depends on some
123
+ * other Signal. A `Computed` where `hasSources` is `false` will always
124
+ * return the same constant.
125
+ *
126
+ * @param s - The `Computed` or `Watcher` to check.
127
+ * @returns `true` if the node has at least one source.
128
+ */
129
+ function hasSources(s: Computed | Watcher): boolean;
130
+
131
+ /**
132
+ * A `Watcher` observes a set of Signals and fires a `notify` callback
133
+ * synchronously when any of their (recursive) dependencies change.
134
+ *
135
+ * It is the low-level primitive on top of which frameworks implement
136
+ * effects and scheduling. It does not hold a value and has no generic
137
+ * type parameter.
138
+ */
139
+ class Watcher {
140
+ /**
141
+ * Creates a new Watcher.
142
+ *
143
+ * The Watcher starts in the waiting state with an empty signals set.
144
+ *
145
+ * @param notify - Called synchronously (with `frozen = true`) the
146
+ * first time a watched dependency changes after each `watch` call. No
147
+ * Signals may be read or written inside this callback.
148
+ */
149
+ constructor(notify: () => void);
150
+ /**
151
+ * Adds the given Signals to the watched set and transitions the Watcher to
152
+ * the watching state.
153
+ *
154
+ * For each newly-watched Signal, the Watcher is registered as a sink and —
155
+ * if it is the first sink — the sink registration is propagated recursively
156
+ * up through the Signal's sources, building the live dependency chain.
157
+ *
158
+ * @param signals - One or more Signals to start watching.
159
+ * @throws If `frozen` is `true` at the time of the call.
160
+ */
161
+ watch(...signals: (State | Computed)[]): void;
162
+ /**
163
+ * Removes the given Signals from the watched set.
164
+ *
165
+ * For each removed Signal, the Watcher is unregistered as a sink. If the
166
+ * Signal's sink set becomes empty as a result, the removal is propagated
167
+ * recursively up through its sources, tearing down the live dependency
168
+ * chain and allowing garbage collection of unwatched nodes.
169
+ *
170
+ * @param signals - One or more Signals to stop watching.
171
+ * @throws If `frozen` is `true` at the time of the call.
172
+ * @throws If any of the given Signals is not currently being watched.
173
+ */
174
+ unwatch(...signals: (State | Computed)[]): void;
175
+ }
176
+ }
177
+ }
178
+ }
179
+
180
+ export {};
@@ -0,0 +1,45 @@
1
+ import { Stack } from '@xaendar/common';
2
+ import { Computed } from './models/computed';
3
+ import { GlobalState } from './types/global-state.type';
4
+
5
+ /**
6
+ * Global state for the signals runtime.
7
+ * Tracks the currently computing `Computed` instance, whether the state is frozen,
8
+ * and the current generation counter.
9
+ *
10
+ * @internal
11
+ */
12
+ export const GLOBAL_STATE: GlobalState = {
13
+ computing: null,
14
+ frozen: false
15
+ };
16
+
17
+ const computingStack = new Stack<Computed>;
18
+
19
+ /**
20
+ * Pushes a `Computed` instance onto the computing stack and sets it as the
21
+ * currently active computation in `GLOBAL_STATE`.
22
+ *
23
+ * Should be called before executing a computed function to register
24
+ * dependency tracking.
25
+ *
26
+ * @param computed - The `Computed` instance entering the computation phase.
27
+ * @internal
28
+ */
29
+ export function pushComputed(computed: Computed): void {
30
+ computingStack.push(computed);
31
+ GLOBAL_STATE.computing = computed;
32
+ }
33
+
34
+ /**
35
+ * Pops the most recent `Computed` instance from the computing stack and
36
+ * restores the previous one as the active computation in `GLOBAL_STATE`.
37
+ *
38
+ * Should be called after a computed function finishes executing.
39
+ *
40
+ * @internal
41
+ */
42
+ export function popComputed(): void {
43
+ computingStack.pop();
44
+ GLOBAL_STATE.computing = computingStack[computingStack.length - 1] ?? null;
45
+ }
@@ -0,0 +1,36 @@
1
+ import { Computed } from './models/computed';
2
+ import { State } from './models/state';
3
+ import { Watcher } from './models/watcher';
4
+ import { currentComputed, hasSinks, hasSources, introspectSinks, introspectSources, untrack } from './subtle';
5
+
6
+ /**
7
+ * Loads the Signals library by defining the `Signal` global object with the following properties:
8
+ * - `State`: The `State` class for creating reactive state variables.
9
+ * - `Computed`: The `Computed` class for creating derived reactive values.
10
+ * - `Watcher`: The `Watcher` class for observing changes in signals.
11
+ * - `subtle`: An object containing internal utility functions for working with signals, including:
12
+ * - `untrack`: Executes a function without tracking dependencies.
13
+ * - `currentComputed`: Returns the currently active `Computed` instance being evaluated, or `null` if none.
14
+ * - `introspectSources`: Returns the list of sources for a given `Computed` or `Watcher`.
15
+ * - `introspectSinks`: Returns the list of sinks for a given `State` or `Computed`.
16
+ * - `hasSinks`: Returns `true` if a given `State` or `Computed` has at least one sink.
17
+ * - `hasSources`: Returns `true` if a given `Computed` or `Watcher` has at least one source.
18
+ * - `Watcher`: The `Watcher` class for observing changes in signals.
19
+ *
20
+ * This function should be called once to initialize the Signals library and make its API available globally.
21
+ */
22
+ export function loadSignals(): void {
23
+ globalThis.Signal ??= {
24
+ State,
25
+ Computed,
26
+ subtle: {
27
+ untrack,
28
+ currentComputed,
29
+ introspectSources: introspectSources as unknown as typeof Signal.subtle.introspectSources,
30
+ introspectSinks: introspectSinks as unknown as typeof Signal.subtle.introspectSinks,
31
+ hasSinks: hasSinks as unknown as typeof Signal.subtle.hasSinks,
32
+ hasSources: hasSources as unknown as typeof Signal.subtle.hasSources,
33
+ Watcher
34
+ }
35
+ }
36
+ }
@@ -0,0 +1,421 @@
1
+
2
+ import { GLOBAL_STATE, pushComputed, popComputed } from '../globals';
3
+ import { PRIVATE, assertPrivateContext } from '../private-symbol';
4
+ import { ComputedState } from '../types/computed-state.type';
5
+ import { SignalEqual } from '../types/signal-equal.type';
6
+ import { SignalOptions } from '../types/signal-options.type';
7
+ import { State } from './state';
8
+ import { Watcher } from './watcher';
9
+
10
+ /**
11
+ * A read-only Signal whose value is derived lazily from other Signals.
12
+ *
13
+ * The value is recomputed only when explicitly read and only if one or more
14
+ * of its (recursive) dependencies have changed since the last evaluation.
15
+ * The result is cached and reused until the Signal becomes stale again.
16
+ *
17
+ * @template T The type of the computed value.
18
+ *
19
+ * @see Signal algorithms — "The Signal.Computed class"
20
+ */
21
+ export class Computed<T = any> {
22
+ /**
23
+ * The current value of the signal.
24
+ *
25
+ * Uninitialised (`!`) until the first evaluation. After that, holds either
26
+ * the return value of `#callback` or a boxed error
27
+ * `{ isError: true; value: Error }` if the last evaluation threw.
28
+ *
29
+ * @internalSlot
30
+ * @see Signal algorithms — "Signal.Computed internal slots"
31
+ */
32
+ #value!: T | { isError: true; value: Error };
33
+ /**
34
+ * The current evaluation state of this Signal.
35
+ *
36
+ * - `~dirty~` — value is known to be stale or has never been evaluated.
37
+ * - `~checked~` — an indirect source changed; may or may not be stale.
38
+ * - `~computing~` — `#callback` is currently executing; guards against cycles.
39
+ * - `~clean~` — cached value is up-to-date.
40
+ *
41
+ * @internalSlot
42
+ * @see Signal algorithms — "Signal.Computed State machine"
43
+ */
44
+ #state: ComputedState;
45
+ /**
46
+ * The ordered set of Signals read during the last evaluation of
47
+ * `#callback`. Cleared and rebuilt on every re-evaluation so that
48
+ * conditional branches that are no longer taken stop being tracked.
49
+ *
50
+ * May contain both `State` and `Computed` instances.
51
+ *
52
+ * @internalSlot
53
+ * @see Signal algorithms — "Signal.Computed internal slots"
54
+ */
55
+ #sources: Set<State<unknown> | Computed<unknown>>;
56
+ /**
57
+ * Returns a snapshot of the current sources set for introspection.
58
+ *
59
+ * @param symbol - Private access symbol; rejects calls from outside the library.
60
+ * @returns An array of `State` and `Computed` instances that this Signal depends on.
61
+ * @internal
62
+ */
63
+ public getSources(symbol: symbol): (State<unknown> | Computed<unknown>)[] {
64
+ assertPrivateContext(symbol);
65
+ return [...this.#sources];
66
+ }
67
+ /**
68
+ * The set of Signals and Watchers that directly depend on this Signal.
69
+ *
70
+ * Populated only when this Signal is reachable from at least one active
71
+ * `Watcher`. An un-watched `Computed` has an empty sinks set, which allows
72
+ * it to be garbage-collected independently from the rest of the graph.
73
+ *
74
+ * @internalSlot
75
+ * @see Signal algorithms — "Signal.Computed internal slots"
76
+ * @see Method — `Signal.Computed.prototype.get` (NOTE on sinks)
77
+ */
78
+ #sinks: Set<Computed<unknown> | Watcher>;
79
+ /**
80
+ * Returns a snapshot of the current sinks set for introspection.
81
+ *
82
+ * @param symbol - Private access symbol; rejects calls from outside the library.
83
+ * @returns An array of `Computed` and `Watcher` instances that depend on this Signal.
84
+ * @internal
85
+ */
86
+ public getSinks(symbol: symbol): (Computed<unknown> | Watcher)[] {
87
+ assertPrivateContext(symbol);
88
+ return [...this.#sinks];
89
+ }
90
+ /**
91
+ * The equality function used to determine whether a newly computed value
92
+ * is meaningfully different from the previously cached one.
93
+ *
94
+ * Called as `equals.call(computed, oldValue, newValue)`. Returns `true` if
95
+ * the values are considered equal, in which case no downstream propagation
96
+ * occurs. Defaults to `Object.is` when not provided via options.
97
+ *
98
+ * If this function throws, the exception is cached as the Signal's value
99
+ * and the outcome is treated as `~dirty~`.
100
+ *
101
+ * @internalSlot
102
+ * @see Signal algorithms — "Signal.Computed internal slots"
103
+ * @see Algorithm — "Set Signal value"
104
+ */
105
+ #equals: SignalEqual<T>;
106
+ /**
107
+ * The pure function that produces this Signal's value. Evaluated lazily
108
+ * whenever the Signal is read while in a `~dirty~` or `~checked~` state.
109
+ *
110
+ * Called with `this` bound to the `Computed` instance itself so that
111
+ * internal methods (e.g. `addSource`) are accessible if needed.
112
+ * Any exception thrown by this function is caught and cached.
113
+ *
114
+ * @internalSlot
115
+ * @see Signal algorithms — "Signal.Computed internal slots"
116
+ */
117
+ #callback: (this: Computed<T>) => T;
118
+
119
+ /**
120
+ * Creates a new `Computed` signal.
121
+ *
122
+ * The Signal starts in the `~dirty~` state with an uninitialised value, so
123
+ * `#callback` will be invoked on the first `get()`.
124
+ *
125
+ * @param cb - Pure function evaluated lazily to produce the value.
126
+ * Receives the `Computed` instance as `this`.
127
+ * @param options - Optional configuration:
128
+ * - `equals` — custom equality function; defaults to `Object.is`.
129
+ *
130
+ * @see Signal algorithms — "Signal.Computed Constructor"
131
+ */
132
+ constructor(cb: (this: Computed<T>) => T, options?: SignalOptions<T>) {
133
+ this.#callback = cb;
134
+ this.#equals = options?.equals ?? Object.is;
135
+ this.#sources = new Set;
136
+ this.#sinks = new Set;
137
+ this.#state = 'dirty';
138
+ }
139
+
140
+ /**
141
+ * Returns the current value of this Signal, re-evaluating `#callback` if
142
+ * the cached value may be stale.
143
+ *
144
+ * Registers this Signal as a source of any outer `Computed` currently
145
+ * being evaluated (automatic dependency tracking).
146
+ *
147
+ * If the state is `~dirty~` or `~checked~`, walks the source graph
148
+ * depth-first to find and recalculate the deepest stale `Computed` first,
149
+ * then re-checks upward until this Signal is `~clean~`.
150
+ *
151
+ * @returns The current computed value, or a boxed error object if the last
152
+ * evaluation threw.
153
+ * @throws If `frozen` is `true`.
154
+ * @throws If the Signal is in the `~computing~` state (cyclic dependency).
155
+ *
156
+ * @see Signal algorithms — "Method: Signal.Computed.prototype.get"
157
+ */
158
+ public get(): T | { isError: true; value: Error } {
159
+ if (GLOBAL_STATE.frozen) {
160
+ throw new Error('Cannot get value of a Computed signal while the global state is frozen');
161
+ }
162
+
163
+ if (this.#state === 'computing') {
164
+ throw new Error('Circular dependency detected while computing a Computed signal');
165
+ }
166
+
167
+ GLOBAL_STATE.computing?.addSource(this, PRIVATE);
168
+
169
+ if (this.#sinks.size === 0) {
170
+ this.#computeValue();
171
+ } else if (this.#state === 'dirty' || this.#state === 'checked') {
172
+ while (this.#state === 'dirty' || this.#state === 'checked') {
173
+ const deepest = this.#findDeepestStale();
174
+ deepest.#computeValue();
175
+ }
176
+ }
177
+
178
+ return this.#value;
179
+ }
180
+
181
+ /**
182
+ * Registers a Signal as a source of this `Computed`, discovered during the
183
+ * execution of `#callback`.
184
+ *
185
+ * If this `Computed` is currently being watched (has at least one sink),
186
+ * the source is also informed of this Signal as a new sink, building the
187
+ * live push-notification chain upward.
188
+ *
189
+ * @param source - The Signal read during evaluation.
190
+ * @param symbol - Private access symbol; rejects calls from outside the library.
191
+ * @internal
192
+ */
193
+ public addSource(source: State | Computed, symbol: symbol) {
194
+ assertPrivateContext(symbol);
195
+ this.#sources.add(source);
196
+ source.addSink(this, PRIVATE)
197
+ }
198
+
199
+ /**
200
+ * Returns the current evaluation state of this Signal.
201
+ *
202
+ * @param symbol - Private access symbol; rejects calls from outside the library.
203
+ * @internal
204
+ * @see Signal algorithms — "Signal.Computed State machine"
205
+ */
206
+ public getState(symbol: symbol): ComputedState {
207
+ assertPrivateContext(symbol);
208
+ return this.#state;
209
+ }
210
+
211
+ /**
212
+ * Transitions this Signal to a new evaluation state.
213
+ *
214
+ * Only valid transitions (as defined by the state machine) are allowed.
215
+ * Invalid transitions throw an error.
216
+ *
217
+ * @param newState - The target state.
218
+ * @param symbol - Private access symbol; rejects calls from outside the library.
219
+ * @throws If the transition from the current state to `newState` is not allowed.
220
+ * @internal
221
+ * @see Signal algorithms — "Signal.Computed State machine"
222
+ */
223
+ public setState(newState: ComputedState, symbol: symbol): void {
224
+ assertPrivateContext(symbol);
225
+
226
+ if (this.#state !== newState && this.#isValidTransition(this.#state, newState)) {
227
+ this.#state = newState;
228
+ }
229
+ }
230
+
231
+ /**
232
+ * Registers a new sink (a `Computed` or `Watcher` that directly depends on
233
+ * this Signal) in the internal sinks set.
234
+ *
235
+ * If this is the first sink, propagates the sink registration recursively
236
+ * up through `#sources`, building the live dependency chain that enables
237
+ * push-based invalidation.
238
+ *
239
+ * @param sink - The dependent node to register.
240
+ * @param symbol - Private access symbol; rejects calls from outside the library.
241
+ * @internal
242
+ */
243
+ public addSink(sink: Computed<unknown> | Watcher, symbol: symbol) {
244
+ assertPrivateContext(symbol);
245
+ if (this.#sinks.size === 0) {
246
+ this.#sources.forEach(source => source.addSink(this, PRIVATE));
247
+ }
248
+ this.#sinks.add(sink);
249
+ }
250
+
251
+ /**
252
+ * Removes a sink from the internal sinks set.
253
+ *
254
+ * If the sinks set becomes empty after removal, propagates the removal
255
+ * recursively up through `#sources`, tearing down the live dependency
256
+ * chain and allowing garbage collection of un-watched nodes.
257
+ *
258
+ * @param sink - The dependent node to remove.
259
+ * @param symbol - Private access symbol; rejects calls from outside the library.
260
+ * @internal
261
+ */
262
+ public removeSink(sink: Computed | Watcher, symbol: symbol) {
263
+ assertPrivateContext(symbol);
264
+ this.#sinks.delete(sink);
265
+
266
+ if (this.#sinks.size === 0) {
267
+ this.#sources.forEach(source => source.removeSink(this, PRIVATE));
268
+ }
269
+ }
270
+
271
+ /**
272
+ * Recursively walks the source graph depth-first to find the deepest,
273
+ * left-most `Computed` node that is in a `~dirty~` or `~checked~` state.
274
+ *
275
+ * This ensures that recalculation always starts from the bottom of the
276
+ * dependency graph, so every node sees already-updated dependencies —
277
+ * the core of glitch-free evaluation.
278
+ *
279
+ * Cuts off the search when hitting a `~clean~` `Computed` source, since
280
+ * its subtree is guaranteed to be up-to-date.
281
+ *
282
+ * @returns The deepest stale `Computed` found, or `node` itself if none of
283
+ * its sources are stale.
284
+ */
285
+ #findDeepestStale(): Computed {
286
+ const unclearNode = [...this.#sources].find((source): source is Computed => source instanceof Computed && (source.#state === 'dirty' || source.#state === 'checked'));
287
+ return unclearNode ? unclearNode.#findDeepestStale() : this;
288
+ }
289
+
290
+ /**
291
+ * Executes `#callback` to recompute this Signal's value.
292
+ *
293
+ * Implements the "recalculate dirty computed Signal" algorithm:
294
+ * 1. Clears stale sources and removes this Signal from their sinks.
295
+ * 2. Sets `computing` to this Signal for automatic dependency tracking.
296
+ * 3. Runs the callback, caching the return value or any thrown exception.
297
+ * 4. Restores the previous `computing` value.
298
+ * 5. Runs the "set Signal value" algorithm to detect value changes.
299
+ * 6. Transitions state to `~clean~`.
300
+ * 7. Propagates `~dirty~` to sinks (or attempts `~clean~` if value unchanged).
301
+ *
302
+ * @see Signal algorithms — "Algorithm: recalculate dirty computed Signal"
303
+ */
304
+ #computeValue(): void {
305
+ this.#sources.forEach(source => source.removeSink(this, PRIVATE));
306
+ this.#sources.clear();
307
+
308
+ pushComputed(this);
309
+ this.#state = 'computing';
310
+
311
+ let newValue: T | { isError: true; value: Error };
312
+
313
+ try {
314
+ newValue = this.#callback.call(this);
315
+ } catch (error) {
316
+ newValue = { isError: true, value: error as Error };
317
+ } finally {
318
+ popComputed();
319
+ }
320
+
321
+ const outcome = this.#setValue(newValue);
322
+
323
+ outcome === 'dirty'
324
+ ? this.#sinks.forEach(sink => sink instanceof Computed ? sink.setState('dirty', PRIVATE) : sink.notify(PRIVATE))
325
+ : this.#propagateClean();
326
+
327
+ this.#state = 'clean';
328
+ }
329
+
330
+ /**
331
+ * Implements the "set Signal value" algorithm.
332
+ *
333
+ * Compares the new value against the cached one using `#equals`. If equal,
334
+ * returns `~clean~` and leaves `#value` untouched. Otherwise updates
335
+ * `#value` and returns `~dirty~`.
336
+ *
337
+ * Special cases:
338
+ * - If `newValue` is a boxed error, `#equals` is skipped and the error is
339
+ * cached directly.
340
+ * - If `#equals` itself throws, the exception is cached as a boxed error
341
+ * and the outcome is `~dirty~`.
342
+ *
343
+ * @param newValue - The value (or boxed error) produced by `#callback`.
344
+ * @returns `~clean~` if the value is unchanged, `~dirty~` otherwise.
345
+ *
346
+ * @see Signal algorithms — "Set Signal value algorithm"
347
+ */
348
+ #setValue(newValue: T | { isError: true; value: Error }): 'clean' | 'dirty' {
349
+ const oldValue = this.#value;
350
+
351
+ /*
352
+ If new value is an error we always update without calling equals
353
+ */
354
+ if (this.#isErrorValue(newValue)) {
355
+ this.#value = newValue;
356
+ return 'dirty';
357
+ }
358
+
359
+ try {
360
+ if (!this.#isErrorValue(oldValue) && this.#equals.call(this, oldValue, newValue)) {
361
+ return 'clean';
362
+ }
363
+ } catch (equalsError) {
364
+ this.#value = { isError: true, value: equalsError as Error };
365
+ return 'dirty';
366
+ }
367
+
368
+ this.#value = newValue;
369
+ return 'dirty';
370
+ }
371
+
372
+ /**
373
+ * Recursively marks `~checked~` sinks as `~clean~` when all of their
374
+ * immediate sources are already `~clean~`.
375
+ *
376
+ * Called after a recalculation that produced an unchanged value (`~clean~`
377
+ * outcome from `#setValue`). Propagates the clean signal upward through
378
+ * the graph so that Computed nodes that were only transitively dirty — and
379
+ * whose dependencies have not actually changed — are not needlessly
380
+ * re-evaluated on the next read.
381
+ *
382
+ * @see Signal algorithms — "Algorithm: recalculate dirty computed Signal"
383
+ */
384
+ #propagateClean(): void {
385
+ [...this.#sinks]
386
+ .filter((sink): sink is Computed<unknown> => sink instanceof Computed && sink.#state === 'checked')
387
+ .forEach(sink => {
388
+ const allSourcesClean = [...sink.#sources].every(source => !(source instanceof Computed) || source.#state === 'clean');
389
+ if (allSourcesClean) {
390
+ sink.#state = 'clean';
391
+ sink.#propagateClean();
392
+ }
393
+ });
394
+ }
395
+
396
+ /**
397
+ * Type guard that checks whether a value is a boxed error object.
398
+ *
399
+ * Used to distinguish a legitimately computed value from a cached
400
+ * exception produced by `#callback` or `#equals`.
401
+ *
402
+ * @param value - The value to inspect.
403
+ * @returns `true` if `value` is `{ isError: true; value: Error }`.
404
+ */
405
+ #isErrorValue(value: unknown): value is { isError: true; value: Error } {
406
+ return typeof value === 'object' && !!value && 'isError' in value;
407
+ }
408
+
409
+ #isValidTransition(from: ComputedState, to: ComputedState): boolean {
410
+ switch (from) {
411
+ case 'checked':
412
+ return to === 'clean' || to === 'dirty';
413
+ case 'clean':
414
+ return to === 'checked' || to === 'dirty';
415
+ case 'dirty':
416
+ return to === 'computing';
417
+ case 'computing':
418
+ return to === 'clean';
419
+ }
420
+ }
421
+ }
@@ -0,0 +1,208 @@
1
+ import { NoArgsVoidFunction } from '@xaendar/common';
2
+ import { GLOBAL_STATE } from '../globals';
3
+ import { PRIVATE, assertPrivateContext } from '../private-symbol';
4
+ import { SignalEqual } from '../types/signal-equal.type';
5
+ import { SignalOptions } from '../types/signal-options.type';
6
+ import { Computed } from './computed';
7
+ import { Watcher } from './watcher';
8
+
9
+ export class State<T = any> {
10
+ /**
11
+ * The current value of the signal.
12
+ *
13
+ * Initialised to `initialValue` in the constructor and updated by `set`
14
+ * whenever the new value is not equal to the current one according to
15
+ * `#equals`.
16
+ *
17
+ * @internalSlot
18
+ * @see Signal algorithms — 'Signal.State internal slots'
19
+ */
20
+ #value: T;
21
+ /**
22
+ * The equality function used to determine whether a new value is
23
+ * meaningfully different from the current one.
24
+ *
25
+ * Called as `equals.call(signal, oldValue, newValue)`. If it returns
26
+ * `true` the signal is considered unchanged and no propagation occurs.
27
+ * Defaults to `Object.is` when not provided via options.
28
+ *
29
+ * @internalSlot
30
+ * @see Signal algorithms — 'Signal.State internal slots'
31
+ * @see Algorithm — 'Set Signal value'
32
+ */
33
+ #equals: SignalEqual<T>
34
+ /**
35
+ * Optional callback invoked (with `frozen = true`) the first time this
36
+ * Signal gains a sink — i.e. when it transitions from un-observed to
37
+ * observed by at least one `Watcher` (directly or transitively).
38
+ *
39
+ * @internalSlot
40
+ * @see Signal algorithms — 'Signal.State internal slots'
41
+ * @see Method — `Signal.subtle.Watcher.prototype.watch`
42
+ */
43
+ #watched?: NoArgsVoidFunction;
44
+ /**
45
+ * Optional callback invoked (with `frozen = true`) when this Signal loses
46
+ * its last sink — i.e. when it transitions from observed back to
47
+ * un-observed.
48
+ *
49
+ * @internalSlot
50
+ * @see Signal algorithms — 'Signal.State internal slots'
51
+ * @see Method — `Signal.subtle.Watcher.prototype.unwatch`
52
+ */
53
+ #unwatched?: NoArgsVoidFunction;
54
+ /**
55
+ * The set of watched signals that directly depend on this one.
56
+ *
57
+ * Populated only when this Signal is reachable from at least one active
58
+ * `Watcher` — un-watched Signals have an empty sinks set, which allows
59
+ * them to be garbage-collected independently from the rest of the graph.
60
+ *
61
+ * Should contain both `Computed` and `Watcher` instances, as both can be
62
+ * direct dependents of a `State`.
63
+ *
64
+ * @internalSlot
65
+ * @see Signal algorithms — 'Signal.State internal slots'
66
+ * @see Method — `Signal.State.prototype.get` (NOTE on sinks)
67
+ */
68
+ #sinks: Set<Computed<unknown> | Watcher>;
69
+ /**
70
+ * Returns a snapshot of the current sinks set for introspection.
71
+ *
72
+ * @param symbol - Private access symbol; rejects calls from outside the library.
73
+ * @returns An array of `Computed` and `Watcher` instances that depend on this Signal.
74
+ * @internal
75
+ */
76
+ public getSinks(symbol: symbol): (Computed<unknown> | Watcher)[] {
77
+ assertPrivateContext(symbol);
78
+ return [...this.#sinks];
79
+ }
80
+
81
+ /**
82
+ * Creates a new `State` signal.
83
+ *
84
+ * @param initialValue - The initial value of the signal.
85
+ * @param options - Optional configuration:
86
+ * - `equals` — custom equality function; defaults to `Object.is`.
87
+ * - `watched` — called when the signal gains its first sink.
88
+ * - `unwatched` — called when the signal loses its last sink.
89
+ *
90
+ * @see Signal algorithms — 'Constructor: Signal.State(initialValue, options)'
91
+ */
92
+ constructor(initialValue: T, options?: SignalOptions<T>) {
93
+ this.#value = initialValue;
94
+ this.#equals = options?.equals ?? Object.is;
95
+ this.#watched = options?.watched;
96
+ this.#unwatched = options?.unwatched;
97
+ this.#sinks = new Set;
98
+ }
99
+
100
+ /**
101
+ * Returns the current value of the signal, registering this Signal as a
102
+ * source of the innermost `Computed` currently being evaluated (if any).
103
+ *
104
+ * @throws If `frozen` is `true` — reads are forbidden while a protected
105
+ * callback (`notify`, `watched`, `unwatched`) is executing.
106
+ *
107
+ * @see Signal algorithms — 'Method: Signal.State.prototype.get()'
108
+ */
109
+ public get(): T {
110
+ if (GLOBAL_STATE.frozen) {
111
+ throw new Error('Cannot get value while signals are frozen');
112
+ }
113
+
114
+ /*
115
+ If there is a currently computing `Computed`, register this `State` as a
116
+ source of that `Computed`.
117
+ This is how the dependency graph is built.
118
+
119
+ THis is done every time a `State` is read to guarantee always up-to-date
120
+ tracking of dependencies, even if they change between computations
121
+ */
122
+ GLOBAL_STATE.computing?.addSource(this, PRIVATE)
123
+
124
+ return this.#value;
125
+ }
126
+
127
+ /**
128
+ * Updates the signal's value and propagates changes to all dependent
129
+ * sinks.
130
+ *
131
+ * If `equals(currentValue, newValue)` returns `true` the call is a no-op
132
+ * and no propagation occurs. Otherwise `#value` is updated, all direct
133
+ * `Computed` sinks are marked `~dirty~`, indirect ones `~checked~`, and
134
+ * each reachable `Watcher` has its `notify` callback invoked synchronously
135
+ * (with `frozen = true`).
136
+ *
137
+ * @param newValue - The new value to set.
138
+ * @throws If `frozen` is `true` — writes are forbidden while a protected
139
+ * callback is executing.
140
+ *
141
+ * @see Signal algorithms — 'Method: Signal.State.prototype.set(newValue)'
142
+ * @see Algorithm — 'Set Signal value'
143
+ */
144
+ public set(newValue: T): void {
145
+ if (GLOBAL_STATE.frozen) {
146
+ throw new Error('Cannot set value while signals are frozen');
147
+ }
148
+
149
+ if (!this.#equals.call(this, this.#value, newValue)) {
150
+ this.#value = newValue;
151
+ this.#sinks.forEach(sink => sink instanceof Computed ? sink.setState('dirty', PRIVATE) : sink.notify(PRIVATE));
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Registers a new sink (a `Computed` or `Watcher` that depends on this
157
+ * Signal) in the internal sinks set.
158
+ *
159
+ * Called by `Watcher.prototype.watch` when building the live dependency
160
+ * chain, and by `Computed` when propagating sink registration up through
161
+ * its sources.
162
+ *
163
+ * @param sink - The dependent node to register.
164
+ * @param symbol - The private symbol for validation.
165
+ * @internal
166
+ */
167
+ public addSink(sink: Computed | Watcher, symbol: symbol): void {
168
+ assertPrivateContext(symbol);
169
+ const empty = this.#sinks.size === 0;
170
+ this.#sinks.add(sink);
171
+
172
+ if (empty && this.#watched) {
173
+ GLOBAL_STATE.frozen = true;
174
+ try {
175
+ this.#watched();
176
+ } finally {
177
+ GLOBAL_STATE.frozen = false;
178
+ }
179
+ }
180
+ }
181
+
182
+ /**
183
+ * Removes a sink from the internal sinks set.
184
+ *
185
+ * Called by `Watcher.prototype.unwatch` when tearing down the live
186
+ * dependency chain. If the sinks set becomes empty after removal, the
187
+ * caller is responsible for propagating the removal up through this
188
+ * Signal's sources.
189
+ *
190
+ * @param sink - The dependent node to remove.
191
+ * @param symbol - The private symbol for validation.
192
+ * @internal
193
+ */
194
+ public removeSink(sink: Computed | Watcher, symbol: symbol): void {
195
+ assertPrivateContext(symbol);
196
+ this.#sinks.delete(sink);
197
+ const empty = this.#sinks.size === 0;
198
+
199
+ if (empty && this.#unwatched) {
200
+ GLOBAL_STATE.frozen = true;
201
+ try {
202
+ this.#unwatched();
203
+ } finally {
204
+ GLOBAL_STATE.frozen = false;
205
+ }
206
+ }
207
+ }
208
+ }
@@ -0,0 +1,229 @@
1
+ import { NoArgsVoidFunction } from '@xaendar/common';
2
+ import { GLOBAL_STATE } from '../globals';
3
+ import { PRIVATE, assertPrivateContext } from '../private-symbol';
4
+ import { WatcherState } from '../types/watcher-state.type';
5
+ import { Computed } from './computed';
6
+ import { State } from './state';
7
+
8
+ /**
9
+ * A `Watcher` observes a set of Signals and fires a `notify` callback
10
+ * synchronously when any of their (recursive) dependencies change.
11
+ *
12
+ * It is the low-level primitive on top of which frameworks implement
13
+ * effects and scheduling. It does not hold a value and has no generic
14
+ * type parameter.
15
+ *
16
+ * @see Signal algorithms — 'The `Signal.subtle.Watcher` class'
17
+ */
18
+ export class Watcher {
19
+ /**
20
+ * The current state of the Watcher.
21
+ *
22
+ * - `~waiting~` — newly created, or `notify` has already been called since
23
+ * the last `watch` call. Not actively observing changes.
24
+ * - `~watching~` — actively watching; no dependency has changed yet.
25
+ * - `~pending~` — a dependency has changed but `notify` has not yet run.
26
+ *
27
+ * @internalSlot
28
+ * @see Signal algorithms — 'Signal.subtle.Watcher State machine'
29
+ */
30
+ #state: WatcherState;
31
+ /**
32
+ * The ordered set of Signals this Watcher is currently watching.
33
+ * May contain both `State` and `Computed` instances.
34
+ *
35
+ * @internalSlot
36
+ * @see Signal algorithms — 'Signal.subtle.Watcher internal slots'
37
+ */
38
+ #signals: Set<State<unknown> | Computed<unknown>>;
39
+ /**
40
+ * Returns a snapshot of the current watched signals set for introspection.
41
+ *
42
+ * @param symbol - Private access symbol; rejects calls from outside the library.
43
+ * @returns An array of `State` and `Computed` instances that this Watcher is watching.
44
+ * @internal
45
+ */
46
+ public getSources(symbol: symbol): (State<unknown> | Computed<unknown>)[] {
47
+ assertPrivateContext(symbol);
48
+ return [...this.#signals];
49
+ }
50
+ /**
51
+ * The callback invoked synchronously when a watched Signal (or one of its
52
+ * recursive dependencies) changes for the first time since the last
53
+ * `watch` call.
54
+ *
55
+ * Receives the Watcher itself as `this`. No Signals may be read or written
56
+ * during its execution (`frozen` is `true` for its entire duration).
57
+ *
58
+ * @internalSlot
59
+ * @see Signal algorithms — 'Signal.subtle.Watcher internal slots'
60
+ */
61
+ #notifyCallback: NoArgsVoidFunction;
62
+
63
+ /**
64
+ * Creates a new Watcher.
65
+ *
66
+ * The Watcher starts in the `~waiting~` state with an empty signals set.
67
+ *
68
+ * @param notifyCallback - Called synchronously (with `frozen = true`) the
69
+ * first time a watched dependency changes after each `watch` call. No
70
+ * Signals may be read or written inside this callback.
71
+ *
72
+ * @see Signal algorithms — 'Constructor: new Signal.subtle.Watcher(callback)'
73
+ */
74
+ constructor(notifyCallback: NoArgsVoidFunction) {
75
+ this.#state = 'waiting';
76
+ this.#signals = new Set;
77
+ this.#notifyCallback = notifyCallback;
78
+ }
79
+
80
+ /**
81
+ * Returns the subset of watched Signals that are `Computed` instances
82
+ * currently in a `~dirty~` or `~checked~` state, meaning they may have a
83
+ * stale value that has not yet been re-evaluated.
84
+ *
85
+ * Typically called inside the microtask scheduled by the `notify` callback
86
+ * to know which Signals need to be pulled.
87
+ *
88
+ * @returns An array of `Computed` signals that are dirty or checked.
89
+ *
90
+ * @see Signal algorithms — 'Method: Signal.subtle.Watcher.prototype.getPending()'
91
+ */
92
+ public getPending(): Computed<unknown>[] {
93
+ return [...this.#signals].filter((signal): signal is Computed<unknown> => {
94
+ if (!(signal instanceof Computed)) {
95
+ return false;
96
+ }
97
+
98
+ const state = signal.getState(PRIVATE);
99
+ return state === 'dirty' || state === 'checked';
100
+ });
101
+ }
102
+
103
+ /**
104
+ * Adds the given Signals to the watched set and transitions the Watcher to
105
+ * the `~watching~` state.
106
+ *
107
+ * For each newly-watched Signal, the Watcher is registered as a sink and —
108
+ * if it is the first sink — the sink registration is propagated recursively
109
+ * up through the Signal's sources, building the live dependency chain.
110
+ *
111
+ * The `watched` callback of each Signal (if any) is called with
112
+ * `frozen = true`.
113
+ *
114
+ * @param signals - One or more `State` signals to start watching.
115
+ * @throws If `frozen` is `true` at the time of the call.
116
+ *
117
+ * @see Signal algorithms — 'Method: Signal.subtle.Watcher.prototype.watch(...signals)'
118
+ */
119
+ public watch(...signals: (State | Computed)[]) {
120
+ if (GLOBAL_STATE.frozen) {
121
+ throw new Error('Cannot watch signals while frozen');
122
+ }
123
+
124
+ signals.forEach(signal => {
125
+ if (this.#signals.has(signal)) {
126
+ throw new Error('Cannot watch a signal that is already being watched');
127
+ }
128
+
129
+ this.#signals.add(signal)
130
+ signal.addSink(this, PRIVATE);
131
+ });
132
+
133
+
134
+ if (this.#state === 'waiting') {
135
+ this.#state = 'watching';
136
+ }
137
+ }
138
+
139
+ /**
140
+ * Removes the given Signals from the watched set.
141
+ *
142
+ * For each removed Signal, the Watcher is unregistered as a sink. If the
143
+ * Signal's sink set becomes empty as a result, the removal is propagated
144
+ * recursively up through its sources, tearing down the live dependency
145
+ * chain and allowing garbage collection of unwatched nodes.
146
+ *
147
+ * The `unwatched` callback of each Signal (if any) is called with
148
+ * `frozen = true`.
149
+ *
150
+ * If no Signals remain in the watched set, the Watcher transitions back to
151
+ * the `~waiting~` state.
152
+ *
153
+ * @param signals - One or more `State` signals to stop watching.
154
+ * @throws If `frozen` is `true` at the time of the call.
155
+ * @throws If any of the given Signals is not currently being watched.
156
+ *
157
+ * @see Signal algorithms — 'Method: Signal.subtle.Watcher.prototype.unwatch(...signals)'
158
+ */
159
+ public unwatch(...signals: (State | Computed)[]) {
160
+ if (GLOBAL_STATE.frozen) {
161
+ throw new Error('Cannot unwatch signals while frozen');
162
+ }
163
+
164
+ signals.forEach(signal => {
165
+ if (this.#signals.has(signal)) {
166
+ throw new Error('Cannot unwatch a signal that is not being watched');
167
+ }
168
+
169
+ this.#signals.delete(signal)
170
+ signal.removeSink(this, PRIVATE);
171
+ });
172
+
173
+ if (!this.#signals.size && this.#state === 'watching') {
174
+ this.#state = 'waiting';
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Get the current state of the Watcher.
180
+ * @param symbol - The private symbol for prevent external calls.
181
+ */
182
+ public getState(symbol: symbol): WatcherState {
183
+ assertPrivateContext(symbol);
184
+ return this.#state;
185
+ }
186
+
187
+ /**
188
+ * Set the current state of the Watcher.
189
+ * @param newState - The new state to set.
190
+ * @param symbol - The private symbol for prevent external calls.
191
+ * @throws If the transition from `pending` to `watching` is attempted.
192
+ */
193
+ public setState(newState: WatcherState, symbol: symbol): void {
194
+ assertPrivateContext(symbol);
195
+
196
+ if (this.#state !== newState && this.#isValidTransition(this.#state, newState)) {
197
+ this.#state = newState;
198
+ }
199
+ }
200
+
201
+ /**
202
+ * Invoce the notify callback when a watched dependency changes
203
+ * @param symbol - The private symbol for prevent external calls.
204
+ */
205
+ public notify(symbol: symbol): void {
206
+ assertPrivateContext(symbol);
207
+
208
+ this.#state = 'pending';
209
+
210
+ GLOBAL_STATE.frozen = true;
211
+ try {
212
+ this.#notifyCallback.call(this);
213
+ } finally {
214
+ this.#state = 'waiting';
215
+ GLOBAL_STATE.frozen = false;
216
+ }
217
+ }
218
+
219
+ #isValidTransition(from: WatcherState, to: WatcherState): boolean {
220
+ switch (from) {
221
+ case 'waiting':
222
+ return to === 'watching';
223
+ case 'watching':
224
+ return to === 'pending' || to === 'waiting';
225
+ case 'pending':
226
+ return to === 'waiting';
227
+ }
228
+ }
229
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Symbol not available in public API,
3
+ * used to call internal methods of `State` and `Computed` from `Watcher` without exposing them in the public API.
4
+ *
5
+ * @internal
6
+ */
7
+ export const PRIVATE = Symbol('signals-private');
8
+
9
+ /**
10
+ * Asserts that the provided symbol matches the internal {@link PRIVATE} symbol,
11
+ * ensuring the caller has access to internal APIs.
12
+ *
13
+ * Throws if the symbol does not match, preventing external code from
14
+ * invoking methods intended for internal use only.
15
+ *
16
+ * @param symbol - The symbol to validate against {@link PRIVATE}.
17
+ * @throws {Error} If `symbol` does not match {@link PRIVATE}.
18
+ * @internal
19
+ */
20
+ export function assertPrivateContext(symbol: symbol): void {
21
+ if (symbol !== PRIVATE) {
22
+ throw new Error('Invalid symbol');
23
+ }
24
+ }
@@ -0,0 +1,79 @@
1
+ import { GLOBAL_STATE } from './globals';
2
+ import { Computed } from './models/computed';
3
+ import { State } from './models/state';
4
+ import { Watcher } from './models/watcher';
5
+ import { PRIVATE } from './private-symbol';
6
+
7
+ /**
8
+ * Executes a function without tracking any dependencies.
9
+ * @param fn - The function to execute without tracking.
10
+ * @returns The result of the function execution.
11
+ */
12
+ export function untrack<T>(fn: () => T): T {
13
+ const prevComputing = GLOBAL_STATE.computing;
14
+ GLOBAL_STATE.computing = null;
15
+
16
+ try {
17
+ return fn();
18
+ } finally {
19
+ GLOBAL_STATE.computing = prevComputing;
20
+ }
21
+ }
22
+
23
+ /**
24
+ * Returns the currently active `Computed` instance being evaluated, or `null`
25
+ * @returns The currently active `Computed` instance, or `null` if none is being evaluated.
26
+ */
27
+ export function currentComputed(): Computed | null {
28
+ return GLOBAL_STATE.computing;
29
+ }
30
+
31
+ /**
32
+ * Returns the ordered list of all Signals which the given `Computed` or
33
+ * `Watcher` referenced during its last evaluation.
34
+ *
35
+ * - For a `Computed`, these are the Signals read inside its callback.
36
+ * - For a `Watcher`, these are the Signals it is currently watching.
37
+ *
38
+ * @param s - The `Computed` or `Watcher` to introspect.
39
+ * @returns An array of `State` and `Computed` instances.
40
+ */
41
+ export function introspectSources(s: Computed | Watcher): (State | Computed)[] {
42
+ return s.getSources(PRIVATE);
43
+ }
44
+
45
+ /**
46
+ * Returns the direct dependents of the given Signal — Watchers that contain
47
+ * it, plus any `Computed` Signals which read it during their last evaluation
48
+ * (if that `Computed` is recursively watched).
49
+ *
50
+ * @param signal - The `State` or `Computed` Signal to introspect.
51
+ * @returns An array of `Computed` and `Watcher` instances.
52
+ */
53
+ export function introspectSinks(signal: State | Computed): (Computed | Watcher)[] {
54
+ return signal.getSinks(PRIVATE);
55
+ }
56
+
57
+ /**
58
+ * Returns `true` if the given Signal is 'live' — i.e. it is watched by a
59
+ * `Watcher`, or it is read by a `Computed` Signal which is (recursively)
60
+ * live.
61
+ *
62
+ * @param signal - The `State` or `Computed` Signal to check.
63
+ * @returns `true` if the Signal has at least one sink.
64
+ */
65
+ export function hasSinks(signal: State | Computed): boolean {
66
+ return signal.getSinks(PRIVATE).length > 0;
67
+ }
68
+
69
+ /**
70
+ * Returns `true` if the given node is 'reactive' — i.e. it depends on some
71
+ * other Signal. A `Computed` where `hasSources` is `false` will always
72
+ * return the same constant.
73
+ *
74
+ * @param signal - The `Computed` or `Watcher` to check.
75
+ * @returns `true` if the node has at least one source.
76
+ */
77
+ export function hasSources(signal: Computed | Watcher): boolean {
78
+ return signal.getSources(PRIVATE).length > 0;
79
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Rapresents the state of a computed signal.
3
+ * The state can be one of the following:
4
+ * - `dirty`: the computed signal is dirty and needs to be recomputed.
5
+ * - `checked`: the computed signal is checked and needs to be recomputed.
6
+ * - `computing`: the computed signal is currently being computed.
7
+ * - `clean`: the computed signal is clean and does not need to be recomputed.
8
+ */
9
+ export type ComputedState = 'dirty' | 'checked' | 'computing' | 'clean';
@@ -0,0 +1,42 @@
1
+ import { Computed } from '../models/computed';
2
+
3
+ export type GlobalState = {
4
+ /**
5
+ * The innermost `Signal.Computed` (or effect Signal) currently being
6
+ * re-evaluated as a side-effect of a `.get()` call.
7
+ *
8
+ * All Signal reads that occur while this is non-null will register
9
+ * themselves as sources of this computed, enabling automatic dependency
10
+ * tracking.
11
+ *
12
+ * Set to `null` when no computed is being evaluated (i.e. a 'root' read).
13
+ *
14
+ * @see Signal algorithms — 'Hidden global state'
15
+ */
16
+ computing: Computed | null;
17
+ /**
18
+ * Whether a callback is currently executing that requires the Signal graph
19
+ * to remain unmodified for the duration of its execution.
20
+ *
21
+ * When `true`, any attempt to read or write a Signal will throw an exception.
22
+ * This prevents the graph from being mutated while it is being traversed,
23
+ * which would risk exposing inconsistent or half-processed state.
24
+ *
25
+ * Set to `true` immediately before invoking:
26
+ * - The `notify` callback of a `Signal.subtle.Watcher` (during `State.prototype.set`)
27
+ * - The `watched` callback (during `Watcher.prototype.watch`)
28
+ * - The `unwatched` callback (during `Watcher.prototype.unwatch`)
29
+ *
30
+ * Restored to `false` in a `finally` block after each such callback returns
31
+ * (or throws), guaranteeing the flag is never left permanently set.
32
+ *
33
+ * Note: `Signal.subtle.untrack` does NOT clear this flag — frozen is always
34
+ * respected regardless of tracking context.
35
+ *
36
+ * @see Signal algorithms — 'Hidden global state'
37
+ * @see Method: `Signal.State.prototype.set`
38
+ * @see Method: `Signal.subtle.Watcher.prototype.watch`
39
+ * @see Method: `Signal.subtle.Watcher.prototype.unwatch`
40
+ */
41
+ frozen: boolean;
42
+ };
@@ -0,0 +1,8 @@
1
+ import { Computed } from "../models/computed";
2
+ import { State } from "../models/state";
3
+
4
+ /**
5
+ * A function that compares two values of type `T` and returns `true` if they are considered equal, or `false` otherwise.
6
+ * This function is used to determine if a signal's value has changed and if dependent computations need to be re-evaluated.
7
+ */
8
+ export type SignalEqual<T> = (this: State<T> | Computed<T>, t: T, t2: T) => boolean;
@@ -0,0 +1,19 @@
1
+ import { Computed } from '../models/computed';
2
+ import { State } from '../models/state';
3
+ import { SignalEqual } from './signal-equal.type';
4
+
5
+ export type SignalOptions<T> = {
6
+ /**
7
+ * Custom comparison function between old and new value. Default: Object.is.
8
+ * The signal is passed in as the this value for context.
9
+ */
10
+ equals?: SignalEqual<T>;
11
+ /**
12
+ * Callback called when isWatched becomes true, if it was previously false
13
+ */
14
+ watched?: (this: State<T> | Computed<T>) => void;
15
+ /**
16
+ * Callback called whenever isWatched becomes false, if it was previously true
17
+ */
18
+ unwatched?: (this: State<T> | Computed<T>) => void;
19
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Type of the state of a watcher.
3
+ * - `waiting`: The watcher is waiting for its dependencies to change.
4
+ * - `watching`: The watcher is currently watching its dependencies for changes.
5
+ * - `pending`: The watcher has been notified of a change and is pending re-evaluation.
6
+ */
7
+ export type WatcherState = 'waiting' | 'watching' | 'pending';
@@ -0,0 +1 @@
1
+ export * from './lib/load-signals';
package/tsconfig.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { readFileSync, writeFileSync } from 'node:fs';
2
+ import { defineConfig } from 'vite';
3
+ import getViteConfig from '../../vite-config';
4
+
5
+ export default defineConfig(getViteConfig('@xaendar/signals', __dirname, {
6
+ plugins: [
7
+ {
8
+ name: 'types',
9
+ writeBundle() {
10
+ const content = readFileSync('../packages/signals/src/globals.d.ts', 'utf-8');
11
+ const dtsContent = readFileSync('../dist/@xaendar/signals/xaendar-signals.es.d.ts', 'utf-8');
12
+ writeFileSync('../dist/@xaendar/signals/xaendar-signals.es.d.ts', `${content}\n\n${dtsContent}`);
13
+ }
14
+ }
15
+ ]
16
+ }));