@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.
@@ -1,94 +0,0 @@
1
- import { type CreateSignalOptions, type Signal, type ValueEqualityFn, type WritableSignal } from '@angular/core';
2
- /**
3
- * A WritableSignal enhanced with undo/redo capabilities and history tracking.
4
- *
5
- * @template T The type of value held by the signal.
6
- */
7
- export type SignalWithHistory<T> = WritableSignal<T> & {
8
- /** A read-only signal of the undo history stack. The oldest changes are at the start of the array. */
9
- history: Signal<T[]>;
10
- /** Reverts the signal to its most recent previous state in the history. */
11
- undo: () => void;
12
- /** Re-applies the last state that was undone. */
13
- redo: () => void;
14
- /** A signal that is `true` if there are states in the redo stack. */
15
- canRedo: Signal<boolean>;
16
- /** A signal that is `true` if there are states in the undo history. */
17
- canUndo: Signal<boolean>;
18
- /** Clears both the undo and redo history stacks. */
19
- clear: () => void;
20
- /** A signal that is `true` if there is any history that can be cleared. */
21
- canClear: Signal<boolean>;
22
- };
23
- /**
24
- * Options for creating a signal with history tracking.
25
- *
26
- * @template T The type of value held by the signal.
27
- */
28
- export type CreateHistoryOptions<T> = Omit<CreateSignalOptions<T[]>, 'equal'> & {
29
- /**
30
- * Optional custom equality function to determine if a value has changed before
31
- * adding it to history. Defaults to the source signal's equality function or `Object.is`.
32
- */
33
- equal?: ValueEqualityFn<T>;
34
- /**
35
- * The maximum number of undo states to keep in the history.
36
- * @default Infinity
37
- */
38
- maxSize?: number;
39
- /**
40
- * The strategy for trimming the history when `maxSize` is reached.
41
- * - `shift`: Removes the single oldest entry from the history.
42
- * - `halve`: Removes the oldest half of the history stack.
43
- * @default 'halve'
44
- */
45
- cleanupStrategy?: 'shift' | 'halve';
46
- };
47
- /**
48
- * Enhances an existing `WritableSignal` by adding a complete undo/redo history
49
- * stack and an API to control it.
50
- *
51
- * @template T The type of value held by the signal.
52
- * @param source The source `WritableSignal` to add history tracking to.
53
- * @param options Optional configuration for the history behavior.
54
- * @returns A `SignalWithHistory<T>` instance, augmenting the source signal with history APIs.
55
- *
56
- * @remarks
57
- * - Any new `.set()` or `.update()` call on the signal will clear the entire redo stack.
58
- * - The primitive attempts to automatically use the source signal's own `equal` function,
59
- * but this relies on an internal Angular API. For maximum stability across Angular
60
- * versions, it is recommended to provide an explicit `equal` function in the options.
61
- *
62
- * @example
63
- * ```ts
64
- * import { signal } from '@angular/core';
65
- * import { withHistory } from '@mmstack/primitives';
66
- *
67
- * const name = withHistory(signal('John'), { maxSize: 5 });
68
- *
69
- * console.log('Initial value:', name()); // "John"
70
- *
71
- * name.set('John Doe');
72
- * name.set('Jane Doe');
73
- *
74
- * console.log('Current value:', name()); // "Jane Doe"
75
- * console.log('History:', name.history()); // ["John", "John Doe"]
76
- * console.log('Can undo:', name.canUndo()); // true
77
- * console.log('Can redo:', name.canRedo()); // false
78
- *
79
- * name.undo();
80
- * console.log('After undo:', name()); // "John Doe"
81
- * console.log('Can redo:', name.canRedo()); // true
82
- *
83
- * name.redo();
84
- * console.log('After redo:', name()); // "Jane Doe"
85
- *
86
- * // A new change will clear the redo history
87
- * name.set('Janine Doe');
88
- * console.log('Can redo:', name.canRedo()); // false
89
- *
90
- * name.clear();
91
- * console.log('Can undo:', name.canUndo()); // false
92
- * ```
93
- */
94
- export declare function withHistory<T>(source: WritableSignal<T>, opt?: CreateHistoryOptions<T>): SignalWithHistory<T>;