@mmstack/primitives 19.1.0 → 19.1.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/lib/derived.d.ts CHANGED
@@ -32,52 +32,75 @@ export type DerivedSignal<T, U> = WritableSignal<U> & {
32
32
  from: (v: T) => U;
33
33
  };
34
34
  /**
35
- * Creates a `DerivedSignal` that derives its value from another `WritableSignal` (the source signal).
35
+ * Creates a `DerivedSignal` that derives its value from another `WritableSignal`.
36
36
  * This overload provides the most flexibility, allowing you to specify custom `from` and `onChange` functions.
37
37
  *
38
- * @typeParam T - The type of the source signal's value.
39
- * @typeParam U - The type of the derived signal's value.
40
- * @param source - The source `WritableSignal`.
41
- * @param options - An object containing the `from` and `onChange` functions, and optional signal options.
38
+ * @typeParam T The type of the source signal's value.
39
+ * @typeParam U The type of the derived signal's value.
40
+ * @param source The source `WritableSignal`.
41
+ * @param options An object containing the `from` and `onChange` functions, and optional signal options.
42
42
  * @returns A `DerivedSignal` instance.
43
43
  *
44
44
  * @example
45
+ * ```ts
45
46
  * const user = signal({ name: 'John', age: 30 });
46
47
  * const name = derived(user, {
47
- * from: (u) => u.name,
48
- * onChange: (newName) => user.update((u) => ({ ...u, name: newName })),
48
+ * from: (u) => u.name,
49
+ * onChange: (newName) => user.update((u) => ({ ...u, name: newName })),
49
50
  * });
51
+ *
52
+ * name.set('Jane'); // Updates the original signal
53
+ * console.log(user().name); // Outputs: Jane
54
+ * ```
50
55
  */
51
56
  export declare function derived<T, U>(source: WritableSignal<T>, opt: CreateDerivedOptions<T, U>): DerivedSignal<T, U>;
52
57
  /**
53
58
  * Creates a `DerivedSignal` that derives a property from an object held by the source signal.
54
- * This overload simplifies creating derived signals for object properties.
59
+ * This overload is a convenient shorthand for accessing object properties.
55
60
  *
56
- * @typeParam T - The type of the source signal's value (must be an object).
57
- * @typeParam TKey - The key of the property to derive.
58
- * @param source - The source `WritableSignal` (holding an object).
59
- * @param key - The key of the property to derive.
60
- * @param options - Optional signal options for the derived signal.
61
+ * @typeParam T The type of the source signal's value (must be an object).
62
+ * @typeParam TKey The key of the property to derive.
63
+ * @param source The source `WritableSignal` (holding an object).
64
+ * @param key The key of the property to derive.
65
+ * @param options Optional signal options for the derived signal.
61
66
  * @returns A `DerivedSignal` instance.
62
67
  *
63
68
  * @example
69
+ * ```ts
64
70
  * const user = signal({ name: 'John', age: 30 });
65
71
  * const name = derived(user, 'name');
72
+ *
73
+ * console.log(name()); // Outputs: John
74
+ *
75
+ * // Update the derived signal, which also updates the source
76
+ * name.set('Jane');
77
+ *
78
+ * console.log(user().name); // Outputs: Jane
79
+ * ```
66
80
  */
67
81
  export declare function derived<T extends UnknownObject, TKey extends keyof T>(source: WritableSignal<T>, key: TKey, opt?: CreateSignalOptions<T[TKey]>): DerivedSignal<T, T[TKey]>;
68
82
  /**
69
- * Creates a `DerivedSignal` from an array, and derives an element by index.
83
+ * Creates a `DerivedSignal` from an array, deriving an element by its index.
84
+ * This overload is a convenient shorthand for accessing array elements.
70
85
  *
71
- * @typeParam T - The type of the source signal's value (must be an array).
72
- * @param source - The source `WritableSignal` (holding an array).
73
- * @param index - The index of the element to derive.
74
- * @param options - Optional signal options for the derived signal.
86
+ * @typeParam T The type of the source signal's value (must be an array).
87
+ * @param source The source `WritableSignal` (holding an array).
88
+ * @param index The index of the element to derive.
89
+ * @param options Optional signal options for the derived signal.
75
90
  * @returns A `DerivedSignal` instance.
76
91
  *
77
92
  * @example
93
+ * ```ts
78
94
  * const numbers = signal([1, 2, 3]);
79
- * const secondNumber = derived(numbers, 1); // secondNumber() === 2
80
- * secondNumber.set(5); // numbers() === [1, 5, 3]
95
+ * const secondNumber = derived(numbers, 1);
96
+ *
97
+ * console.log(secondNumber()); // Outputs: 2
98
+ *
99
+ * // Update the derived signal, which also updates the source
100
+ * secondNumber.set(5);
101
+ *
102
+ * console.log(numbers()); // Outputs: [1, 5, 3]
103
+ * ```
81
104
  */
82
105
  export declare function derived<T extends any[]>(source: WritableSignal<T>, index: number, opt?: CreateSignalOptions<T[number]>): DerivedSignal<T, T[number]>;
83
106
  /**
@@ -54,5 +54,8 @@ import { type CreateSignalOptions, type Signal } from '@angular/core';
54
54
  * { equal: (a, b) => a.id === b.id && a.name === b.name }
55
55
  * );
56
56
  * ```
57
+ * @remarks
58
+ * This function achieves its high performance by leveraging the new `linkedSignal`
59
+ * API from Angular, which allows for efficient memoization and reuse of array items.
57
60
  */
58
61
  export declare function mapArray<T, U>(source: () => T[], map: (value: Signal<T>, index: number) => U, opt?: CreateSignalOptions<T>): Signal<U[]>;
package/lib/mutable.d.ts CHANGED
@@ -37,22 +37,37 @@ export type MutableSignal<T> = WritableSignal<T> & {
37
37
  * Creates a `MutableSignal`. This function overloads the standard `signal` function to provide
38
38
  * the additional `mutate` and `inline` methods.
39
39
  *
40
- * @typeParam T - The type of value held by the signal.
40
+ * @typeParam T The type of value held by the signal.
41
+ * @param initial The initial value of the signal.
42
+ * @param options Optional signal options, including a custom `equal` function.
43
+ * @returns A `MutableSignal` instance.
41
44
  *
42
- * @param initial - The initial value of the signal. If no initial value is provided, it defaults to `undefined`.
43
- * @param options - Optional. An object containing signal options, including a custom equality function (`equal`).
45
+ * ### Important Note on `computed` Signals
44
46
  *
45
- * @returns A `MutableSignal` instance.
47
+ * When creating a `computed` signal that derives a non-primitive value (e.g., an object or array)
48
+ * from a `mutable` signal, you **must** provide the `{ equal: false }` option to the `computed`
49
+ * function.
50
+ *
51
+ * This is because a `.mutate()` call notifies its dependents that it has changed, but if the
52
+ * reference to a derived object hasn't changed, the `computed` signal will not trigger its
53
+ * own dependents by default.
46
54
  *
47
55
  * @example
48
- * // Create a mutable signal with an initial value:
49
- * const mySignal = mutable({ count: 0 }) // MutableSignal<{ count: number }>;
56
+ * ```ts
57
+ * const state = mutable({ user: { name: 'John' }, lastUpdated: new Date() });
58
+ *
59
+ * // ✅ CORRECT: Deriving a primitive value works as expected.
60
+ * const name = computed(() => state().user.name);
61
+ *
62
+ * // ❌ INCORRECT: This will not update reliably after the first change.
63
+ * const userObject = computed(() => state().user);
50
64
  *
51
- * // Create a mutable signal with no initial value (starts as undefined):
52
- * const = mutable<number>(); // MutableSignal<number | undefined>
65
+ * // CORRECT: For object derivations, `equal: false` is required.
66
+ * const userObjectFixed = computed(() => state().user, { equal: false });
53
67
  *
54
- * // Create a mutable signal with a custom equality function:
55
- * const myCustomSignal = mutable({ a: 1 }, { equal: (a, b) => a.a === b.a });
68
+ * // This mutation will now correctly trigger effects depending on `userObjectFixed`.
69
+ * state.mutate(s => s.lastUpdated = new Date());
70
+ * ```
56
71
  */
57
72
  export declare function mutable<T>(): MutableSignal<T | undefined>;
58
73
  export declare function mutable<T>(initial: T): MutableSignal<T>;
package/lib/stored.d.ts CHANGED
@@ -51,6 +51,16 @@ export type CreateStoredOptions<T> = CreateSignalOptions<T> & {
51
51
  * Requires a browser environment. Defaults to `false`.
52
52
  */
53
53
  syncTabs?: boolean;
54
+ /**
55
+ * Optional parameter to specify how key changes should be handled, load is the default.
56
+ * - `load`: The signal will load the value from storage when the key changes & replace the signal's value.
57
+ * - `store`: The signal will store the current value to the new key when the key changes.
58
+ */
59
+ onKeyChange?: 'load' | 'store';
60
+ /**
61
+ * If 'true', the signal will remove the old key from storage when the key changes, defaults to `false`.
62
+ */
63
+ cleanupOldKey?: boolean;
54
64
  };
55
65
  /**
56
66
  * A specialized `WritableSignal` returned by the `stored()` function.
@@ -122,5 +132,5 @@ export type StoredSignal<T> = WritableSignal<T> & {
122
132
  * }
123
133
  * ```
124
134
  */
125
- export declare function stored<T>(fallback: T, { key, store: providedStore, serialize, deserialize, syncTabs, equal, ...rest }: CreateStoredOptions<T>): StoredSignal<T>;
135
+ export declare function stored<T>(fallback: T, { key, store: providedStore, serialize, deserialize, syncTabs, equal, onKeyChange, cleanupOldKey, ...rest }: CreateStoredOptions<T>): StoredSignal<T>;
126
136
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmstack/primitives",
3
- "version": "19.1.0",
3
+ "version": "19.1.2",
4
4
  "keywords": [
5
5
  "angular",
6
6
  "signals",