@alwatr/signal 5.2.1 → 5.2.2
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/CHANGELOG.md +18 -0
- package/dist/core/effect-signal.d.ts.map +1 -1
- package/dist/core/state-signal.d.ts.map +1 -1
- package/dist/main.cjs +2 -621
- package/dist/main.cjs.map +2 -2
- package/dist/main.mjs +2 -586
- package/dist/main.mjs.map +2 -2
- package/package.json +6 -6
- package/src/core/computed-signal.test.js +16 -0
- package/src/core/state-signal.test.js +19 -4
- package/src/operators/debounce.test.js +206 -0
package/dist/main.mjs
CHANGED
|
@@ -1,587 +1,3 @@
|
|
|
1
|
-
/* @alwatr/signal v5.2.
|
|
2
|
-
|
|
3
|
-
// src/core/signal-base.ts
|
|
4
|
-
var SignalBase = class {
|
|
5
|
-
constructor(config_) {
|
|
6
|
-
this.config_ = config_;
|
|
7
|
-
/**
|
|
8
|
-
* The unique identifier for this signal instance.
|
|
9
|
-
* Useful for debugging and logging.
|
|
10
|
-
*/
|
|
11
|
-
this.signalId = this.config_.signalId;
|
|
12
|
-
/**
|
|
13
|
-
* The list of observers (listeners) subscribed to this signal.
|
|
14
|
-
* @protected
|
|
15
|
-
*/
|
|
16
|
-
this.observers_ = [];
|
|
17
|
-
/**
|
|
18
|
-
* A flag indicating whether the signal has been destroyed.
|
|
19
|
-
* @private
|
|
20
|
-
*/
|
|
21
|
-
this.isDestroyed__ = false;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Indicates whether the signal has been destroyed.
|
|
25
|
-
* A destroyed signal cannot be used and will throw an error if interacted with.
|
|
26
|
-
*
|
|
27
|
-
* @returns `true` if the signal is destroyed, `false` otherwise.
|
|
28
|
-
*/
|
|
29
|
-
get isDestroyed() {
|
|
30
|
-
return this.isDestroyed__;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Removes a specific observer from the observers list.
|
|
34
|
-
*
|
|
35
|
-
* @param observer The observer instance to remove.
|
|
36
|
-
* @protected
|
|
37
|
-
*/
|
|
38
|
-
removeObserver_(observer) {
|
|
39
|
-
this.logger_.logMethod?.("removeObserver_");
|
|
40
|
-
if (this.isDestroyed__) {
|
|
41
|
-
this.logger_.incident?.("removeObserver_", "remove_observer_on_destroyed_signal");
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
const index = this.observers_.indexOf(observer);
|
|
45
|
-
if (index !== -1) {
|
|
46
|
-
this.observers_.splice(index, 1);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Subscribes a listener function to this signal.
|
|
51
|
-
*
|
|
52
|
-
* The listener will be called whenever the signal is notified (e.g., when `dispatch` or `set` is called).
|
|
53
|
-
*
|
|
54
|
-
* @param callback The function to be called when the signal is dispatched.
|
|
55
|
-
* @param options Subscription options to customize the behavior (e.g., `once`, `priority`).
|
|
56
|
-
* @returns A `SubscribeResult` object with an `unsubscribe` method to remove the listener.
|
|
57
|
-
*/
|
|
58
|
-
subscribe(callback, options) {
|
|
59
|
-
this.logger_.logMethodArgs?.("subscribe.base", { options });
|
|
60
|
-
this.checkDestroyed_();
|
|
61
|
-
const observer = { callback, options };
|
|
62
|
-
if (options?.priority) {
|
|
63
|
-
this.observers_.unshift(observer);
|
|
64
|
-
} else {
|
|
65
|
-
this.observers_.push(observer);
|
|
66
|
-
}
|
|
67
|
-
return {
|
|
68
|
-
unsubscribe: () => this.removeObserver_(observer)
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Notifies all registered observers about a new value.
|
|
73
|
-
*
|
|
74
|
-
* This method iterates through a snapshot of the current observers to prevent issues
|
|
75
|
-
* with subscriptions changing during notification (e.g., an observer unsubscribing itself).
|
|
76
|
-
*
|
|
77
|
-
* @param value The new value to notify observers about.
|
|
78
|
-
* @protected
|
|
79
|
-
*/
|
|
80
|
-
notify_(value) {
|
|
81
|
-
this.logger_.logMethodArgs?.("notify_", value);
|
|
82
|
-
if (this.isDestroyed__) {
|
|
83
|
-
this.logger_.incident?.("notify_", "notify_on_destroyed_signal");
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const currentObservers = [...this.observers_];
|
|
87
|
-
for (const observer of currentObservers) {
|
|
88
|
-
if (observer.options?.once) {
|
|
89
|
-
this.removeObserver_(observer);
|
|
90
|
-
}
|
|
91
|
-
try {
|
|
92
|
-
const result = observer.callback(value);
|
|
93
|
-
if (result instanceof Promise) {
|
|
94
|
-
result.catch((err) => this.logger_.error("notify_", "async_callback_failed", err, { observer }));
|
|
95
|
-
}
|
|
96
|
-
} catch (err) {
|
|
97
|
-
this.logger_.error("notify_", "sync_callback_failed", err);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Returns a Promise that resolves with the next value dispatched by the signal.
|
|
103
|
-
* This provides an elegant way to wait for a single, future event using `async/await`.
|
|
104
|
-
*
|
|
105
|
-
* @returns A Promise that resolves with the next dispatched value.
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* async function onButtonClick() {
|
|
109
|
-
* console.log('Waiting for the next signal...');
|
|
110
|
-
* const nextValue = await mySignal.untilNext();
|
|
111
|
-
* console.log('Signal received:', nextValue);
|
|
112
|
-
* }
|
|
113
|
-
*/
|
|
114
|
-
untilNext() {
|
|
115
|
-
this.logger_.logMethod?.("untilNext");
|
|
116
|
-
this.checkDestroyed_();
|
|
117
|
-
return new Promise((resolve) => {
|
|
118
|
-
this.subscribe(resolve, {
|
|
119
|
-
once: true,
|
|
120
|
-
priority: true,
|
|
121
|
-
// Resolve the promise before other listeners are called.
|
|
122
|
-
receivePrevious: false
|
|
123
|
-
// We only want the *next* value, not the current one.
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Destroys the signal, clearing all its listeners and making it inactive.
|
|
129
|
-
*
|
|
130
|
-
* After destruction, any interaction with the signal (like `subscribe` or `untilNext`)
|
|
131
|
-
* will throw an error. This is crucial for preventing memory leaks by allowing
|
|
132
|
-
* garbage collection of the signal and its observers.
|
|
133
|
-
*/
|
|
134
|
-
destroy() {
|
|
135
|
-
this.logger_.logMethod?.("destroy");
|
|
136
|
-
if (this.isDestroyed__) {
|
|
137
|
-
this.logger_.incident?.("destroy_", "double_destroy_attempt");
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
this.isDestroyed__ = true;
|
|
141
|
-
this.observers_.length = 0;
|
|
142
|
-
this.config_.onDestroy?.();
|
|
143
|
-
this.config_ = null;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Throws an error if the signal has been destroyed.
|
|
147
|
-
* This is a safeguard to prevent interaction with a defunct signal.
|
|
148
|
-
* @protected
|
|
149
|
-
*/
|
|
150
|
-
checkDestroyed_() {
|
|
151
|
-
if (this.isDestroyed__) {
|
|
152
|
-
this.logger_.accident("checkDestroyed_", "attempt_to_use_destroyed_signal");
|
|
153
|
-
throw new Error(`Cannot interact with a destroyed signal (id: ${this.signalId})`);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
// src/core/event-signal.ts
|
|
159
|
-
import { delay } from "@alwatr/delay";
|
|
160
|
-
import { createLogger } from "@alwatr/logger";
|
|
161
|
-
var EventSignal = class extends SignalBase {
|
|
162
|
-
constructor(config) {
|
|
163
|
-
super(config);
|
|
164
|
-
/**
|
|
165
|
-
* The logger instance for this signal.
|
|
166
|
-
* @protected
|
|
167
|
-
*/
|
|
168
|
-
this.logger_ = createLogger(`event-signal: ${this.signalId}`);
|
|
169
|
-
this.logger_.logMethod?.("constructor");
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Dispatches an event with an optional payload to all active listeners.
|
|
173
|
-
* The notification is scheduled as a microtask to prevent blocking and ensure
|
|
174
|
-
* a consistent, non-blocking flow.
|
|
175
|
-
*
|
|
176
|
-
* @param payload The data to send with the event.
|
|
177
|
-
*/
|
|
178
|
-
dispatch(payload) {
|
|
179
|
-
this.logger_.logMethodArgs?.("dispatch", { payload });
|
|
180
|
-
this.checkDestroyed_();
|
|
181
|
-
delay.nextMicrotask().then(() => this.notify_(payload));
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
// src/core/state-signal.ts
|
|
186
|
-
import { delay as delay2 } from "@alwatr/delay";
|
|
187
|
-
import { createLogger as createLogger2 } from "@alwatr/logger";
|
|
188
|
-
var StateSignal = class extends SignalBase {
|
|
189
|
-
constructor(config) {
|
|
190
|
-
super(config);
|
|
191
|
-
/**
|
|
192
|
-
* The logger instance for this signal.
|
|
193
|
-
* @protected
|
|
194
|
-
*/
|
|
195
|
-
this.logger_ = createLogger2(`state-signal: ${this.signalId}`);
|
|
196
|
-
this.value__ = config.initialValue;
|
|
197
|
-
this.logger_.logMethodArgs?.("constructor", { initialValue: this.value__ });
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Retrieves the current value of the signal.
|
|
201
|
-
*
|
|
202
|
-
* @returns The current value.
|
|
203
|
-
*
|
|
204
|
-
* @example
|
|
205
|
-
* console.log(mySignal.get());
|
|
206
|
-
*/
|
|
207
|
-
get() {
|
|
208
|
-
this.checkDestroyed_();
|
|
209
|
-
return this.value__;
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Updates the signal's value and notifies all active listeners.
|
|
213
|
-
*
|
|
214
|
-
* The notification is scheduled as a microtask, which means the update is deferred
|
|
215
|
-
* slightly to batch multiple synchronous changes.
|
|
216
|
-
*
|
|
217
|
-
* @param newValue The new value to set.
|
|
218
|
-
*
|
|
219
|
-
* @example
|
|
220
|
-
* // For primitive types
|
|
221
|
-
* mySignal.set(42);
|
|
222
|
-
*
|
|
223
|
-
* // For object types, it's best practice to set an immutable new object.
|
|
224
|
-
* mySignal.set({ ...mySignal.get(), property: 'new-value' });
|
|
225
|
-
*/
|
|
226
|
-
set(newValue) {
|
|
227
|
-
this.logger_.logMethodArgs?.("set", { newValue });
|
|
228
|
-
this.checkDestroyed_();
|
|
229
|
-
if (Object.is(this.value__, newValue) && (typeof newValue !== "object" || newValue === null)) {
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
this.value__ = newValue;
|
|
233
|
-
delay2.nextMicrotask().then(() => this.notify_(newValue));
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Updates the signal's value based on its previous value.
|
|
237
|
-
*
|
|
238
|
-
* This method is particularly useful for state transitions that depend on the current value,
|
|
239
|
-
* especially for objects or arrays, as it promotes an immutable update pattern.
|
|
240
|
-
*
|
|
241
|
-
* @param updater A function that receives the current value and returns the new value.
|
|
242
|
-
*
|
|
243
|
-
* @example
|
|
244
|
-
* // For a counter
|
|
245
|
-
* counterSignal.update(current => current + 1);
|
|
246
|
-
*
|
|
247
|
-
* // For an object state
|
|
248
|
-
* userSignal.update(currentUser => ({ ...currentUser, loggedIn: true }));
|
|
249
|
-
*/
|
|
250
|
-
update(updater) {
|
|
251
|
-
this.logger_.logMethod?.("update");
|
|
252
|
-
this.checkDestroyed_();
|
|
253
|
-
this.set(updater(this.value__));
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Subscribes a listener to this signal.
|
|
257
|
-
*
|
|
258
|
-
* By default, the listener is immediately called with the signal's current value (`receivePrevious: true`).
|
|
259
|
-
* This behavior can be customized via the `options` parameter.
|
|
260
|
-
*
|
|
261
|
-
* @param callback The function to be called when the signal's value changes.
|
|
262
|
-
* @param options Subscription options, including `receivePrevious` and `once`.
|
|
263
|
-
* @returns An object with an `unsubscribe` method to remove the listener.
|
|
264
|
-
*/
|
|
265
|
-
subscribe(callback, options = {}) {
|
|
266
|
-
this.logger_.logMethodArgs?.("subscribe", { options });
|
|
267
|
-
this.checkDestroyed_();
|
|
268
|
-
if (options.receivePrevious !== false) {
|
|
269
|
-
delay2.nextMicrotask().then(() => callback(this.value__)).catch((err) => this.logger_.error("subscribe", "immediate_callback_failed", err));
|
|
270
|
-
if (options.once) {
|
|
271
|
-
return { unsubscribe: () => {
|
|
272
|
-
} };
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
return super.subscribe(callback, options);
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Destroys the signal, clearing its value and all listeners.
|
|
279
|
-
* This is crucial for memory management to prevent leaks.
|
|
280
|
-
*/
|
|
281
|
-
destroy() {
|
|
282
|
-
this.value__ = null;
|
|
283
|
-
super.destroy();
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
// src/core/computed-signal.ts
|
|
288
|
-
import { delay as delay3 } from "@alwatr/delay";
|
|
289
|
-
import { createLogger as createLogger3 } from "@alwatr/logger";
|
|
290
|
-
var ComputedSignal = class {
|
|
291
|
-
constructor(config_) {
|
|
292
|
-
this.config_ = config_;
|
|
293
|
-
/**
|
|
294
|
-
* The unique identifier for this signal instance.
|
|
295
|
-
*/
|
|
296
|
-
this.signalId = this.config_.signalId;
|
|
297
|
-
/**
|
|
298
|
-
* The logger instance for this signal.
|
|
299
|
-
* @protected
|
|
300
|
-
*/
|
|
301
|
-
this.logger_ = createLogger3(`computed-signal: ${this.signalId}`);
|
|
302
|
-
/**
|
|
303
|
-
* The internal `StateSignal` that holds the computed value.
|
|
304
|
-
* This is how the computed signal provides `.get()` and `.subscribe()` methods.
|
|
305
|
-
* @protected
|
|
306
|
-
*/
|
|
307
|
-
this.internalSignal_ = new StateSignal({
|
|
308
|
-
signalId: `${this.signalId}-internal`,
|
|
309
|
-
initialValue: this.config_.get()
|
|
310
|
-
});
|
|
311
|
-
/**
|
|
312
|
-
* A list of subscriptions to dependency signals.
|
|
313
|
-
* @private
|
|
314
|
-
*/
|
|
315
|
-
this.dependencySubscriptions__ = [];
|
|
316
|
-
/**
|
|
317
|
-
* A flag to prevent concurrent recalculations.
|
|
318
|
-
* @private
|
|
319
|
-
*/
|
|
320
|
-
this.isRecalculating__ = false;
|
|
321
|
-
this.logger_.logMethod?.("constructor");
|
|
322
|
-
this.recalculate_ = this.recalculate_.bind(this);
|
|
323
|
-
for (const signal of config_.deps) {
|
|
324
|
-
this.dependencySubscriptions__.push(signal.subscribe(this.recalculate_, { receivePrevious: false }));
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* The current value of the computed signal.
|
|
329
|
-
* Accessing this property returns the memoized value and does not trigger a recalculation.
|
|
330
|
-
*
|
|
331
|
-
* @returns The current computed value.
|
|
332
|
-
* @throws {Error} If accessed after the signal has been destroyed.
|
|
333
|
-
*/
|
|
334
|
-
get() {
|
|
335
|
-
return this.internalSignal_.get();
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Indicates whether the computed signal has been destroyed.
|
|
339
|
-
* A destroyed signal cannot be used and will throw an error if interacted with.
|
|
340
|
-
* @returns `true` if the signal is destroyed, `false` otherwise.
|
|
341
|
-
*/
|
|
342
|
-
get isDestroyed() {
|
|
343
|
-
return this.internalSignal_.isDestroyed;
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* Subscribes a listener to this signal.
|
|
347
|
-
* The listener will be called whenever the computed value changes.
|
|
348
|
-
*
|
|
349
|
-
* @param callback The function to be called with the new value.
|
|
350
|
-
* @param options Subscription options.
|
|
351
|
-
* @returns A `SubscribeResult` object with an `unsubscribe` method.
|
|
352
|
-
*/
|
|
353
|
-
subscribe(callback, options) {
|
|
354
|
-
return this.internalSignal_.subscribe(callback, options);
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* Returns a Promise that resolves with the next computed value.
|
|
358
|
-
*
|
|
359
|
-
* @returns A Promise that resolves with the next value.
|
|
360
|
-
*/
|
|
361
|
-
untilNext() {
|
|
362
|
-
return this.internalSignal_.untilNext();
|
|
363
|
-
}
|
|
364
|
-
/**
|
|
365
|
-
* Permanently disposes of the computed signal.
|
|
366
|
-
*
|
|
367
|
-
* This is a critical cleanup step. It unsubscribes from all dependency signals,
|
|
368
|
-
* stopping future recalculations and allowing the signal to be garbage collected.
|
|
369
|
-
* Failure to call `destroy()` will result in memory leaks.
|
|
370
|
-
*
|
|
371
|
-
* After `destroy()` is called, any attempt to access `.get()` or `.subscribe()` will throw an error.
|
|
372
|
-
*/
|
|
373
|
-
destroy() {
|
|
374
|
-
this.logger_.logMethod?.("destroy");
|
|
375
|
-
if (this.isDestroyed) {
|
|
376
|
-
this.logger_.incident?.("destroy", "already_destroyed");
|
|
377
|
-
return;
|
|
378
|
-
}
|
|
379
|
-
for (const subscription of this.dependencySubscriptions__) {
|
|
380
|
-
subscription.unsubscribe();
|
|
381
|
-
}
|
|
382
|
-
this.dependencySubscriptions__.length = 0;
|
|
383
|
-
this.internalSignal_.destroy();
|
|
384
|
-
this.config_.onDestroy?.();
|
|
385
|
-
this.config_ = null;
|
|
386
|
-
}
|
|
387
|
-
/**
|
|
388
|
-
* Schedules a recalculation of the signal's value.
|
|
389
|
-
*
|
|
390
|
-
* This method batches updates using a macrotask (`delay.nextMacrotask`) to ensure the
|
|
391
|
-
* `get` function runs only once per event loop tick, even if multiple dependencies
|
|
392
|
-
* change in the same synchronous block of code.
|
|
393
|
-
* @protected
|
|
394
|
-
*/
|
|
395
|
-
async recalculate_() {
|
|
396
|
-
this.logger_.logMethod?.("recalculate_");
|
|
397
|
-
if (this.internalSignal_.isDestroyed) {
|
|
398
|
-
this.logger_.incident?.("recalculate_", "recalculate_on_destroyed_signal");
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
if (this.isRecalculating__) {
|
|
402
|
-
this.logger_.logStep?.("recalculate_", "skipping_recalculation_already_scheduled");
|
|
403
|
-
return;
|
|
404
|
-
}
|
|
405
|
-
this.isRecalculating__ = true;
|
|
406
|
-
try {
|
|
407
|
-
await delay3.nextMacrotask();
|
|
408
|
-
if (this.isDestroyed) {
|
|
409
|
-
this.logger_.incident?.("recalculate_", "destroyed_during_delay");
|
|
410
|
-
this.isRecalculating__ = false;
|
|
411
|
-
return;
|
|
412
|
-
}
|
|
413
|
-
this.logger_.logStep?.("recalculate_", "recalculating_value");
|
|
414
|
-
this.internalSignal_.set(this.config_.get());
|
|
415
|
-
} catch (err) {
|
|
416
|
-
this.logger_.error("recalculate_", "recalculation_failed", err);
|
|
417
|
-
}
|
|
418
|
-
this.isRecalculating__ = false;
|
|
419
|
-
}
|
|
420
|
-
};
|
|
421
|
-
|
|
422
|
-
// src/core/effect-signal.ts
|
|
423
|
-
import { delay as delay4 } from "@alwatr/delay";
|
|
424
|
-
import { createLogger as createLogger4 } from "@alwatr/logger";
|
|
425
|
-
var EffectSignal = class {
|
|
426
|
-
constructor(config_) {
|
|
427
|
-
this.config_ = config_;
|
|
428
|
-
/**
|
|
429
|
-
* The unique identifier for this signal instance.
|
|
430
|
-
*/
|
|
431
|
-
this.signalId = this.config_.signalId ? this.config_.signalId : `[${this.config_.deps.map((dep) => dep.signalId).join(", ")}]`;
|
|
432
|
-
/**
|
|
433
|
-
* The logger instance for this signal.
|
|
434
|
-
* @protected
|
|
435
|
-
*/
|
|
436
|
-
this.logger_ = createLogger4(`effect-signal: ${this.signalId}`);
|
|
437
|
-
/**
|
|
438
|
-
* A list of subscriptions to dependency signals.
|
|
439
|
-
* @private
|
|
440
|
-
*/
|
|
441
|
-
this.dependencySubscriptions__ = [];
|
|
442
|
-
/**
|
|
443
|
-
* A flag to prevent concurrent executions of the effect.
|
|
444
|
-
* @private
|
|
445
|
-
*/
|
|
446
|
-
this.isRunning__ = false;
|
|
447
|
-
/**
|
|
448
|
-
* A flag indicating whether the effect has been destroyed.
|
|
449
|
-
* @private
|
|
450
|
-
*/
|
|
451
|
-
this.isDestroyed__ = false;
|
|
452
|
-
this.logger_.logMethod?.("constructor");
|
|
453
|
-
this.scheduleExecution_ = this.scheduleExecution_.bind(this);
|
|
454
|
-
for (const signal of config_.deps) {
|
|
455
|
-
this.dependencySubscriptions__.push(signal.subscribe(this.scheduleExecution_, { receivePrevious: false }));
|
|
456
|
-
}
|
|
457
|
-
if (config_.runImmediately === true) {
|
|
458
|
-
void this.scheduleExecution_();
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
/**
|
|
462
|
-
* Indicates whether the effect signal has been destroyed.
|
|
463
|
-
* A destroyed signal will no longer execute its effect and cannot be reused.
|
|
464
|
-
*
|
|
465
|
-
* @returns `true` if the signal is destroyed, `false` otherwise.
|
|
466
|
-
*/
|
|
467
|
-
get isDestroyed() {
|
|
468
|
-
return this.isDestroyed__;
|
|
469
|
-
}
|
|
470
|
-
/**
|
|
471
|
-
* Schedules the execution of the effect's `run` function.
|
|
472
|
-
*
|
|
473
|
-
* This method batches updates using a macrotask (`delay.nextMacrotask`) to ensure the
|
|
474
|
-
* `run` function executes only once per event loop tick, even if multiple
|
|
475
|
-
* dependencies change simultaneously.
|
|
476
|
-
* @protected
|
|
477
|
-
*/
|
|
478
|
-
async scheduleExecution_() {
|
|
479
|
-
this.logger_.logMethod?.("scheduleExecution_");
|
|
480
|
-
if (this.isDestroyed__) {
|
|
481
|
-
this.logger_.incident?.("scheduleExecution_", "schedule_execution_on_destroyed_signal");
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
if (this.isRunning__) {
|
|
485
|
-
this.logger_.logStep?.("scheduleExecution_", "skipped_because_already_running");
|
|
486
|
-
return;
|
|
487
|
-
}
|
|
488
|
-
this.isRunning__ = true;
|
|
489
|
-
try {
|
|
490
|
-
await delay4.nextMacrotask();
|
|
491
|
-
if (this.isDestroyed__) {
|
|
492
|
-
this.logger_.incident?.("scheduleExecution_", "destroyed_during_delay");
|
|
493
|
-
this.isRunning__ = false;
|
|
494
|
-
return;
|
|
495
|
-
}
|
|
496
|
-
this.logger_.logStep?.("scheduleExecution_", "executing_effect");
|
|
497
|
-
await this.config_.run();
|
|
498
|
-
} catch (err) {
|
|
499
|
-
this.logger_.error("scheduleExecution_", "effect_failed", err);
|
|
500
|
-
}
|
|
501
|
-
this.isRunning__ = false;
|
|
502
|
-
}
|
|
503
|
-
/**
|
|
504
|
-
* Permanently disposes of the effect signal.
|
|
505
|
-
*
|
|
506
|
-
* This is a critical cleanup step. It unsubscribes from all dependency signals,
|
|
507
|
-
* stopping any future executions of the effect and allowing it to be garbage collected.
|
|
508
|
-
* Failure to call `destroy()` will result in memory leaks and potentially unwanted side effects.
|
|
509
|
-
*/
|
|
510
|
-
destroy() {
|
|
511
|
-
this.logger_.logMethod?.("destroy");
|
|
512
|
-
if (this.isDestroyed__) {
|
|
513
|
-
this.logger_.incident?.("destroy", "already_destroyed");
|
|
514
|
-
return;
|
|
515
|
-
}
|
|
516
|
-
this.isDestroyed__ = true;
|
|
517
|
-
for (const subscription of this.dependencySubscriptions__) {
|
|
518
|
-
subscription.unsubscribe();
|
|
519
|
-
}
|
|
520
|
-
this.dependencySubscriptions__.length = 0;
|
|
521
|
-
this.config_.onDestroy?.();
|
|
522
|
-
this.config_ = null;
|
|
523
|
-
}
|
|
524
|
-
};
|
|
525
|
-
|
|
526
|
-
// src/creators/event.ts
|
|
527
|
-
function createEventSignal(config) {
|
|
528
|
-
return new EventSignal(config);
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
// src/creators/state.ts
|
|
532
|
-
function createStateSignal(config) {
|
|
533
|
-
return new StateSignal(config);
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
// src/creators/computed.ts
|
|
537
|
-
function createComputedSignal(config) {
|
|
538
|
-
return new ComputedSignal(config);
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
// src/creators/effect.ts
|
|
542
|
-
function createEffect(config) {
|
|
543
|
-
return new EffectSignal(config);
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
// src/operators/debounce.ts
|
|
547
|
-
import { createDebouncer } from "@alwatr/debounce";
|
|
548
|
-
function createDebouncedSignal(sourceSignal, config) {
|
|
549
|
-
const signalId = config.signalId ?? `${sourceSignal.signalId}-debounced`;
|
|
550
|
-
const internalSignal = new StateSignal({
|
|
551
|
-
signalId: `${signalId}-internal`,
|
|
552
|
-
initialValue: sourceSignal.get()
|
|
553
|
-
});
|
|
554
|
-
const debouncer = createDebouncer({
|
|
555
|
-
...config,
|
|
556
|
-
func: (value) => {
|
|
557
|
-
internalSignal.set(value);
|
|
558
|
-
}
|
|
559
|
-
});
|
|
560
|
-
const subscription = sourceSignal.subscribe(debouncer.trigger);
|
|
561
|
-
return createComputedSignal({
|
|
562
|
-
signalId,
|
|
563
|
-
deps: [internalSignal],
|
|
564
|
-
get: () => internalSignal.get(),
|
|
565
|
-
onDestroy: () => {
|
|
566
|
-
if (internalSignal.isDestroyed) return;
|
|
567
|
-
subscription.unsubscribe();
|
|
568
|
-
debouncer.cancel();
|
|
569
|
-
internalSignal.destroy();
|
|
570
|
-
config.onDestroy?.();
|
|
571
|
-
config = null;
|
|
572
|
-
}
|
|
573
|
-
});
|
|
574
|
-
}
|
|
575
|
-
export {
|
|
576
|
-
ComputedSignal,
|
|
577
|
-
EffectSignal,
|
|
578
|
-
EventSignal,
|
|
579
|
-
SignalBase,
|
|
580
|
-
StateSignal,
|
|
581
|
-
createComputedSignal,
|
|
582
|
-
createDebouncedSignal,
|
|
583
|
-
createEffect,
|
|
584
|
-
createEventSignal,
|
|
585
|
-
createStateSignal
|
|
586
|
-
};
|
|
1
|
+
/* @alwatr/signal v5.2.2 */
|
|
2
|
+
var SignalBase=class{constructor(config_){this.config_=config_;this.signalId=this.config_.signalId;this.observers_=[];this.isDestroyed__=false}get isDestroyed(){return this.isDestroyed__}removeObserver_(observer){this.logger_.logMethod?.("removeObserver_");if(this.isDestroyed__){this.logger_.incident?.("removeObserver_","remove_observer_on_destroyed_signal");return}const index=this.observers_.indexOf(observer);if(index!==-1){this.observers_.splice(index,1)}}subscribe(callback,options){this.logger_.logMethodArgs?.("subscribe.base",options);this.checkDestroyed_();const observer={callback,options};if(options?.priority){this.observers_.unshift(observer)}else{this.observers_.push(observer)}return{unsubscribe:()=>this.removeObserver_(observer)}}notify_(value){this.logger_.logMethodArgs?.("notify_",value);if(this.isDestroyed__){this.logger_.incident?.("notify_","notify_on_destroyed_signal");return}const currentObservers=[...this.observers_];for(const observer of currentObservers){if(observer.options?.once){this.removeObserver_(observer)}try{const result=observer.callback(value);if(result instanceof Promise){result.catch(err=>this.logger_.error("notify_","async_callback_failed",err,{observer}))}}catch(err){this.logger_.error("notify_","sync_callback_failed",err)}}}untilNext(){this.logger_.logMethod?.("untilNext");this.checkDestroyed_();return new Promise(resolve=>{this.subscribe(resolve,{once:true,priority:true,receivePrevious:false})})}destroy(){this.logger_.logMethod?.("destroy");if(this.isDestroyed__){this.logger_.incident?.("destroy_","double_destroy_attempt");return}this.isDestroyed__=true;this.observers_.length=0;this.config_.onDestroy?.();this.config_=null}checkDestroyed_(){if(this.isDestroyed__){this.logger_.accident("checkDestroyed_","attempt_to_use_destroyed_signal");throw new Error(`Cannot interact with a destroyed signal (id: ${this.signalId})`)}}};import{delay}from"@alwatr/delay";import{createLogger}from"@alwatr/logger";var EventSignal=class extends SignalBase{constructor(config){super(config);this.logger_=createLogger(`event-signal: ${this.signalId}`);this.logger_.logMethod?.("constructor")}dispatch(payload){this.logger_.logMethodArgs?.("dispatch",{payload});this.checkDestroyed_();delay.nextMicrotask().then(()=>this.notify_(payload))}};import{delay as delay2}from"@alwatr/delay";import{createLogger as createLogger2}from"@alwatr/logger";var StateSignal=class extends SignalBase{constructor(config){super(config);this.logger_=createLogger2(`state-signal: ${this.signalId}`);this.value__=config.initialValue;this.logger_.logMethodArgs?.("constructor",{initialValue:this.value__})}get(){this.checkDestroyed_();return this.value__}set(newValue){this.logger_.logMethodArgs?.("set",{newValue});this.checkDestroyed_();if(Object.is(this.value__,newValue)&&(typeof newValue!=="object"||newValue===null)){return}this.value__=newValue;delay2.nextMicrotask().then(()=>this.notify_(newValue))}update(updater){this.logger_.logMethod?.("update");this.checkDestroyed_();this.set(updater(this.value__))}subscribe(callback,options={}){this.logger_.logMethodArgs?.("subscribe",options);this.checkDestroyed_();if(options.receivePrevious!==false){delay2.nextMicrotask().then(()=>{this.logger_.logStep?.("subscribe","immediate_callback");callback(this.value__)}).catch(err=>this.logger_.error("subscribe","immediate_callback_failed",err));if(options.once){return{unsubscribe:()=>{}}}}return super.subscribe(callback,options)}destroy(){this.value__=null;super.destroy()}};import{delay as delay3}from"@alwatr/delay";import{createLogger as createLogger3}from"@alwatr/logger";var ComputedSignal=class{constructor(config_){this.config_=config_;this.signalId=this.config_.signalId;this.logger_=createLogger3(`computed-signal: ${this.signalId}`);this.internalSignal_=new StateSignal({signalId:`${this.signalId}-internal`,initialValue:this.config_.get()});this.dependencySubscriptions__=[];this.isRecalculating__=false;this.logger_.logMethod?.("constructor");this.recalculate_=this.recalculate_.bind(this);for(const signal of config_.deps){this.dependencySubscriptions__.push(signal.subscribe(this.recalculate_,{receivePrevious:false}))}}get(){return this.internalSignal_.get()}get isDestroyed(){return this.internalSignal_.isDestroyed}subscribe(callback,options){return this.internalSignal_.subscribe(callback,options)}untilNext(){return this.internalSignal_.untilNext()}destroy(){this.logger_.logMethod?.("destroy");if(this.isDestroyed){this.logger_.incident?.("destroy","already_destroyed");return}for(const subscription of this.dependencySubscriptions__){subscription.unsubscribe()}this.dependencySubscriptions__.length=0;this.internalSignal_.destroy();this.config_.onDestroy?.();this.config_=null}async recalculate_(){this.logger_.logMethod?.("recalculate_");if(this.internalSignal_.isDestroyed){this.logger_.incident?.("recalculate_","recalculate_on_destroyed_signal");return}if(this.isRecalculating__){this.logger_.logStep?.("recalculate_","skipping_recalculation_already_scheduled");return}this.isRecalculating__=true;try{await delay3.nextMacrotask();if(this.isDestroyed){this.logger_.incident?.("recalculate_","destroyed_during_delay");this.isRecalculating__=false;return}this.logger_.logStep?.("recalculate_","recalculating_value");this.internalSignal_.set(this.config_.get())}catch(err){this.logger_.error("recalculate_","recalculation_failed",err)}this.isRecalculating__=false}};import{delay as delay4}from"@alwatr/delay";import{createLogger as createLogger4}from"@alwatr/logger";var EffectSignal=class{constructor(config_){this.config_=config_;this.signalId=this.config_.signalId??`[${this.config_.deps.map(dep=>dep.signalId).join(", ")}]`;this.logger_=createLogger4(`effect-signal: ${this.signalId}`);this.dependencySubscriptions__=[];this.isRunning__=false;this.isDestroyed__=false;this.logger_.logMethod?.("constructor");this.scheduleExecution_=this.scheduleExecution_.bind(this);for(const signal of config_.deps){this.dependencySubscriptions__.push(signal.subscribe(this.scheduleExecution_,{receivePrevious:false}))}if(config_.runImmediately===true){void this.scheduleExecution_()}}get isDestroyed(){return this.isDestroyed__}async scheduleExecution_(){this.logger_.logMethod?.("scheduleExecution_");if(this.isDestroyed__){this.logger_.incident?.("scheduleExecution_","schedule_execution_on_destroyed_signal");return}if(this.isRunning__){this.logger_.logStep?.("scheduleExecution_","skipped_because_already_running");return}this.isRunning__=true;try{await delay4.nextMacrotask();if(this.isDestroyed__){this.logger_.incident?.("scheduleExecution_","destroyed_during_delay");this.isRunning__=false;return}this.logger_.logStep?.("scheduleExecution_","executing_effect");await this.config_.run()}catch(err){this.logger_.error("scheduleExecution_","effect_failed",err)}this.isRunning__=false}destroy(){this.logger_.logMethod?.("destroy");if(this.isDestroyed__){this.logger_.incident?.("destroy","already_destroyed");return}this.isDestroyed__=true;for(const subscription of this.dependencySubscriptions__){subscription.unsubscribe()}this.dependencySubscriptions__.length=0;this.config_.onDestroy?.();this.config_=null}};function createEventSignal(config){return new EventSignal(config)}function createStateSignal(config){return new StateSignal(config)}function createComputedSignal(config){return new ComputedSignal(config)}function createEffect(config){return new EffectSignal(config)}import{createDebouncer}from"@alwatr/debounce";function createDebouncedSignal(sourceSignal,config){const signalId=config.signalId??`${sourceSignal.signalId}-debounced`;const internalSignal=new StateSignal({signalId:`${signalId}-internal`,initialValue:sourceSignal.get()});const debouncer=createDebouncer({...config,func:value=>{internalSignal.set(value)}});const subscription=sourceSignal.subscribe(value=>debouncer.trigger(value),{receivePrevious:false});return createComputedSignal({signalId,deps:[internalSignal],get:()=>internalSignal.get(),onDestroy:()=>{if(internalSignal.isDestroyed)return;subscription.unsubscribe();debouncer.cancel();internalSignal.destroy();config.onDestroy?.();config=null}})}export{ComputedSignal,EffectSignal,EventSignal,SignalBase,StateSignal,createComputedSignal,createDebouncedSignal,createEffect,createEventSignal,createStateSignal};
|
|
587
3
|
//# sourceMappingURL=main.mjs.map
|