@mmstack/primitives 19.0.0 → 19.0.1

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,4 +1,96 @@
1
- import { signal, untracked } from '@angular/core';
1
+ import { untracked, computed, signal } from '@angular/core';
2
+
3
+ /**
4
+ * Converts a read-only `Signal` into a `WritableSignal` by providing custom `set` and, optionally, `update` functions.
5
+ * This can be useful for creating controlled write access to a signal that is otherwise read-only.
6
+ *
7
+ * @typeParam T - The type of value held by the signal.
8
+ *
9
+ * @param signal - The read-only `Signal` to be made writable.
10
+ * @param set - A function that will be used to set the signal's value. This function *must* handle
11
+ * the actual update mechanism (e.g., updating a backing store, emitting an event, etc.).
12
+ * @param update - (Optional) A function that will be used to update the signal's value based on its
13
+ * previous value. If not provided, a default `update` implementation is used that
14
+ * calls the provided `set` function with the result of the updater function. The
15
+ * default implementation uses `untracked` to avoid creating unnecessary dependencies
16
+ * within the updater function.
17
+ *
18
+ * @returns A `WritableSignal` that uses the provided `set` and `update` functions. The `asReadonly`
19
+ * method of the returned signal will still return the original read-only signal.
20
+ *
21
+ * @example
22
+ * // Basic usage: Making a read-only signal writable with a custom set function.
23
+ * const originalValue = signal({a: 0});
24
+ * const readOnlySignal = computed(() => originalValue().a);
25
+ * const writableSignal = toWritable(readOnlySignal, (newValue) => {
26
+ * originalValue.update((prev) => { ...prev, a: newValue });
27
+ * });
28
+ *
29
+ * writableSignal.set(5); // sets value of originalValue.a to 5 & triggers all signals
30
+ */
31
+ function toWritable(signal, set, update) {
32
+ const internal = signal;
33
+ internal.asReadonly = () => signal;
34
+ internal.set = set;
35
+ internal.update = update ?? ((updater) => set(updater(untracked(internal))));
36
+ return internal;
37
+ }
38
+
39
+ function derived(source, optOrKey, opt) {
40
+ const from = typeof optOrKey === 'object' ? optOrKey.from : (v) => v[optOrKey];
41
+ const onChange = typeof optOrKey === 'object'
42
+ ? optOrKey.onChange
43
+ : (next) => {
44
+ source.update((cur) => ({ ...cur, [optOrKey]: next }));
45
+ };
46
+ const rest = typeof optOrKey === 'object' ? optOrKey : opt;
47
+ const sig = toWritable(computed(() => from(source()), rest), (newVal) => onChange(newVal));
48
+ sig.from = from;
49
+ return sig;
50
+ }
51
+ /**
52
+ * Creates a "fake" `DerivedSignal` from a simple value. This is useful for creating
53
+ * `FormControlSignal` instances that are not directly derived from another signal.
54
+ * The returned signal's `from` function will always return the initial value.
55
+ *
56
+ * @typeParam T - This type parameter is not used in the implementation but is kept for type compatibility with `DerivedSignal`.
57
+ * @typeParam U - The type of the signal's value.
58
+ * @param initial - The initial value of the signal.
59
+ * @returns A `DerivedSignal` instance.
60
+ * @internal
61
+ */
62
+ function toFakeDerivation(initial) {
63
+ const sig = signal(initial);
64
+ sig.from = () => initial;
65
+ return sig;
66
+ }
67
+ /**
68
+ * Creates a "fake" `DerivedSignal` from an existing `WritableSignal`. This is useful
69
+ * for treating a regular `WritableSignal` as a `DerivedSignal` without changing its behavior.
70
+ * The returned signal's `from` function returns the current value of signal, using `untracked`.
71
+ *
72
+ * @typeParam T - This type parameter is not used in the implementation but is kept for type compatibility with `DerivedSignal`.
73
+ * @typeParam U - The type of the signal's value.
74
+ * @param initial - The existing `WritableSignal`.
75
+ * @returns A `DerivedSignal` instance.
76
+ * @internal
77
+ */
78
+ function toFakeSignalDerivation(initial) {
79
+ const sig = initial;
80
+ sig.from = () => untracked(initial);
81
+ return sig;
82
+ }
83
+ /**
84
+ * Type guard function to check if a given `WritableSignal` is a `DerivedSignal`.
85
+ *
86
+ * @typeParam T - The type of the source signal's value (optional, defaults to `any`).
87
+ * @typeParam U - The type of the derived signal's value (optional, defaults to `any`).
88
+ * @param sig - The `WritableSignal` to check.
89
+ * @returns `true` if the signal is a `DerivedSignal`, `false` otherwise.
90
+ */
91
+ function isDerivation(sig) {
92
+ return 'from' in sig;
93
+ }
2
94
 
3
95
  const { is } = Object;
4
96
  function mutable(initial, opt) {
@@ -51,45 +143,9 @@ function isMutable(value) {
51
143
  return 'mutate' in value && typeof value.mutate === 'function';
52
144
  }
53
145
 
54
- /**
55
- * Converts a read-only `Signal` into a `WritableSignal` by providing custom `set` and, optionally, `update` functions.
56
- * This can be useful for creating controlled write access to a signal that is otherwise read-only.
57
- *
58
- * @typeParam T - The type of value held by the signal.
59
- *
60
- * @param signal - The read-only `Signal` to be made writable.
61
- * @param set - A function that will be used to set the signal's value. This function *must* handle
62
- * the actual update mechanism (e.g., updating a backing store, emitting an event, etc.).
63
- * @param update - (Optional) A function that will be used to update the signal's value based on its
64
- * previous value. If not provided, a default `update` implementation is used that
65
- * calls the provided `set` function with the result of the updater function. The
66
- * default implementation uses `untracked` to avoid creating unnecessary dependencies
67
- * within the updater function.
68
- *
69
- * @returns A `WritableSignal` that uses the provided `set` and `update` functions. The `asReadonly`
70
- * method of the returned signal will still return the original read-only signal.
71
- *
72
- * @example
73
- * // Basic usage: Making a read-only signal writable with a custom set function.
74
- * const originalValue = signal({a: 0});
75
- * const readOnlySignal = computed(() => originalValue().a);
76
- * const writableSignal = toWritable(readOnlySignal, (newValue) => {
77
- * originalValue.update((prev) => { ...prev, a: newValue });
78
- * });
79
- *
80
- * writableSignal.set(5); // sets value of originalValue.a to 5 & triggers all signals
81
- */
82
- function toWritable(signal, set, update) {
83
- const internal = signal;
84
- internal.asReadonly = () => signal;
85
- internal.set = set;
86
- internal.update = update ?? ((updater) => set(updater(untracked(internal))));
87
- return internal;
88
- }
89
-
90
146
  /**
91
147
  * Generated bundle index. Do not edit.
92
148
  */
93
149
 
94
- export { isMutable, mutable, toWritable };
150
+ export { derived, isDerivation, isMutable, mutable, toFakeDerivation, toFakeSignalDerivation, toWritable };
95
151
  //# sourceMappingURL=mmstack-primitives.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"mmstack-primitives.mjs","sources":["../../../../packages/primitives/src/lib/mutable.ts","../../../../packages/primitives/src/lib/to-writable.ts","../../../../packages/primitives/src/mmstack-primitives.ts"],"sourcesContent":["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","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","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;AAOA,MAAM,EAAE,EAAE,EAAE,GAAG,MAAM;AAkEL,SAAA,OAAO,CACrB,OAAW,EACX,GAA4B,EAAA;AAE5B,IAAA,MAAM,SAAS,GAAG,GAAG,EAAE,KAAK,IAAI,EAAE;IAClC,IAAI,OAAO,GAAG,KAAK;AAEnB,IAAA,MAAM,KAAK,GAAmC,CAAC,CAAC,EAAE,CAAC,KAAI;AACrD,QAAA,IAAI,OAA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to-writable.ts","../../../../packages/primitives/src/lib/derived.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 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 from =\n typeof optOrKey === 'object' ? optOrKey.from : (v: T) => v[optOrKey] as U;\n const onChange =\n typeof optOrKey === 'object'\n ? optOrKey.onChange\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 {\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;;SCiEgB,OAAO,CACrB,MAAyB,EACzB,QAA8C,EAC9C,GAA4B,EAAA;IAE5B,MAAM,IAAI,GACR,OAAO,QAAQ,KAAK,QAAQ,GAAG,QAAQ,CAAC,IAAI,GAAG,CAAC,CAAI,KAAK,CAAC,CAAC,QAAQ,CA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,GAAmC,CAAC,CAAC,EAAE,CAAC,KAAI;AACrD,QAAA,IAAI,OAAO;AAAE,YAAA,OAAO,KAAK;AACzB,QAAA,OAAO,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC;AACxB,KAAC;AAED,IAAA,MAAM,GAAG,GAAG,MAAM,CAAgB,OAAO,EAAE;AACzC,QAAA,GAAG,GAAG;QACN,KAAK;AACN,KAAA,CAAqB;AAEtB,IAAA,MAAM,cAAc,GAAG,GAAG,CAAC,MAAM;AAEjC,IAAA,GAAG,CAAC,MAAM,GAAG,CAAC,OAAO,KAAI;QACvB,OAAO,GAAG,IAAI;QACd,cAAc,CAAC,OAAO,CAAC;QACvB,OAAO,GAAG,KAAK;AACjB,KAAC;AAED,IAAA,GAAG,CAAC,MAAM,GAAG,CAAC,OAAO,KAAI;AACvB,QAAA,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,KAAI;YAClB,OAAO,CAAC,IAAI,CAAC;AACb,YAAA,OAAO,IAAI;AACb,SAAC,CAAC;AACJ,KAAC;AAED,IAAA,OAAO,GAAG;AACZ;AAEA;;;;;;;;;;;;;;;;;;;AAmBG;AACG,SAAU,SAAS,CACvB,KAAwB,EAAA;IAExB,OAAO,QAAQ,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,MAAM,KAAK,UAAU;AAChE;;ACpIA;;AAEG;;;;"}
package/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
+ export * from './lib/derived';
1
2
  export * from './lib/mutable';
2
3
  export * from './lib/to-writable';
@@ -0,0 +1,116 @@
1
+ import { CreateSignalOptions, type WritableSignal } from '@angular/core';
2
+ import type { UnknownObject } from '@mmstack/object';
3
+ /**
4
+ * Options for creating a derived signal using the full `derived` function signature.
5
+ * @typeParam T - The type of the source signal's value (parent).
6
+ * @typeParam U - The type of the derived signal's value (child).
7
+ */
8
+ type CreateDerivedOptions<T, U> = CreateSignalOptions<U> & {
9
+ /**
10
+ * A function that extracts the derived value (`U`) from the source signal's value (`T`).
11
+ */
12
+ from: (v: T) => U;
13
+ /**
14
+ * A function that updates the source signal's value (`T`) when the derived signal's value (`U`) changes.
15
+ * This establishes the two-way binding.
16
+ */
17
+ onChange: (newValue: U) => void;
18
+ };
19
+ /**
20
+ * A `WritableSignal` that derives its value from another `WritableSignal` (the "source" signal).
21
+ * It provides two-way binding: changes to the source signal update the derived signal, and
22
+ * changes to the derived signal update the source signal.
23
+ *
24
+ * @typeParam T - The type of the source signal's value (parent).
25
+ * @typeParam U - The type of the derived signal's value (child).
26
+ */
27
+ export type DerivedSignal<T, U> = WritableSignal<U> & {
28
+ /**
29
+ * The function used to derive the derived signal's value from the source signal's value.
30
+ * This is primarily for internal use and introspection.
31
+ */
32
+ from: (v: T) => U;
33
+ };
34
+ /**
35
+ * Creates a `DerivedSignal` that derives its value from another `WritableSignal` (the source signal).
36
+ * This overload provides the most flexibility, allowing you to specify custom `from` and `onChange` functions.
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.
42
+ * @returns A `DerivedSignal` instance.
43
+ *
44
+ * @example
45
+ * const user = signal({ name: 'John', age: 30 });
46
+ * const name = derived(user, {
47
+ * from: (u) => u.name,
48
+ * onChange: (newName) => user.update((u) => ({ ...u, name: newName })),
49
+ * });
50
+ */
51
+ export declare function derived<T, U>(source: WritableSignal<T>, opt: CreateDerivedOptions<T, U>): DerivedSignal<T, U>;
52
+ /**
53
+ * 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.
55
+ *
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
+ * @returns A `DerivedSignal` instance.
62
+ *
63
+ * @example
64
+ * const user = signal({ name: 'John', age: 30 });
65
+ * const name = derived(user, 'name');
66
+ */
67
+ 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
+ /**
69
+ * Creates a `DerivedSignal` from an array, and derives an element by index.
70
+ *
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.
75
+ * @returns A `DerivedSignal` instance.
76
+ *
77
+ * @example
78
+ * const numbers = signal([1, 2, 3]);
79
+ * const secondNumber = derived(numbers, 1); // secondNumber() === 2
80
+ * secondNumber.set(5); // numbers() === [1, 5, 3]
81
+ */
82
+ export declare function derived<T extends any[]>(source: WritableSignal<T>, index: number, opt?: CreateSignalOptions<T[number]>): DerivedSignal<T, T[number]>;
83
+ /**
84
+ * Creates a "fake" `DerivedSignal` from a simple value. This is useful for creating
85
+ * `FormControlSignal` instances that are not directly derived from another signal.
86
+ * The returned signal's `from` function will always return the initial value.
87
+ *
88
+ * @typeParam T - This type parameter is not used in the implementation but is kept for type compatibility with `DerivedSignal`.
89
+ * @typeParam U - The type of the signal's value.
90
+ * @param initial - The initial value of the signal.
91
+ * @returns A `DerivedSignal` instance.
92
+ * @internal
93
+ */
94
+ export declare function toFakeDerivation<T, U>(initial: U): DerivedSignal<T, U>;
95
+ /**
96
+ * Creates a "fake" `DerivedSignal` from an existing `WritableSignal`. This is useful
97
+ * for treating a regular `WritableSignal` as a `DerivedSignal` without changing its behavior.
98
+ * The returned signal's `from` function returns the current value of signal, using `untracked`.
99
+ *
100
+ * @typeParam T - This type parameter is not used in the implementation but is kept for type compatibility with `DerivedSignal`.
101
+ * @typeParam U - The type of the signal's value.
102
+ * @param initial - The existing `WritableSignal`.
103
+ * @returns A `DerivedSignal` instance.
104
+ * @internal
105
+ */
106
+ export declare function toFakeSignalDerivation<T, U>(initial: WritableSignal<U>): DerivedSignal<T, U>;
107
+ /**
108
+ * Type guard function to check if a given `WritableSignal` is a `DerivedSignal`.
109
+ *
110
+ * @typeParam T - The type of the source signal's value (optional, defaults to `any`).
111
+ * @typeParam U - The type of the derived signal's value (optional, defaults to `any`).
112
+ * @param sig - The `WritableSignal` to check.
113
+ * @returns `true` if the signal is a `DerivedSignal`, `false` otherwise.
114
+ */
115
+ export declare function isDerivation<T, U>(sig: WritableSignal<U>): sig is DerivedSignal<T, U>;
116
+ export {};
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@mmstack/primitives",
3
- "version": "19.0.0",
3
+ "version": "19.0.1",
4
4
  "peerDependencies": {
5
- "@angular/core": "19.2.3"
5
+ "@angular/core": "~19.2.3"
6
6
  },
7
7
  "sideEffects": false,
8
8
  "module": "fesm2022/mmstack-primitives.mjs",