@mmstack/primitives 19.0.4 → 19.1.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/LICENSE +21 -0
- package/README.md +229 -2
- package/fesm2022/mmstack-primitives.mjs +335 -45
- package/fesm2022/mmstack-primitives.mjs.map +1 -1
- package/index.d.ts +2 -0
- package/lib/debounced.d.ts +62 -54
- package/lib/map-array.d.ts +55 -53
- package/lib/mutable.d.ts +1 -1
- package/lib/stored.d.ts +126 -0
- package/lib/with-history.d.ts +14 -0
- package/package.json +19 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mmstack-primitives.mjs","sources":["../../../../packages/primitives/src/lib/to-writable.ts","../../../../packages/primitives/src/lib/debounced.ts","../../../../packages/primitives/src/lib/derived.ts","../../../../packages/primitives/src/lib/map-array.ts","../../../../packages/primitives/src/lib/mutable.ts","../../../../packages/primitives/src/mmstack-primitives.ts"],"sourcesContent":["import { Signal, untracked, WritableSignal } from '@angular/core';\n\n/**\n * Converts a read-only `Signal` into a `WritableSignal` by providing custom `set` and, optionally, `update` functions.\n * This can be useful for creating controlled write access to a signal that is otherwise read-only.\n *\n * @typeParam T - The type of value held by the signal.\n *\n * @param signal - The read-only `Signal` to be made writable.\n * @param set - A function that will be used to set the signal's value. This function *must* handle\n * the actual update mechanism (e.g., updating a backing store, emitting an event, etc.).\n * @param update - (Optional) A function that will be used to update the signal's value based on its\n * previous value. If not provided, a default `update` implementation is used that\n * calls the provided `set` function with the result of the updater function. The\n * default implementation uses `untracked` to avoid creating unnecessary dependencies\n * within the updater function.\n *\n * @returns A `WritableSignal` that uses the provided `set` and `update` functions. The `asReadonly`\n * method of the returned signal will still return the original read-only signal.\n *\n * @example\n * // Basic usage: Making a read-only signal writable with a custom set function.\n * const originalValue = signal({a: 0});\n * const readOnlySignal = computed(() => originalValue().a);\n * const writableSignal = toWritable(readOnlySignal, (newValue) => {\n * originalValue.update((prev) => { ...prev, a: newValue });\n * });\n *\n * writableSignal.set(5); // sets value of originalValue.a to 5 & triggers all signals\n */\nexport function toWritable<T>(\n signal: Signal<T>,\n set: (value: T) => void,\n update?: (updater: (value: T) => T) => void,\n): WritableSignal<T> {\n const internal = signal as WritableSignal<T>;\n internal.asReadonly = () => signal;\n internal.set = set;\n internal.update = update ?? ((updater) => set(updater(untracked(internal))));\n\n return internal;\n}\n","import {\n computed,\n type CreateSignalOptions,\n signal,\n untracked,\n type WritableSignal,\n} from '@angular/core';\nimport { toWritable } from './to-writable';\n\n/**\n * A `DebouncedSignal` is a special type of `WritableSignal` that delays updates\n * to its value. This is useful for scenarios where you want to avoid\n * frequent updates, such as responding to user input in a search field.\n * It keeps a reference to the original `WritableSignal` via the `original` property.\n *\n * @typeParam T - The type of value held by the signal.\n */\nexport type DebouncedSignal<T> = WritableSignal<T> & {\n /**\n * A reference to the original, un-debounced `WritableSignal`. This allows\n * you to access the immediate value (without the debounce delay) if needed,\n * and also ensures that any direct modifications to the original signal\n * are reflected in the debounced signal after the debounce period.\n */\n original: WritableSignal<T>;\n};\n\n/**\n * Options for creating a debounced signal.\n *\n * @typeParam T - The type of value held by the signal.\n */\nexport type CreateDebouncedOptions<T> = CreateSignalOptions<T> & {\n /**\n * The debounce delay in milliseconds. Defaults to 300.\n */\n ms?: number;\n};\n\n/**\n * Creates a debounced signal. The signal's value will only be propagated to\n * subscribers after a specified delay (debounce time) has passed since the last\n * time it was set or updated. Crucially, updates to the *original* signal\n * are also debounced.\n *\n * @see {@link DebouncedSignal}\n * @see {@link CreateDebouncedOptions}\n *\n * @example\n * ```typescript\n * // Create a debounced signal with an initial value and a custom delay.\n * const searchTerm = debounced('initial value', { ms: 500 });\n *\n * // Update the debounced signal. The actual update will be delayed by 500ms.\n * searchTerm.set('new value');\n *\n * // Access the original, un-debounced signal.\n * console.log(searchTerm.original()); // Outputs 'new value' (immediately)\n * // ... after 500ms ...\n * console.log(searchTerm()); // Outputs 'new value' (debounced)\n *\n * // Directly update the *original* signal.\n * searchTerm.original.set('direct update');\n * console.log(searchTerm.original()); // Outputs 'direct update' (immediately)\n * console.log(searchTerm()); // Outputs 'new value' (still debounced from the previous set)\n * // ... after 500ms ...\n * console.log(searchTerm()); // Outputs 'direct update' (now reflects the original signal)\n *\n * // Create a debounced signal with undefined initial value and default delay\n * const anotherSignal = debounced();\n * ```\n * @typeParam T - The type of the signal's value.\n * @param initial The initial value of the signal. Optional; defaults to `undefined`.\n * @param opt Configuration options for the signal, including the debounce delay (`ms`).\n * @returns A `DebouncedSignal` instance.\n */\nexport function debounced<T>(): DebouncedSignal<T | undefined>;\n/**\n * Creates a debounced signal with a defined initial value.\n *\n * @typeParam T - The type of the signal's value.\n * @param initial The initial value of the signal.\n * @param opt Configuration options for the signal, including the debounce delay (`ms`).\n * @returns A `DebouncedSignal` instance.\n */\nexport function debounced<T>(\n initial: T,\n opt?: CreateDebouncedOptions<T>,\n): DebouncedSignal<T>;\nexport function debounced<T>(\n value?: T,\n opt?: CreateDebouncedOptions<T | undefined>,\n): DebouncedSignal<T | undefined> {\n const sig = signal<T | undefined>(value, opt) as DebouncedSignal<\n T | undefined\n >;\n const ms = opt?.ms ?? 300;\n\n let timeout: ReturnType<typeof setTimeout> | undefined;\n\n const originalSet = sig.set;\n const originalUpdate = sig.update;\n\n const trigger = signal(false);\n\n // Set on the original signal, then trigger the debounced update\n const set = (value: T | undefined) => {\n originalSet(value); // Update the *original* signal immediately\n\n if (timeout) clearTimeout(timeout);\n timeout = setTimeout(() => {\n trigger.update((cur) => !cur); // Trigger the computed signal\n }, ms);\n };\n\n // Update on the original signal, then trigger the debounced update\n const update = (fn: (prev: T | undefined) => T | undefined) => {\n originalUpdate(fn); // Update the *original* signal immediately\n\n if (timeout) clearTimeout(timeout);\n timeout = setTimeout(() => {\n trigger.update((cur) => !cur); // Trigger the computed signal\n }, ms);\n };\n\n // Create a computed signal that depends on the trigger.\n // This computed signal is what provides the debounced behavior.\n const writable = toWritable(\n computed(() => {\n trigger();\n return untracked(sig);\n }),\n set,\n update,\n ) as DebouncedSignal<T | undefined>;\n\n writable.original = sig;\n\n return writable;\n}\n","import {\n computed,\n CreateSignalOptions,\n signal,\n untracked,\n type WritableSignal,\n} from '@angular/core';\nimport type { UnknownObject } from '@mmstack/object';\nimport { toWritable } from './to-writable';\n\n/**\n * Options for creating a derived signal using the full `derived` function signature.\n * @typeParam T - The type of the source signal's value (parent).\n * @typeParam U - The type of the derived signal's value (child).\n */\ntype CreateDerivedOptions<T, U> = CreateSignalOptions<U> & {\n /**\n * A function that extracts the derived value (`U`) from the source signal's value (`T`).\n */\n from: (v: T) => U;\n /**\n * A function that updates the source signal's value (`T`) when the derived signal's value (`U`) changes.\n * This establishes the two-way binding.\n */\n onChange: (newValue: U) => void;\n};\n\n/**\n * A `WritableSignal` that derives its value from another `WritableSignal` (the \"source\" signal).\n * It provides two-way binding: changes to the source signal update the derived signal, and\n * changes to the derived signal update the source signal.\n *\n * @typeParam T - The type of the source signal's value (parent).\n * @typeParam U - The type of the derived signal's value (child).\n */\nexport type DerivedSignal<T, U> = WritableSignal<U> & {\n /**\n * The function used to derive the derived signal's value from the source signal's value.\n * This is primarily for internal use and introspection.\n */\n from: (v: T) => U;\n};\n\n/**\n * Creates a `DerivedSignal` that derives its value from another `WritableSignal` (the source signal).\n * This overload provides the most flexibility, allowing you to specify custom `from` and `onChange` functions.\n *\n * @typeParam T - The type of the source signal's value.\n * @typeParam U - The type of the derived signal's value.\n * @param source - The source `WritableSignal`.\n * @param options - An object containing the `from` and `onChange` functions, and optional signal options.\n * @returns A `DerivedSignal` instance.\n *\n * @example\n * const user = signal({ name: 'John', age: 30 });\n * const name = derived(user, {\n * from: (u) => u.name,\n * onChange: (newName) => user.update((u) => ({ ...u, name: newName })),\n * });\n */\nexport function derived<T, U>(\n source: WritableSignal<T>,\n opt: CreateDerivedOptions<T, U>,\n): DerivedSignal<T, U>;\n\n/**\n * Creates a `DerivedSignal` that derives a property from an object held by the source signal.\n * This overload simplifies creating derived signals for object properties.\n *\n * @typeParam T - The type of the source signal's value (must be an object).\n * @typeParam TKey - The key of the property to derive.\n * @param source - The source `WritableSignal` (holding an object).\n * @param key - The key of the property to derive.\n * @param options - Optional signal options for the derived signal.\n * @returns A `DerivedSignal` instance.\n *\n * @example\n * const user = signal({ name: 'John', age: 30 });\n * const name = derived(user, 'name');\n */\nexport function derived<T extends UnknownObject, TKey extends keyof T>(\n source: WritableSignal<T>,\n key: TKey,\n opt?: CreateSignalOptions<T[TKey]>,\n): DerivedSignal<T, T[TKey]>;\n\n/**\n * Creates a `DerivedSignal` from an array, and derives an element by index.\n *\n * @typeParam T - The type of the source signal's value (must be an array).\n * @param source - The source `WritableSignal` (holding an array).\n * @param index - The index of the element to derive.\n * @param options - Optional signal options for the derived signal.\n * @returns A `DerivedSignal` instance.\n *\n * @example\n * const numbers = signal([1, 2, 3]);\n * const secondNumber = derived(numbers, 1); // secondNumber() === 2\n * secondNumber.set(5); // numbers() === [1, 5, 3]\n */\nexport function derived<T extends any[]>(\n source: WritableSignal<T>,\n index: number,\n opt?: CreateSignalOptions<T[number]>,\n): DerivedSignal<T, T[number]>;\n\nexport function derived<T, U>(\n source: WritableSignal<T>,\n optOrKey: CreateDerivedOptions<T, U> | keyof T,\n opt?: CreateSignalOptions<U>,\n): DerivedSignal<T, U> {\n const isArray =\n Array.isArray(untracked(source)) && typeof optOrKey === 'number';\n\n const from =\n typeof optOrKey === 'object' ? optOrKey.from : (v: T) => v[optOrKey] as U;\n const onChange =\n typeof optOrKey === 'object'\n ? optOrKey.onChange\n : isArray\n ? (next: U) => {\n source.update(\n (cur) =>\n (cur as unknown as any[]).map((v, i) =>\n i === optOrKey ? next : v,\n ) as T,\n );\n }\n : (next: U) => {\n source.update((cur) => ({ ...cur, [optOrKey]: next }));\n };\n\n const rest = typeof optOrKey === 'object' ? optOrKey : opt;\n\n const sig = toWritable<U>(\n computed(() => from(source()), rest),\n (newVal) => onChange(newVal),\n ) as DerivedSignal<T, U>;\n\n sig.from = from;\n\n return sig;\n}\n\n/**\n * Creates a \"fake\" `DerivedSignal` from a simple value. This is useful for creating\n * `FormControlSignal` instances that are not directly derived from another signal.\n * The returned signal's `from` function will always return the initial value.\n *\n * @typeParam T - This type parameter is not used in the implementation but is kept for type compatibility with `DerivedSignal`.\n * @typeParam U - The type of the signal's value.\n * @param initial - The initial value of the signal.\n * @returns A `DerivedSignal` instance.\n * @internal\n */\nexport function toFakeDerivation<T, U>(initial: U): DerivedSignal<T, U> {\n const sig = signal(initial) as DerivedSignal<T, U>;\n sig.from = () => initial;\n\n return sig;\n}\n\n/**\n * Creates a \"fake\" `DerivedSignal` from an existing `WritableSignal`. This is useful\n * for treating a regular `WritableSignal` as a `DerivedSignal` without changing its behavior.\n * The returned signal's `from` function returns the current value of signal, using `untracked`.\n *\n * @typeParam T - This type parameter is not used in the implementation but is kept for type compatibility with `DerivedSignal`.\n * @typeParam U - The type of the signal's value.\n * @param initial - The existing `WritableSignal`.\n * @returns A `DerivedSignal` instance.\n * @internal\n */\nexport function toFakeSignalDerivation<T, U>(\n initial: WritableSignal<U>,\n): DerivedSignal<T, U> {\n const sig = initial as DerivedSignal<T, U>;\n sig.from = () => untracked(initial);\n return sig;\n}\n\n/**\n * Type guard function to check if a given `WritableSignal` is a `DerivedSignal`.\n *\n * @typeParam T - The type of the source signal's value (optional, defaults to `any`).\n * @typeParam U - The type of the derived signal's value (optional, defaults to `any`).\n * @param sig - The `WritableSignal` to check.\n * @returns `true` if the signal is a `DerivedSignal`, `false` otherwise.\n */\nexport function isDerivation<T, U>(\n sig: WritableSignal<U>,\n): sig is DerivedSignal<T, U> {\n return 'from' in sig;\n}\n","import {\n computed,\n type CreateSignalOptions,\n isSignal,\n linkedSignal,\n type Signal,\n} from '@angular/core';\n\n/**\n * Options for the mapArray function.\n * @template T The type of elements in the array.\n * @extends CreateSignalOptions<T> Inherits options for creating individual signals.\n */\nexport type MapArrayOptions<T> = CreateSignalOptions<T> & {\n /**\n * An optional function to transform each element from the source array.\n * If not provided, the original element is used.\n * @param source The current value of the source array signal.\n * @param index The index of the element being mapped.\n * @returns The transformed element for the corresponding signal.\n */\n map?: (source: T[], index: number) => T;\n};\n\nfunction createReconciler<T>(source: Signal<T[]>, opt?: MapArrayOptions<T>) {\n const map = opt?.map ?? ((source, index) => source[index]);\n\n return (\n length: number,\n prev?: {\n value: Signal<T>[];\n source: number;\n },\n ): Signal<T>[] => {\n if (!prev)\n return Array.from({ length }, (_, i) =>\n computed(() => map(source(), i), opt),\n );\n\n if (length === prev.source) return prev.value;\n\n if (length < prev.source) {\n return prev.value.slice(0, length);\n } else {\n const next = [...prev.value];\n for (let i = prev.source; i < length; i++) {\n next.push(computed(() => map(source(), i), opt));\n }\n\n return next;\n }\n };\n}\n\n/**\n * Creates a reactive array of signals from a source array signal (or a function returning one),\n * applying an optional mapping function to each element.\n *\n * This is useful for scenarios like rendering lists where each item\n * needs its own reactive state derived from the source array. It efficiently\n * handles changes in the source array's length by reusing existing signals\n * for elements that remain, adding signals for new elements, and removing signals\n * for deleted elements.\n *\n * @template T The type of elements in the source array.\n * @param source A function that returns the source array (or readonly array).\n * This function will be tracked for changes.\n * @param opt Optional configuration including a `map` function to transform elements\n * and options (`CreateSignalOptions`) for the created signals.\n * @returns A signal (`Signal<Signal<T | undefined>[]>`) where the outer signal updates\n * when the array length changes, and the inner array contains signals\n * representing each element (potentially mapped).\n */\nexport function mapArray<T>(\n source: () => T[],\n opt?: MapArrayOptions<T | undefined>,\n): Signal<Signal<T | undefined>[]>;\n\n/**\n * Creates a reactive array of signals from a source readonly array (or a function returning one),\n * applying an optional mapping function to each element.\n *\n * This is useful for scenarios like rendering lists where each item\n * needs its own reactive state derived from the source array. It efficiently\n * handles changes in the source array's length by reusing existing signals\n * for elements that remain, adding signals for new elements, and removing signals\n * for deleted elements.\n *\n * @template T The type of elements in the source array.\n * @param source A function that returns the source readonly array.\n * This function will be tracked for changes.\n * @param opt Optional configuration including a `map` function to transform elements\n * and options (`CreateSignalOptions`) for the created signals.\n * @returns A signal (`Signal<Signal<T>[]>`) where the outer signal updates\n * when the array length changes, and the inner array contains signals\n * representing each element (potentially mapped).\n */\nexport function mapArray<T>(\n source: () => readonly T[],\n opt?: MapArrayOptions<T>,\n): Signal<Signal<T>[]>;\n\nexport function mapArray<T>(\n source: (() => T[]) | (() => readonly T[]),\n opt?: MapArrayOptions<T>,\n): Signal<Signal<T | undefined>[]> {\n const data = isSignal(source) ? source : computed(source);\n const length = computed(() => data().length);\n\n const reconciler = createReconciler<T>(data as Signal<T[]>, opt);\n\n return linkedSignal<number, Signal<T>[]>({\n source: () => length(),\n computation: (len, prev) => reconciler(len, prev),\n });\n}\n","import {\r\n signal,\r\n type CreateSignalOptions,\r\n type ValueEqualityFn,\r\n type WritableSignal,\r\n} from '@angular/core';\r\n\r\nconst { is } = Object;\r\n\r\n/**\r\n * A `MutableSignal` is a special type of `WritableSignal` that allows for in-place mutation of its value.\r\n * In addition to the standard `set` and `update` methods, it provides a `mutate` method. This is useful\r\n * for performance optimization when dealing with complex objects or arrays, as it avoids unnecessary\r\n * object copying.\r\n *\r\n * @typeParam T - The type of value held by the signal.\r\n */\r\nexport type MutableSignal<T> = WritableSignal<T> & {\r\n /**\r\n * Mutates the signal's value in-place. This is similar to `update`, but it's optimized for\r\n * scenarios where you want to modify the existing object directly rather than creating a new one.\r\n *\r\n * @param updater - A function that takes the current value as input and modifies it directly.\r\n *\r\n * @example\r\n * const myArray = mutable([1, 2, 3]);\r\n * myArray.mutate((arr) => {\r\n * arr.push(4);\r\n * return arr;\r\n * }); // myArray() now returns [1, 2, 3, 4]\r\n */\r\n mutate: WritableSignal<T>['update'];\r\n\r\n /**\r\n * Mutates the signal's value in-place, similar to `mutate`, but with a void-returning value in updater\r\n * function. This further emphasizes that the mutation is happening inline, improving readability\r\n * in some cases.\r\n * @param updater - Function to change to the current value\r\n * @example\r\n * const myObject = mutable({ a: 1, b: 2 });\r\n * myObject.inline((obj) => (obj.a = 3)); // myObject() now returns { a: 3, b: 2 }\r\n */\r\n inline: (updater: Parameters<WritableSignal<T>['update']>[0]) => void;\r\n};\r\n\r\n/**\r\n * Creates a `MutableSignal`. This function overloads the standard `signal` function to provide\r\n * the additional `mutate` and `inline` methods.\r\n *\r\n * @typeParam T - The type of value held by the signal.\r\n *\r\n * @param initial - The initial value of the signal. If no initial value is provided, it defaults to `undefined`.\r\n * @param options - Optional. An object containing signal options, including a custom equality function (`equal`).\r\n *\r\n * @returns A `MutableSignal` instance.\r\n *\r\n * @example\r\n * // Create a mutable signal with an initial value:\r\n * const mySignal = mutable({ count: 0 }) // MutableSignal<{ count: number }>;\r\n *\r\n * // Create a mutable signal with no initial value (starts as undefined):\r\n * const = mutable<number>(); // MutableSignal<number | undefined>\r\n *\r\n * // Create a mutable signal with a custom equality function:\r\n * const myCustomSignal = mutable({ a: 1 }, { equal: (a, b) => a.a === b.a });\r\n */\r\nexport function mutable<T>(): MutableSignal<T | undefined>;\r\nexport function mutable<T>(initial: T): MutableSignal<T>;\r\nexport function mutable<T>(\r\n initial: T,\r\n opt?: CreateSignalOptions<T>,\r\n): MutableSignal<T>;\r\n\r\nexport function mutable<T>(\r\n initial?: T,\r\n opt?: CreateSignalOptions<T>,\r\n): MutableSignal<T> {\r\n const baseEqual = opt?.equal ?? is;\r\n let trigger = false;\r\n\r\n const equal: ValueEqualityFn<T | undefined> = (a, b) => {\r\n if (trigger) return false;\r\n return baseEqual(a, b);\r\n };\r\n\r\n const sig = signal<T | undefined>(initial, {\r\n ...opt,\r\n equal,\r\n }) as MutableSignal<T>;\r\n\r\n const internalUpdate = sig.update;\r\n\r\n sig.mutate = (updater) => {\r\n trigger = true;\r\n internalUpdate(updater);\r\n trigger = false;\r\n };\r\n\r\n sig.inline = (updater) => {\r\n sig.mutate((prev) => {\r\n updater(prev);\r\n return prev;\r\n });\r\n };\r\n\r\n return sig;\r\n}\r\n\r\n/**\r\n * Type guard function to check if a given `WritableSignal` is a `MutableSignal`. This is useful\r\n * for situations where you need to conditionally use the `mutate` or `inline` methods.\r\n *\r\n * @typeParam T - The type of the signal's value (optional, defaults to `any`).\r\n * @param value - The `WritableSignal` to check.\r\n * @returns `true` if the signal is a `MutableSignal`, `false` otherwise.\r\n *\r\n * @example\r\n * const mySignal = signal(0);\r\n * const myMutableSignal = mutable(0);\r\n *\r\n * if (isMutable(mySignal)) {\r\n * mySignal.mutate(x => x + 1); // This would cause a type error, as mySignal is not a MutableSignal.\r\n * }\r\n *\r\n * if (isMutable(myMutableSignal)) {\r\n * myMutableSignal.mutate(x => x + 1); // This is safe.\r\n * }\r\n */\r\nexport function isMutable<T = any>(\r\n value: WritableSignal<T>,\r\n): value is MutableSignal<T> {\r\n return 'mutate' in value && typeof value.mutate === 'function';\r\n}\r\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BG;SACa,UAAU,CACxB,MAAiB,EACjB,GAAuB,EACvB,MAA2C,EAAA;IAE3C,MAAM,QAAQ,GAAG,MAA2B;AAC5C,IAAA,QAAQ,CAAC,UAAU,GAAG,MAAM,MAAM;AAClC,IAAA,QAAQ,CAAC,GAAG,GAAG,GAAG;IAClB,QAAQ,CAAC,MAAM,GAAG,MAAM,KAAK,CAAC,OAAO,KAAK,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAE5E,IAAA,OAAO,QAAQ;AACjB;;ACgDgB,SAAA,SAAS,CACvB,KAAS,EACT,GAA2C,EAAA;IAE3C,MAAM,GAAG,GAAG,MAAM,CAAgB,KAAK,EAAE,GAAG,CAE3C;AACD,IAAA,MAAM,EAAE,GAAG,GAAG,EAAE,EAAE,IAAI,GAAG;AAEzB,IAAA,IAAI,OAAkD;AAEtD,IAAA,MAAM,WAAW,GAAG,GAAG,CAAC,GAAG;AAC3B,IAAA,MAAM,cAAc,GAAG,GAAG,CAAC,MAAM;AAEjC,IAA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A;;AAEG;;;;"}
|
|
1
|
+
{"version":3,"file":"mmstack-primitives.mjs","sources":["../../../../packages/primitives/src/lib/to-writable.ts","../../../../packages/primitives/src/lib/debounced.ts","../../../../packages/primitives/src/lib/derived.ts","../../../../packages/primitives/src/lib/map-array.ts","../../../../packages/primitives/src/lib/mutable.ts","../../../../packages/primitives/src/lib/stored.ts","../../../../packages/primitives/src/lib/with-history.ts","../../../../packages/primitives/src/mmstack-primitives.ts"],"sourcesContent":["import { Signal, untracked, WritableSignal } from '@angular/core';\n\n/**\n * Converts a read-only `Signal` into a `WritableSignal` by providing custom `set` and, optionally, `update` functions.\n * This can be useful for creating controlled write access to a signal that is otherwise read-only.\n *\n * @typeParam T - The type of value held by the signal.\n *\n * @param signal - The read-only `Signal` to be made writable.\n * @param set - A function that will be used to set the signal's value. This function *must* handle\n * the actual update mechanism (e.g., updating a backing store, emitting an event, etc.).\n * @param update - (Optional) A function that will be used to update the signal's value based on its\n * previous value. If not provided, a default `update` implementation is used that\n * calls the provided `set` function with the result of the updater function. The\n * default implementation uses `untracked` to avoid creating unnecessary dependencies\n * within the updater function.\n *\n * @returns A `WritableSignal` that uses the provided `set` and `update` functions. The `asReadonly`\n * method of the returned signal will still return the original read-only signal.\n *\n * @example\n * // Basic usage: Making a read-only signal writable with a custom set function.\n * const originalValue = signal({a: 0});\n * const readOnlySignal = computed(() => originalValue().a);\n * const writableSignal = toWritable(readOnlySignal, (newValue) => {\n * originalValue.update((prev) => { ...prev, a: newValue });\n * });\n *\n * writableSignal.set(5); // sets value of originalValue.a to 5 & triggers all signals\n */\nexport function toWritable<T>(\n signal: Signal<T>,\n set: (value: T) => void,\n update?: (updater: (value: T) => T) => void,\n): WritableSignal<T> {\n const internal = signal as WritableSignal<T>;\n internal.asReadonly = () => signal;\n internal.set = set;\n internal.update = update ?? ((updater) => set(updater(untracked(internal))));\n\n return internal;\n}\n","import {\n computed,\n type CreateSignalOptions,\n type Signal,\n signal,\n type WritableSignal,\n} from '@angular/core';\nimport { toWritable } from './to-writable';\n\n/**\n * Options for creating a debounced writable signal.\n * Extends Angular's `CreateSignalOptions` with a debounce time setting.\n *\n * @template T The type of value held by the signal.\n */\nexport type CreateDebouncedOptions<T> = CreateSignalOptions<T> & {\n /**\n * The debounce delay in milliseconds. Specifies how long to wait after the\n * last `set` or `update` call before the debounced signal reflects the new value.\n */\n ms?: number;\n};\n\n/**\n * A specialized `WritableSignal` whose publicly readable value updates are debounced.\n *\n * It provides access to the underlying, non-debounced signal via the `original` property.\n *\n * @template T The type of value held by the signal.\n */\nexport type DebouncedSignal<T> = WritableSignal<T> & {\n /**\n * A reference to the original, inner `WritableSignal`.\n * This signal's value is updated *immediately* upon calls to `set` or `update`\n * on the parent `DebouncedSignal`. Useful for accessing the latest value\n * without the debounce delay.\n */\n original: Signal<T>;\n};\n\n/**\n * Creates a `WritableSignal` whose publicly readable value is updated only after\n * a specified debounce period (`ms`) has passed since the last call to its\n * `.set()` or `.update()` method.\n *\n * This implementation avoids using `effect` by leveraging intermediate `computed`\n * signals and a custom `equal` function to delay value propagation based on a timer.\n *\n * @template T The type of value the signal holds.\n * @param initial The initial value of the signal.\n * @param opt Options for signal creation, including:\n * - `ms`: The debounce time in milliseconds. Defaults to 0 if omitted (no debounce).\n * - Other `CreateSignalOptions` (like `equal`) are passed to underlying signals.\n * @returns A `DebouncedSignal<T>` instance. Its readable value updates are debounced,\n * and it includes an `.original` property providing immediate access to the latest set value.\n *\n * @example\n * ```ts\n * import { effect } from '@angular/core';\n *\n * // Create a debounced signal with a 500ms delay\n * const query = debounced('', { ms: 500 });\n *\n * effect(() => {\n * // This effect runs 500ms after the last change to 'query'\n * console.log('Debounced Query:', query());\n * });\n *\n * effect(() => {\n * // This effect runs immediately when 'query.original' changes\n * console.log('Original Query:', query.original());\n * });\n *\n * console.log('Setting query to \"a\"');\n * query.set('a');\n * // Output: Original Query: a\n *\n * setTimeout(() => {\n * console.log('Setting query to \"ab\"');\n * query.set('ab');\n * // Output: Original Query: ab\n * }, 200); // Before debounce timeout\n *\n * setTimeout(() => {\n * console.log('Setting query to \"abc\"');\n * query.set('abc');\n * // Output: Original Query: abc\n * }, 400); // Before debounce timeout\n *\n * // ~500ms after the *last* set (at 400ms), the debounced effect runs:\n * // Output (at ~900ms): Debounced Query: abc\n * ```\n */\nexport function debounced<T>(\n initial: T,\n opt: CreateDebouncedOptions<T>,\n): DebouncedSignal<T> {\n const internal = signal(initial, opt);\n const ms = opt.ms ?? 0;\n\n const trigger = signal(false);\n\n let timeout: ReturnType<typeof setTimeout> | undefined;\n\n const set = (value: T) => {\n if (timeout) clearTimeout(timeout);\n internal.set(value);\n\n timeout = setTimeout(() => {\n trigger.update((c) => !c);\n }, ms);\n };\n\n const update = (fn: (prev: T) => T) => {\n if (timeout) clearTimeout(timeout);\n internal.update(fn);\n\n timeout = setTimeout(() => {\n trigger.update((c) => !c);\n }, ms);\n };\n\n const stable = computed(\n () => ({\n trigger: trigger(),\n value: internal(),\n }),\n {\n equal: (a, b) => a.trigger === b.trigger,\n },\n );\n\n const writable = toWritable(\n computed(() => stable().value, opt),\n set,\n update,\n ) as DebouncedSignal<T>;\n writable.original = internal;\n\n return writable;\n}\n","import {\n computed,\n CreateSignalOptions,\n signal,\n untracked,\n type WritableSignal,\n} from '@angular/core';\nimport type { UnknownObject } from '@mmstack/object';\nimport { toWritable } from './to-writable';\n\n/**\n * Options for creating a derived signal using the full `derived` function signature.\n * @typeParam T - The type of the source signal's value (parent).\n * @typeParam U - The type of the derived signal's value (child).\n */\ntype CreateDerivedOptions<T, U> = CreateSignalOptions<U> & {\n /**\n * A function that extracts the derived value (`U`) from the source signal's value (`T`).\n */\n from: (v: T) => U;\n /**\n * A function that updates the source signal's value (`T`) when the derived signal's value (`U`) changes.\n * This establishes the two-way binding.\n */\n onChange: (newValue: U) => void;\n};\n\n/**\n * A `WritableSignal` that derives its value from another `WritableSignal` (the \"source\" signal).\n * It provides two-way binding: changes to the source signal update the derived signal, and\n * changes to the derived signal update the source signal.\n *\n * @typeParam T - The type of the source signal's value (parent).\n * @typeParam U - The type of the derived signal's value (child).\n */\nexport type DerivedSignal<T, U> = WritableSignal<U> & {\n /**\n * The function used to derive the derived signal's value from the source signal's value.\n * This is primarily for internal use and introspection.\n */\n from: (v: T) => U;\n};\n\n/**\n * Creates a `DerivedSignal` that derives its value from another `WritableSignal` (the source signal).\n * This overload provides the most flexibility, allowing you to specify custom `from` and `onChange` functions.\n *\n * @typeParam T - The type of the source signal's value.\n * @typeParam U - The type of the derived signal's value.\n * @param source - The source `WritableSignal`.\n * @param options - An object containing the `from` and `onChange` functions, and optional signal options.\n * @returns A `DerivedSignal` instance.\n *\n * @example\n * const user = signal({ name: 'John', age: 30 });\n * const name = derived(user, {\n * from: (u) => u.name,\n * onChange: (newName) => user.update((u) => ({ ...u, name: newName })),\n * });\n */\nexport function derived<T, U>(\n source: WritableSignal<T>,\n opt: CreateDerivedOptions<T, U>,\n): DerivedSignal<T, U>;\n\n/**\n * Creates a `DerivedSignal` that derives a property from an object held by the source signal.\n * This overload simplifies creating derived signals for object properties.\n *\n * @typeParam T - The type of the source signal's value (must be an object).\n * @typeParam TKey - The key of the property to derive.\n * @param source - The source `WritableSignal` (holding an object).\n * @param key - The key of the property to derive.\n * @param options - Optional signal options for the derived signal.\n * @returns A `DerivedSignal` instance.\n *\n * @example\n * const user = signal({ name: 'John', age: 30 });\n * const name = derived(user, 'name');\n */\nexport function derived<T extends UnknownObject, TKey extends keyof T>(\n source: WritableSignal<T>,\n key: TKey,\n opt?: CreateSignalOptions<T[TKey]>,\n): DerivedSignal<T, T[TKey]>;\n\n/**\n * Creates a `DerivedSignal` from an array, and derives an element by index.\n *\n * @typeParam T - The type of the source signal's value (must be an array).\n * @param source - The source `WritableSignal` (holding an array).\n * @param index - The index of the element to derive.\n * @param options - Optional signal options for the derived signal.\n * @returns A `DerivedSignal` instance.\n *\n * @example\n * const numbers = signal([1, 2, 3]);\n * const secondNumber = derived(numbers, 1); // secondNumber() === 2\n * secondNumber.set(5); // numbers() === [1, 5, 3]\n */\nexport function derived<T extends any[]>(\n source: WritableSignal<T>,\n index: number,\n opt?: CreateSignalOptions<T[number]>,\n): DerivedSignal<T, T[number]>;\n\nexport function derived<T, U>(\n source: WritableSignal<T>,\n optOrKey: CreateDerivedOptions<T, U> | keyof T,\n opt?: CreateSignalOptions<U>,\n): DerivedSignal<T, U> {\n const isArray =\n Array.isArray(untracked(source)) && typeof optOrKey === 'number';\n\n const from =\n typeof optOrKey === 'object' ? optOrKey.from : (v: T) => v[optOrKey] as U;\n const onChange =\n typeof optOrKey === 'object'\n ? optOrKey.onChange\n : isArray\n ? (next: U) => {\n source.update(\n (cur) =>\n (cur as unknown as any[]).map((v, i) =>\n i === optOrKey ? next : v,\n ) as T,\n );\n }\n : (next: U) => {\n source.update((cur) => ({ ...cur, [optOrKey]: next }));\n };\n\n const rest = typeof optOrKey === 'object' ? optOrKey : opt;\n\n const sig = toWritable<U>(\n computed(() => from(source()), rest),\n (newVal) => onChange(newVal),\n ) as DerivedSignal<T, U>;\n\n sig.from = from;\n\n return sig;\n}\n\n/**\n * Creates a \"fake\" `DerivedSignal` from a simple value. This is useful for creating\n * `FormControlSignal` instances that are not directly derived from another signal.\n * The returned signal's `from` function will always return the initial value.\n *\n * @typeParam T - This type parameter is not used in the implementation but is kept for type compatibility with `DerivedSignal`.\n * @typeParam U - The type of the signal's value.\n * @param initial - The initial value of the signal.\n * @returns A `DerivedSignal` instance.\n * @internal\n */\nexport function toFakeDerivation<T, U>(initial: U): DerivedSignal<T, U> {\n const sig = signal(initial) as DerivedSignal<T, U>;\n sig.from = () => initial;\n\n return sig;\n}\n\n/**\n * Creates a \"fake\" `DerivedSignal` from an existing `WritableSignal`. This is useful\n * for treating a regular `WritableSignal` as a `DerivedSignal` without changing its behavior.\n * The returned signal's `from` function returns the current value of signal, using `untracked`.\n *\n * @typeParam T - This type parameter is not used in the implementation but is kept for type compatibility with `DerivedSignal`.\n * @typeParam U - The type of the signal's value.\n * @param initial - The existing `WritableSignal`.\n * @returns A `DerivedSignal` instance.\n * @internal\n */\nexport function toFakeSignalDerivation<T, U>(\n initial: WritableSignal<U>,\n): DerivedSignal<T, U> {\n const sig = initial as DerivedSignal<T, U>;\n sig.from = () => untracked(initial);\n return sig;\n}\n\n/**\n * Type guard function to check if a given `WritableSignal` is a `DerivedSignal`.\n *\n * @typeParam T - The type of the source signal's value (optional, defaults to `any`).\n * @typeParam U - The type of the derived signal's value (optional, defaults to `any`).\n * @param sig - The `WritableSignal` to check.\n * @returns `true` if the signal is a `DerivedSignal`, `false` otherwise.\n */\nexport function isDerivation<T, U>(\n sig: WritableSignal<U>,\n): sig is DerivedSignal<T, U> {\n return 'from' in sig;\n}\n","import {\n computed,\n type CreateSignalOptions,\n isSignal,\n linkedSignal,\n type Signal,\n} from '@angular/core';\n\n/**\n * Reactively maps items from a source array (or signal of an array) using a provided mapping function.\n *\n * This function serves a similar purpose to SolidJS's `mapArray` by providing stability\n * for mapped items. It receives a source function returning an array (or a Signal<T[]>)\n * and a mapping function.\n *\n * For each item in the source array, it creates a stable `computed` signal representing\n * that item's value at its current index. This stable signal (`Signal<T>`) is passed\n * to the mapping function. This ensures that downstream computations or components\n * depending on the mapped result only re-render or re-calculate for the specific items\n * that have changed, or when items are added/removed, rather than re-evaluating everything\n * when the source array reference changes but items remain the same.\n *\n * It efficiently handles changes in the source array's length by reusing existing mapped\n * results when possible, slicing when the array shrinks, and appending new mapped items\n * when it grows.\n *\n * @template T The type of items in the source array.\n * @template U The type of items in the resulting mapped array.\n *\n * @param source A function returning the source array `T[]`, or a `Signal<T[]>` itself.\n * The `mapArray` function will reactively update based on changes to this source.\n * @param map The mapping function. It is called for each item in the source array.\n * It receives:\n * - `value`: A stable `Signal<T>` representing the item at the current index.\n * Use this signal within your mapping logic if you need reactivity\n * tied to the specific item's value changes.\n * - `index`: The number index of the item in the array.\n * It should return the mapped value `U`.\n * @param [opt] Optional `CreateSignalOptions<T>`. These options are passed directly\n * to the `computed` signal created for each individual item (`Signal<T>`).\n * This allows specifying options like a custom `equal` function for item comparison.\n *\n * @returns A `Signal<U[]>` containing the mapped array. This signal updates whenever\n * the source array changes (either length or the values of its items).\n *\n * @example\n * ```ts\n * const sourceItems = signal([\n * { id: 1, name: 'Apple' },\n * { id: 2, name: 'Banana' }\n * ]);\n *\n * const mappedItems = mapArray(\n * sourceItems,\n * (itemSignal, index) => {\n * // itemSignal is stable for a given item based on its index.\n * // We create a computed here to react to changes in the item's name.\n * return computed(() => `${index}: ${itemSignal().name.toUpperCase()}`);\n * },\n * // Example optional options (e.g., custom equality for item signals)\n * { equal: (a, b) => a.id === b.id && a.name === b.name }\n * );\n * ```\n */\nexport function mapArray<T, U>(\n source: () => T[],\n map: (value: Signal<T>, index: number) => U,\n opt?: CreateSignalOptions<T>,\n): Signal<U[]> {\n const data = isSignal(source) ? source : computed(source);\n const len = computed(() => data().length);\n\n return linkedSignal<number, U[]>({\n source: () => len(),\n computation: (len, prev) => {\n if (!prev)\n return Array.from({ length: len }, (_, i) =>\n map(\n computed(() => source()[i], opt),\n i,\n ),\n );\n\n if (len === prev.value.length) return prev.value;\n\n if (len < prev.value.length) {\n return prev.value.slice(0, len);\n } else {\n const next = [...prev.value];\n for (let i = prev.value.length; i < len; i++) {\n next[i] = map(\n computed(() => source()[i], opt),\n i,\n );\n }\n return next;\n }\n },\n equal: (a, b) => a.length === b.length,\n });\n}\n","import {\r\n signal,\r\n type CreateSignalOptions,\r\n type ValueEqualityFn,\r\n type WritableSignal,\r\n} from '@angular/core';\r\n\r\nconst { is } = Object;\r\n\r\n/**\r\n * A `MutableSignal` is a special type of `WritableSignal` that allows for in-place mutation of its value.\r\n * In addition to the standard `set` and `update` methods, it provides a `mutate` method. This is useful\r\n * for performance optimization when dealing with complex objects or arrays, as it avoids unnecessary\r\n * object copying.\r\n *\r\n * @typeParam T - The type of value held by the signal.\r\n */\r\nexport type MutableSignal<T> = WritableSignal<T> & {\r\n /**\r\n * Mutates the signal's value in-place. This is similar to `update`, but it's optimized for\r\n * scenarios where you want to modify the existing object directly rather than creating a new one.\r\n *\r\n * @param updater - A function that takes the current value as input and modifies it directly.\r\n *\r\n * @example\r\n * const myArray = mutable([1, 2, 3]);\r\n * myArray.mutate((arr) => {\r\n * arr.push(4);\r\n * return arr;\r\n * }); // myArray() now returns [1, 2, 3, 4]\r\n */\r\n mutate: WritableSignal<T>['update'];\r\n\r\n /**\r\n * Mutates the signal's value in-place, similar to `mutate`, but with a void-returning value in updater\r\n * function. This further emphasizes that the mutation is happening inline, improving readability\r\n * in some cases.\r\n * @param updater - Function to change to the current value\r\n * @example\r\n * const myObject = mutable({ a: 1, b: 2 });\r\n * myObject.inline((obj) => (obj.a = 3)); // myObject() now returns { a: 3, b: 2 }\r\n */\r\n inline: (updater: (value: T) => void) => void;\r\n};\r\n\r\n/**\r\n * Creates a `MutableSignal`. This function overloads the standard `signal` function to provide\r\n * the additional `mutate` and `inline` methods.\r\n *\r\n * @typeParam T - The type of value held by the signal.\r\n *\r\n * @param initial - The initial value of the signal. If no initial value is provided, it defaults to `undefined`.\r\n * @param options - Optional. An object containing signal options, including a custom equality function (`equal`).\r\n *\r\n * @returns A `MutableSignal` instance.\r\n *\r\n * @example\r\n * // Create a mutable signal with an initial value:\r\n * const mySignal = mutable({ count: 0 }) // MutableSignal<{ count: number }>;\r\n *\r\n * // Create a mutable signal with no initial value (starts as undefined):\r\n * const = mutable<number>(); // MutableSignal<number | undefined>\r\n *\r\n * // Create a mutable signal with a custom equality function:\r\n * const myCustomSignal = mutable({ a: 1 }, { equal: (a, b) => a.a === b.a });\r\n */\r\nexport function mutable<T>(): MutableSignal<T | undefined>;\r\nexport function mutable<T>(initial: T): MutableSignal<T>;\r\nexport function mutable<T>(\r\n initial: T,\r\n opt?: CreateSignalOptions<T>,\r\n): MutableSignal<T>;\r\n\r\nexport function mutable<T>(\r\n initial?: T,\r\n opt?: CreateSignalOptions<T>,\r\n): MutableSignal<T> {\r\n const baseEqual = opt?.equal ?? is;\r\n let trigger = false;\r\n\r\n const equal: ValueEqualityFn<T | undefined> = (a, b) => {\r\n if (trigger) return false;\r\n return baseEqual(a, b);\r\n };\r\n\r\n const sig = signal<T | undefined>(initial, {\r\n ...opt,\r\n equal,\r\n }) as MutableSignal<T>;\r\n\r\n const internalUpdate = sig.update;\r\n\r\n sig.mutate = (updater) => {\r\n trigger = true;\r\n internalUpdate(updater);\r\n trigger = false;\r\n };\r\n\r\n sig.inline = (updater) => {\r\n sig.mutate((prev) => {\r\n updater(prev);\r\n return prev;\r\n });\r\n };\r\n\r\n return sig;\r\n}\r\n\r\n/**\r\n * Type guard function to check if a given `WritableSignal` is a `MutableSignal`. This is useful\r\n * for situations where you need to conditionally use the `mutate` or `inline` methods.\r\n *\r\n * @typeParam T - The type of the signal's value (optional, defaults to `any`).\r\n * @param value - The `WritableSignal` to check.\r\n * @returns `true` if the signal is a `MutableSignal`, `false` otherwise.\r\n *\r\n * @example\r\n * const mySignal = signal(0);\r\n * const myMutableSignal = mutable(0);\r\n *\r\n * if (isMutable(mySignal)) {\r\n * mySignal.mutate(x => x + 1); // This would cause a type error, as mySignal is not a MutableSignal.\r\n * }\r\n *\r\n * if (isMutable(myMutableSignal)) {\r\n * myMutableSignal.mutate(x => x + 1); // This is safe.\r\n * }\r\n */\r\nexport function isMutable<T = any>(\r\n value: WritableSignal<T>,\r\n): value is MutableSignal<T> {\r\n return 'mutate' in value && typeof value.mutate === 'function';\r\n}\r\n","import { isPlatformServer } from '@angular/common';\r\nimport {\r\n computed,\r\n DestroyRef,\r\n effect,\r\n inject,\r\n isDevMode,\r\n isSignal,\r\n PLATFORM_ID,\r\n Signal,\r\n signal,\r\n untracked,\r\n type CreateSignalOptions,\r\n type WritableSignal,\r\n} from '@angular/core';\r\nimport { toWritable } from './to-writable';\r\n\r\n/**\r\n * Interface for storage mechanisms compatible with the `stored` signal.\r\n * Matches the essential parts of the `Storage` interface (`localStorage`, `sessionStorage`).\r\n */\r\ntype Store = {\r\n /** Retrieves an item from storage for a given key. */\r\n getItem: (key: string) => string | null;\r\n /** Sets an item in storage for a given key. */\r\n setItem: (key: string, value: string) => void;\r\n /** Removes an item from storage for a given key. */\r\n removeItem: (key: string) => void;\r\n};\r\n\r\n// Internal dummy store for server-side rendering\r\nconst noopStore: Store = {\r\n getItem: () => null,\r\n setItem: () => {\r\n /* noop */\r\n },\r\n removeItem: () => {\r\n /* noop */\r\n },\r\n};\r\n\r\n/**\r\n * Options for creating a signal synchronized with persistent storage using `stored()`.\r\n * Extends Angular's `CreateSignalOptions`.\r\n *\r\n * @template T The type of value held by the signal.\r\n */\r\nexport type CreateStoredOptions<T> = CreateSignalOptions<T> & {\r\n /**\r\n * The key used to identify the item in storage.\r\n * Can be a static string or a function/signal returning a string for dynamic keys\r\n * (e.g., based on user ID or other application state).\r\n */\r\n key: string | (() => string);\r\n /**\r\n * Optional custom storage implementation (e.g., `sessionStorage` or a custom adapter).\r\n * Must conform to the `Store` interface (`getItem`, `setItem`, `removeItem`).\r\n * Defaults to `localStorage` in browser environments and a no-op store on the server.\r\n */\r\n store?: Store;\r\n /**\r\n * Optional function to serialize the value (type `T`) into a string before storing.\r\n * Defaults to `JSON.stringify`.\r\n * @param {T} value The value to serialize.\r\n * @returns {string} The serialized string representation.\r\n */\r\n serialize?: (value: T) => string;\r\n /**\r\n * Optional function to deserialize the string retrieved from storage back into the value (type `T`).\r\n * Defaults to `JSON.parse`.\r\n * @param {string} value The string retrieved from storage.\r\n * @returns {T} The deserialized value.\r\n */\r\n deserialize?: (value: string) => T;\r\n /**\r\n * If `true`, the signal will attempt to synchronize its state across multiple browser tabs\r\n * using the `storage` event. Changes made in one tab (set, update, clear) will be\r\n * reflected in other tabs using the same storage key.\r\n * Requires a browser environment. Defaults to `false`.\r\n */\r\n syncTabs?: boolean;\r\n};\r\n\r\n/**\r\n * A specialized `WritableSignal` returned by the `stored()` function.\r\n * It synchronizes its value with persistent storage and provides additional methods.\r\n *\r\n * @template T The type of value held by the signal (matches the fallback type).\r\n */\r\nexport type StoredSignal<T> = WritableSignal<T> & {\r\n /**\r\n * Removes the item associated with the signal's key from the configured storage.\r\n * After clearing, reading the signal will return the fallback value until it's set again.\r\n */\r\n clear: () => void;\r\n /**\r\n * A `Signal<string>` containing the current storage key being used by this stored signal.\r\n * This is particularly useful if the key was configured dynamically. You can read or react\r\n * to this signal to know the active key.\r\n */\r\n key: Signal<string>;\r\n};\r\n\r\n/**\r\n * Creates a `WritableSignal` whose state is automatically synchronized with persistent storage\r\n * (like `localStorage` or `sessionStorage`).\r\n *\r\n * It handles Server-Side Rendering (SSR) gracefully, allows dynamic storage keys,\r\n * custom serialization/deserialization, custom storage providers, and optional\r\n * synchronization across browser tabs.\r\n *\r\n * @template T The type of value held by the signal and stored (after serialization).\r\n * @param fallback The default value of type `T` to use when no value is found in storage\r\n * or when deserialization fails. The signal's value will never be `null` or `undefined`\r\n * publicly, it will always revert to this fallback.\r\n * @param options Configuration options (`CreateStoredOptions<T>`). Requires at least the `key`.\r\n * @returns A `StoredSignal<T>` instance. This signal behaves like a standard `WritableSignal<T>`,\r\n * but its value is persisted. It includes a `.clear()` method to remove the item from storage\r\n * and a `.key` signal providing the current storage key.\r\n *\r\n * @remarks\r\n * - **Persistence:** The signal automatically saves its value to storage whenever the signal's\r\n * value or its configured `key` changes. This is managed internally using `effect`.\r\n * - **SSR Safety:** Detects server environments and uses a no-op storage, preventing errors.\r\n * - **Error Handling:** Catches and logs errors during serialization/deserialization in dev mode.\r\n * - **Tab Sync:** If `syncTabs` is true, listens to `storage` events to keep the signal value\r\n * consistent across browser tabs using the same key. Cleanup is handled automatically\r\n * using `DestroyRef`.\r\n * - **Removal:** Use the `.clear()` method on the returned signal to remove the item from storage.\r\n * Setting the signal to the fallback value will store the fallback value, not remove the item.\r\n *\r\n * @example\r\n * ```ts\r\n * import { Component, effect, signal } from '@angular/core';\r\n * import { stored } from '@mmstack/primitives'; // Adjust import path\r\n *\r\n * @Component({\r\n * selector: 'app-settings',\r\n * standalone: true,\r\n * template: `\r\n * Theme:\r\n * <select [ngModel]=\"theme()\" (ngModelChange)=\"theme.set($event)\">\r\n * <option value=\"light\">Light</option>\r\n * <option value=\"dark\">Dark</option>\r\n * </select>\r\n * <button (click)=\"theme.clear()\">Clear Theme Setting</button>\r\n * <p>Storage Key Used: {{ theme.key() }}</p>\r\n * ` // Requires FormsModule for ngModel\r\n * })\r\n * export class SettingsComponent {\r\n * theme = stored<'light' | 'dark'>('light', { key: 'app-theme', syncTabs: true });\r\n * }\r\n * ```\r\n */\r\nexport function stored<T>(\r\n fallback: T,\r\n {\r\n key,\r\n store: providedStore,\r\n serialize = JSON.stringify,\r\n deserialize = JSON.parse,\r\n syncTabs = false,\r\n equal = Object.is,\r\n ...rest\r\n }: CreateStoredOptions<T>,\r\n): StoredSignal<T> {\r\n const isServer = isPlatformServer(inject(PLATFORM_ID));\r\n\r\n const fallbackStore = isServer ? noopStore : localStorage;\r\n const store = providedStore ?? fallbackStore;\r\n\r\n const keySig =\r\n typeof key === 'string'\r\n ? computed(() => key)\r\n : isSignal(key)\r\n ? key\r\n : computed(key);\r\n\r\n const getValue = (key: string): T | null => {\r\n const found = store.getItem(key);\r\n if (found === null) return null;\r\n try {\r\n return deserialize(found);\r\n } catch (err) {\r\n if (isDevMode())\r\n console.error(`Failed to parse stored value for key \"${key}\":`, err);\r\n return null;\r\n }\r\n };\r\n\r\n const storeValue = (key: string, value: T | null) => {\r\n try {\r\n if (value === null) return store.removeItem(key);\r\n const serialized = serialize(value);\r\n store.setItem(key, serialized);\r\n } catch (err) {\r\n if (isDevMode())\r\n console.error(`Failed to store value for key \"${key}\":`, err);\r\n }\r\n };\r\n\r\n const opt = {\r\n ...rest,\r\n equal,\r\n };\r\n\r\n const internal = signal(getValue(untracked(keySig)), {\r\n ...opt,\r\n equal: (a, b) => {\r\n if (a === null && b === null) return true;\r\n if (a === null || b === null) return false;\r\n return equal(a, b);\r\n },\r\n });\r\n\r\n effect(() => storeValue(keySig(), internal()));\r\n\r\n if (syncTabs && !isServer) {\r\n const destroyRef = inject(DestroyRef);\r\n const sync = (e: StorageEvent) => {\r\n if (e.key !== untracked(keySig)) return;\r\n\r\n if (e.newValue === null) internal.set(null);\r\n else internal.set(getValue(e.key));\r\n };\r\n\r\n window.addEventListener('storage', sync);\r\n\r\n destroyRef.onDestroy(() => window.removeEventListener('storage', sync));\r\n }\r\n\r\n const writable = toWritable<T>(\r\n computed(() => internal() ?? fallback, opt),\r\n internal.set,\r\n ) as StoredSignal<T>;\r\n\r\n writable.clear = () => {\r\n internal.set(null);\r\n };\r\n writable.key = keySig;\r\n return writable;\r\n}\r\n","import {\n computed,\n type CreateSignalOptions,\n type Signal,\n untracked,\n type ValueEqualityFn,\n type WritableSignal,\n} from '@angular/core';\nimport { SIGNAL } from '@angular/core/primitives/signals';\nimport { mutable } from './mutable';\nimport { toWritable } from './to-writable';\n\nexport type SignalWithHistory<T> = WritableSignal<T> & {\n history: Signal<T[]>;\n undo: () => void;\n redo: () => void;\n canRedo: Signal<boolean>;\n canUndo: Signal<boolean>;\n clear: () => void;\n canClear: Signal<boolean>;\n};\n\nexport type CreateHistoryOptions<T> = Omit<\n CreateSignalOptions<T[]>,\n 'equal'\n> & {\n maxSize?: number;\n};\n\nexport function withHistory<T>(\n source: WritableSignal<T>,\n opt?: CreateHistoryOptions<T>,\n): SignalWithHistory<T> {\n const { equal = Object.is, debugName } = source[SIGNAL] as {\n equal?: ValueEqualityFn<T>;\n debugName?: string;\n };\n\n const maxSize = opt?.maxSize ?? Infinity;\n\n const history = mutable<T[]>([], opt);\n\n const redoArray = mutable<T[]>([]);\n\n const set = (value: T) => {\n const current = untracked(source);\n if (equal(value, current)) return;\n\n source.set(value);\n\n history.mutate((c) => {\n if (c.length >= maxSize) {\n c = c.slice(Math.floor(maxSize / 2));\n }\n c.push(current);\n return c;\n });\n redoArray.set([]);\n };\n\n const update = (updater: (prev: T) => T) => {\n set(updater(untracked(source)));\n };\n\n const internal = toWritable(\n computed(() => source(), {\n equal,\n debugName,\n }),\n set,\n update,\n ) as SignalWithHistory<T>;\n internal.history = history;\n\n internal.undo = () => {\n const last = untracked(history);\n if (last.length === 0) return;\n\n const prev = last.at(-1)!;\n const cur = untracked(source);\n\n history.inline((c) => c.pop());\n redoArray.inline((c) => c.push(cur));\n\n source.set(prev);\n };\n\n internal.redo = () => {\n const last = untracked(redoArray);\n if (last.length === 0) return;\n\n const prev = last.at(-1)!;\n\n redoArray.inline((c) => c.pop());\n\n set(prev);\n };\n\n internal.clear = () => {\n history.set([]);\n redoArray.set([]);\n };\n\n internal.canUndo = computed(() => history().length > 0);\n internal.canRedo = computed(() => redoArray().length > 0);\n internal.canClear = computed(() => internal.canUndo() || internal.canRedo());\n\n return internal;\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BG;SACa,UAAU,CACxB,MAAiB,EACjB,GAAuB,EACvB,MAA2C,EAAA;IAE3C,MAAM,QAAQ,GAAG,MAA2B;AAC5C,IAAA,QAAQ,CAAC,UAAU,GAAG,MAAM,MAAM;AAClC,IAAA,QAAQ,CAAC,GAAG,GAAG,GAAG;IAClB,QAAQ,CAAC,MAAM,GAAG,MAAM,KAAK,CAAC,OAAO,KAAK,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAE5E,IAAA,OAAO,QAAQ;AACjB;;ACDA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDG;AACa,SAAA,SAAS,CACvB,OAAU,EACV,GAA8B,EAAA;IAE9B,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC;AACrC,IAAA,MAAM,EAAE,GAAG,GAAG,CAAC,EAAE,IAAI,CAAC;AAEtB,IAAA,MAAM,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC;AAE7B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MAAK;AACnB,QAAA,MAAM,IAAI,GAAG,SAAS,CAAC,SAAS,CAAC;AACjC,QAAA,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;YAAE;QAEvB,MAAM,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAE;AAEzB,QAAA,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,CAAC;QAEhC,GAAG,CAAC,IAAI,CAAC;AACX,KAAC;AAED,IAAA,QAAQ,CAAC,KAAK,GAAG,MAAK;AACpB,QAAA,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;AACf,QAAA,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC;AACnB,KAAC;AAED,IAAA,QAAQ,CAAC,OAAO,GAAG,QAAQ,CAAC,MAAM,OAAO,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC;AACvD,IAAA,QAAQ,CAAC,OAAO,GAAG,QAAQ,CAAC,MAAM,SAAS,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC;AACzD,IAAA,QAAQ,CAAC,QAAQ,GAAG,QAAQ,CAAC,MAAM,QAAQ,CAAC,OAAO,EAAE,IAAI,QAAQ,CAAC,OAAO,EAAE,CAAC;AAE5E,IAAA,OAAO,QAAQ;AACjB;;AC5GA;;AAEG;;;;"}
|
package/index.d.ts
CHANGED
package/lib/debounced.d.ts
CHANGED
|
@@ -1,76 +1,84 @@
|
|
|
1
|
-
import { type CreateSignalOptions, type WritableSignal } from '@angular/core';
|
|
1
|
+
import { type CreateSignalOptions, type Signal, type WritableSignal } from '@angular/core';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* frequent updates, such as responding to user input in a search field.
|
|
6
|
-
* It keeps a reference to the original `WritableSignal` via the `original` property.
|
|
3
|
+
* Options for creating a debounced writable signal.
|
|
4
|
+
* Extends Angular's `CreateSignalOptions` with a debounce time setting.
|
|
7
5
|
*
|
|
8
|
-
* @
|
|
6
|
+
* @template T The type of value held by the signal.
|
|
9
7
|
*/
|
|
10
|
-
export type
|
|
8
|
+
export type CreateDebouncedOptions<T> = CreateSignalOptions<T> & {
|
|
11
9
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* and also ensures that any direct modifications to the original signal
|
|
15
|
-
* are reflected in the debounced signal after the debounce period.
|
|
10
|
+
* The debounce delay in milliseconds. Specifies how long to wait after the
|
|
11
|
+
* last `set` or `update` call before the debounced signal reflects the new value.
|
|
16
12
|
*/
|
|
17
|
-
|
|
13
|
+
ms?: number;
|
|
18
14
|
};
|
|
19
15
|
/**
|
|
20
|
-
*
|
|
16
|
+
* A specialized `WritableSignal` whose publicly readable value updates are debounced.
|
|
17
|
+
*
|
|
18
|
+
* It provides access to the underlying, non-debounced signal via the `original` property.
|
|
21
19
|
*
|
|
22
|
-
* @
|
|
20
|
+
* @template T The type of value held by the signal.
|
|
23
21
|
*/
|
|
24
|
-
export type
|
|
22
|
+
export type DebouncedSignal<T> = WritableSignal<T> & {
|
|
25
23
|
/**
|
|
26
|
-
*
|
|
24
|
+
* A reference to the original, inner `WritableSignal`.
|
|
25
|
+
* This signal's value is updated *immediately* upon calls to `set` or `update`
|
|
26
|
+
* on the parent `DebouncedSignal`. Useful for accessing the latest value
|
|
27
|
+
* without the debounce delay.
|
|
27
28
|
*/
|
|
28
|
-
|
|
29
|
+
original: Signal<T>;
|
|
29
30
|
};
|
|
30
31
|
/**
|
|
31
|
-
* Creates a
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
32
|
+
* Creates a `WritableSignal` whose publicly readable value is updated only after
|
|
33
|
+
* a specified debounce period (`ms`) has passed since the last call to its
|
|
34
|
+
* `.set()` or `.update()` method.
|
|
35
|
+
*
|
|
36
|
+
* This implementation avoids using `effect` by leveraging intermediate `computed`
|
|
37
|
+
* signals and a custom `equal` function to delay value propagation based on a timer.
|
|
35
38
|
*
|
|
36
|
-
* @
|
|
37
|
-
* @
|
|
39
|
+
* @template T The type of value the signal holds.
|
|
40
|
+
* @param initial The initial value of the signal.
|
|
41
|
+
* @param opt Options for signal creation, including:
|
|
42
|
+
* - `ms`: The debounce time in milliseconds. Defaults to 0 if omitted (no debounce).
|
|
43
|
+
* - Other `CreateSignalOptions` (like `equal`) are passed to underlying signals.
|
|
44
|
+
* @returns A `DebouncedSignal<T>` instance. Its readable value updates are debounced,
|
|
45
|
+
* and it includes an `.original` property providing immediate access to the latest set value.
|
|
38
46
|
*
|
|
39
47
|
* @example
|
|
40
|
-
* ```
|
|
41
|
-
*
|
|
42
|
-
* const searchTerm = debounced('initial value', { ms: 500 });
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { effect } from '@angular/core';
|
|
43
50
|
*
|
|
44
|
-
* //
|
|
45
|
-
*
|
|
51
|
+
* // Create a debounced signal with a 500ms delay
|
|
52
|
+
* const query = debounced('', { ms: 500 });
|
|
46
53
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
54
|
+
* effect(() => {
|
|
55
|
+
* // This effect runs 500ms after the last change to 'query'
|
|
56
|
+
* console.log('Debounced Query:', query());
|
|
57
|
+
* });
|
|
51
58
|
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* console.log(
|
|
55
|
-
*
|
|
56
|
-
* // ... after 500ms ...
|
|
57
|
-
* console.log(searchTerm()); // Outputs 'direct update' (now reflects the original signal)
|
|
59
|
+
* effect(() => {
|
|
60
|
+
* // This effect runs immediately when 'query.original' changes
|
|
61
|
+
* console.log('Original Query:', query.original());
|
|
62
|
+
* });
|
|
58
63
|
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @typeParam T - The type of the signal's value.
|
|
63
|
-
* @param initial The initial value of the signal. Optional; defaults to `undefined`.
|
|
64
|
-
* @param opt Configuration options for the signal, including the debounce delay (`ms`).
|
|
65
|
-
* @returns A `DebouncedSignal` instance.
|
|
66
|
-
*/
|
|
67
|
-
export declare function debounced<T>(): DebouncedSignal<T | undefined>;
|
|
68
|
-
/**
|
|
69
|
-
* Creates a debounced signal with a defined initial value.
|
|
64
|
+
* console.log('Setting query to "a"');
|
|
65
|
+
* query.set('a');
|
|
66
|
+
* // Output: Original Query: a
|
|
70
67
|
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
68
|
+
* setTimeout(() => {
|
|
69
|
+
* console.log('Setting query to "ab"');
|
|
70
|
+
* query.set('ab');
|
|
71
|
+
* // Output: Original Query: ab
|
|
72
|
+
* }, 200); // Before debounce timeout
|
|
73
|
+
*
|
|
74
|
+
* setTimeout(() => {
|
|
75
|
+
* console.log('Setting query to "abc"');
|
|
76
|
+
* query.set('abc');
|
|
77
|
+
* // Output: Original Query: abc
|
|
78
|
+
* }, 400); // Before debounce timeout
|
|
79
|
+
*
|
|
80
|
+
* // ~500ms after the *last* set (at 400ms), the debounced effect runs:
|
|
81
|
+
* // Output (at ~900ms): Debounced Query: abc
|
|
82
|
+
* ```
|
|
75
83
|
*/
|
|
76
|
-
export declare function debounced<T>(initial: T, opt
|
|
84
|
+
export declare function debounced<T>(initial: T, opt: CreateDebouncedOptions<T>): DebouncedSignal<T>;
|
package/lib/map-array.d.ts
CHANGED
|
@@ -1,56 +1,58 @@
|
|
|
1
1
|
import { type CreateSignalOptions, type Signal } from '@angular/core';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* for
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
* @
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
3
|
+
* Reactively maps items from a source array (or signal of an array) using a provided mapping function.
|
|
4
|
+
*
|
|
5
|
+
* This function serves a similar purpose to SolidJS's `mapArray` by providing stability
|
|
6
|
+
* for mapped items. It receives a source function returning an array (or a Signal<T[]>)
|
|
7
|
+
* and a mapping function.
|
|
8
|
+
*
|
|
9
|
+
* For each item in the source array, it creates a stable `computed` signal representing
|
|
10
|
+
* that item's value at its current index. This stable signal (`Signal<T>`) is passed
|
|
11
|
+
* to the mapping function. This ensures that downstream computations or components
|
|
12
|
+
* depending on the mapped result only re-render or re-calculate for the specific items
|
|
13
|
+
* that have changed, or when items are added/removed, rather than re-evaluating everything
|
|
14
|
+
* when the source array reference changes but items remain the same.
|
|
15
|
+
*
|
|
16
|
+
* It efficiently handles changes in the source array's length by reusing existing mapped
|
|
17
|
+
* results when possible, slicing when the array shrinks, and appending new mapped items
|
|
18
|
+
* when it grows.
|
|
19
|
+
*
|
|
20
|
+
* @template T The type of items in the source array.
|
|
21
|
+
* @template U The type of items in the resulting mapped array.
|
|
22
|
+
*
|
|
23
|
+
* @param source A function returning the source array `T[]`, or a `Signal<T[]>` itself.
|
|
24
|
+
* The `mapArray` function will reactively update based on changes to this source.
|
|
25
|
+
* @param map The mapping function. It is called for each item in the source array.
|
|
26
|
+
* It receives:
|
|
27
|
+
* - `value`: A stable `Signal<T>` representing the item at the current index.
|
|
28
|
+
* Use this signal within your mapping logic if you need reactivity
|
|
29
|
+
* tied to the specific item's value changes.
|
|
30
|
+
* - `index`: The number index of the item in the array.
|
|
31
|
+
* It should return the mapped value `U`.
|
|
32
|
+
* @param [opt] Optional `CreateSignalOptions<T>`. These options are passed directly
|
|
33
|
+
* to the `computed` signal created for each individual item (`Signal<T>`).
|
|
34
|
+
* This allows specifying options like a custom `equal` function for item comparison.
|
|
35
|
+
*
|
|
36
|
+
* @returns A `Signal<U[]>` containing the mapped array. This signal updates whenever
|
|
37
|
+
* the source array changes (either length or the values of its items).
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const sourceItems = signal([
|
|
42
|
+
* { id: 1, name: 'Apple' },
|
|
43
|
+
* { id: 2, name: 'Banana' }
|
|
44
|
+
* ]);
|
|
45
|
+
*
|
|
46
|
+
* const mappedItems = mapArray(
|
|
47
|
+
* sourceItems,
|
|
48
|
+
* (itemSignal, index) => {
|
|
49
|
+
* // itemSignal is stable for a given item based on its index.
|
|
50
|
+
* // We create a computed here to react to changes in the item's name.
|
|
51
|
+
* return computed(() => `${index}: ${itemSignal().name.toUpperCase()}`);
|
|
52
|
+
* },
|
|
53
|
+
* // Example optional options (e.g., custom equality for item signals)
|
|
54
|
+
* { equal: (a, b) => a.id === b.id && a.name === b.name }
|
|
55
|
+
* );
|
|
56
|
+
* ```
|
|
55
57
|
*/
|
|
56
|
-
export declare function mapArray<T>(source: () =>
|
|
58
|
+
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
|
@@ -31,7 +31,7 @@ export type MutableSignal<T> = WritableSignal<T> & {
|
|
|
31
31
|
* const myObject = mutable({ a: 1, b: 2 });
|
|
32
32
|
* myObject.inline((obj) => (obj.a = 3)); // myObject() now returns { a: 3, b: 2 }
|
|
33
33
|
*/
|
|
34
|
-
inline: (updater:
|
|
34
|
+
inline: (updater: (value: T) => void) => void;
|
|
35
35
|
};
|
|
36
36
|
/**
|
|
37
37
|
* Creates a `MutableSignal`. This function overloads the standard `signal` function to provide
|
package/lib/stored.d.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { Signal, type CreateSignalOptions, type WritableSignal } from '@angular/core';
|
|
2
|
+
/**
|
|
3
|
+
* Interface for storage mechanisms compatible with the `stored` signal.
|
|
4
|
+
* Matches the essential parts of the `Storage` interface (`localStorage`, `sessionStorage`).
|
|
5
|
+
*/
|
|
6
|
+
type Store = {
|
|
7
|
+
/** Retrieves an item from storage for a given key. */
|
|
8
|
+
getItem: (key: string) => string | null;
|
|
9
|
+
/** Sets an item in storage for a given key. */
|
|
10
|
+
setItem: (key: string, value: string) => void;
|
|
11
|
+
/** Removes an item from storage for a given key. */
|
|
12
|
+
removeItem: (key: string) => void;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Options for creating a signal synchronized with persistent storage using `stored()`.
|
|
16
|
+
* Extends Angular's `CreateSignalOptions`.
|
|
17
|
+
*
|
|
18
|
+
* @template T The type of value held by the signal.
|
|
19
|
+
*/
|
|
20
|
+
export type CreateStoredOptions<T> = CreateSignalOptions<T> & {
|
|
21
|
+
/**
|
|
22
|
+
* The key used to identify the item in storage.
|
|
23
|
+
* Can be a static string or a function/signal returning a string for dynamic keys
|
|
24
|
+
* (e.g., based on user ID or other application state).
|
|
25
|
+
*/
|
|
26
|
+
key: string | (() => string);
|
|
27
|
+
/**
|
|
28
|
+
* Optional custom storage implementation (e.g., `sessionStorage` or a custom adapter).
|
|
29
|
+
* Must conform to the `Store` interface (`getItem`, `setItem`, `removeItem`).
|
|
30
|
+
* Defaults to `localStorage` in browser environments and a no-op store on the server.
|
|
31
|
+
*/
|
|
32
|
+
store?: Store;
|
|
33
|
+
/**
|
|
34
|
+
* Optional function to serialize the value (type `T`) into a string before storing.
|
|
35
|
+
* Defaults to `JSON.stringify`.
|
|
36
|
+
* @param {T} value The value to serialize.
|
|
37
|
+
* @returns {string} The serialized string representation.
|
|
38
|
+
*/
|
|
39
|
+
serialize?: (value: T) => string;
|
|
40
|
+
/**
|
|
41
|
+
* Optional function to deserialize the string retrieved from storage back into the value (type `T`).
|
|
42
|
+
* Defaults to `JSON.parse`.
|
|
43
|
+
* @param {string} value The string retrieved from storage.
|
|
44
|
+
* @returns {T} The deserialized value.
|
|
45
|
+
*/
|
|
46
|
+
deserialize?: (value: string) => T;
|
|
47
|
+
/**
|
|
48
|
+
* If `true`, the signal will attempt to synchronize its state across multiple browser tabs
|
|
49
|
+
* using the `storage` event. Changes made in one tab (set, update, clear) will be
|
|
50
|
+
* reflected in other tabs using the same storage key.
|
|
51
|
+
* Requires a browser environment. Defaults to `false`.
|
|
52
|
+
*/
|
|
53
|
+
syncTabs?: boolean;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* A specialized `WritableSignal` returned by the `stored()` function.
|
|
57
|
+
* It synchronizes its value with persistent storage and provides additional methods.
|
|
58
|
+
*
|
|
59
|
+
* @template T The type of value held by the signal (matches the fallback type).
|
|
60
|
+
*/
|
|
61
|
+
export type StoredSignal<T> = WritableSignal<T> & {
|
|
62
|
+
/**
|
|
63
|
+
* Removes the item associated with the signal's key from the configured storage.
|
|
64
|
+
* After clearing, reading the signal will return the fallback value until it's set again.
|
|
65
|
+
*/
|
|
66
|
+
clear: () => void;
|
|
67
|
+
/**
|
|
68
|
+
* A `Signal<string>` containing the current storage key being used by this stored signal.
|
|
69
|
+
* This is particularly useful if the key was configured dynamically. You can read or react
|
|
70
|
+
* to this signal to know the active key.
|
|
71
|
+
*/
|
|
72
|
+
key: Signal<string>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Creates a `WritableSignal` whose state is automatically synchronized with persistent storage
|
|
76
|
+
* (like `localStorage` or `sessionStorage`).
|
|
77
|
+
*
|
|
78
|
+
* It handles Server-Side Rendering (SSR) gracefully, allows dynamic storage keys,
|
|
79
|
+
* custom serialization/deserialization, custom storage providers, and optional
|
|
80
|
+
* synchronization across browser tabs.
|
|
81
|
+
*
|
|
82
|
+
* @template T The type of value held by the signal and stored (after serialization).
|
|
83
|
+
* @param fallback The default value of type `T` to use when no value is found in storage
|
|
84
|
+
* or when deserialization fails. The signal's value will never be `null` or `undefined`
|
|
85
|
+
* publicly, it will always revert to this fallback.
|
|
86
|
+
* @param options Configuration options (`CreateStoredOptions<T>`). Requires at least the `key`.
|
|
87
|
+
* @returns A `StoredSignal<T>` instance. This signal behaves like a standard `WritableSignal<T>`,
|
|
88
|
+
* but its value is persisted. It includes a `.clear()` method to remove the item from storage
|
|
89
|
+
* and a `.key` signal providing the current storage key.
|
|
90
|
+
*
|
|
91
|
+
* @remarks
|
|
92
|
+
* - **Persistence:** The signal automatically saves its value to storage whenever the signal's
|
|
93
|
+
* value or its configured `key` changes. This is managed internally using `effect`.
|
|
94
|
+
* - **SSR Safety:** Detects server environments and uses a no-op storage, preventing errors.
|
|
95
|
+
* - **Error Handling:** Catches and logs errors during serialization/deserialization in dev mode.
|
|
96
|
+
* - **Tab Sync:** If `syncTabs` is true, listens to `storage` events to keep the signal value
|
|
97
|
+
* consistent across browser tabs using the same key. Cleanup is handled automatically
|
|
98
|
+
* using `DestroyRef`.
|
|
99
|
+
* - **Removal:** Use the `.clear()` method on the returned signal to remove the item from storage.
|
|
100
|
+
* Setting the signal to the fallback value will store the fallback value, not remove the item.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* import { Component, effect, signal } from '@angular/core';
|
|
105
|
+
* import { stored } from '@mmstack/primitives'; // Adjust import path
|
|
106
|
+
*
|
|
107
|
+
* @Component({
|
|
108
|
+
* selector: 'app-settings',
|
|
109
|
+
* standalone: true,
|
|
110
|
+
* template: `
|
|
111
|
+
* Theme:
|
|
112
|
+
* <select [ngModel]="theme()" (ngModelChange)="theme.set($event)">
|
|
113
|
+
* <option value="light">Light</option>
|
|
114
|
+
* <option value="dark">Dark</option>
|
|
115
|
+
* </select>
|
|
116
|
+
* <button (click)="theme.clear()">Clear Theme Setting</button>
|
|
117
|
+
* <p>Storage Key Used: {{ theme.key() }}</p>
|
|
118
|
+
* ` // Requires FormsModule for ngModel
|
|
119
|
+
* })
|
|
120
|
+
* export class SettingsComponent {
|
|
121
|
+
* theme = stored<'light' | 'dark'>('light', { key: 'app-theme', syncTabs: true });
|
|
122
|
+
* }
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export declare function stored<T>(fallback: T, { key, store: providedStore, serialize, deserialize, syncTabs, equal, ...rest }: CreateStoredOptions<T>): StoredSignal<T>;
|
|
126
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type CreateSignalOptions, type Signal, type WritableSignal } from '@angular/core';
|
|
2
|
+
export type SignalWithHistory<T> = WritableSignal<T> & {
|
|
3
|
+
history: Signal<T[]>;
|
|
4
|
+
undo: () => void;
|
|
5
|
+
redo: () => void;
|
|
6
|
+
canRedo: Signal<boolean>;
|
|
7
|
+
canUndo: Signal<boolean>;
|
|
8
|
+
clear: () => void;
|
|
9
|
+
canClear: Signal<boolean>;
|
|
10
|
+
};
|
|
11
|
+
export type CreateHistoryOptions<T> = Omit<CreateSignalOptions<T[]>, 'equal'> & {
|
|
12
|
+
maxSize?: number;
|
|
13
|
+
};
|
|
14
|
+
export declare function withHistory<T>(source: WritableSignal<T>, opt?: CreateHistoryOptions<T>): SignalWithHistory<T>;
|