@xaendar/signals 0.9.22 → 0.9.23
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/dist/xaendar-signals.d.ts +0 -194
- package/dist/xaendar-signals.js +883 -887
- package/package.json +3 -3
package/dist/xaendar-signals.js
CHANGED
|
@@ -1,964 +1,960 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { Stack } from "@xaendar/common";
|
|
2
|
+
//#region ../packages/signals/src/lib/utils/globals/globals.ts
|
|
3
|
+
/**
|
|
4
|
+
* Global state for the signals runtime.
|
|
5
|
+
* Tracks the currently computing `Computed` instance, whether the state is frozen,
|
|
6
|
+
* and the current generation counter.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
var GLOBAL_STATE = {
|
|
11
|
+
computing: null,
|
|
12
|
+
frozen: false
|
|
13
|
+
};
|
|
14
|
+
var computingStack = new Stack();
|
|
15
|
+
/**
|
|
16
|
+
* Pushes a `Computed` instance onto the computing stack and sets it as the
|
|
17
|
+
* currently active computation in `GLOBAL_STATE`.
|
|
18
|
+
*
|
|
19
|
+
* Should be called before executing a computed function to register
|
|
20
|
+
* dependency tracking.
|
|
21
|
+
*
|
|
22
|
+
* @param computed - The `Computed` instance entering the computation phase.
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
function pushComputed(computed) {
|
|
26
|
+
computingStack.push(computed);
|
|
27
|
+
GLOBAL_STATE.computing = computed;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Pops the most recent `Computed` instance from the computing stack and
|
|
31
|
+
* restores the previous one as the active computation in `GLOBAL_STATE`.
|
|
32
|
+
*
|
|
33
|
+
* Should be called after a computed function finishes executing.
|
|
34
|
+
*
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
function popComputed() {
|
|
38
|
+
computingStack.pop();
|
|
39
|
+
GLOBAL_STATE.computing = computingStack[computingStack.length - 1] ?? null;
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region ../packages/signals/src/lib/utils/private-symbol/private-symbol.ts
|
|
43
|
+
/**
|
|
44
|
+
* Symbol not available in public API,
|
|
45
|
+
* used to call internal methods of `State` and `Computed` from `Watcher` without exposing them in the public API.
|
|
46
|
+
*
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
var PRIVATE = Symbol("signals-private");
|
|
50
|
+
/**
|
|
51
|
+
* Asserts that the provided symbol matches the internal {@link PRIVATE} symbol,
|
|
52
|
+
* ensuring the caller has access to internal APIs.
|
|
53
|
+
*
|
|
54
|
+
* Throws if the symbol does not match, preventing external code from
|
|
55
|
+
* invoking methods intended for internal use only.
|
|
56
|
+
*
|
|
57
|
+
* @param symbol - The symbol to validate against {@link PRIVATE}.
|
|
58
|
+
* @throws {Error} If `symbol` does not match {@link PRIVATE}.
|
|
59
|
+
* @internal
|
|
60
|
+
*/
|
|
61
|
+
function assertPrivateContext(symbol) {
|
|
62
|
+
if (symbol !== PRIVATE) throw new Error("Invalid symbol");
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region ../packages/signals/src/lib/utils/dev-mode/dev-mode.ts
|
|
66
|
+
/**
|
|
67
|
+
* Whether the library is runnign in development mode
|
|
68
|
+
* Used to log extra information for example
|
|
69
|
+
* - Invalid transition states for {@link Computed} or {@link Watcher}
|
|
70
|
+
*/
|
|
71
|
+
var devMode = false;
|
|
72
|
+
/**
|
|
73
|
+
* Sets the development mode flag.
|
|
74
|
+
* @param mode - `true` to enable dev mode, `false` to disable it.
|
|
75
|
+
*/
|
|
76
|
+
function setDevMode(mode) {
|
|
77
|
+
devMode = mode;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Returns the current development mode state.
|
|
81
|
+
* @returns `true` if dev mode is enabled, `false` otherwise.
|
|
82
|
+
*/
|
|
83
|
+
function isDevMode() {
|
|
84
|
+
return devMode;
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region ../packages/signals/src/lib/models/computed/computed.ts
|
|
88
|
+
/**
|
|
89
|
+
* A read-only Signal whose value is derived lazily from other Signals.
|
|
90
|
+
*
|
|
91
|
+
* The value is recomputed only when explicitly read and only if one or more
|
|
92
|
+
* of its (recursive) dependencies have changed since the last evaluation.
|
|
93
|
+
* The result is cached and reused until the Signal becomes stale again.
|
|
94
|
+
*
|
|
95
|
+
* @template T The type of the computed value.
|
|
96
|
+
*
|
|
97
|
+
* @see Signal algorithms — "The Signal.Computed class"
|
|
98
|
+
*/
|
|
99
|
+
var Computed = class Computed {
|
|
6
100
|
/**
|
|
7
|
-
*
|
|
8
|
-
* Tracks the currently computing `Computed` instance, whether the state is frozen,
|
|
9
|
-
* and the current generation counter.
|
|
101
|
+
* The current value of the signal.
|
|
10
102
|
*
|
|
103
|
+
* Uninitialised (`!`) until the first evaluation. After that, holds either
|
|
104
|
+
* the return value of `#callback` or a boxed error
|
|
105
|
+
* `{ isError: true; value: Error }` if the last evaluation threw.
|
|
106
|
+
*
|
|
107
|
+
* @internalSlot
|
|
108
|
+
* @see Signal algorithms — "Signal.Computed internal slots"
|
|
109
|
+
*/
|
|
110
|
+
#value;
|
|
111
|
+
/**
|
|
112
|
+
* The current evaluation state of this Signal.
|
|
113
|
+
*
|
|
114
|
+
* - `~dirty~` — value is known to be stale or has never been evaluated.
|
|
115
|
+
* - `~checked~` — an indirect source changed; may or may not be stale.
|
|
116
|
+
* - `~computing~` — `#callback` is currently executing; guards against cycles.
|
|
117
|
+
* - `~clean~` — cached value is up-to-date.
|
|
118
|
+
*
|
|
119
|
+
* @internalSlot
|
|
120
|
+
* @see Signal algorithms — "Signal.Computed State machine"
|
|
121
|
+
*/
|
|
122
|
+
#state;
|
|
123
|
+
/**
|
|
124
|
+
* The ordered set of Signals read during the last evaluation of
|
|
125
|
+
* `#callback`. Cleared and rebuilt on every re-evaluation so that
|
|
126
|
+
* conditional branches that are no longer taken stop being tracked.
|
|
127
|
+
*
|
|
128
|
+
* May contain both `State` and `Computed` instances.
|
|
129
|
+
*
|
|
130
|
+
* @internalSlot
|
|
131
|
+
* @see Signal algorithms — "Signal.Computed internal slots"
|
|
132
|
+
*/
|
|
133
|
+
#sources;
|
|
134
|
+
/**
|
|
135
|
+
* Returns a snapshot of the current sources set for introspection.
|
|
136
|
+
*
|
|
137
|
+
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
138
|
+
* @returns An array of `State` and `Computed` instances that this Signal depends on.
|
|
11
139
|
* @internal
|
|
12
140
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
var computingStack = new _xaendar_common.Stack();
|
|
141
|
+
getSources(symbol) {
|
|
142
|
+
assertPrivateContext(symbol);
|
|
143
|
+
return [...this.#sources];
|
|
144
|
+
}
|
|
18
145
|
/**
|
|
19
|
-
*
|
|
20
|
-
* currently active computation in `GLOBAL_STATE`.
|
|
146
|
+
* The set of Signals and Watchers that directly depend on this Signal.
|
|
21
147
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
148
|
+
* Populated only when this Signal is reachable from at least one active
|
|
149
|
+
* `Watcher`. An un-watched `Computed` has an empty sinks set, which allows
|
|
150
|
+
* it to be garbage-collected independently from the rest of the graph.
|
|
24
151
|
*
|
|
25
|
-
* @
|
|
152
|
+
* @internalSlot
|
|
153
|
+
* @see Signal algorithms — "Signal.Computed internal slots"
|
|
154
|
+
* @see Method — `Signal.Computed.prototype.get` (NOTE on sinks)
|
|
155
|
+
*/
|
|
156
|
+
#sinks;
|
|
157
|
+
/**
|
|
158
|
+
* Returns a snapshot of the current sinks set for introspection.
|
|
159
|
+
*
|
|
160
|
+
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
161
|
+
* @returns An array of `Computed` and `Watcher` instances that depend on this Signal.
|
|
26
162
|
* @internal
|
|
27
163
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
164
|
+
getSinks(symbol) {
|
|
165
|
+
assertPrivateContext(symbol);
|
|
166
|
+
return [...this.#sinks];
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The equality function used to determine whether a newly computed value
|
|
170
|
+
* is meaningfully different from the previously cached one.
|
|
171
|
+
*
|
|
172
|
+
* Called as `equals.call(computed, oldValue, newValue)`. Returns `true` if
|
|
173
|
+
* the values are considered equal, in which case no downstream propagation
|
|
174
|
+
* occurs. Defaults to `Object.is` when not provided via options.
|
|
175
|
+
*
|
|
176
|
+
* If this function throws, the exception is cached as the Signal's value
|
|
177
|
+
* and the outcome is treated as `~dirty~`.
|
|
178
|
+
*
|
|
179
|
+
* @internalSlot
|
|
180
|
+
* @see Signal algorithms — "Signal.Computed internal slots"
|
|
181
|
+
* @see Algorithm — "Set Signal value"
|
|
182
|
+
*/
|
|
183
|
+
#equals;
|
|
184
|
+
/**
|
|
185
|
+
* The pure function that produces this Signal's value. Evaluated lazily
|
|
186
|
+
* whenever the Signal is read while in a `~dirty~` or `~checked~` state.
|
|
187
|
+
*
|
|
188
|
+
* Called with `this` bound to the `Computed` instance itself so that
|
|
189
|
+
* internal methods (e.g. `addSource`) are accessible if needed.
|
|
190
|
+
* Any exception thrown by this function is caught and cached.
|
|
191
|
+
*
|
|
192
|
+
* @internalSlot
|
|
193
|
+
* @see Signal algorithms — "Signal.Computed internal slots"
|
|
194
|
+
*/
|
|
195
|
+
#callback;
|
|
196
|
+
/**
|
|
197
|
+
* Creates a new `Computed` signal.
|
|
198
|
+
*
|
|
199
|
+
* The Signal starts in the `~dirty~` state with an uninitialised value, so
|
|
200
|
+
* `#callback` will be invoked on the first `get()`.
|
|
201
|
+
*
|
|
202
|
+
* @param cb - Pure function evaluated lazily to produce the value.
|
|
203
|
+
* Receives the `Computed` instance as `this`.
|
|
204
|
+
* @param options - Optional configuration:
|
|
205
|
+
* - `equals` — custom equality function; defaults to `Object.is`.
|
|
206
|
+
*
|
|
207
|
+
* @see Signal algorithms — "Signal.Computed Constructor"
|
|
208
|
+
*/
|
|
209
|
+
constructor(cb, options) {
|
|
210
|
+
this.#callback = cb;
|
|
211
|
+
this.#equals = options?.equals ?? Object.is;
|
|
212
|
+
this.#sources = /* @__PURE__ */ new Set();
|
|
213
|
+
this.#sinks = /* @__PURE__ */ new Set();
|
|
214
|
+
this.#state = "dirty";
|
|
31
215
|
}
|
|
32
216
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
217
|
+
* Returns the current value of this Signal, re-evaluating `#callback` if
|
|
218
|
+
* the cached value may be stale.
|
|
219
|
+
*
|
|
220
|
+
* Registers this Signal as a source of any outer `Computed` currently
|
|
221
|
+
* being evaluated (automatic dependency tracking).
|
|
35
222
|
*
|
|
36
|
-
*
|
|
223
|
+
* If the state is `~dirty~` or `~checked~`, walks the source graph
|
|
224
|
+
* depth-first to find and recalculate the deepest stale `Computed` first,
|
|
225
|
+
* then re-checks upward until this Signal is `~clean~`.
|
|
37
226
|
*
|
|
227
|
+
* @returns The current computed value, or a boxed error object if the last
|
|
228
|
+
* evaluation threw.
|
|
229
|
+
* @throws If `frozen` is `true`.
|
|
230
|
+
* @throws If the Signal is in the `~computing~` state (cyclic dependency).
|
|
231
|
+
*
|
|
232
|
+
* @see Signal algorithms — "Method: Signal.Computed.prototype.get"
|
|
233
|
+
*/
|
|
234
|
+
get() {
|
|
235
|
+
if (GLOBAL_STATE.frozen) throw new Error("Cannot get value of a Computed signal while the global state is frozen");
|
|
236
|
+
if (this.#state === "computing") throw new Error("Circular dependency detected while computing a Computed signal");
|
|
237
|
+
GLOBAL_STATE.computing?.addSource(this, PRIVATE);
|
|
238
|
+
if (this.#sinks.size === 0) this.#computeValue();
|
|
239
|
+
else if (this.#state === "dirty" || this.#state === "checked") while (this.#state === "dirty" || this.#state === "checked") this.#findDeepestStale().#computeValue();
|
|
240
|
+
return this.#value;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Registers a Signal as a source of this `Computed`, discovered during the
|
|
244
|
+
* execution of `#callback`.
|
|
245
|
+
*
|
|
246
|
+
* If this `Computed` is currently being watched (has at least one sink),
|
|
247
|
+
* the source is also informed of this Signal as a new sink, building the
|
|
248
|
+
* live push-notification chain upward.
|
|
249
|
+
*
|
|
250
|
+
* @param source - The Signal read during evaluation.
|
|
251
|
+
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
38
252
|
* @internal
|
|
39
253
|
*/
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
254
|
+
addSource(source, symbol) {
|
|
255
|
+
assertPrivateContext(symbol);
|
|
256
|
+
this.#sources.add(source);
|
|
257
|
+
source.addSink(this, PRIVATE);
|
|
43
258
|
}
|
|
44
|
-
//#endregion
|
|
45
|
-
//#region ../packages/signals/src/lib/utils/private-symbol/private-symbol.ts
|
|
46
259
|
/**
|
|
47
|
-
*
|
|
48
|
-
* used to call internal methods of `State` and `Computed` from `Watcher` without exposing them in the public API.
|
|
260
|
+
* Returns the current evaluation state of this Signal.
|
|
49
261
|
*
|
|
262
|
+
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
50
263
|
* @internal
|
|
264
|
+
* @see Signal algorithms — "Signal.Computed State machine"
|
|
51
265
|
*/
|
|
52
|
-
|
|
266
|
+
getState(symbol) {
|
|
267
|
+
assertPrivateContext(symbol);
|
|
268
|
+
return this.#state;
|
|
269
|
+
}
|
|
53
270
|
/**
|
|
54
|
-
*
|
|
55
|
-
* ensuring the caller has access to internal APIs.
|
|
271
|
+
* Transitions this Signal to a new evaluation state.
|
|
56
272
|
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
273
|
+
* Only valid transitions (as defined by the state machine) are allowed.
|
|
274
|
+
* Invalid transitions throw an error.
|
|
59
275
|
*
|
|
60
|
-
* @param
|
|
61
|
-
* @
|
|
276
|
+
* @param newState - The target state.
|
|
277
|
+
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
278
|
+
* @throws If the transition from the current state to `newState` is not allowed.
|
|
62
279
|
* @internal
|
|
280
|
+
* @see Signal algorithms — "Signal.Computed State machine"
|
|
63
281
|
*/
|
|
64
|
-
|
|
65
|
-
|
|
282
|
+
setState(newState, symbol) {
|
|
283
|
+
assertPrivateContext(symbol);
|
|
284
|
+
if (this.#state === newState) return;
|
|
285
|
+
if (!this.#isValidTransition(this.#state, newState)) {
|
|
286
|
+
if (isDevMode()) {
|
|
287
|
+
console.warn(`Invalid state transition from ${this.#state} to ${newState} in Computed Signal`);
|
|
288
|
+
console.warn((/* @__PURE__ */ new Error()).stack);
|
|
289
|
+
}
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
this.#state = newState;
|
|
293
|
+
if (this.#state === "dirty" || this.#state === "checked") for (const sink of this.#sinks) sink instanceof Computed ? sink.setState("checked", PRIVATE) : sink.notify(PRIVATE);
|
|
66
294
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
295
|
+
/**
|
|
296
|
+
* Registers a new sink (a `Computed` or `Watcher` that directly depends on
|
|
297
|
+
* this Signal) in the internal sinks set.
|
|
298
|
+
*
|
|
299
|
+
* If this is the first sink, propagates the sink registration recursively
|
|
300
|
+
* up through `#sources`, building the live dependency chain that enables
|
|
301
|
+
* push-based invalidation.
|
|
302
|
+
*
|
|
303
|
+
* @param sink - The dependent node to register.
|
|
304
|
+
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
305
|
+
* @internal
|
|
73
306
|
*/
|
|
74
|
-
|
|
307
|
+
addSink(sink, symbol) {
|
|
308
|
+
assertPrivateContext(symbol);
|
|
309
|
+
if (this.#sinks.size === 0) for (const source of this.#sources) source.addSink(this, PRIVATE);
|
|
310
|
+
this.#sinks.add(sink);
|
|
311
|
+
}
|
|
75
312
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
313
|
+
* Removes a sink from the internal sinks set.
|
|
314
|
+
*
|
|
315
|
+
* If the sinks set becomes empty after removal, propagates the removal
|
|
316
|
+
* recursively up through `#sources`, tearing down the live dependency
|
|
317
|
+
* chain and allowing garbage collection of un-watched nodes.
|
|
318
|
+
*
|
|
319
|
+
* @param sink - The dependent node to remove.
|
|
320
|
+
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
321
|
+
* @internal
|
|
78
322
|
*/
|
|
79
|
-
|
|
80
|
-
|
|
323
|
+
removeSink(sink, symbol) {
|
|
324
|
+
assertPrivateContext(symbol);
|
|
325
|
+
this.#sinks.delete(sink);
|
|
326
|
+
if (this.#sinks.size === 0) for (const source of this.#sources) source.removeSink(this, PRIVATE);
|
|
81
327
|
}
|
|
82
328
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
329
|
+
* Recursively walks the source graph depth-first to find the deepest,
|
|
330
|
+
* left-most `Computed` node that is in a `~dirty~` or `~checked~` state.
|
|
331
|
+
*
|
|
332
|
+
* This ensures that recalculation always starts from the bottom of the
|
|
333
|
+
* dependency graph, so every node sees already-updated dependencies —
|
|
334
|
+
* the core of glitch-free evaluation.
|
|
335
|
+
*
|
|
336
|
+
* Cuts off the search when hitting a `~clean~` `Computed` source, since
|
|
337
|
+
* its subtree is guaranteed to be up-to-date.
|
|
338
|
+
*
|
|
339
|
+
* @returns The deepest stale `Computed` found, or `node` itself if none of
|
|
340
|
+
* its sources are stale.
|
|
85
341
|
*/
|
|
86
|
-
|
|
87
|
-
|
|
342
|
+
#findDeepestStale() {
|
|
343
|
+
const unclearNode = [...this.#sources].find((source) => source instanceof Computed && (source.#state === "dirty" || source.#state === "checked"));
|
|
344
|
+
return unclearNode ? unclearNode.#findDeepestStale() : this;
|
|
88
345
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
* - `~computing~` — `#callback` is currently executing; guards against cycles.
|
|
120
|
-
* - `~clean~` — cached value is up-to-date.
|
|
121
|
-
*
|
|
122
|
-
* @internalSlot
|
|
123
|
-
* @see Signal algorithms — "Signal.Computed State machine"
|
|
124
|
-
*/
|
|
125
|
-
#state;
|
|
126
|
-
/**
|
|
127
|
-
* The ordered set of Signals read during the last evaluation of
|
|
128
|
-
* `#callback`. Cleared and rebuilt on every re-evaluation so that
|
|
129
|
-
* conditional branches that are no longer taken stop being tracked.
|
|
130
|
-
*
|
|
131
|
-
* May contain both `State` and `Computed` instances.
|
|
132
|
-
*
|
|
133
|
-
* @internalSlot
|
|
134
|
-
* @see Signal algorithms — "Signal.Computed internal slots"
|
|
135
|
-
*/
|
|
136
|
-
#sources;
|
|
137
|
-
/**
|
|
138
|
-
* Returns a snapshot of the current sources set for introspection.
|
|
139
|
-
*
|
|
140
|
-
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
141
|
-
* @returns An array of `State` and `Computed` instances that this Signal depends on.
|
|
142
|
-
* @internal
|
|
143
|
-
*/
|
|
144
|
-
getSources(symbol) {
|
|
145
|
-
assertPrivateContext(symbol);
|
|
146
|
-
return [...this.#sources];
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* The set of Signals and Watchers that directly depend on this Signal.
|
|
150
|
-
*
|
|
151
|
-
* Populated only when this Signal is reachable from at least one active
|
|
152
|
-
* `Watcher`. An un-watched `Computed` has an empty sinks set, which allows
|
|
153
|
-
* it to be garbage-collected independently from the rest of the graph.
|
|
154
|
-
*
|
|
155
|
-
* @internalSlot
|
|
156
|
-
* @see Signal algorithms — "Signal.Computed internal slots"
|
|
157
|
-
* @see Method — `Signal.Computed.prototype.get` (NOTE on sinks)
|
|
158
|
-
*/
|
|
159
|
-
#sinks;
|
|
160
|
-
/**
|
|
161
|
-
* Returns a snapshot of the current sinks set for introspection.
|
|
162
|
-
*
|
|
163
|
-
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
164
|
-
* @returns An array of `Computed` and `Watcher` instances that depend on this Signal.
|
|
165
|
-
* @internal
|
|
166
|
-
*/
|
|
167
|
-
getSinks(symbol) {
|
|
168
|
-
assertPrivateContext(symbol);
|
|
169
|
-
return [...this.#sinks];
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* The equality function used to determine whether a newly computed value
|
|
173
|
-
* is meaningfully different from the previously cached one.
|
|
174
|
-
*
|
|
175
|
-
* Called as `equals.call(computed, oldValue, newValue)`. Returns `true` if
|
|
176
|
-
* the values are considered equal, in which case no downstream propagation
|
|
177
|
-
* occurs. Defaults to `Object.is` when not provided via options.
|
|
178
|
-
*
|
|
179
|
-
* If this function throws, the exception is cached as the Signal's value
|
|
180
|
-
* and the outcome is treated as `~dirty~`.
|
|
181
|
-
*
|
|
182
|
-
* @internalSlot
|
|
183
|
-
* @see Signal algorithms — "Signal.Computed internal slots"
|
|
184
|
-
* @see Algorithm — "Set Signal value"
|
|
185
|
-
*/
|
|
186
|
-
#equals;
|
|
187
|
-
/**
|
|
188
|
-
* The pure function that produces this Signal's value. Evaluated lazily
|
|
189
|
-
* whenever the Signal is read while in a `~dirty~` or `~checked~` state.
|
|
190
|
-
*
|
|
191
|
-
* Called with `this` bound to the `Computed` instance itself so that
|
|
192
|
-
* internal methods (e.g. `addSource`) are accessible if needed.
|
|
193
|
-
* Any exception thrown by this function is caught and cached.
|
|
194
|
-
*
|
|
195
|
-
* @internalSlot
|
|
196
|
-
* @see Signal algorithms — "Signal.Computed internal slots"
|
|
197
|
-
*/
|
|
198
|
-
#callback;
|
|
199
|
-
/**
|
|
200
|
-
* Creates a new `Computed` signal.
|
|
201
|
-
*
|
|
202
|
-
* The Signal starts in the `~dirty~` state with an uninitialised value, so
|
|
203
|
-
* `#callback` will be invoked on the first `get()`.
|
|
204
|
-
*
|
|
205
|
-
* @param cb - Pure function evaluated lazily to produce the value.
|
|
206
|
-
* Receives the `Computed` instance as `this`.
|
|
207
|
-
* @param options - Optional configuration:
|
|
208
|
-
* - `equals` — custom equality function; defaults to `Object.is`.
|
|
209
|
-
*
|
|
210
|
-
* @see Signal algorithms — "Signal.Computed Constructor"
|
|
211
|
-
*/
|
|
212
|
-
constructor(cb, options) {
|
|
213
|
-
this.#callback = cb;
|
|
214
|
-
this.#equals = options?.equals ?? Object.is;
|
|
215
|
-
this.#sources = /* @__PURE__ */ new Set();
|
|
216
|
-
this.#sinks = /* @__PURE__ */ new Set();
|
|
217
|
-
this.#state = "dirty";
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Returns the current value of this Signal, re-evaluating `#callback` if
|
|
221
|
-
* the cached value may be stale.
|
|
222
|
-
*
|
|
223
|
-
* Registers this Signal as a source of any outer `Computed` currently
|
|
224
|
-
* being evaluated (automatic dependency tracking).
|
|
225
|
-
*
|
|
226
|
-
* If the state is `~dirty~` or `~checked~`, walks the source graph
|
|
227
|
-
* depth-first to find and recalculate the deepest stale `Computed` first,
|
|
228
|
-
* then re-checks upward until this Signal is `~clean~`.
|
|
229
|
-
*
|
|
230
|
-
* @returns The current computed value, or a boxed error object if the last
|
|
231
|
-
* evaluation threw.
|
|
232
|
-
* @throws If `frozen` is `true`.
|
|
233
|
-
* @throws If the Signal is in the `~computing~` state (cyclic dependency).
|
|
234
|
-
*
|
|
235
|
-
* @see Signal algorithms — "Method: Signal.Computed.prototype.get"
|
|
236
|
-
*/
|
|
237
|
-
get() {
|
|
238
|
-
if (GLOBAL_STATE.frozen) throw new Error("Cannot get value of a Computed signal while the global state is frozen");
|
|
239
|
-
if (this.#state === "computing") throw new Error("Circular dependency detected while computing a Computed signal");
|
|
240
|
-
GLOBAL_STATE.computing?.addSource(this, PRIVATE);
|
|
241
|
-
if (this.#sinks.size === 0) this.#computeValue();
|
|
242
|
-
else if (this.#state === "dirty" || this.#state === "checked") while (this.#state === "dirty" || this.#state === "checked") this.#findDeepestStale().#computeValue();
|
|
243
|
-
return this.#value;
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Registers a Signal as a source of this `Computed`, discovered during the
|
|
247
|
-
* execution of `#callback`.
|
|
248
|
-
*
|
|
249
|
-
* If this `Computed` is currently being watched (has at least one sink),
|
|
250
|
-
* the source is also informed of this Signal as a new sink, building the
|
|
251
|
-
* live push-notification chain upward.
|
|
252
|
-
*
|
|
253
|
-
* @param source - The Signal read during evaluation.
|
|
254
|
-
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
255
|
-
* @internal
|
|
256
|
-
*/
|
|
257
|
-
addSource(source, symbol) {
|
|
258
|
-
assertPrivateContext(symbol);
|
|
259
|
-
this.#sources.add(source);
|
|
260
|
-
source.addSink(this, PRIVATE);
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Returns the current evaluation state of this Signal.
|
|
264
|
-
*
|
|
265
|
-
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
266
|
-
* @internal
|
|
267
|
-
* @see Signal algorithms — "Signal.Computed State machine"
|
|
268
|
-
*/
|
|
269
|
-
getState(symbol) {
|
|
270
|
-
assertPrivateContext(symbol);
|
|
271
|
-
return this.#state;
|
|
272
|
-
}
|
|
273
|
-
/**
|
|
274
|
-
* Transitions this Signal to a new evaluation state.
|
|
275
|
-
*
|
|
276
|
-
* Only valid transitions (as defined by the state machine) are allowed.
|
|
277
|
-
* Invalid transitions throw an error.
|
|
278
|
-
*
|
|
279
|
-
* @param newState - The target state.
|
|
280
|
-
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
281
|
-
* @throws If the transition from the current state to `newState` is not allowed.
|
|
282
|
-
* @internal
|
|
283
|
-
* @see Signal algorithms — "Signal.Computed State machine"
|
|
284
|
-
*/
|
|
285
|
-
setState(newState, symbol) {
|
|
286
|
-
assertPrivateContext(symbol);
|
|
287
|
-
if (this.#state === newState) return;
|
|
288
|
-
if (!this.#isValidTransition(this.#state, newState)) {
|
|
289
|
-
if (isDevMode()) {
|
|
290
|
-
console.warn(`Invalid state transition from ${this.#state} to ${newState} in Computed Signal`);
|
|
291
|
-
console.warn((/* @__PURE__ */ new Error()).stack);
|
|
292
|
-
}
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
this.#state = newState;
|
|
296
|
-
if (this.#state === "dirty" || this.#state === "checked") for (const sink of this.#sinks) sink instanceof Computed ? sink.setState("checked", PRIVATE) : sink.notify(PRIVATE);
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* Registers a new sink (a `Computed` or `Watcher` that directly depends on
|
|
300
|
-
* this Signal) in the internal sinks set.
|
|
301
|
-
*
|
|
302
|
-
* If this is the first sink, propagates the sink registration recursively
|
|
303
|
-
* up through `#sources`, building the live dependency chain that enables
|
|
304
|
-
* push-based invalidation.
|
|
305
|
-
*
|
|
306
|
-
* @param sink - The dependent node to register.
|
|
307
|
-
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
308
|
-
* @internal
|
|
309
|
-
*/
|
|
310
|
-
addSink(sink, symbol) {
|
|
311
|
-
assertPrivateContext(symbol);
|
|
312
|
-
if (this.#sinks.size === 0) for (const source of this.#sources) source.addSink(this, PRIVATE);
|
|
313
|
-
this.#sinks.add(sink);
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Removes a sink from the internal sinks set.
|
|
317
|
-
*
|
|
318
|
-
* If the sinks set becomes empty after removal, propagates the removal
|
|
319
|
-
* recursively up through `#sources`, tearing down the live dependency
|
|
320
|
-
* chain and allowing garbage collection of un-watched nodes.
|
|
321
|
-
*
|
|
322
|
-
* @param sink - The dependent node to remove.
|
|
323
|
-
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
324
|
-
* @internal
|
|
325
|
-
*/
|
|
326
|
-
removeSink(sink, symbol) {
|
|
327
|
-
assertPrivateContext(symbol);
|
|
328
|
-
this.#sinks.delete(sink);
|
|
329
|
-
if (this.#sinks.size === 0) for (const source of this.#sources) source.removeSink(this, PRIVATE);
|
|
330
|
-
}
|
|
331
|
-
/**
|
|
332
|
-
* Recursively walks the source graph depth-first to find the deepest,
|
|
333
|
-
* left-most `Computed` node that is in a `~dirty~` or `~checked~` state.
|
|
334
|
-
*
|
|
335
|
-
* This ensures that recalculation always starts from the bottom of the
|
|
336
|
-
* dependency graph, so every node sees already-updated dependencies —
|
|
337
|
-
* the core of glitch-free evaluation.
|
|
338
|
-
*
|
|
339
|
-
* Cuts off the search when hitting a `~clean~` `Computed` source, since
|
|
340
|
-
* its subtree is guaranteed to be up-to-date.
|
|
341
|
-
*
|
|
342
|
-
* @returns The deepest stale `Computed` found, or `node` itself if none of
|
|
343
|
-
* its sources are stale.
|
|
344
|
-
*/
|
|
345
|
-
#findDeepestStale() {
|
|
346
|
-
const unclearNode = [...this.#sources].find((source) => source instanceof Computed && (source.#state === "dirty" || source.#state === "checked"));
|
|
347
|
-
return unclearNode ? unclearNode.#findDeepestStale() : this;
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Executes `#callback` to recompute this Signal's value.
|
|
351
|
-
*
|
|
352
|
-
* Implements the "recalculate dirty computed Signal" algorithm:
|
|
353
|
-
* 1. Clears stale sources and removes this Signal from their sinks.
|
|
354
|
-
* 2. Sets `computing` to this Signal for automatic dependency tracking.
|
|
355
|
-
* 3. Runs the callback, caching the return value or any thrown exception.
|
|
356
|
-
* 4. Restores the previous `computing` value.
|
|
357
|
-
* 5. Runs the "set Signal value" algorithm to detect value changes.
|
|
358
|
-
* 6. Transitions state to `~clean~`.
|
|
359
|
-
* 7. Propagates `~dirty~` to sinks (or attempts `~clean~` if value unchanged).
|
|
360
|
-
*
|
|
361
|
-
* @see Signal algorithms — "Algorithm: recalculate dirty computed Signal"
|
|
362
|
-
*/
|
|
363
|
-
#computeValue() {
|
|
364
|
-
for (const source of this.#sources) source.removeSink(this, PRIVATE);
|
|
365
|
-
this.#sources.clear();
|
|
366
|
-
pushComputed(this);
|
|
367
|
-
this.setState("computing", PRIVATE);
|
|
368
|
-
let newValue;
|
|
369
|
-
try {
|
|
370
|
-
newValue = this.#callback.call(this);
|
|
371
|
-
} catch (error) {
|
|
372
|
-
if (isDevMode()) console.error("Error thrown while computing a Computed signal:", error);
|
|
373
|
-
newValue = {
|
|
374
|
-
isError: true,
|
|
375
|
-
value: error
|
|
376
|
-
};
|
|
377
|
-
} finally {
|
|
378
|
-
popComputed();
|
|
379
|
-
}
|
|
380
|
-
const outcome = this.#setValue(newValue);
|
|
381
|
-
this.setState("clean", PRIVATE);
|
|
382
|
-
if (outcome === "dirty") for (const sink of this.#sinks) sink instanceof Computed ? sink.setState("dirty", PRIVATE) : sink.notify(PRIVATE);
|
|
383
|
-
else this.#propagateClean();
|
|
346
|
+
/**
|
|
347
|
+
* Executes `#callback` to recompute this Signal's value.
|
|
348
|
+
*
|
|
349
|
+
* Implements the "recalculate dirty computed Signal" algorithm:
|
|
350
|
+
* 1. Clears stale sources and removes this Signal from their sinks.
|
|
351
|
+
* 2. Sets `computing` to this Signal for automatic dependency tracking.
|
|
352
|
+
* 3. Runs the callback, caching the return value or any thrown exception.
|
|
353
|
+
* 4. Restores the previous `computing` value.
|
|
354
|
+
* 5. Runs the "set Signal value" algorithm to detect value changes.
|
|
355
|
+
* 6. Transitions state to `~clean~`.
|
|
356
|
+
* 7. Propagates `~dirty~` to sinks (or attempts `~clean~` if value unchanged).
|
|
357
|
+
*
|
|
358
|
+
* @see Signal algorithms — "Algorithm: recalculate dirty computed Signal"
|
|
359
|
+
*/
|
|
360
|
+
#computeValue() {
|
|
361
|
+
for (const source of this.#sources) source.removeSink(this, PRIVATE);
|
|
362
|
+
this.#sources.clear();
|
|
363
|
+
pushComputed(this);
|
|
364
|
+
this.setState("computing", PRIVATE);
|
|
365
|
+
let newValue;
|
|
366
|
+
try {
|
|
367
|
+
newValue = this.#callback.call(this);
|
|
368
|
+
} catch (error) {
|
|
369
|
+
if (isDevMode()) console.error("Error thrown while computing a Computed signal:", error);
|
|
370
|
+
newValue = {
|
|
371
|
+
isError: true,
|
|
372
|
+
value: error
|
|
373
|
+
};
|
|
374
|
+
} finally {
|
|
375
|
+
popComputed();
|
|
384
376
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
} catch (equalsError) {
|
|
412
|
-
this.#value = {
|
|
413
|
-
isError: true,
|
|
414
|
-
value: equalsError
|
|
415
|
-
};
|
|
416
|
-
return "dirty";
|
|
417
|
-
}
|
|
377
|
+
const outcome = this.#setValue(newValue);
|
|
378
|
+
this.setState("clean", PRIVATE);
|
|
379
|
+
if (outcome === "dirty") for (const sink of this.#sinks) sink instanceof Computed ? sink.setState("dirty", PRIVATE) : sink.notify(PRIVATE);
|
|
380
|
+
else this.#propagateClean();
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Implements the "set Signal value" algorithm.
|
|
384
|
+
*
|
|
385
|
+
* Compares the new value against the cached one using `#equals`. If equal,
|
|
386
|
+
* returns `~clean~` and leaves `#value` untouched. Otherwise updates
|
|
387
|
+
* `#value` and returns `~dirty~`.
|
|
388
|
+
*
|
|
389
|
+
* Special cases:
|
|
390
|
+
* - If `newValue` is a boxed error, `#equals` is skipped and the error is
|
|
391
|
+
* cached directly.
|
|
392
|
+
* - If `#equals` itself throws, the exception is cached as a boxed error
|
|
393
|
+
* and the outcome is `~dirty~`.
|
|
394
|
+
*
|
|
395
|
+
* @param newValue - The value (or boxed error) produced by `#callback`.
|
|
396
|
+
* @returns `~clean~` if the value is unchanged, `~dirty~` otherwise.
|
|
397
|
+
*
|
|
398
|
+
* @see Signal algorithms — "Set Signal value algorithm"
|
|
399
|
+
*/
|
|
400
|
+
#setValue(newValue) {
|
|
401
|
+
const oldValue = this.#value;
|
|
402
|
+
if (this.#isErrorValue(newValue)) {
|
|
418
403
|
this.#value = newValue;
|
|
419
404
|
return "dirty";
|
|
420
405
|
}
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
* re-evaluated on the next read.
|
|
430
|
-
*
|
|
431
|
-
* @see Signal algorithms — "Algorithm: recalculate dirty computed Signal"
|
|
432
|
-
*/
|
|
433
|
-
#propagateClean() {
|
|
434
|
-
for (const sink of this.#sinks) if (sink instanceof Computed && sink.#state === "checked") {
|
|
435
|
-
let allSourcesClean = true;
|
|
436
|
-
for (const source of sink.#sources) if (source instanceof Computed && source.#state !== "clean") {
|
|
437
|
-
allSourcesClean = false;
|
|
438
|
-
break;
|
|
439
|
-
}
|
|
440
|
-
if (allSourcesClean) {
|
|
441
|
-
sink.#state = "clean";
|
|
442
|
-
sink.#propagateClean();
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* Type guard that checks whether a value is a boxed error object.
|
|
448
|
-
*
|
|
449
|
-
* Used to distinguish a legitimately computed value from a cached
|
|
450
|
-
* exception produced by `#callback` or `#equals`.
|
|
451
|
-
*
|
|
452
|
-
* @param value - The value to inspect.
|
|
453
|
-
* @returns `true` if `value` is `{ isError: true; value: Error }`.
|
|
454
|
-
*/
|
|
455
|
-
#isErrorValue(value) {
|
|
456
|
-
return typeof value === "object" && !!value && "isError" in value;
|
|
457
|
-
}
|
|
458
|
-
#isValidTransition(from, to) {
|
|
459
|
-
switch (from) {
|
|
460
|
-
case "checked": return to === "clean" || to === "dirty";
|
|
461
|
-
case "clean": return to === "checked" || to === "dirty";
|
|
462
|
-
case "dirty": return to === "computing";
|
|
463
|
-
case "computing": return to === "clean";
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
};
|
|
467
|
-
//#endregion
|
|
468
|
-
//#region ../packages/signals/src/lib/models/state/state.ts
|
|
469
|
-
var State = class {
|
|
470
|
-
/**
|
|
471
|
-
* The current value of the signal.
|
|
472
|
-
*
|
|
473
|
-
* Initialised to `initialValue` in the constructor and updated by `set`
|
|
474
|
-
* whenever the new value is not equal to the current one according to
|
|
475
|
-
* `#equals`.
|
|
476
|
-
*
|
|
477
|
-
* @internalSlot
|
|
478
|
-
* @see Signal algorithms — 'Signal.State internal slots'
|
|
479
|
-
*/
|
|
480
|
-
#value;
|
|
481
|
-
/**
|
|
482
|
-
* The equality function used to determine whether a new value is
|
|
483
|
-
* meaningfully different from the current one.
|
|
484
|
-
*
|
|
485
|
-
* Called as `equals.call(signal, oldValue, newValue)`. If it returns
|
|
486
|
-
* `true` the signal is considered unchanged and no propagation occurs.
|
|
487
|
-
* Defaults to `Object.is` when not provided via options.
|
|
488
|
-
*
|
|
489
|
-
* @internalSlot
|
|
490
|
-
* @see Signal algorithms — 'Signal.State internal slots'
|
|
491
|
-
* @see Algorithm — 'Set Signal value'
|
|
492
|
-
*/
|
|
493
|
-
#equals;
|
|
494
|
-
/**
|
|
495
|
-
* Optional callback invoked (with `frozen = true`) the first time this
|
|
496
|
-
* Signal gains a sink — i.e. when it transitions from un-observed to
|
|
497
|
-
* observed by at least one `Watcher` (directly or transitively).
|
|
498
|
-
*
|
|
499
|
-
* @internalSlot
|
|
500
|
-
* @see Signal algorithms — 'Signal.State internal slots'
|
|
501
|
-
* @see Method — `Signal.subtle.Watcher.prototype.watch`
|
|
502
|
-
*/
|
|
503
|
-
#watched;
|
|
504
|
-
/**
|
|
505
|
-
* Optional callback invoked (with `frozen = true`) when this Signal loses
|
|
506
|
-
* its last sink — i.e. when it transitions from observed back to
|
|
507
|
-
* un-observed.
|
|
508
|
-
*
|
|
509
|
-
* @internalSlot
|
|
510
|
-
* @see Signal algorithms — 'Signal.State internal slots'
|
|
511
|
-
* @see Method — `Signal.subtle.Watcher.prototype.unwatch`
|
|
512
|
-
*/
|
|
513
|
-
#unwatched;
|
|
514
|
-
/**
|
|
515
|
-
* The set of watched signals that directly depend on this one.
|
|
516
|
-
*
|
|
517
|
-
* Populated only when this Signal is reachable from at least one active
|
|
518
|
-
* `Watcher` — un-watched Signals have an empty sinks set, which allows
|
|
519
|
-
* them to be garbage-collected independently from the rest of the graph.
|
|
520
|
-
*
|
|
521
|
-
* Should contain both `Computed` and `Watcher` instances, as both can be
|
|
522
|
-
* direct dependents of a `State`.
|
|
523
|
-
*
|
|
524
|
-
* @internalSlot
|
|
525
|
-
* @see Signal algorithms — 'Signal.State internal slots'
|
|
526
|
-
* @see Method — `Signal.State.prototype.get` (NOTE on sinks)
|
|
527
|
-
*/
|
|
528
|
-
#sinks;
|
|
529
|
-
/**
|
|
530
|
-
* Returns a snapshot of the current sinks set for introspection.
|
|
531
|
-
*
|
|
532
|
-
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
533
|
-
* @returns An array of `Computed` and `Watcher` instances that depend on this Signal.
|
|
534
|
-
* @internal
|
|
535
|
-
*/
|
|
536
|
-
getSinks(symbol) {
|
|
537
|
-
assertPrivateContext(symbol);
|
|
538
|
-
return [...this.#sinks];
|
|
539
|
-
}
|
|
540
|
-
/**
|
|
541
|
-
* Creates a new `State` signal.
|
|
542
|
-
*
|
|
543
|
-
* @param initialValue - The initial value of the signal.
|
|
544
|
-
* @param options - Optional configuration:
|
|
545
|
-
* - `equals` — custom equality function; defaults to `Object.is`.
|
|
546
|
-
* - `watched` — called when the signal gains its first sink.
|
|
547
|
-
* - `unwatched` — called when the signal loses its last sink.
|
|
548
|
-
*
|
|
549
|
-
* @see Signal algorithms — 'Constructor: Signal.State(initialValue, options)'
|
|
550
|
-
*/
|
|
551
|
-
constructor(initialValue, options) {
|
|
552
|
-
this.#value = initialValue;
|
|
553
|
-
this.#equals = options?.equals ?? Object.is;
|
|
554
|
-
this.#watched = options?.watched;
|
|
555
|
-
this.#unwatched = options?.unwatched;
|
|
556
|
-
this.#sinks = /* @__PURE__ */ new Set();
|
|
557
|
-
}
|
|
558
|
-
/**
|
|
559
|
-
* Returns the current value of the signal, registering this Signal as a
|
|
560
|
-
* source of the innermost `Computed` currently being evaluated (if any).
|
|
561
|
-
*
|
|
562
|
-
* @throws If `frozen` is `true` — reads are forbidden while a protected
|
|
563
|
-
* callback (`notify`, `watched`, `unwatched`) is executing.
|
|
564
|
-
*
|
|
565
|
-
* @see Signal algorithms — 'Method: Signal.State.prototype.get()'
|
|
566
|
-
*/
|
|
567
|
-
get() {
|
|
568
|
-
if (GLOBAL_STATE.frozen) throw new Error("Cannot get value while signals are frozen");
|
|
569
|
-
GLOBAL_STATE.computing?.addSource(this, PRIVATE);
|
|
570
|
-
return this.#value;
|
|
571
|
-
}
|
|
572
|
-
/**
|
|
573
|
-
* Updates the signal's value and propagates changes to all dependent
|
|
574
|
-
* sinks.
|
|
575
|
-
*
|
|
576
|
-
* If `equals(currentValue, newValue)` returns `true` the call is a no-op
|
|
577
|
-
* and no propagation occurs. Otherwise `#value` is updated, all direct
|
|
578
|
-
* `Computed` sinks are marked `~dirty~`, indirect ones `~checked~`, and
|
|
579
|
-
* each reachable `Watcher` has its `notify` callback invoked synchronously
|
|
580
|
-
* (with `frozen = true`).
|
|
581
|
-
*
|
|
582
|
-
* @param newValue - The new value to set.
|
|
583
|
-
* @throws If `frozen` is `true` — writes are forbidden while a protected
|
|
584
|
-
* callback is executing.
|
|
585
|
-
*
|
|
586
|
-
* @see Signal algorithms — 'Method: Signal.State.prototype.set(newValue)'
|
|
587
|
-
* @see Algorithm — 'Set Signal value'
|
|
588
|
-
*/
|
|
589
|
-
set(newValue) {
|
|
590
|
-
if (GLOBAL_STATE.frozen) throw new Error("Cannot set value while signals are frozen");
|
|
591
|
-
if (!this.#equals.call(this, this.#value, newValue)) {
|
|
592
|
-
this.#value = newValue;
|
|
593
|
-
for (const sink of this.#sinks) sink instanceof Computed ? sink.setState("dirty", PRIVATE) : sink.notify(PRIVATE);
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
/**
|
|
597
|
-
* Registers a new sink (a `Computed` or `Watcher` that depends on this
|
|
598
|
-
* Signal) in the internal sinks set.
|
|
599
|
-
*
|
|
600
|
-
* Called by `Watcher.prototype.watch` when building the live dependency
|
|
601
|
-
* chain, and by `Computed` when propagating sink registration up through
|
|
602
|
-
* its sources.
|
|
603
|
-
*
|
|
604
|
-
* @param sink - The dependent node to register.
|
|
605
|
-
* @param symbol - The private symbol for validation.
|
|
606
|
-
* @internal
|
|
607
|
-
*/
|
|
608
|
-
addSink(sink, symbol) {
|
|
609
|
-
assertPrivateContext(symbol);
|
|
610
|
-
const empty = this.#sinks.size === 0;
|
|
611
|
-
this.#sinks.add(sink);
|
|
612
|
-
if (empty && this.#watched) {
|
|
613
|
-
GLOBAL_STATE.frozen = true;
|
|
614
|
-
try {
|
|
615
|
-
this.#watched();
|
|
616
|
-
} catch (error) {
|
|
617
|
-
if (isDevMode()) console.error("Error thrown while running a State Signal watched callback:", error);
|
|
618
|
-
throw error;
|
|
619
|
-
} finally {
|
|
620
|
-
GLOBAL_STATE.frozen = false;
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
/**
|
|
625
|
-
* Removes a sink from the internal sinks set.
|
|
626
|
-
*
|
|
627
|
-
* Called by `Watcher.prototype.unwatch` when tearing down the live
|
|
628
|
-
* dependency chain. If the sinks set becomes empty after removal, the
|
|
629
|
-
* caller is responsible for propagating the removal up through this
|
|
630
|
-
* Signal's sources.
|
|
631
|
-
*
|
|
632
|
-
* @param sink - The dependent node to remove.
|
|
633
|
-
* @param symbol - The private symbol for validation.
|
|
634
|
-
* @internal
|
|
635
|
-
*/
|
|
636
|
-
removeSink(sink, symbol) {
|
|
637
|
-
assertPrivateContext(symbol);
|
|
638
|
-
this.#sinks.delete(sink);
|
|
639
|
-
if (this.#sinks.size === 0 && this.#unwatched) {
|
|
640
|
-
GLOBAL_STATE.frozen = true;
|
|
641
|
-
try {
|
|
642
|
-
this.#unwatched();
|
|
643
|
-
} catch (error) {
|
|
644
|
-
if (isDevMode()) console.error("Error thrown while running a State Signal unwatched callback:", error);
|
|
645
|
-
throw error;
|
|
646
|
-
} finally {
|
|
647
|
-
GLOBAL_STATE.frozen = false;
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
};
|
|
652
|
-
//#endregion
|
|
653
|
-
//#region ../packages/signals/src/lib/models/watcher/watcher.ts
|
|
654
|
-
/**
|
|
655
|
-
* A `Watcher` observes a set of Signals and fires a `notify` callback
|
|
656
|
-
* synchronously when any of their (recursive) dependencies change.
|
|
657
|
-
*
|
|
658
|
-
* It is the low-level primitive on top of which frameworks implement
|
|
659
|
-
* effects and scheduling. It does not hold a value and has no generic
|
|
660
|
-
* type parameter.
|
|
661
|
-
*
|
|
662
|
-
* @see Signal algorithms — 'The `Signal.subtle.Watcher` class'
|
|
663
|
-
*/
|
|
664
|
-
var Watcher = class {
|
|
665
|
-
/**
|
|
666
|
-
* The current state of the Watcher.
|
|
667
|
-
*
|
|
668
|
-
* - `~waiting~` — newly created, or `notify` has already been called since
|
|
669
|
-
* the last `watch` call. Not actively observing changes.
|
|
670
|
-
* - `~watching~` — actively watching; no dependency has changed yet.
|
|
671
|
-
* - `~pending~` — a dependency has changed but `notify` has not yet run.
|
|
672
|
-
*
|
|
673
|
-
* @internalSlot
|
|
674
|
-
* @see Signal algorithms — 'Signal.subtle.Watcher State machine'
|
|
675
|
-
*/
|
|
676
|
-
#state;
|
|
677
|
-
/**
|
|
678
|
-
* The ordered set of Signals this Watcher is currently watching.
|
|
679
|
-
* May contain both `State` and `Computed` instances.
|
|
680
|
-
*
|
|
681
|
-
* @internalSlot
|
|
682
|
-
* @see Signal algorithms — 'Signal.subtle.Watcher internal slots'
|
|
683
|
-
*/
|
|
684
|
-
#signals;
|
|
685
|
-
/**
|
|
686
|
-
* Returns a snapshot of the current watched signals set for introspection.
|
|
687
|
-
*
|
|
688
|
-
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
689
|
-
* @returns An array of `State` and `Computed` instances that this Watcher is watching.
|
|
690
|
-
* @internal
|
|
691
|
-
*/
|
|
692
|
-
getSources(symbol) {
|
|
693
|
-
assertPrivateContext(symbol);
|
|
694
|
-
return [...this.#signals];
|
|
695
|
-
}
|
|
696
|
-
/**
|
|
697
|
-
* The callback invoked synchronously when a watched Signal (or one of its
|
|
698
|
-
* recursive dependencies) changes for the first time since the last
|
|
699
|
-
* `watch` call.
|
|
700
|
-
*
|
|
701
|
-
* Receives the Watcher itself as `this`. No Signals may be read or written
|
|
702
|
-
* during its execution (`frozen` is `true` for its entire duration).
|
|
703
|
-
*
|
|
704
|
-
* @internalSlot
|
|
705
|
-
* @see Signal algorithms — 'Signal.subtle.Watcher internal slots'
|
|
706
|
-
*/
|
|
707
|
-
#notifyCallback;
|
|
708
|
-
/**
|
|
709
|
-
* Creates a new Watcher.
|
|
710
|
-
*
|
|
711
|
-
* The Watcher starts in the `~waiting~` state with an empty signals set.
|
|
712
|
-
*
|
|
713
|
-
* @param notifyCallback - Called synchronously (with `frozen = true`) the
|
|
714
|
-
* first time a watched dependency changes after each `watch` call. No
|
|
715
|
-
* Signals may be read or written inside this callback.
|
|
716
|
-
*
|
|
717
|
-
* @see Signal algorithms — 'Constructor: new Signal.subtle.Watcher(callback)'
|
|
718
|
-
*/
|
|
719
|
-
constructor(notifyCallback) {
|
|
720
|
-
this.#state = "waiting";
|
|
721
|
-
this.#signals = /* @__PURE__ */ new Set();
|
|
722
|
-
this.#notifyCallback = notifyCallback;
|
|
723
|
-
}
|
|
724
|
-
/**
|
|
725
|
-
* Returns the subset of watched Signals that are `Computed` instances
|
|
726
|
-
* currently in a `~dirty~` or `~checked~` state, meaning they may have a
|
|
727
|
-
* stale value that has not yet been re-evaluated.
|
|
728
|
-
*
|
|
729
|
-
* Typically called inside the microtask scheduled by the `notify` callback
|
|
730
|
-
* to know which Signals need to be pulled.
|
|
731
|
-
*
|
|
732
|
-
* @returns An array of `Computed` signals that are dirty or checked.
|
|
733
|
-
*
|
|
734
|
-
* @see Signal algorithms — 'Method: Signal.subtle.Watcher.prototype.getPending()'
|
|
735
|
-
*/
|
|
736
|
-
getPending() {
|
|
737
|
-
const filteredSignals = new Array();
|
|
738
|
-
for (const signal of this.#signals) {
|
|
739
|
-
if (!(signal instanceof Computed)) continue;
|
|
740
|
-
const state = signal.getState(PRIVATE);
|
|
741
|
-
if (state === "dirty" || state === "checked") filteredSignals.push(signal);
|
|
742
|
-
}
|
|
743
|
-
return filteredSignals;
|
|
406
|
+
try {
|
|
407
|
+
if (!this.#isErrorValue(oldValue) && this.#equals.call(this, oldValue, newValue)) return "clean";
|
|
408
|
+
} catch (equalsError) {
|
|
409
|
+
this.#value = {
|
|
410
|
+
isError: true,
|
|
411
|
+
value: equalsError
|
|
412
|
+
};
|
|
413
|
+
return "dirty";
|
|
744
414
|
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
for (
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
this.#signals.add(signal);
|
|
767
|
-
signal.addSink(this, PRIVATE);
|
|
415
|
+
this.#value = newValue;
|
|
416
|
+
return "dirty";
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Recursively marks `~checked~` sinks as `~clean~` when all of their
|
|
420
|
+
* immediate sources are already `~clean~`.
|
|
421
|
+
*
|
|
422
|
+
* Called after a recalculation that produced an unchanged value (`~clean~`
|
|
423
|
+
* outcome from `#setValue`). Propagates the clean signal upward through
|
|
424
|
+
* the graph so that Computed nodes that were only transitively dirty — and
|
|
425
|
+
* whose dependencies have not actually changed — are not needlessly
|
|
426
|
+
* re-evaluated on the next read.
|
|
427
|
+
*
|
|
428
|
+
* @see Signal algorithms — "Algorithm: recalculate dirty computed Signal"
|
|
429
|
+
*/
|
|
430
|
+
#propagateClean() {
|
|
431
|
+
for (const sink of this.#sinks) if (sink instanceof Computed && sink.#state === "checked") {
|
|
432
|
+
let allSourcesClean = true;
|
|
433
|
+
for (const source of sink.#sources) if (source instanceof Computed && source.#state !== "clean") {
|
|
434
|
+
allSourcesClean = false;
|
|
435
|
+
break;
|
|
768
436
|
}
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
* Removes the given Signals from the watched set.
|
|
773
|
-
*
|
|
774
|
-
* For each removed Signal, the Watcher is unregistered as a sink. If the
|
|
775
|
-
* Signal's sink set becomes empty as a result, the removal is propagated
|
|
776
|
-
* recursively up through its sources, tearing down the live dependency
|
|
777
|
-
* chain and allowing garbage collection of unwatched nodes.
|
|
778
|
-
*
|
|
779
|
-
* The `unwatched` callback of each Signal (if any) is called with
|
|
780
|
-
* `frozen = true`.
|
|
781
|
-
*
|
|
782
|
-
* If no Signals remain in the watched set, the Watcher transitions back to
|
|
783
|
-
* the `~waiting~` state.
|
|
784
|
-
*
|
|
785
|
-
* @param signals - One or more `State` signals to stop watching.
|
|
786
|
-
* @throws If `frozen` is `true` at the time of the call.
|
|
787
|
-
* @throws If any of the given Signals is not currently being watched.
|
|
788
|
-
*
|
|
789
|
-
* @see Signal algorithms — 'Method: Signal.subtle.Watcher.prototype.unwatch(...signals)'
|
|
790
|
-
*/
|
|
791
|
-
unwatch(...signals) {
|
|
792
|
-
if (GLOBAL_STATE.frozen) throw new Error("Cannot unwatch signals while frozen");
|
|
793
|
-
for (let i = 0; i < signals.length; i++) {
|
|
794
|
-
const signal = signals[i];
|
|
795
|
-
if (!this.#signals.has(signal)) throw new Error("Cannot unwatch a signal that is not being watched");
|
|
796
|
-
this.#signals.delete(signal);
|
|
797
|
-
signal.removeSink(this, PRIVATE);
|
|
437
|
+
if (allSourcesClean) {
|
|
438
|
+
sink.#state = "clean";
|
|
439
|
+
sink.#propagateClean();
|
|
798
440
|
}
|
|
799
|
-
if (!this.#signals.size) this.setState("waiting", PRIVATE);
|
|
800
441
|
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Type guard that checks whether a value is a boxed error object.
|
|
445
|
+
*
|
|
446
|
+
* Used to distinguish a legitimately computed value from a cached
|
|
447
|
+
* exception produced by `#callback` or `#equals`.
|
|
448
|
+
*
|
|
449
|
+
* @param value - The value to inspect.
|
|
450
|
+
* @returns `true` if `value` is `{ isError: true; value: Error }`.
|
|
451
|
+
*/
|
|
452
|
+
#isErrorValue(value) {
|
|
453
|
+
return typeof value === "object" && !!value && "isError" in value;
|
|
454
|
+
}
|
|
455
|
+
#isValidTransition(from, to) {
|
|
456
|
+
switch (from) {
|
|
457
|
+
case "checked": return to === "clean" || to === "dirty";
|
|
458
|
+
case "clean": return to === "checked" || to === "dirty";
|
|
459
|
+
case "dirty": return to === "computing";
|
|
460
|
+
case "computing": return to === "clean";
|
|
808
461
|
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
//#endregion
|
|
465
|
+
//#region ../packages/signals/src/lib/models/state/state.ts
|
|
466
|
+
var State = class {
|
|
467
|
+
/**
|
|
468
|
+
* The current value of the signal.
|
|
469
|
+
*
|
|
470
|
+
* Initialised to `initialValue` in the constructor and updated by `set`
|
|
471
|
+
* whenever the new value is not equal to the current one according to
|
|
472
|
+
* `#equals`.
|
|
473
|
+
*
|
|
474
|
+
* @internalSlot
|
|
475
|
+
* @see Signal algorithms — 'Signal.State internal slots'
|
|
476
|
+
*/
|
|
477
|
+
#value;
|
|
478
|
+
/**
|
|
479
|
+
* The equality function used to determine whether a new value is
|
|
480
|
+
* meaningfully different from the current one.
|
|
481
|
+
*
|
|
482
|
+
* Called as `equals.call(signal, oldValue, newValue)`. If it returns
|
|
483
|
+
* `true` the signal is considered unchanged and no propagation occurs.
|
|
484
|
+
* Defaults to `Object.is` when not provided via options.
|
|
485
|
+
*
|
|
486
|
+
* @internalSlot
|
|
487
|
+
* @see Signal algorithms — 'Signal.State internal slots'
|
|
488
|
+
* @see Algorithm — 'Set Signal value'
|
|
489
|
+
*/
|
|
490
|
+
#equals;
|
|
491
|
+
/**
|
|
492
|
+
* Optional callback invoked (with `frozen = true`) the first time this
|
|
493
|
+
* Signal gains a sink — i.e. when it transitions from un-observed to
|
|
494
|
+
* observed by at least one `Watcher` (directly or transitively).
|
|
495
|
+
*
|
|
496
|
+
* @internalSlot
|
|
497
|
+
* @see Signal algorithms — 'Signal.State internal slots'
|
|
498
|
+
* @see Method — `Signal.subtle.Watcher.prototype.watch`
|
|
499
|
+
*/
|
|
500
|
+
#watched;
|
|
501
|
+
/**
|
|
502
|
+
* Optional callback invoked (with `frozen = true`) when this Signal loses
|
|
503
|
+
* its last sink — i.e. when it transitions from observed back to
|
|
504
|
+
* un-observed.
|
|
505
|
+
*
|
|
506
|
+
* @internalSlot
|
|
507
|
+
* @see Signal algorithms — 'Signal.State internal slots'
|
|
508
|
+
* @see Method — `Signal.subtle.Watcher.prototype.unwatch`
|
|
509
|
+
*/
|
|
510
|
+
#unwatched;
|
|
511
|
+
/**
|
|
512
|
+
* The set of watched signals that directly depend on this one.
|
|
513
|
+
*
|
|
514
|
+
* Populated only when this Signal is reachable from at least one active
|
|
515
|
+
* `Watcher` — un-watched Signals have an empty sinks set, which allows
|
|
516
|
+
* them to be garbage-collected independently from the rest of the graph.
|
|
517
|
+
*
|
|
518
|
+
* Should contain both `Computed` and `Watcher` instances, as both can be
|
|
519
|
+
* direct dependents of a `State`.
|
|
520
|
+
*
|
|
521
|
+
* @internalSlot
|
|
522
|
+
* @see Signal algorithms — 'Signal.State internal slots'
|
|
523
|
+
* @see Method — `Signal.State.prototype.get` (NOTE on sinks)
|
|
524
|
+
*/
|
|
525
|
+
#sinks;
|
|
526
|
+
/**
|
|
527
|
+
* Returns a snapshot of the current sinks set for introspection.
|
|
528
|
+
*
|
|
529
|
+
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
530
|
+
* @returns An array of `Computed` and `Watcher` instances that depend on this Signal.
|
|
531
|
+
* @internal
|
|
532
|
+
*/
|
|
533
|
+
getSinks(symbol) {
|
|
534
|
+
assertPrivateContext(symbol);
|
|
535
|
+
return [...this.#sinks];
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Creates a new `State` signal.
|
|
539
|
+
*
|
|
540
|
+
* @param initialValue - The initial value of the signal.
|
|
541
|
+
* @param options - Optional configuration:
|
|
542
|
+
* - `equals` — custom equality function; defaults to `Object.is`.
|
|
543
|
+
* - `watched` — called when the signal gains its first sink.
|
|
544
|
+
* - `unwatched` — called when the signal loses its last sink.
|
|
545
|
+
*
|
|
546
|
+
* @see Signal algorithms — 'Constructor: Signal.State(initialValue, options)'
|
|
547
|
+
*/
|
|
548
|
+
constructor(initialValue, options) {
|
|
549
|
+
this.#value = initialValue;
|
|
550
|
+
this.#equals = options?.equals ?? Object.is;
|
|
551
|
+
this.#watched = options?.watched;
|
|
552
|
+
this.#unwatched = options?.unwatched;
|
|
553
|
+
this.#sinks = /* @__PURE__ */ new Set();
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Returns the current value of the signal, registering this Signal as a
|
|
557
|
+
* source of the innermost `Computed` currently being evaluated (if any).
|
|
558
|
+
*
|
|
559
|
+
* @throws If `frozen` is `true` — reads are forbidden while a protected
|
|
560
|
+
* callback (`notify`, `watched`, `unwatched`) is executing.
|
|
561
|
+
*
|
|
562
|
+
* @see Signal algorithms — 'Method: Signal.State.prototype.get()'
|
|
563
|
+
*/
|
|
564
|
+
get() {
|
|
565
|
+
if (GLOBAL_STATE.frozen) throw new Error("Cannot get value while signals are frozen");
|
|
566
|
+
GLOBAL_STATE.computing?.addSource(this, PRIVATE);
|
|
567
|
+
return this.#value;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Updates the signal's value and propagates changes to all dependent
|
|
571
|
+
* sinks.
|
|
572
|
+
*
|
|
573
|
+
* If `equals(currentValue, newValue)` returns `true` the call is a no-op
|
|
574
|
+
* and no propagation occurs. Otherwise `#value` is updated, all direct
|
|
575
|
+
* `Computed` sinks are marked `~dirty~`, indirect ones `~checked~`, and
|
|
576
|
+
* each reachable `Watcher` has its `notify` callback invoked synchronously
|
|
577
|
+
* (with `frozen = true`).
|
|
578
|
+
*
|
|
579
|
+
* @param newValue - The new value to set.
|
|
580
|
+
* @throws If `frozen` is `true` — writes are forbidden while a protected
|
|
581
|
+
* callback is executing.
|
|
582
|
+
*
|
|
583
|
+
* @see Signal algorithms — 'Method: Signal.State.prototype.set(newValue)'
|
|
584
|
+
* @see Algorithm — 'Set Signal value'
|
|
585
|
+
*/
|
|
586
|
+
set(newValue) {
|
|
587
|
+
if (GLOBAL_STATE.frozen) throw new Error("Cannot set value while signals are frozen");
|
|
588
|
+
if (!this.#equals.call(this, this.#value, newValue)) {
|
|
589
|
+
this.#value = newValue;
|
|
590
|
+
for (const sink of this.#sinks) sink instanceof Computed ? sink.setState("dirty", PRIVATE) : sink.notify(PRIVATE);
|
|
826
591
|
}
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Registers a new sink (a `Computed` or `Watcher` that depends on this
|
|
595
|
+
* Signal) in the internal sinks set.
|
|
596
|
+
*
|
|
597
|
+
* Called by `Watcher.prototype.watch` when building the live dependency
|
|
598
|
+
* chain, and by `Computed` when propagating sink registration up through
|
|
599
|
+
* its sources.
|
|
600
|
+
*
|
|
601
|
+
* @param sink - The dependent node to register.
|
|
602
|
+
* @param symbol - The private symbol for validation.
|
|
603
|
+
* @internal
|
|
604
|
+
*/
|
|
605
|
+
addSink(sink, symbol) {
|
|
606
|
+
assertPrivateContext(symbol);
|
|
607
|
+
const empty = this.#sinks.size === 0;
|
|
608
|
+
this.#sinks.add(sink);
|
|
609
|
+
if (empty && this.#watched) {
|
|
833
610
|
GLOBAL_STATE.frozen = true;
|
|
834
611
|
try {
|
|
835
|
-
this
|
|
836
|
-
this.#notifyCallback.call(this);
|
|
612
|
+
this.#watched();
|
|
837
613
|
} catch (error) {
|
|
838
|
-
if (isDevMode()) console.error("Error thrown while running a
|
|
614
|
+
if (isDevMode()) console.error("Error thrown while running a State Signal watched callback:", error);
|
|
839
615
|
throw error;
|
|
840
616
|
} finally {
|
|
841
|
-
this.setState("waiting", PRIVATE);
|
|
842
617
|
GLOBAL_STATE.frozen = false;
|
|
843
618
|
}
|
|
844
619
|
}
|
|
845
|
-
|
|
846
|
-
switch (from) {
|
|
847
|
-
case "waiting": return to === "watching";
|
|
848
|
-
case "watching": return to === "pending" || to === "waiting";
|
|
849
|
-
case "pending": return to === "waiting";
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
};
|
|
853
|
-
//#endregion
|
|
854
|
-
//#region ../packages/signals/src/lib/subtle.ts
|
|
620
|
+
}
|
|
855
621
|
/**
|
|
856
|
-
*
|
|
857
|
-
*
|
|
858
|
-
*
|
|
622
|
+
* Removes a sink from the internal sinks set.
|
|
623
|
+
*
|
|
624
|
+
* Called by `Watcher.prototype.unwatch` when tearing down the live
|
|
625
|
+
* dependency chain. If the sinks set becomes empty after removal, the
|
|
626
|
+
* caller is responsible for propagating the removal up through this
|
|
627
|
+
* Signal's sources.
|
|
628
|
+
*
|
|
629
|
+
* @param sink - The dependent node to remove.
|
|
630
|
+
* @param symbol - The private symbol for validation.
|
|
631
|
+
* @internal
|
|
859
632
|
*/
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
633
|
+
removeSink(sink, symbol) {
|
|
634
|
+
assertPrivateContext(symbol);
|
|
635
|
+
this.#sinks.delete(sink);
|
|
636
|
+
if (this.#sinks.size === 0 && this.#unwatched) {
|
|
637
|
+
GLOBAL_STATE.frozen = true;
|
|
638
|
+
try {
|
|
639
|
+
this.#unwatched();
|
|
640
|
+
} catch (error) {
|
|
641
|
+
if (isDevMode()) console.error("Error thrown while running a State Signal unwatched callback:", error);
|
|
642
|
+
throw error;
|
|
643
|
+
} finally {
|
|
644
|
+
GLOBAL_STATE.frozen = false;
|
|
645
|
+
}
|
|
870
646
|
}
|
|
871
647
|
}
|
|
648
|
+
};
|
|
649
|
+
//#endregion
|
|
650
|
+
//#region ../packages/signals/src/lib/models/watcher/watcher.ts
|
|
651
|
+
/**
|
|
652
|
+
* A `Watcher` observes a set of Signals and fires a `notify` callback
|
|
653
|
+
* synchronously when any of their (recursive) dependencies change.
|
|
654
|
+
*
|
|
655
|
+
* It is the low-level primitive on top of which frameworks implement
|
|
656
|
+
* effects and scheduling. It does not hold a value and has no generic
|
|
657
|
+
* type parameter.
|
|
658
|
+
*
|
|
659
|
+
* @see Signal algorithms — 'The `Signal.subtle.Watcher` class'
|
|
660
|
+
*/
|
|
661
|
+
var Watcher = class {
|
|
872
662
|
/**
|
|
873
|
-
*
|
|
874
|
-
*
|
|
663
|
+
* The current state of the Watcher.
|
|
664
|
+
*
|
|
665
|
+
* - `~waiting~` — newly created, or `notify` has already been called since
|
|
666
|
+
* the last `watch` call. Not actively observing changes.
|
|
667
|
+
* - `~watching~` — actively watching; no dependency has changed yet.
|
|
668
|
+
* - `~pending~` — a dependency has changed but `notify` has not yet run.
|
|
669
|
+
*
|
|
670
|
+
* @internalSlot
|
|
671
|
+
* @see Signal algorithms — 'Signal.subtle.Watcher State machine'
|
|
875
672
|
*/
|
|
876
|
-
|
|
877
|
-
|
|
673
|
+
#state;
|
|
674
|
+
/**
|
|
675
|
+
* The ordered set of Signals this Watcher is currently watching.
|
|
676
|
+
* May contain both `State` and `Computed` instances.
|
|
677
|
+
*
|
|
678
|
+
* @internalSlot
|
|
679
|
+
* @see Signal algorithms — 'Signal.subtle.Watcher internal slots'
|
|
680
|
+
*/
|
|
681
|
+
#signals;
|
|
682
|
+
/**
|
|
683
|
+
* Returns a snapshot of the current watched signals set for introspection.
|
|
684
|
+
*
|
|
685
|
+
* @param symbol - Private access symbol; rejects calls from outside the library.
|
|
686
|
+
* @returns An array of `State` and `Computed` instances that this Watcher is watching.
|
|
687
|
+
* @internal
|
|
688
|
+
*/
|
|
689
|
+
getSources(symbol) {
|
|
690
|
+
assertPrivateContext(symbol);
|
|
691
|
+
return [...this.#signals];
|
|
878
692
|
}
|
|
879
693
|
/**
|
|
880
|
-
*
|
|
881
|
-
*
|
|
694
|
+
* The callback invoked synchronously when a watched Signal (or one of its
|
|
695
|
+
* recursive dependencies) changes for the first time since the last
|
|
696
|
+
* `watch` call.
|
|
697
|
+
*
|
|
698
|
+
* Receives the Watcher itself as `this`. No Signals may be read or written
|
|
699
|
+
* during its execution (`frozen` is `true` for its entire duration).
|
|
700
|
+
*
|
|
701
|
+
* @internalSlot
|
|
702
|
+
* @see Signal algorithms — 'Signal.subtle.Watcher internal slots'
|
|
703
|
+
*/
|
|
704
|
+
#notifyCallback;
|
|
705
|
+
/**
|
|
706
|
+
* Creates a new Watcher.
|
|
707
|
+
*
|
|
708
|
+
* The Watcher starts in the `~waiting~` state with an empty signals set.
|
|
882
709
|
*
|
|
883
|
-
* -
|
|
884
|
-
*
|
|
710
|
+
* @param notifyCallback - Called synchronously (with `frozen = true`) the
|
|
711
|
+
* first time a watched dependency changes after each `watch` call. No
|
|
712
|
+
* Signals may be read or written inside this callback.
|
|
885
713
|
*
|
|
886
|
-
* @
|
|
887
|
-
* @returns An array of `State` and `Computed` instances.
|
|
714
|
+
* @see Signal algorithms — 'Constructor: new Signal.subtle.Watcher(callback)'
|
|
888
715
|
*/
|
|
889
|
-
|
|
890
|
-
|
|
716
|
+
constructor(notifyCallback) {
|
|
717
|
+
this.#state = "waiting";
|
|
718
|
+
this.#signals = /* @__PURE__ */ new Set();
|
|
719
|
+
this.#notifyCallback = notifyCallback;
|
|
891
720
|
}
|
|
892
721
|
/**
|
|
893
|
-
* Returns the
|
|
894
|
-
*
|
|
895
|
-
*
|
|
722
|
+
* Returns the subset of watched Signals that are `Computed` instances
|
|
723
|
+
* currently in a `~dirty~` or `~checked~` state, meaning they may have a
|
|
724
|
+
* stale value that has not yet been re-evaluated.
|
|
896
725
|
*
|
|
897
|
-
*
|
|
898
|
-
*
|
|
726
|
+
* Typically called inside the microtask scheduled by the `notify` callback
|
|
727
|
+
* to know which Signals need to be pulled.
|
|
728
|
+
*
|
|
729
|
+
* @returns An array of `Computed` signals that are dirty or checked.
|
|
730
|
+
*
|
|
731
|
+
* @see Signal algorithms — 'Method: Signal.subtle.Watcher.prototype.getPending()'
|
|
899
732
|
*/
|
|
900
|
-
|
|
901
|
-
|
|
733
|
+
getPending() {
|
|
734
|
+
const filteredSignals = new Array();
|
|
735
|
+
for (const signal of this.#signals) {
|
|
736
|
+
if (!(signal instanceof Computed)) continue;
|
|
737
|
+
const state = signal.getState(PRIVATE);
|
|
738
|
+
if (state === "dirty" || state === "checked") filteredSignals.push(signal);
|
|
739
|
+
}
|
|
740
|
+
return filteredSignals;
|
|
902
741
|
}
|
|
903
742
|
/**
|
|
904
|
-
*
|
|
905
|
-
*
|
|
906
|
-
* live.
|
|
743
|
+
* Adds the given Signals to the watched set and transitions the Watcher to
|
|
744
|
+
* the `~watching~` state.
|
|
907
745
|
*
|
|
908
|
-
*
|
|
909
|
-
*
|
|
746
|
+
* For each newly-watched Signal, the Watcher is registered as a sink and —
|
|
747
|
+
* if it is the first sink — the sink registration is propagated recursively
|
|
748
|
+
* up through the Signal's sources, building the live dependency chain.
|
|
749
|
+
*
|
|
750
|
+
* The `watched` callback of each Signal (if any) is called with
|
|
751
|
+
* `frozen = true`.
|
|
752
|
+
*
|
|
753
|
+
* @param signals - One or more `State` signals to start watching.
|
|
754
|
+
* @throws If `frozen` is `true` at the time of the call.
|
|
755
|
+
*
|
|
756
|
+
* @see Signal algorithms — 'Method: Signal.subtle.Watcher.prototype.watch(...signals)'
|
|
910
757
|
*/
|
|
911
|
-
|
|
912
|
-
|
|
758
|
+
watch(...signals) {
|
|
759
|
+
if (GLOBAL_STATE.frozen) throw new Error("Cannot watch signals while frozen");
|
|
760
|
+
for (let i = 0; i < signals.length; i++) {
|
|
761
|
+
const signal = signals[i];
|
|
762
|
+
if (this.#signals.has(signal)) throw new Error("Cannot watch a signal that is already being watched");
|
|
763
|
+
this.#signals.add(signal);
|
|
764
|
+
signal.addSink(this, PRIVATE);
|
|
765
|
+
}
|
|
766
|
+
this.setState("watching", PRIVATE);
|
|
913
767
|
}
|
|
914
768
|
/**
|
|
915
|
-
*
|
|
916
|
-
*
|
|
917
|
-
*
|
|
769
|
+
* Removes the given Signals from the watched set.
|
|
770
|
+
*
|
|
771
|
+
* For each removed Signal, the Watcher is unregistered as a sink. If the
|
|
772
|
+
* Signal's sink set becomes empty as a result, the removal is propagated
|
|
773
|
+
* recursively up through its sources, tearing down the live dependency
|
|
774
|
+
* chain and allowing garbage collection of unwatched nodes.
|
|
775
|
+
*
|
|
776
|
+
* The `unwatched` callback of each Signal (if any) is called with
|
|
777
|
+
* `frozen = true`.
|
|
778
|
+
*
|
|
779
|
+
* If no Signals remain in the watched set, the Watcher transitions back to
|
|
780
|
+
* the `~waiting~` state.
|
|
918
781
|
*
|
|
919
|
-
* @param
|
|
920
|
-
* @
|
|
782
|
+
* @param signals - One or more `State` signals to stop watching.
|
|
783
|
+
* @throws If `frozen` is `true` at the time of the call.
|
|
784
|
+
* @throws If any of the given Signals is not currently being watched.
|
|
785
|
+
*
|
|
786
|
+
* @see Signal algorithms — 'Method: Signal.subtle.Watcher.prototype.unwatch(...signals)'
|
|
921
787
|
*/
|
|
922
|
-
|
|
923
|
-
|
|
788
|
+
unwatch(...signals) {
|
|
789
|
+
if (GLOBAL_STATE.frozen) throw new Error("Cannot unwatch signals while frozen");
|
|
790
|
+
for (let i = 0; i < signals.length; i++) {
|
|
791
|
+
const signal = signals[i];
|
|
792
|
+
if (!this.#signals.has(signal)) throw new Error("Cannot unwatch a signal that is not being watched");
|
|
793
|
+
this.#signals.delete(signal);
|
|
794
|
+
signal.removeSink(this, PRIVATE);
|
|
795
|
+
}
|
|
796
|
+
if (!this.#signals.size) this.setState("waiting", PRIVATE);
|
|
924
797
|
}
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
*
|
|
935
|
-
*
|
|
936
|
-
*
|
|
937
|
-
*
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
function loadSignals(options) {
|
|
947
|
-
setDevMode(options?.devMode ?? false);
|
|
948
|
-
globalThis.Signal ??= {
|
|
949
|
-
State,
|
|
950
|
-
Computed,
|
|
951
|
-
subtle: {
|
|
952
|
-
untrack,
|
|
953
|
-
currentComputed,
|
|
954
|
-
introspectSources,
|
|
955
|
-
introspectSinks,
|
|
956
|
-
hasSinks,
|
|
957
|
-
hasSources,
|
|
958
|
-
Watcher
|
|
798
|
+
/**
|
|
799
|
+
* Get the current state of the Watcher.
|
|
800
|
+
* @param symbol - The private symbol for prevent external calls.
|
|
801
|
+
*/
|
|
802
|
+
getState(symbol) {
|
|
803
|
+
assertPrivateContext(symbol);
|
|
804
|
+
return this.#state;
|
|
805
|
+
}
|
|
806
|
+
/**
|
|
807
|
+
* Set the current state of the Watcher.
|
|
808
|
+
* @param newState - The new state to set.
|
|
809
|
+
* @param symbol - The private symbol for prevent external calls.
|
|
810
|
+
* @throws If the transition from `pending` to `watching` is attempted.
|
|
811
|
+
*/
|
|
812
|
+
setState(newState, symbol) {
|
|
813
|
+
assertPrivateContext(symbol);
|
|
814
|
+
if (this.#state === newState) return;
|
|
815
|
+
if (!this.#isValidTransition(this.#state, newState)) {
|
|
816
|
+
if (isDevMode()) {
|
|
817
|
+
console.warn(`Invalid state transition from ${this.#state} to ${newState} in Watcher`);
|
|
818
|
+
console.warn((/* @__PURE__ */ new Error()).stack);
|
|
959
819
|
}
|
|
960
|
-
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
this.#state = newState;
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* Invoce the notify callback when a watched dependency changes
|
|
826
|
+
* @param symbol - The private symbol for prevent external calls.
|
|
827
|
+
*/
|
|
828
|
+
notify(symbol) {
|
|
829
|
+
assertPrivateContext(symbol);
|
|
830
|
+
GLOBAL_STATE.frozen = true;
|
|
831
|
+
try {
|
|
832
|
+
this.setState("pending", PRIVATE);
|
|
833
|
+
this.#notifyCallback.call(this);
|
|
834
|
+
} catch (error) {
|
|
835
|
+
if (isDevMode()) console.error("Error thrown while running a Watcher notify callback:", error);
|
|
836
|
+
throw error;
|
|
837
|
+
} finally {
|
|
838
|
+
this.setState("waiting", PRIVATE);
|
|
839
|
+
GLOBAL_STATE.frozen = false;
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
#isValidTransition(from, to) {
|
|
843
|
+
switch (from) {
|
|
844
|
+
case "waiting": return to === "watching";
|
|
845
|
+
case "watching": return to === "pending" || to === "waiting";
|
|
846
|
+
case "pending": return to === "waiting";
|
|
847
|
+
}
|
|
961
848
|
}
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
849
|
+
};
|
|
850
|
+
//#endregion
|
|
851
|
+
//#region ../packages/signals/src/lib/subtle.ts
|
|
852
|
+
/**
|
|
853
|
+
* Executes a function without tracking any dependencies.
|
|
854
|
+
* @param fn - The function to execute without tracking.
|
|
855
|
+
* @returns The result of the function execution.
|
|
856
|
+
*/
|
|
857
|
+
function untrack(fn) {
|
|
858
|
+
const prevComputing = GLOBAL_STATE.computing;
|
|
859
|
+
GLOBAL_STATE.computing = null;
|
|
860
|
+
try {
|
|
861
|
+
return fn();
|
|
862
|
+
} catch (error) {
|
|
863
|
+
if (isDevMode()) console.error("Error thrown while running an Untracked signal:", error);
|
|
864
|
+
throw error;
|
|
865
|
+
} finally {
|
|
866
|
+
GLOBAL_STATE.computing = prevComputing;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Returns the currently active `Computed` instance being evaluated, or `null`
|
|
871
|
+
* @returns The currently active `Computed` instance, or `null` if none is being evaluated.
|
|
872
|
+
*/
|
|
873
|
+
function currentComputed() {
|
|
874
|
+
return GLOBAL_STATE.computing;
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* Returns the ordered list of all Signals which the given `Computed` or
|
|
878
|
+
* `Watcher` referenced during its last evaluation.
|
|
879
|
+
*
|
|
880
|
+
* - For a `Computed`, these are the Signals read inside its callback.
|
|
881
|
+
* - For a `Watcher`, these are the Signals it is currently watching.
|
|
882
|
+
*
|
|
883
|
+
* @param signal - The `Computed` or `Watcher` to introspect.
|
|
884
|
+
* @returns An array of `State` and `Computed` instances.
|
|
885
|
+
*/
|
|
886
|
+
function introspectSources(signal) {
|
|
887
|
+
return signal.getSources(PRIVATE);
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* Returns the direct dependents of the given Signal — Watchers that contain
|
|
891
|
+
* it, plus any `Computed` Signals which read it during their last evaluation
|
|
892
|
+
* (if that `Computed` is recursively watched).
|
|
893
|
+
*
|
|
894
|
+
* @param signal - The `State` or `Computed` Signal to introspect.
|
|
895
|
+
* @returns An array of `Computed` and `Watcher` instances.
|
|
896
|
+
*/
|
|
897
|
+
function introspectSinks(signal) {
|
|
898
|
+
return signal.getSinks(PRIVATE);
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Returns `true` if the given Signal is 'live' — i.e. it is watched by a
|
|
902
|
+
* `Watcher`, or it is read by a `Computed` Signal which is (recursively)
|
|
903
|
+
* live.
|
|
904
|
+
*
|
|
905
|
+
* @param signal - The `State` or `Computed` Signal to check.
|
|
906
|
+
* @returns `true` if the Signal has at least one sink.
|
|
907
|
+
*/
|
|
908
|
+
function hasSinks(signal) {
|
|
909
|
+
return signal.getSinks(PRIVATE).length > 0;
|
|
910
|
+
}
|
|
911
|
+
/**
|
|
912
|
+
* Returns `true` if the given node is 'reactive' — i.e. it depends on some
|
|
913
|
+
* other Signal. A `Computed` where `hasSources` is `false` will always
|
|
914
|
+
* return the same constant.
|
|
915
|
+
*
|
|
916
|
+
* @param signal - The `Computed` or `Watcher` to check.
|
|
917
|
+
* @returns `true` if the node has at least one source.
|
|
918
|
+
*/
|
|
919
|
+
function hasSources(signal) {
|
|
920
|
+
return signal.getSources(PRIVATE).length > 0;
|
|
921
|
+
}
|
|
922
|
+
//#endregion
|
|
923
|
+
//#region ../packages/signals/src/lib/load-signals.ts
|
|
924
|
+
/**
|
|
925
|
+
* Loads the Signals library by defining the `Signal` global object with the following properties:
|
|
926
|
+
* - `State`: The `State` class for creating reactive state variables.
|
|
927
|
+
* - `Computed`: The `Computed` class for creating derived reactive values.
|
|
928
|
+
* - `Watcher`: The `Watcher` class for observing changes in signals.
|
|
929
|
+
* - `subtle`: An object containing internal utility functions for working with signals, including:
|
|
930
|
+
* - `untrack`: Executes a function without tracking dependencies.
|
|
931
|
+
* - `currentComputed`: Returns the currently active `Computed` instance being evaluated, or `null` if none.
|
|
932
|
+
* - `introspectSources`: Returns the list of sources for a given `Computed` or `Watcher`.
|
|
933
|
+
* - `introspectSinks`: Returns the list of sinks for a given `State` or `Computed`.
|
|
934
|
+
* - `hasSinks`: Returns `true` if a given `State` or `Computed` has at least one sink.
|
|
935
|
+
* - `hasSources`: Returns `true` if a given `Computed` or `Watcher` has at least one source.
|
|
936
|
+
* - `Watcher`: The `Watcher` class for observing changes in signals.
|
|
937
|
+
*
|
|
938
|
+
* This function should be called once to initialize the Signals library and make its API available globally.
|
|
939
|
+
*
|
|
940
|
+
* @param options - Optional configuration object.
|
|
941
|
+
* @param options.devMode - When `true`, enables development mode with extra runtime checks and logging.
|
|
942
|
+
*/
|
|
943
|
+
function loadSignals(options) {
|
|
944
|
+
setDevMode(options?.devMode ?? false);
|
|
945
|
+
globalThis.Signal ??= {
|
|
946
|
+
State,
|
|
947
|
+
Computed,
|
|
948
|
+
subtle: {
|
|
949
|
+
untrack,
|
|
950
|
+
currentComputed,
|
|
951
|
+
introspectSources,
|
|
952
|
+
introspectSinks,
|
|
953
|
+
hasSinks,
|
|
954
|
+
hasSources,
|
|
955
|
+
Watcher
|
|
956
|
+
}
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
//#endregion
|
|
960
|
+
export { loadSignals };
|