@mmstack/primitives 19.2.3 → 20.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,11 +1,1306 @@
1
- export * from './lib/debounced';
2
- export * from './lib/derived';
3
- export * from './lib/element-visibility';
4
- export * from './lib/map-array';
5
- export * from './lib/mutable';
6
- export * from './lib/sensors';
7
- export * from './lib/stored';
8
- export * from './lib/throttled';
9
- export * from './lib/to-writable';
10
- export * from './lib/until';
11
- export * from './lib/with-history';
1
+ import { CreateSignalOptions, DestroyRef, WritableSignal, Signal, ElementRef, Injector, ValueEqualityFn } from '@angular/core';
2
+ import { UnknownObject } from '@mmstack/object';
3
+
4
+ /**
5
+ * Options for creating a debounced writable signal.
6
+ * Extends Angular's `CreateSignalOptions` with a debounce time setting.
7
+ *
8
+ * @template T The type of value held by the signal.
9
+ */
10
+ type CreateDebouncedOptions<T> = CreateSignalOptions<T> & {
11
+ /**
12
+ * The debounce delay in milliseconds. Specifies how long to wait after the
13
+ * last `set` or `update` call before the debounced signal reflects the new value.
14
+ */
15
+ ms?: number;
16
+ /**
17
+ * Optional `DestroyRef` to clean up the debounce timer when the signal is destroyed.
18
+ * If provided, the timer will be cleared when the signal is destroyed.
19
+ * If the signal is called within a reactive context a DestroyRef is injected automatically.
20
+ * If it is not provided or injected, the timer will not be cleared automatically...which is usually fine :)
21
+ */
22
+ destroyRef?: DestroyRef;
23
+ };
24
+ /**
25
+ * A specialized `WritableSignal` whose publicly readable value updates are debounced.
26
+ *
27
+ * It provides access to the underlying, non-debounced signal via the `original` property.
28
+ *
29
+ * @template T The type of value held by the signal.
30
+ */
31
+ type DebouncedSignal<T> = WritableSignal<T> & {
32
+ /**
33
+ * A reference to the original, inner `WritableSignal`.
34
+ * This signal's value is updated *immediately* upon calls to `set` or `update`
35
+ * on the parent `DebouncedSignal`. Useful for accessing the latest value
36
+ * without the debounce delay.
37
+ */
38
+ original: Signal<T>;
39
+ };
40
+ /**
41
+ * A convenience function that creates and debounces a new `WritableSignal` in one step.
42
+ *
43
+ * @see {debounce} for the core implementation details.
44
+ *
45
+ * @template T The type of value the signal holds.
46
+ * @param initial The initial value of the signal.
47
+ * @param opt Options for signal creation, including debounce time `ms`.
48
+ * @returns A `DebouncedSignal<T>` instance.
49
+ *
50
+ * @example
51
+ * // The existing example remains perfect here.
52
+ * const query = debounced('', { ms: 500 });
53
+ * effect(() => console.log('Debounced Query:', query()));
54
+ * query.set('abc');
55
+ * // ...500ms later...
56
+ * // Output: Debounced Query: abc
57
+ */
58
+ declare function debounced<T>(initial: T, opt?: CreateDebouncedOptions<T>): DebouncedSignal<T>;
59
+ /**
60
+ * Wraps an existing `WritableSignal` to create a new one whose readable value is debounced.
61
+ *
62
+ * This implementation avoids using `effect` by pairing a trigger signal with an `untracked`
63
+ * read of the source signal to control when the debounced value is re-evaluated.
64
+ *
65
+ * @template T The type of value the signal holds.
66
+ * @param source The source `WritableSignal` to wrap. Writes are applied to this signal immediately.
67
+ * @param opt Options for debouncing, including debounce time `ms` and an optional `DestroyRef`.
68
+ * @returns A new `DebouncedSignal<T>` whose read value is debounced. The `.original` property
69
+ * of the returned signal is a reference back to the provided `source` signal.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * import { signal, effect } from '@angular/core';
74
+ *
75
+ * // 1. Create a standard source signal.
76
+ * const sourceQuery = signal('');
77
+ *
78
+ * // 2. Create a debounced version of it.
79
+ * const debouncedQuery = debounce(sourceQuery, { ms: 500 });
80
+ *
81
+ * // This effect tracks the original signal and runs immediately.
82
+ * effect(() => {
83
+ * console.log('Original Query:', debouncedQuery.original());
84
+ * });
85
+ *
86
+ * // This effect tracks the debounced signal and runs after the delay.
87
+ * effect(() => {
88
+ * console.log('Debounced Query:', debouncedQuery());
89
+ * });
90
+ *
91
+ * console.log('Setting query to "a"');
92
+ * debouncedQuery.set('a');
93
+ * // Output: Original Query: a
94
+ *
95
+ * // ...500ms later...
96
+ * // Output: Debounced Query: a
97
+ * ```
98
+ */
99
+ declare function debounce<T>(source: WritableSignal<T>, opt?: CreateDebouncedOptions<T>): DebouncedSignal<T>;
100
+
101
+ /**
102
+ * Options for creating a derived signal using the full `derived` function signature.
103
+ * @typeParam T - The type of the source signal's value (parent).
104
+ * @typeParam U - The type of the derived signal's value (child).
105
+ */
106
+ type CreateDerivedOptions<T, U> = CreateSignalOptions<U> & {
107
+ /**
108
+ * A function that extracts the derived value (`U`) from the source signal's value (`T`).
109
+ */
110
+ from: (v: T) => U;
111
+ /**
112
+ * A function that updates the source signal's value (`T`) when the derived signal's value (`U`) changes.
113
+ * This establishes the two-way binding.
114
+ */
115
+ onChange: (newValue: U) => void;
116
+ };
117
+ /**
118
+ * A `WritableSignal` that derives its value from another `WritableSignal` (the "source" signal).
119
+ * It provides two-way binding: changes to the source signal update the derived signal, and
120
+ * changes to the derived signal update the source signal.
121
+ *
122
+ * @typeParam T - The type of the source signal's value (parent).
123
+ * @typeParam U - The type of the derived signal's value (child).
124
+ */
125
+ type DerivedSignal<T, U> = WritableSignal<U> & {
126
+ /**
127
+ * The function used to derive the derived signal's value from the source signal's value.
128
+ * This is primarily for internal use and introspection.
129
+ */
130
+ from: (v: T) => U;
131
+ };
132
+ /**
133
+ * Creates a `DerivedSignal` that derives its value from another `WritableSignal`.
134
+ * This overload provides the most flexibility, allowing you to specify custom `from` and `onChange` functions.
135
+ *
136
+ * @typeParam T The type of the source signal's value.
137
+ * @typeParam U The type of the derived signal's value.
138
+ * @param source The source `WritableSignal`.
139
+ * @param options An object containing the `from` and `onChange` functions, and optional signal options.
140
+ * @returns A `DerivedSignal` instance.
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * const user = signal({ name: 'John', age: 30 });
145
+ * const name = derived(user, {
146
+ * from: (u) => u.name,
147
+ * onChange: (newName) => user.update((u) => ({ ...u, name: newName })),
148
+ * });
149
+ *
150
+ * name.set('Jane'); // Updates the original signal
151
+ * console.log(user().name); // Outputs: Jane
152
+ * ```
153
+ */
154
+ declare function derived<T, U>(source: WritableSignal<T>, opt: CreateDerivedOptions<T, U>): DerivedSignal<T, U>;
155
+ /**
156
+ * Creates a `DerivedSignal` that derives a property from an object held by the source signal.
157
+ * This overload is a convenient shorthand for accessing object properties.
158
+ *
159
+ * @typeParam T The type of the source signal's value (must be an object).
160
+ * @typeParam TKey The key of the property to derive.
161
+ * @param source The source `WritableSignal` (holding an object).
162
+ * @param key The key of the property to derive.
163
+ * @param options Optional signal options for the derived signal.
164
+ * @returns A `DerivedSignal` instance.
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * const user = signal({ name: 'John', age: 30 });
169
+ * const name = derived(user, 'name');
170
+ *
171
+ * console.log(name()); // Outputs: John
172
+ *
173
+ * // Update the derived signal, which also updates the source
174
+ * name.set('Jane');
175
+ *
176
+ * console.log(user().name); // Outputs: Jane
177
+ * ```
178
+ */
179
+ declare function derived<T extends UnknownObject, TKey extends keyof T>(source: WritableSignal<T>, key: TKey, opt?: CreateSignalOptions<T[TKey]>): DerivedSignal<T, T[TKey]>;
180
+ /**
181
+ * Creates a `DerivedSignal` from an array, deriving an element by its index.
182
+ * This overload is a convenient shorthand for accessing array elements.
183
+ *
184
+ * @typeParam T The type of the source signal's value (must be an array).
185
+ * @param source The source `WritableSignal` (holding an array).
186
+ * @param index The index of the element to derive.
187
+ * @param options Optional signal options for the derived signal.
188
+ * @returns A `DerivedSignal` instance.
189
+ *
190
+ * @example
191
+ * ```ts
192
+ * const numbers = signal([1, 2, 3]);
193
+ * const secondNumber = derived(numbers, 1);
194
+ *
195
+ * console.log(secondNumber()); // Outputs: 2
196
+ *
197
+ * // Update the derived signal, which also updates the source
198
+ * secondNumber.set(5);
199
+ *
200
+ * console.log(numbers()); // Outputs: [1, 5, 3]
201
+ * ```
202
+ */
203
+ declare function derived<T extends any[]>(source: WritableSignal<T>, index: number, opt?: CreateSignalOptions<T[number]>): DerivedSignal<T, T[number]>;
204
+ /**
205
+ * Creates a "fake" `DerivedSignal` from a simple value. This is useful for creating
206
+ * `FormControlSignal` instances that are not directly derived from another signal.
207
+ * The returned signal's `from` function will always return the initial value.
208
+ *
209
+ * @typeParam T - This type parameter is not used in the implementation but is kept for type compatibility with `DerivedSignal`.
210
+ * @typeParam U - The type of the signal's value.
211
+ * @param initial - The initial value of the signal.
212
+ * @returns A `DerivedSignal` instance.
213
+ * @internal
214
+ */
215
+ declare function toFakeDerivation<T, U>(initial: U): DerivedSignal<T, U>;
216
+ /**
217
+ * Creates a "fake" `DerivedSignal` from an existing `WritableSignal`. This is useful
218
+ * for treating a regular `WritableSignal` as a `DerivedSignal` without changing its behavior.
219
+ * The returned signal's `from` function returns the current value of signal, using `untracked`.
220
+ *
221
+ * @typeParam T - This type parameter is not used in the implementation but is kept for type compatibility with `DerivedSignal`.
222
+ * @typeParam U - The type of the signal's value.
223
+ * @param initial - The existing `WritableSignal`.
224
+ * @returns A `DerivedSignal` instance.
225
+ * @internal
226
+ */
227
+ declare function toFakeSignalDerivation<T, U>(initial: WritableSignal<U>): DerivedSignal<T, U>;
228
+ /**
229
+ * Type guard function to check if a given `WritableSignal` is a `DerivedSignal`.
230
+ *
231
+ * @typeParam T - The type of the source signal's value (optional, defaults to `any`).
232
+ * @typeParam U - The type of the derived signal's value (optional, defaults to `any`).
233
+ * @param sig - The `WritableSignal` to check.
234
+ * @returns `true` if the signal is a `DerivedSignal`, `false` otherwise.
235
+ */
236
+ declare function isDerivation<T, U>(sig: WritableSignal<U>): sig is DerivedSignal<T, U>;
237
+
238
+ /**
239
+ * Options for configuring the `elementVisibility` sensor, extending
240
+ * standard `IntersectionObserverInit` options.
241
+ */
242
+ type ElementVisibilityOptions = IntersectionObserverInit & {
243
+ /** Optional debug name for the internal signal. */
244
+ debugName?: string;
245
+ };
246
+ type ElementVisibilitySignal = Signal<IntersectionObserverEntry | undefined> & {
247
+ readonly visible: Signal<boolean>;
248
+ };
249
+ /**
250
+ * Creates a read-only signal that tracks the intersection status of a target DOM element
251
+ * with the viewport or a specified root element, using the `IntersectionObserver` API.
252
+ *
253
+ * It can observe a static `ElementRef`/`Element` or a `Signal` that resolves to one,
254
+ * allowing for dynamic targets.
255
+ *
256
+ * @param target The DOM element (or `ElementRef`, or a `Signal` resolving to one) to observe.
257
+ * If the signal resolves to `null`, observation stops.
258
+ * @param options Optional `IntersectionObserverInit` options (e.g., `root`, `rootMargin`, `threshold`)
259
+ * and an optional `debugName`.
260
+ * @returns A `Signal<IntersectionObserverEntry | undefined>`. It emits `undefined` initially,
261
+ * on the server, or if the target is `null`. Otherwise, it emits the latest
262
+ * `IntersectionObserverEntry`. Consumers can derive a boolean `isVisible` from
263
+ * this entry's `isIntersecting` property.
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * import { Component, effect, ElementRef, viewChild } from '@angular/core';
268
+ * import { elementVisibility } from '@mmstack/primitives';
269
+ * import { computed } from '@angular/core'; // For derived boolean
270
+ *
271
+ * @Component({
272
+ * selector: 'app-lazy-image',
273
+ * template: `
274
+ * <div #imageContainer style="height: 200px; border: 1px dashed grey;">
275
+ * @if (isVisible()) {
276
+ * <img src="your-image-url.jpg" alt="Lazy loaded image" />
277
+ * <p>Image is VISIBLE!</p>
278
+ * } @else {
279
+ * <p>Scroll down to see the image...</p>
280
+ * }
281
+ * </div>
282
+ * `
283
+ * })
284
+ * export class LazyImageComponent {
285
+ * readonly imageContainer = viewChild.required<ElementRef<HTMLDivElement>>('imageContainer');
286
+ *
287
+ * // Observe the element, get the full IntersectionObserverEntry
288
+ * readonly intersectionEntry = elementVisibility(this.imageContainer);
289
+ *
290
+ * // Derive a simple boolean for visibility
291
+ * readonly isVisible = computed(() => this.intersectionEntry()?.isIntersecting ?? false);
292
+ *
293
+ * constructor() {
294
+ * effect(() => {
295
+ * console.log('Intersection Entry:', this.intersectionEntry());
296
+ * console.log('Is Visible:', this.isVisible());
297
+ * });
298
+ * }
299
+ * }
300
+ * ```
301
+ */
302
+ declare function elementVisibility(target?: ElementRef<Element> | Element | Signal<ElementRef<Element> | Element | null>, opt?: ElementVisibilityOptions): ElementVisibilitySignal;
303
+
304
+ /**
305
+ * Reactively maps items from a source array (or signal of an array) using a provided mapping function.
306
+ *
307
+ * This function serves a similar purpose to SolidJS's `mapArray` by providing stability
308
+ * for mapped items. It receives a source function returning an array (or a Signal<T[]>)
309
+ * and a mapping function.
310
+ *
311
+ * For each item in the source array, it creates a stable `computed` signal representing
312
+ * that item's value at its current index. This stable signal (`Signal<T>`) is passed
313
+ * to the mapping function. This ensures that downstream computations or components
314
+ * depending on the mapped result only re-render or re-calculate for the specific items
315
+ * that have changed, or when items are added/removed, rather than re-evaluating everything
316
+ * when the source array reference changes but items remain the same.
317
+ *
318
+ * It efficiently handles changes in the source array's length by reusing existing mapped
319
+ * results when possible, slicing when the array shrinks, and appending new mapped items
320
+ * when it grows.
321
+ *
322
+ * @template T The type of items in the source array.
323
+ * @template U The type of items in the resulting mapped array.
324
+ *
325
+ * @param source A function returning the source array `T[]`, or a `Signal<T[]>` itself.
326
+ * The `mapArray` function will reactively update based on changes to this source.
327
+ * @param map The mapping function. It is called for each item in the source array.
328
+ * It receives:
329
+ * - `value`: A stable `Signal<T>` representing the item at the current index.
330
+ * Use this signal within your mapping logic if you need reactivity
331
+ * tied to the specific item's value changes.
332
+ * - `index`: The number index of the item in the array.
333
+ * It should return the mapped value `U`.
334
+ * @param [opt] Optional `CreateSignalOptions<T>`. These options are passed directly
335
+ * to the `computed` signal created for each individual item (`Signal<T>`).
336
+ * This allows specifying options like a custom `equal` function for item comparison.
337
+ *
338
+ * @returns A `Signal<U[]>` containing the mapped array. This signal updates whenever
339
+ * the source array changes (either length or the values of its items).
340
+ *
341
+ * @example
342
+ * ```ts
343
+ * const sourceItems = signal([
344
+ * { id: 1, name: 'Apple' },
345
+ * { id: 2, name: 'Banana' }
346
+ * ]);
347
+ *
348
+ * const mappedItems = mapArray(
349
+ * sourceItems,
350
+ * (itemSignal, index) => {
351
+ * // itemSignal is stable for a given item based on its index.
352
+ * // We create a computed here to react to changes in the item's name.
353
+ * return computed(() => `${index}: ${itemSignal().name.toUpperCase()}`);
354
+ * },
355
+ * // Example optional options (e.g., custom equality for item signals)
356
+ * { equal: (a, b) => a.id === b.id && a.name === b.name }
357
+ * );
358
+ * ```
359
+ * @remarks
360
+ * This function achieves its high performance by leveraging the new `linkedSignal`
361
+ * API from Angular, which allows for efficient memoization and reuse of array items.
362
+ */
363
+ declare function mapArray<T, U>(source: () => T[], map: (value: Signal<T>, index: number) => U, opt?: CreateSignalOptions<T>): Signal<U[]>;
364
+
365
+ /**
366
+ * A `MutableSignal` is a special type of `WritableSignal` that allows for in-place mutation of its value.
367
+ * In addition to the standard `set` and `update` methods, it provides a `mutate` method. This is useful
368
+ * for performance optimization when dealing with complex objects or arrays, as it avoids unnecessary
369
+ * object copying.
370
+ *
371
+ * @typeParam T - The type of value held by the signal.
372
+ */
373
+ type MutableSignal<T> = WritableSignal<T> & {
374
+ /**
375
+ * Mutates the signal's value in-place. This is similar to `update`, but it's optimized for
376
+ * scenarios where you want to modify the existing object directly rather than creating a new one.
377
+ *
378
+ * @param updater - A function that takes the current value as input and modifies it directly.
379
+ *
380
+ * @example
381
+ * const myArray = mutable([1, 2, 3]);
382
+ * myArray.mutate((arr) => {
383
+ * arr.push(4);
384
+ * return arr;
385
+ * }); // myArray() now returns [1, 2, 3, 4]
386
+ */
387
+ mutate: WritableSignal<T>['update'];
388
+ /**
389
+ * Mutates the signal's value in-place, similar to `mutate`, but with a void-returning value in updater
390
+ * function. This further emphasizes that the mutation is happening inline, improving readability
391
+ * in some cases.
392
+ * @param updater - Function to change to the current value
393
+ * @example
394
+ * const myObject = mutable({ a: 1, b: 2 });
395
+ * myObject.inline((obj) => (obj.a = 3)); // myObject() now returns { a: 3, b: 2 }
396
+ */
397
+ inline: (updater: (value: T) => void) => void;
398
+ };
399
+ /**
400
+ * Creates a `MutableSignal`. This function overloads the standard `signal` function to provide
401
+ * the additional `mutate` and `inline` methods.
402
+ *
403
+ * @typeParam T The type of value held by the signal.
404
+ * @param initial The initial value of the signal.
405
+ * @param options Optional signal options, including a custom `equal` function.
406
+ * @returns A `MutableSignal` instance.
407
+ *
408
+ * ### Important Note on `computed` Signals
409
+ *
410
+ * When creating a `computed` signal that derives a non-primitive value (e.g., an object or array)
411
+ * from a `mutable` signal, you **must** provide the `{ equal: false }` option to the `computed`
412
+ * function.
413
+ *
414
+ * This is because a `.mutate()` call notifies its dependents that it has changed, but if the
415
+ * reference to a derived object hasn't changed, the `computed` signal will not trigger its
416
+ * own dependents by default.
417
+ *
418
+ * @example
419
+ * ```ts
420
+ * const state = mutable({ user: { name: 'John' }, lastUpdated: new Date() });
421
+ *
422
+ * // ✅ CORRECT: Deriving a primitive value works as expected.
423
+ * const name = computed(() => state().user.name);
424
+ *
425
+ * // ❌ INCORRECT: This will not update reliably after the first change.
426
+ * const userObject = computed(() => state().user);
427
+ *
428
+ * // ✅ CORRECT: For object derivations, `equal: false` is required.
429
+ * const userObjectFixed = computed(() => state().user, { equal: false });
430
+ *
431
+ * // This mutation will now correctly trigger effects depending on `userObjectFixed`.
432
+ * state.mutate(s => s.lastUpdated = new Date());
433
+ * ```
434
+ */
435
+ declare function mutable<T>(): MutableSignal<T | undefined>;
436
+ declare function mutable<T>(initial: T): MutableSignal<T>;
437
+ declare function mutable<T>(initial: T, opt?: CreateSignalOptions<T>): MutableSignal<T>;
438
+ /**
439
+ * Type guard function to check if a given `WritableSignal` is a `MutableSignal`. This is useful
440
+ * for situations where you need to conditionally use the `mutate` or `inline` methods.
441
+ *
442
+ * @typeParam T - The type of the signal's value (optional, defaults to `any`).
443
+ * @param value - The `WritableSignal` to check.
444
+ * @returns `true` if the signal is a `MutableSignal`, `false` otherwise.
445
+ *
446
+ * @example
447
+ * const mySignal = signal(0);
448
+ * const myMutableSignal = mutable(0);
449
+ *
450
+ * if (isMutable(mySignal)) {
451
+ * mySignal.mutate(x => x + 1); // This would cause a type error, as mySignal is not a MutableSignal.
452
+ * }
453
+ *
454
+ * if (isMutable(myMutableSignal)) {
455
+ * myMutableSignal.mutate(x => x + 1); // This is safe.
456
+ * }
457
+ */
458
+ declare function isMutable<T = any>(value: WritableSignal<T>): value is MutableSignal<T>;
459
+
460
+ /**
461
+ * Creates a read-only signal that reactively tracks whether a CSS media query
462
+ * string currently matches.
463
+ *
464
+ * It uses `window.matchMedia` to evaluate the query and listen for changes.
465
+ * The primitive is SSR-safe (defaults to `false` on the server) and automatically
466
+ * cleans up its event listeners when the creating context is destroyed.
467
+ *
468
+ * @param query The CSS media query string to evaluate (e.g., `'(min-width: 768px)'`, `'(prefers-color-scheme: dark)'`).
469
+ * @param debugName Optional debug name for the signal.
470
+ * @returns A read-only `Signal<boolean>` which is `true` if the media query
471
+ * currently matches, and `false` otherwise.
472
+ *
473
+ * @remarks
474
+ * - On the server, this signal will always return `false` by default.
475
+ * - It automatically updates if the match status of the media query changes in the browser.
476
+ * - Event listeners are cleaned up automatically via `DestroyRef` if created in an injection context.
477
+ *
478
+ * @example
479
+ * ```ts
480
+ * import { Component, effect } from '@angular/core';
481
+ * import { mediaQuery } from '@mmstack/primitives';
482
+ *
483
+ * @Component({
484
+ * selector: 'app-responsive-layout',
485
+ * template: `
486
+ * @if (isDesktop()) {
487
+ * <p>Showing desktop layout.</p>
488
+ * } @else {
489
+ * <p>Showing mobile layout.</p>
490
+ * }
491
+ * `
492
+ * })
493
+ * export class ResponsiveLayoutComponent {
494
+ * readonly isDesktop = mediaQuery('(min-width: 1024px)');
495
+ *
496
+ * constructor() {
497
+ * effect(() => {
498
+ * console.log('Is desktop view:', this.isDesktop());
499
+ * });
500
+ * }
501
+ * }
502
+ * ```
503
+ */
504
+ declare function mediaQuery(query: string, debugName?: string): Signal<boolean>;
505
+ /**
506
+ * Creates a read-only signal that tracks the user's OS/browser preference
507
+ * for a dark color scheme using the `(prefers-color-scheme: dark)` media query.
508
+ *
509
+ * This is a convenience wrapper around the generic `mediaQuery` primitive.
510
+ * It's SSR-safe (defaults to `false` on the server) and automatically
511
+ * cleans up its event listeners.
512
+ *
513
+ * @param debugName Optional debug name for the signal.
514
+ * @returns A read-only `Signal<boolean>` which is `true` if a dark theme
515
+ * is preferred, and `false` otherwise.
516
+ * @see {mediaQuery} for the underlying implementation.
517
+ *
518
+ * @example
519
+ * ```ts
520
+ * const isDarkMode = prefersDarkMode();
521
+ * effect(() => {
522
+ * document.body.classList.toggle('dark-theme', isDarkMode());
523
+ * });
524
+ * ```
525
+ */
526
+ declare function prefersDarkMode(debugName?: string): Signal<boolean>;
527
+ /**
528
+ * Creates a read-only signal that tracks the user's OS/browser preference
529
+ * for reduced motion using the `(prefers-reduced-motion: reduce)` media query.
530
+ *
531
+ * This is a convenience wrapper around the generic `mediaQuery` primitive.
532
+ * It's SSR-safe (defaults to `false` on the server) and automatically
533
+ * cleans up its event listeners.
534
+ *
535
+ * @param debugName Optional debug name for the signal.
536
+ * @returns A read-only `Signal<boolean>` which is `true` if reduced motion
537
+ * is preferred, and `false` otherwise.
538
+ * @see {mediaQuery} for the underlying implementation.
539
+ *
540
+ * @example
541
+ * ```ts
542
+ * const reduceMotion = prefersReducedMotion();
543
+ * effect(() => {
544
+ * if (reduceMotion()) {
545
+ * // Apply simplified animations or disable them
546
+ * } else {
547
+ * // Apply full animations
548
+ * }
549
+ * });
550
+ * ```
551
+ */
552
+ declare function prefersReducedMotion(debugName?: string): Signal<boolean>;
553
+
554
+ type MousePosition = {
555
+ x: number;
556
+ y: number;
557
+ };
558
+ /**
559
+ * Options for configuring the `mousePosition` sensor.
560
+ */
561
+ type MousePositionOptions = {
562
+ /**
563
+ * The target element to listen for mouse movements on.
564
+ * Can be `window`, `document`, an `HTMLElement`, or an `ElementRef<HTMLElement>`.
565
+ * @default window
566
+ */
567
+ target?: Window | Document | HTMLElement | ElementRef<HTMLElement>;
568
+ /**
569
+ * Defines the coordinate system for the reported position.
570
+ * - `'client'`: Coordinates relative to the viewport (`clientX`, `clientY`).
571
+ * - `'page'`: Coordinates relative to the entire document (`pageX`, `pageY`).
572
+ * @default 'client'
573
+ */
574
+ coordinateSpace?: 'client' | 'page';
575
+ /**
576
+ * If `true`, the sensor will also listen to `touchmove` events and report
577
+ * the coordinates of the first touch point.
578
+ * @default false
579
+ */
580
+ touch?: boolean;
581
+ /**
582
+ * Optional debug name for the internal signal.
583
+ */
584
+ debugName?: string;
585
+ /**
586
+ * Optional delay in milliseconds to throttle the updates.
587
+ * @default 100
588
+ */
589
+ throttle?: number;
590
+ };
591
+ /**
592
+ * A specialized Signal that tracks mouse position within an element.
593
+ * It's a throttled signal of the mouse coordinates with an attached `unthrottled` signal.
594
+ */
595
+ type MousePositionSignal = Signal<MousePosition> & {
596
+ /** A signal providing the raw, unthrottled mouse position. */
597
+ readonly unthrottled: Signal<MousePosition>;
598
+ };
599
+ /**
600
+ * Creates a read-only signal that tracks the mouse cursor's position.
601
+ *
602
+ * It can track mouse movements on a specific target (window, document, or element)
603
+ * and optionally include touch movements. The coordinate space ('client' or 'page')
604
+ * can also be configured.
605
+ * The primitive is SSR-safe and automatically cleans up its event listeners.
606
+ *
607
+ * @param options Optional configuration for the sensor.
608
+ * @returns A read-only `Signal<MousePosition>`. On the server, it returns a static
609
+ * signal with `{ x: 0, y: 0 }`.
610
+ *
611
+ * @example
612
+ * ```ts
613
+ * import { Component, effect } from '@angular/core';
614
+ * import { mousePosition } from '@mmstack/primitives';
615
+ *
616
+ * @Component({
617
+ * selector: 'app-mouse-tracker',
618
+ * template: `<p>Mouse Position: X: {{ pos().x }}, Y: {{ pos().y }}</p>`
619
+ * })
620
+ * export class MouseTrackerComponent {
621
+ * readonly pos = mousePosition({ coordinateSpace: 'page' });
622
+ *
623
+ * constructor() {
624
+ * effect(() => {
625
+ * console.log('Mouse moved to:', this.pos());
626
+ * });
627
+ * }
628
+ * }
629
+ * ```
630
+ */
631
+ declare function mousePosition(opt?: MousePositionOptions): MousePositionSignal;
632
+
633
+ /**
634
+ * A specialized Signal that tracks network status.
635
+ * It's a boolean signal with an attached `since` signal.
636
+ */
637
+ type NetworkStatusSignal = Signal<boolean> & {
638
+ /** A signal tracking the timestamp of the last status change. */
639
+ readonly since: Signal<Date>;
640
+ };
641
+ /**
642
+ * Creates a read-only signal that tracks the browser's online status.
643
+ *
644
+ * The main signal returns a boolean (`true` for online, `false` for offline).
645
+ * An additional `since` signal is attached, tracking when the status last changed.
646
+ * It's SSR-safe and automatically cleans up its event listeners.
647
+ *
648
+ * @param debugName Optional debug name for the signal.
649
+ * @returns A `NetworkStatusSignal` instance.
650
+ */
651
+ declare function networkStatus(debugName?: string): NetworkStatusSignal;
652
+
653
+ /**
654
+ * Creates a read-only signal that tracks the page's visibility state.
655
+ *
656
+ * It uses the browser's Page Visibility API to reactively report if the
657
+ * current document is `'visible'`, `'hidden'`, or in another state.
658
+ * The primitive is SSR-safe and automatically cleans up its event listeners
659
+ * when the creating context is destroyed.
660
+ *
661
+ * @param debugName Optional debug name for the signal.
662
+ * @returns A read-only `Signal<DocumentVisibilityState>`. On the server,
663
+ * it returns a static signal with a value of `'visible'`.
664
+ *
665
+ * @example
666
+ * ```ts
667
+ * import { Component, effect } from '@angular/core';
668
+ * import { pageVisibility } from '@mmstack/primitives';
669
+ *
670
+ * @Component({
671
+ * selector: 'app-visibility-tracker',
672
+ * template: `<p>Page is currently: {{ visibilityState() }}</p>`
673
+ * })
674
+ * export class VisibilityTrackerComponent {
675
+ * readonly visibilityState = pageVisibility();
676
+ *
677
+ * constructor() {
678
+ * effect(() => {
679
+ * if (this.visibilityState() === 'hidden') {
680
+ * console.log('Page is hidden, pausing expensive animations...');
681
+ * } else {
682
+ * console.log('Page is visible, resuming activity.');
683
+ * }
684
+ * });
685
+ * }
686
+ * }
687
+ * ```
688
+ */
689
+ declare function pageVisibility(debugName?: string): Signal<DocumentVisibilityState>;
690
+
691
+ /**
692
+ * Represents the scroll position.
693
+ */
694
+ type ScrollPosition = {
695
+ /** The horizontal scroll position (pixels from the left). */
696
+ readonly x: number;
697
+ /** The vertical scroll position (pixels from the top). */
698
+ readonly y: number;
699
+ };
700
+ /**
701
+ * Options for configuring the `scrollPosition` sensor.
702
+ */
703
+ type ScrollPositionOptions = {
704
+ /**
705
+ * The target to listen for scroll events on.
706
+ * Can be `window` (for page scroll) or an `HTMLElement`/`ElementRef<HTMLElement>`.
707
+ * @default window
708
+ */
709
+ target?: Window | HTMLElement | ElementRef<HTMLElement>;
710
+ /**
711
+ * Optional delay in milliseconds to throttle the updates.
712
+ * Scroll events can fire very rapidly.
713
+ * @default 100 // A common default for scroll throttling
714
+ */
715
+ throttle?: number;
716
+ /** Optional debug name for the internal signal. */
717
+ debugName?: string;
718
+ };
719
+ /**
720
+ * A specialized Signal that tracks scroll position.
721
+ * It's a throttled signal of the scroll coordinates with an attached `unthrottled` signal.
722
+ */
723
+ type ScrollPositionSignal = Signal<ScrollPosition> & {
724
+ /** A signal providing the raw, unthrottled scroll position. */
725
+ readonly unthrottled: Signal<ScrollPosition>;
726
+ };
727
+ /**
728
+ * Creates a read-only signal that tracks the scroll position (x, y) of the window
729
+ * or a specified HTML element.
730
+ *
731
+ * Updates are throttled by default to optimize performance. An `unthrottled`
732
+ * property is available on the returned signal for direct access to raw updates.
733
+ * The primitive is SSR-safe and automatically cleans up its event listeners.
734
+ *
735
+ * @param options Optional configuration for the scroll sensor.
736
+ * @returns A `ScrollPositionSignal`. On the server, it returns a static
737
+ * signal with `{ x: 0, y: 0 }`.
738
+ *
739
+ * @example
740
+ * ```ts
741
+ * import { Component, effect, ElementRef, viewChild } from '@angular/core';
742
+ * import { scrollPosition } from '@mmstack/primitives';
743
+ *
744
+ * @Component({
745
+ * selector: 'app-scroll-tracker',
746
+ * template: `
747
+ * <p>Window Scroll: X: {{ windowScroll().x }}, Y: {{ windowScroll().y }}</p>
748
+ * <div #scrollableDiv style="height: 200px; width: 200px; overflow: auto; border: 1px solid black;">
749
+ * <div style="height: 400px; width: 400px;">Scroll me!</div>
750
+ * </div>
751
+ * @if (divScroll()) {
752
+ * <p>Div Scroll: X: {{ divScroll().x }}, Y: {{ divScroll().y }}</p>
753
+ * }
754
+ * `
755
+ * })
756
+ * export class ScrollTrackerComponent {
757
+ * readonly windowScroll = scrollPosition(); // Defaults to window
758
+ * readonly scrollableDiv = viewChild<ElementRef<HTMLDivElement>>('scrollableDiv');
759
+ * readonly divScroll = scrollPosition({ target: this.scrollableDiv() }); // Example with element target
760
+ *
761
+ * constructor() {
762
+ * effect(() => {
763
+ * console.log('Window scrolled to:', this.windowScroll());
764
+ * if (this.divScroll()) {
765
+ * console.log('Div scrolled to:', this.divScroll());
766
+ * }
767
+ * });
768
+ * }
769
+ * }
770
+ * ```
771
+ */
772
+ declare function scrollPosition(opt?: ScrollPositionOptions): ScrollPositionSignal;
773
+
774
+ /**
775
+ * Represents the dimensions of the window.
776
+ */
777
+ type WindowSize = {
778
+ /** The current inner width of the window in pixels. */
779
+ readonly width: number;
780
+ /** The current inner height of the window in pixels. */
781
+ readonly height: number;
782
+ };
783
+ /**
784
+ * Options for configuring the `mousePosition` sensor.
785
+ */
786
+ type WindowSizeOptions = {
787
+ /**
788
+ * Optional debug name for the internal signal.
789
+ */
790
+ debugName?: string;
791
+ /**
792
+ * Optional delay in milliseconds to throttle the updates.
793
+ * @default 100
794
+ */
795
+ throttle?: number;
796
+ };
797
+ /**
798
+ * A specialized Signal that tracks window size.
799
+ * It's a throttled signal of the window.innerHeight/innerWidth properties
800
+ * with an attached `unthrottled` signal.
801
+ */
802
+ type WindowSizeSignal = Signal<WindowSize> & {
803
+ /** A signal providing the raw, unthrottled window size. */
804
+ readonly unthrottled: Signal<WindowSize>;
805
+ };
806
+ /**
807
+ * Creates a read-only signal that tracks the browser window's inner dimensions (width and height).
808
+ *
809
+ * Updates are throttled by default (100ms) to optimize performance during resize events.
810
+ * An `unthrottled` property is available on the returned signal for direct access to raw updates.
811
+ * The primitive is SSR-safe (returns a default size on the server) and automatically
812
+ * cleans up its event listeners.
813
+ *
814
+ * @param opt Optional configuration, including `throttle` (ms) and `debugName`.
815
+ * @returns A `WindowSizeSignal` (a `Signal<WindowSize>` with an `unthrottled` property).
816
+ *
817
+ * @example
818
+ * ```ts
819
+ * import { Component, effect } from '@angular/core';
820
+ * import { windowSize } from '@mmstack/primitives';
821
+ *
822
+ * @Component({
823
+ * selector: 'app-responsive-header',
824
+ * template: `
825
+ * <header>
826
+ * Current Window Size: {{ size().width }}px x {{ size().height }}px
827
+ * @if (isMobile()) {
828
+ * <p>Mobile Menu</p>
829
+ * } @else {
830
+ * <p>Desktop Menu</p>
831
+ * }
832
+ * </header>
833
+ * `
834
+ * })
835
+ * export class ResponsiveHeaderComponent {
836
+ * readonly size = windowSize();
837
+ * readonly isMobile = computed(() => this.size().width < 768);
838
+ *
839
+ * constructor() {
840
+ * effect(() => {
841
+ * console.log('Window resized to:', this.size());
842
+ * });
843
+ * }
844
+ * }
845
+ * ```
846
+ */
847
+ declare function windowSize(opt?: WindowSizeOptions): WindowSizeSignal;
848
+
849
+ /**
850
+ * Creates a sensor signal that tracks the mouse cursor's position.
851
+ * @param type Must be `'mousePosition'`.
852
+ * @param options Optional configuration for the mouse position sensor.
853
+ * @returns A `MousePositionSignal` that tracks mouse coordinates and provides an unthrottled version.
854
+ * @see {mousePosition} for detailed documentation and examples.
855
+ * @example const pos = sensor('mousePosition', { coordinateSpace: 'page', throttle: 50 });
856
+ */
857
+ declare function sensor(type: 'mousePosition', options?: MousePositionOptions): MousePositionSignal;
858
+ /**
859
+ * Creates a sensor signal that tracks the browser's online/offline status.
860
+ * @param type Must be `'networkStatus'`.
861
+ * @param options Optional configuration, currently only `debugName`.
862
+ * @returns A `NetworkStatusSignal` which is a boolean indicating online status, with an attached `since` signal.
863
+ * @see {networkStatus} for detailed documentation and examples.
864
+ * @example const onlineStatus = sensor('networkStatus');
865
+ */
866
+ declare function sensor(type: 'networkStatus', options?: {
867
+ debugName?: string;
868
+ }): NetworkStatusSignal;
869
+ /**
870
+ * Creates a sensor signal that tracks the page's visibility state (e.g., 'visible', 'hidden').
871
+ * @param type Must be `'pageVisibility'`.
872
+ * @param options Optional configuration, currently only `debugName`.
873
+ * @returns A `Signal<DocumentVisibilityState>` indicating the page's current visibility.
874
+ * @see {pageVisibility} for detailed documentation and examples.
875
+ * @example const visibility = sensor('pageVisibility');
876
+ */
877
+ declare function sensor(type: 'pageVisibility', options?: {
878
+ debugName?: string;
879
+ }): Signal<DocumentVisibilityState>;
880
+ /**
881
+ * Creates a sensor signal that tracks the user's OS/browser preference for a dark color scheme.
882
+ * @param type Must be `'dark-mode'`.
883
+ * @param options Optional configuration, currently only `debugName`.
884
+ * @returns A `Signal<boolean>` which is `true` if a dark theme is preferred.
885
+ * @see {prefersDarkMode} for detailed documentation and examples.
886
+ * @example const isDarkMode = sensor('dark-mode');
887
+ */
888
+ declare function sensor(type: 'dark-mode', options?: {
889
+ debugName?: string;
890
+ }): Signal<boolean>;
891
+ /**
892
+ * Creates a sensor signal that tracks the user's OS/browser preference for reduced motion.
893
+ * @param type Must be `'reduced-motion'`.
894
+ * @param options Optional configuration, currently only `debugName`.
895
+ * @returns A `Signal<boolean>` which is `true` if reduced motion is preferred.
896
+ * @see {prefersReducedMotion} for detailed documentation and examples.
897
+ * @example const wantsReducedMotion = sensor('reduced-motion');
898
+ */
899
+ declare function sensor(type: 'reduced-motion', options?: {
900
+ debugName?: string;
901
+ }): Signal<boolean>;
902
+ /**
903
+ * Creates a sensor signal that tracks the browser window's inner dimensions (width and height).
904
+ * @param type Must be `'windowSize'`.
905
+ * @param options Optional configuration for the window size sensor, including `throttle` and `debugName`.
906
+ * @returns A `WindowSizeSignal` that tracks window dimensions and provides an unthrottled version.
907
+ * @see {windowSize} for detailed documentation and examples.
908
+ * @example const size = sensor('windowSize', { throttle: 200 });
909
+ */
910
+ declare function sensor(type: 'windowSize', options?: WindowSizeOptions): WindowSizeSignal;
911
+ /**
912
+ * Creates a sensor signal that tracks the scroll position (x, y) of the window or a specified element.
913
+ * @param type Must be `'scrollPosition'`.
914
+ * @param options Optional configuration for the scroll position sensor, including `target`, `throttle`, and `debugName`.
915
+ * @returns A `ScrollPositionSignal` that tracks scroll coordinates and provides an unthrottled version.
916
+ * @see {scrollPosition} for detailed documentation and examples.
917
+ * @example const pageScroll = sensor('scrollPosition', { throttle: 150 });
918
+ */
919
+ declare function sensor(type: 'scrollPosition', options?: ScrollPositionOptions): ScrollPositionSignal;
920
+
921
+ /**
922
+ * Interface for storage mechanisms compatible with the `stored` signal.
923
+ * Matches the essential parts of the `Storage` interface (`localStorage`, `sessionStorage`).
924
+ */
925
+ type Store = {
926
+ /** Retrieves an item from storage for a given key. */
927
+ getItem: (key: string) => string | null;
928
+ /** Sets an item in storage for a given key. */
929
+ setItem: (key: string, value: string) => void;
930
+ /** Removes an item from storage for a given key. */
931
+ removeItem: (key: string) => void;
932
+ };
933
+ /**
934
+ * Options for creating a signal synchronized with persistent storage using `stored()`.
935
+ * Extends Angular's `CreateSignalOptions`.
936
+ *
937
+ * @template T The type of value held by the signal.
938
+ */
939
+ type CreateStoredOptions<T> = CreateSignalOptions<T> & {
940
+ /**
941
+ * The key used to identify the item in storage.
942
+ * Can be a static string or a function/signal returning a string for dynamic keys
943
+ * (e.g., based on user ID or other application state).
944
+ */
945
+ key: string | (() => string);
946
+ /**
947
+ * Optional custom storage implementation (e.g., `sessionStorage` or a custom adapter).
948
+ * Must conform to the `Store` interface (`getItem`, `setItem`, `removeItem`).
949
+ * Defaults to `localStorage` in browser environments and a no-op store on the server.
950
+ */
951
+ store?: Store;
952
+ /**
953
+ * Optional function to serialize the value (type `T`) into a string before storing.
954
+ * Defaults to `JSON.stringify`.
955
+ * @param {T} value The value to serialize.
956
+ * @returns {string} The serialized string representation.
957
+ */
958
+ serialize?: (value: T) => string;
959
+ /**
960
+ * Optional function to deserialize the string retrieved from storage back into the value (type `T`).
961
+ * Defaults to `JSON.parse`.
962
+ * @param {string} value The string retrieved from storage.
963
+ * @returns {T} The deserialized value.
964
+ */
965
+ deserialize?: (value: string) => T;
966
+ /**
967
+ * If `true`, the signal will attempt to synchronize its state across multiple browser tabs
968
+ * using the `storage` event. Changes made in one tab (set, update, clear) will be
969
+ * reflected in other tabs using the same storage key.
970
+ * Requires a browser environment. Defaults to `false`.
971
+ */
972
+ syncTabs?: boolean;
973
+ /**
974
+ * Optional parameter to specify how key changes should be handled, load is the default.
975
+ * - `load`: The signal will load the value from storage when the key changes & replace the signal's value.
976
+ * - `store`: The signal will store the current value to the new key when the key changes.
977
+ */
978
+ onKeyChange?: 'load' | 'store';
979
+ /**
980
+ * If 'true', the signal will remove the old key from storage when the key changes, defaults to `false`.
981
+ */
982
+ cleanupOldKey?: boolean;
983
+ };
984
+ /**
985
+ * A specialized `WritableSignal` returned by the `stored()` function.
986
+ * It synchronizes its value with persistent storage and provides additional methods.
987
+ *
988
+ * @template T The type of value held by the signal (matches the fallback type).
989
+ */
990
+ type StoredSignal<T> = WritableSignal<T> & {
991
+ /**
992
+ * Removes the item associated with the signal's key from the configured storage.
993
+ * After clearing, reading the signal will return the fallback value until it's set again.
994
+ */
995
+ clear: () => void;
996
+ /**
997
+ * A `Signal<string>` containing the current storage key being used by this stored signal.
998
+ * This is particularly useful if the key was configured dynamically. You can read or react
999
+ * to this signal to know the active key.
1000
+ */
1001
+ key: Signal<string>;
1002
+ };
1003
+ /**
1004
+ * Creates a `WritableSignal` whose state is automatically synchronized with persistent storage
1005
+ * (like `localStorage` or `sessionStorage`).
1006
+ *
1007
+ * It handles Server-Side Rendering (SSR) gracefully, allows dynamic storage keys,
1008
+ * custom serialization/deserialization, custom storage providers, and optional
1009
+ * synchronization across browser tabs.
1010
+ *
1011
+ * @template T The type of value held by the signal and stored (after serialization).
1012
+ * @param fallback The default value of type `T` to use when no value is found in storage
1013
+ * or when deserialization fails. The signal's value will never be `null` or `undefined`
1014
+ * publicly, it will always revert to this fallback.
1015
+ * @param options Configuration options (`CreateStoredOptions<T>`). Requires at least the `key`.
1016
+ * @returns A `StoredSignal<T>` instance. This signal behaves like a standard `WritableSignal<T>`,
1017
+ * but its value is persisted. It includes a `.clear()` method to remove the item from storage
1018
+ * and a `.key` signal providing the current storage key.
1019
+ *
1020
+ * @remarks
1021
+ * - **Persistence:** The signal automatically saves its value to storage whenever the signal's
1022
+ * value or its configured `key` changes. This is managed internally using `effect`.
1023
+ * - **SSR Safety:** Detects server environments and uses a no-op storage, preventing errors.
1024
+ * - **Error Handling:** Catches and logs errors during serialization/deserialization in dev mode.
1025
+ * - **Tab Sync:** If `syncTabs` is true, listens to `storage` events to keep the signal value
1026
+ * consistent across browser tabs using the same key. Cleanup is handled automatically
1027
+ * using `DestroyRef`.
1028
+ * - **Removal:** Use the `.clear()` method on the returned signal to remove the item from storage.
1029
+ * Setting the signal to the fallback value will store the fallback value, not remove the item.
1030
+ *
1031
+ * @example
1032
+ * ```ts
1033
+ * import { Component, effect, signal } from '@angular/core';
1034
+ * import { stored } from '@mmstack/primitives'; // Adjust import path
1035
+ *
1036
+ * @Component({
1037
+ * selector: 'app-settings',
1038
+ * standalone: true,
1039
+ * template: `
1040
+ * Theme:
1041
+ * <select [ngModel]="theme()" (ngModelChange)="theme.set($event)">
1042
+ * <option value="light">Light</option>
1043
+ * <option value="dark">Dark</option>
1044
+ * </select>
1045
+ * <button (click)="theme.clear()">Clear Theme Setting</button>
1046
+ * <p>Storage Key Used: {{ theme.key() }}</p>
1047
+ * ` // Requires FormsModule for ngModel
1048
+ * })
1049
+ * export class SettingsComponent {
1050
+ * theme = stored<'light' | 'dark'>('light', { key: 'app-theme', syncTabs: true });
1051
+ * }
1052
+ * ```
1053
+ */
1054
+ declare function stored<T>(fallback: T, { key, store: providedStore, serialize, deserialize, syncTabs, equal, onKeyChange, cleanupOldKey, ...rest }: CreateStoredOptions<T>): StoredSignal<T>;
1055
+
1056
+ /**
1057
+ * Options for creating a throttled writable signal.
1058
+ * Extends Angular's `CreateSignalOptions` with a throttle time setting.
1059
+ *
1060
+ * @template T The type of value held by the signal.
1061
+ */
1062
+ type CreateThrottledOptions<T> = CreateSignalOptions<T> & {
1063
+ /**
1064
+ * The throttle delay in milliseconds. The minimum time
1065
+ * in milliseconds that must pass between updates to the throttled signal's value.
1066
+ */
1067
+ ms?: number;
1068
+ /**
1069
+ * Optional `DestroyRef` to clean up the throttle timer when the signal is destroyed.
1070
+ * If provided, the timer will be cleared when the signal is destroyed.
1071
+ * If the signal is called within a reactive context a DestroyRef is injected automatically.
1072
+ * If it is not provided or injected, the timer will not be cleared automatically...which is usually fine :)
1073
+ */
1074
+ destroyRef?: DestroyRef;
1075
+ };
1076
+ /**
1077
+ * A specialized `WritableSignal` whose publicly readable value updates are throttled.
1078
+ *
1079
+ * It provides access to the underlying, non-throttled signal via the `original` property.
1080
+ *
1081
+ * @template T The type of value held by the signal.
1082
+ * @see {DebouncedSignal} as the output type has the same structure.
1083
+ */
1084
+ type ThrottledSignal<T> = DebouncedSignal<T>;
1085
+ /**
1086
+ * A convenience function that creates and throttles a new `WritableSignal` in one step.
1087
+ *
1088
+ * @see {throttle} for the core implementation details.
1089
+ *
1090
+ * @template T The type of value the signal holds.
1091
+ * @param initial The initial value of the signal.
1092
+ * @param opt Options for signal creation, including throttle time `ms`.
1093
+ * @returns A `ThrottledSignal<T>` instance.
1094
+ *
1095
+ * @example
1096
+ * const query = throttled('', { ms: 500 });
1097
+ * effect(() => console.log('Throttled Query:', query()));
1098
+ *
1099
+ * query.set('a');
1100
+ * query.set('b');
1101
+ * query.set('c');
1102
+ * // With a trailing-edge throttle, the final value 'c' would be set
1103
+ * // after the 500ms cooldown.
1104
+ */
1105
+ declare function throttled<T>(initial: T, opt?: CreateThrottledOptions<T>): DebouncedSignal<T>;
1106
+ /**
1107
+ * Wraps an existing `WritableSignal` to create a new one whose readable value is throttled.
1108
+ *
1109
+ * This implementation avoids using `effect` by pairing a trigger signal with an `untracked`
1110
+ * read of the source signal to control when the throttled value is re-evaluated.
1111
+ *
1112
+ * @template T The type of value the signal holds.
1113
+ * @param source The source `WritableSignal` to wrap. Writes are applied to this signal immediately.
1114
+ * @param opt Options for throttling, including throttle time `ms` and an optional `DestroyRef`.
1115
+ * @returns A new `ThrottledSignal<T>` whose read value is throttled. The `.original` property
1116
+ * of the returned signal is a reference back to the provided `source` signal.
1117
+ *
1118
+ * @example
1119
+ * const query = throttled('', { ms: 500 });
1120
+ * effect(() => console.log('Throttled Query:', query()));
1121
+ *
1122
+ * query.set('a');
1123
+ * query.set('b');
1124
+ * query.set('c');
1125
+ * // With a trailing-edge throttle, the final value 'c' would be set
1126
+ * // after the 500ms cooldown.
1127
+ */
1128
+ declare function throttle<T>(source: WritableSignal<T>, opt?: CreateThrottledOptions<T>): ThrottledSignal<T>;
1129
+
1130
+ /**
1131
+ * Converts a read-only `Signal` into a `WritableSignal` by providing custom `set` and, optionally, `update` functions.
1132
+ * This can be useful for creating controlled write access to a signal that is otherwise read-only.
1133
+ *
1134
+ * @typeParam T - The type of value held by the signal.
1135
+ *
1136
+ * @param signal - The read-only `Signal` to be made writable.
1137
+ * @param set - A function that will be used to set the signal's value. This function *must* handle
1138
+ * the actual update mechanism (e.g., updating a backing store, emitting an event, etc.).
1139
+ * @param update - (Optional) A function that will be used to update the signal's value based on its
1140
+ * previous value. If not provided, a default `update` implementation is used that
1141
+ * calls the provided `set` function with the result of the updater function. The
1142
+ * default implementation uses `untracked` to avoid creating unnecessary dependencies
1143
+ * within the updater function.
1144
+ *
1145
+ * @returns A `WritableSignal` that uses the provided `set` and `update` functions. The `asReadonly`
1146
+ * method of the returned signal will still return the original read-only signal.
1147
+ *
1148
+ * @example
1149
+ * // Basic usage: Making a read-only signal writable with a custom set function.
1150
+ * const originalValue = signal({a: 0});
1151
+ * const readOnlySignal = computed(() => originalValue().a);
1152
+ * const writableSignal = toWritable(readOnlySignal, (newValue) => {
1153
+ * originalValue.update((prev) => { ...prev, a: newValue });
1154
+ * });
1155
+ *
1156
+ * writableSignal.set(5); // sets value of originalValue.a to 5 & triggers all signals
1157
+ */
1158
+ declare function toWritable<T>(signal: Signal<T>, set: (value: T) => void, update?: (updater: (value: T) => T) => void): WritableSignal<T>;
1159
+
1160
+ type UntilOptions = {
1161
+ /**
1162
+ * Optional timeout in milliseconds. If the condition is not met
1163
+ * within this period, the promise will reject.
1164
+ */
1165
+ timeout?: number;
1166
+ /**
1167
+ * Optional DestroyRef. If provided and the component/context is destroyed
1168
+ * before the condition is met or timeout occurs, the promise will reject.
1169
+ * If not provided, it will attempt to inject one if called in an injection context.
1170
+ */
1171
+ destroyRef?: DestroyRef;
1172
+ injector?: Injector;
1173
+ };
1174
+ /**
1175
+ * Creates a Promise that resolves when a signal's value satisfies a given predicate.
1176
+ *
1177
+ * This is useful for imperatively waiting for a reactive state to change,
1178
+ * for example, in tests or to orchestrate complex asynchronous operations.
1179
+ *
1180
+ * @template T The type of the signal's value.
1181
+ * @param sourceSignal The signal to observe.
1182
+ * @param predicate A function that takes the signal's value and returns `true` if the condition is met.
1183
+ * @param options Optional configuration for timeout and explicit destruction.
1184
+ * @returns A Promise that resolves with the signal's value when the predicate is true,
1185
+ * or rejects on timeout or context destruction.
1186
+ *
1187
+ * @example
1188
+ * ```ts
1189
+ * const count = signal(0);
1190
+ *
1191
+ * async function waitForCount() {
1192
+ * console.log('Waiting for count to be >= 3...');
1193
+ * try {
1194
+ * const finalCount = await until(count, c => c >= 3, { timeout: 5000 });
1195
+ * console.log(`Count reached: ${finalCount}`);
1196
+ * } catch (e: any) { // Ensure 'e' is typed if you access properties like e.message
1197
+ * console.error(e.message); // e.g., "until: Timeout after 5000ms."
1198
+ * }
1199
+ * }
1200
+ *
1201
+ * // Simulate updates
1202
+ * setTimeout(() => count.set(1), 500);
1203
+ * setTimeout(() => count.set(2), 1000);
1204
+ * setTimeout(() => count.set(3), 1500);
1205
+ *
1206
+ * waitForCount();
1207
+ * ```
1208
+ */
1209
+ declare function until<T>(sourceSignal: Signal<T>, predicate: (value: T) => boolean, options?: UntilOptions): Promise<T>;
1210
+
1211
+ /**
1212
+ * A WritableSignal enhanced with undo/redo capabilities and history tracking.
1213
+ *
1214
+ * @template T The type of value held by the signal.
1215
+ */
1216
+ type SignalWithHistory<T> = WritableSignal<T> & {
1217
+ /** A read-only signal of the undo history stack. The oldest changes are at the start of the array. */
1218
+ history: Signal<T[]>;
1219
+ /** Reverts the signal to its most recent previous state in the history. */
1220
+ undo: () => void;
1221
+ /** Re-applies the last state that was undone. */
1222
+ redo: () => void;
1223
+ /** A signal that is `true` if there are states in the redo stack. */
1224
+ canRedo: Signal<boolean>;
1225
+ /** A signal that is `true` if there are states in the undo history. */
1226
+ canUndo: Signal<boolean>;
1227
+ /** Clears both the undo and redo history stacks. */
1228
+ clear: () => void;
1229
+ /** A signal that is `true` if there is any history that can be cleared. */
1230
+ canClear: Signal<boolean>;
1231
+ };
1232
+ /**
1233
+ * Options for creating a signal with history tracking.
1234
+ *
1235
+ * @template T The type of value held by the signal.
1236
+ */
1237
+ type CreateHistoryOptions<T> = Omit<CreateSignalOptions<T[]>, 'equal'> & {
1238
+ /**
1239
+ * Optional custom equality function to determine if a value has changed before
1240
+ * adding it to history. Defaults to the source signal's equality function or `Object.is`.
1241
+ */
1242
+ equal?: ValueEqualityFn<T>;
1243
+ /**
1244
+ * The maximum number of undo states to keep in the history.
1245
+ * @default Infinity
1246
+ */
1247
+ maxSize?: number;
1248
+ /**
1249
+ * The strategy for trimming the history when `maxSize` is reached.
1250
+ * - `shift`: Removes the single oldest entry from the history.
1251
+ * - `halve`: Removes the oldest half of the history stack.
1252
+ * @default 'halve'
1253
+ */
1254
+ cleanupStrategy?: 'shift' | 'halve';
1255
+ };
1256
+ /**
1257
+ * Enhances an existing `WritableSignal` by adding a complete undo/redo history
1258
+ * stack and an API to control it.
1259
+ *
1260
+ * @template T The type of value held by the signal.
1261
+ * @param source The source `WritableSignal` to add history tracking to.
1262
+ * @param options Optional configuration for the history behavior.
1263
+ * @returns A `SignalWithHistory<T>` instance, augmenting the source signal with history APIs.
1264
+ *
1265
+ * @remarks
1266
+ * - Any new `.set()` or `.update()` call on the signal will clear the entire redo stack.
1267
+ * - The primitive attempts to automatically use the source signal's own `equal` function,
1268
+ * but this relies on an internal Angular API. For maximum stability across Angular
1269
+ * versions, it is recommended to provide an explicit `equal` function in the options.
1270
+ *
1271
+ * @example
1272
+ * ```ts
1273
+ * import { signal } from '@angular/core';
1274
+ * import { withHistory } from '@mmstack/primitives';
1275
+ *
1276
+ * const name = withHistory(signal('John'), { maxSize: 5 });
1277
+ *
1278
+ * console.log('Initial value:', name()); // "John"
1279
+ *
1280
+ * name.set('John Doe');
1281
+ * name.set('Jane Doe');
1282
+ *
1283
+ * console.log('Current value:', name()); // "Jane Doe"
1284
+ * console.log('History:', name.history()); // ["John", "John Doe"]
1285
+ * console.log('Can undo:', name.canUndo()); // true
1286
+ * console.log('Can redo:', name.canRedo()); // false
1287
+ *
1288
+ * name.undo();
1289
+ * console.log('After undo:', name()); // "John Doe"
1290
+ * console.log('Can redo:', name.canRedo()); // true
1291
+ *
1292
+ * name.redo();
1293
+ * console.log('After redo:', name()); // "Jane Doe"
1294
+ *
1295
+ * // A new change will clear the redo history
1296
+ * name.set('Janine Doe');
1297
+ * console.log('Can redo:', name.canRedo()); // false
1298
+ *
1299
+ * name.clear();
1300
+ * console.log('Can undo:', name.canUndo()); // false
1301
+ * ```
1302
+ */
1303
+ declare function withHistory<T>(source: WritableSignal<T>, opt?: CreateHistoryOptions<T>): SignalWithHistory<T>;
1304
+
1305
+ export { debounce, debounced, derived, elementVisibility, isDerivation, isMutable, mapArray, mediaQuery, mousePosition, mutable, networkStatus, pageVisibility, prefersDarkMode, prefersReducedMotion, scrollPosition, sensor, stored, throttle, throttled, toFakeDerivation, toFakeSignalDerivation, toWritable, until, windowSize, withHistory };
1306
+ export type { CreateDebouncedOptions, CreateHistoryOptions, CreateStoredOptions, CreateThrottledOptions, DebouncedSignal, DerivedSignal, ElementVisibilityOptions, ElementVisibilitySignal, MousePositionOptions, MousePositionSignal, MutableSignal, NetworkStatusSignal, ScrollPosition, ScrollPositionOptions, ScrollPositionSignal, SignalWithHistory, StoredSignal, ThrottledSignal, UntilOptions, WindowSize, WindowSizeOptions, WindowSizeSignal };