@cognite/signal-utils 1.0.1 → 1.48535.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.
Files changed (47) hide show
  1. package/README.md +7 -6
  2. package/dist/core/array-signal.d.ts +52 -52
  3. package/dist/core/array-signal.js +105 -34
  4. package/dist/core/array-signal.js.map +1 -0
  5. package/dist/core/batch.d.ts +21 -21
  6. package/dist/core/batch.js +26 -6
  7. package/dist/core/batch.js.map +1 -0
  8. package/dist/core/computed.d.ts +17 -17
  9. package/dist/core/computed.js +27 -10
  10. package/dist/core/computed.js.map +1 -0
  11. package/dist/core/effect.d.ts +29 -29
  12. package/dist/core/effect.js +34 -6
  13. package/dist/core/effect.js.map +1 -0
  14. package/dist/core/signal.d.ts +73 -73
  15. package/dist/core/signal.js +25 -17
  16. package/dist/core/signal.js.map +1 -0
  17. package/dist/core/untracked.d.ts +26 -26
  18. package/dist/core/untracked.js +31 -6
  19. package/dist/core/untracked.js.map +1 -0
  20. package/dist/extensions/fragmentSignal.d.ts +84 -84
  21. package/dist/extensions/fragmentSignal.js +38 -20
  22. package/dist/extensions/fragmentSignal.js.map +1 -0
  23. package/dist/extensions.d.ts +1 -1
  24. package/dist/extensions.js +3 -2
  25. package/dist/extensions.js.map +1 -0
  26. package/dist/index.d.ts +10 -10
  27. package/dist/index.js +9 -8
  28. package/dist/index.js.map +1 -0
  29. package/dist/misc/debounced-computed.d.ts +20 -20
  30. package/dist/misc/debounced-computed.js +37 -16
  31. package/dist/misc/debounced-computed.js.map +1 -0
  32. package/dist/react/use-signal-value.d.ts +7 -7
  33. package/dist/react/use-signal-value.js +11 -6
  34. package/dist/react/use-signal-value.js.map +1 -0
  35. package/dist/react.d.ts +1 -1
  36. package/dist/react.js +3 -2
  37. package/dist/react.js.map +1 -0
  38. package/dist/utils/arrays.d.ts +22 -22
  39. package/dist/utils/arrays.js +41 -13
  40. package/dist/utils/arrays.js.map +1 -0
  41. package/dist/utils/debounce.d.ts +8 -8
  42. package/dist/utils/debounce.js +30 -11
  43. package/dist/utils/debounce.js.map +1 -0
  44. package/dist/utils/repeat.d.ts +5 -5
  45. package/dist/utils/repeat.js +12 -0
  46. package/dist/utils/repeat.js.map +1 -0
  47. package/package.json +7 -3
package/README.md CHANGED
@@ -45,9 +45,9 @@ import { signal, computed } from '@cognite/signal-utils';
45
45
 
46
46
  const myNumber = signal(42);
47
47
  const double = computed(() => myNumber() * 2);
48
- double() // 84
48
+ double(); // 84
49
49
  myNumber(43);
50
- double() // 86
50
+ double(); // 86
51
51
  ```
52
52
 
53
53
  ### `effect(fn)`
@@ -57,7 +57,7 @@ import { signal, effect } from '@cognite/signal-utils';
57
57
 
58
58
  const mySignal = signal(42);
59
59
  effect(() => {
60
- console.log(mySignal());
60
+ console.log(mySignal());
61
61
  });
62
62
  // 42 is logged
63
63
  mySignal(43);
@@ -66,11 +66,12 @@ mySignal(43);
66
66
  // Cleanup
67
67
  effect(() => {
68
68
  addEventListener('click', handler);
69
- return () => { removeEventListener('click', handler); };
69
+ return () => {
70
+ removeEventListener('click', handler);
71
+ };
70
72
  });
71
73
  ```
72
74
 
73
-
74
75
  ### `batch(fn)`
75
76
 
76
77
  ```ts
@@ -106,7 +107,7 @@ function someFunction() {
106
107
  effect(() => {
107
108
  console.log(signalA());
108
109
  untracked(someFunction);
109
- })
110
+ });
110
111
  // A & B is logged
111
112
 
112
113
  signalA('A2');
@@ -1,61 +1,61 @@
1
- import { ReadonlySignal, Signal } from './signal';
1
+ import type { ReadonlySignal, Signal } from "./signal";
2
2
  declare const arraySignalSymbol: unique symbol;
3
3
  export type ReadonlyArraySignal<T> = ReadonlySignal<readonly T[]>;
4
4
  export type ArraySignal<T> = Signal<readonly T[]> & {
5
- push(...values: T[]): void;
6
- insertAt(index: number, value: T): void;
7
- remove(value: T): void;
8
- removeAt(index: number): void;
9
- swap(i: number, j: number): void;
10
- clear(): void;
11
- [arraySignalSymbol]: boolean;
5
+ push(...values: T[]): void;
6
+ insertAt(index: number, value: T): void;
7
+ remove(value: T): void;
8
+ removeAt(index: number): void;
9
+ swap(i: number, j: number): void;
10
+ clear(): void;
11
+ [arraySignalSymbol]: boolean;
12
12
  };
13
13
  /**
14
- * Creates an `ArraySignal` which is a reactive signal for an array of values.
15
- *
16
- * @template T - The type of elements in the array.
17
- * @param {readonly T[]} [value=[]] - The initial value of the array.
18
- * @returns {ArraySignal<T>} A reactive signal for the array.
19
- *
20
- * @example
21
- * ```ts
22
- * const myArraySignal = arraySignal([1, 2, 3]);
23
- *
24
- * // Getter
25
- * const currentArray = myArraySignal();
26
- *
27
- * // Setter
28
- * myArraySignal([4, 5, 6]);
29
- *
30
- * // Push new values
31
- * myArraySignal.push(7, 8);
32
- *
33
- * // Insert value at specific index
34
- * myArraySignal.insertAt(1, 9);
35
- *
36
- * // Remove specific value
37
- * myArraySignal.remove(2);
38
- *
39
- * // Remove value at specific index
40
- * myArraySignal.removeAt(0);
41
- *
42
- * // Swap values at two indices
43
- * myArraySignal.swap(0, 1);
44
- *
45
- * // Clear the array
46
- * myArraySignal.clear();
47
- *
48
- * // Peek the current value without taking a dependency
49
- * const peekedArray = myArraySignal.peek();
50
- * ```
51
- */
14
+ * Creates an `ArraySignal` which is a reactive signal for an array of values.
15
+ *
16
+ * @template T - The type of elements in the array.
17
+ * @param {readonly T[]} [value=[]] - The initial value of the array.
18
+ * @returns {ArraySignal<T>} A reactive signal for the array.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const myArraySignal = arraySignal([1, 2, 3]);
23
+ *
24
+ * // Getter
25
+ * const currentArray = myArraySignal();
26
+ *
27
+ * // Setter
28
+ * myArraySignal([4, 5, 6]);
29
+ *
30
+ * // Push new values
31
+ * myArraySignal.push(7, 8);
32
+ *
33
+ * // Insert value at specific index
34
+ * myArraySignal.insertAt(1, 9);
35
+ *
36
+ * // Remove specific value
37
+ * myArraySignal.remove(2);
38
+ *
39
+ * // Remove value at specific index
40
+ * myArraySignal.removeAt(0);
41
+ *
42
+ * // Swap values at two indices
43
+ * myArraySignal.swap(0, 1);
44
+ *
45
+ * // Clear the array
46
+ * myArraySignal.clear();
47
+ *
48
+ * // Peek the current value without taking a dependency
49
+ * const peekedArray = myArraySignal.peek();
50
+ * ```
51
+ */
52
52
  export declare function arraySignal<T>(value?: readonly T[]): ArraySignal<T>;
53
53
  /**
54
- * Checks if the given value is an ArraySignal.
55
- *
56
- * @template T - The type of elements in the array signal.
57
- * @param value - The value to check.
58
- * @returns A boolean indicating whether the value is an ArraySignal.
59
- */
54
+ * Checks if the given value is an ArraySignal.
55
+ *
56
+ * @template T - The type of elements in the array signal.
57
+ * @param value - The value to check.
58
+ * @returns A boolean indicating whether the value is an ArraySignal.
59
+ */
60
60
  export declare function isArraySignal<T>(value: unknown): value is ArraySignal<T>;
61
61
  export {};
@@ -1,36 +1,107 @@
1
- import { insertAt as e, remove as t, removeAt as n, swap as r } from "../utils/arrays.js";
2
- import { signal as i } from "./signal.js";
3
- //#region libs/shared/signal-utils/src/core/array-signal.ts
4
- var a = Symbol("ArraySignal");
5
- function o(o = []) {
6
- let s = i([...o]);
7
- function c(e) {
8
- if (arguments.length === 1) {
9
- s([...e]);
10
- return;
11
- }
12
- return s();
13
- }
14
- return c[a] = !0, c.push = (...e) => {
15
- s([...s.peek(), ...e]);
16
- }, c.insertAt = (t, n) => {
17
- let r = [...s.peek()];
18
- e(r, t, n), s(r);
19
- }, c.remove = (e) => {
20
- let n = [...s.peek()];
21
- t(n, e), s(n);
22
- }, c.removeAt = (e) => {
23
- let t = [...s.peek()];
24
- n(t, e), s(t);
25
- }, c.swap = (e, t) => {
26
- let n = [...s.peek()];
27
- r(n, e, t), s(n);
28
- }, c.clear = () => {
29
- s([]);
30
- }, c.peek = () => s.peek(), c.subscribe = (e) => s.subscribe(e), c;
1
+ import { insertAt, remove, removeAt, swap } from "../utils/arrays.js";
2
+ import { signal } from "./signal.js";
3
+ const arraySignalSymbol = Symbol('ArraySignal');
4
+ /**
5
+ * Creates an `ArraySignal` which is a reactive signal for an array of values.
6
+ *
7
+ * @template T - The type of elements in the array.
8
+ * @param {readonly T[]} [value=[]] - The initial value of the array.
9
+ * @returns {ArraySignal<T>} A reactive signal for the array.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const myArraySignal = arraySignal([1, 2, 3]);
14
+ *
15
+ * // Getter
16
+ * const currentArray = myArraySignal();
17
+ *
18
+ * // Setter
19
+ * myArraySignal([4, 5, 6]);
20
+ *
21
+ * // Push new values
22
+ * myArraySignal.push(7, 8);
23
+ *
24
+ * // Insert value at specific index
25
+ * myArraySignal.insertAt(1, 9);
26
+ *
27
+ * // Remove specific value
28
+ * myArraySignal.remove(2);
29
+ *
30
+ * // Remove value at specific index
31
+ * myArraySignal.removeAt(0);
32
+ *
33
+ * // Swap values at two indices
34
+ * myArraySignal.swap(0, 1);
35
+ *
36
+ * // Clear the array
37
+ * myArraySignal.clear();
38
+ *
39
+ * // Peek the current value without taking a dependency
40
+ * const peekedArray = myArraySignal.peek();
41
+ * ```
42
+ */ export function arraySignal(value = []) {
43
+ const innerSignal = signal([
44
+ ...value
45
+ ]);
46
+ function fn(newValue) {
47
+ if (arguments.length === 1) {
48
+ innerSignal([
49
+ ...newValue
50
+ ]);
51
+ return;
52
+ }
53
+ return innerSignal();
54
+ }
55
+ fn[arraySignalSymbol] = true;
56
+ fn.push = (...values)=>{
57
+ innerSignal([
58
+ ...innerSignal.peek(),
59
+ ...values
60
+ ]);
61
+ };
62
+ fn.insertAt = (index, value)=>{
63
+ const values = [
64
+ ...innerSignal.peek()
65
+ ];
66
+ insertAt(values, index, value);
67
+ innerSignal(values);
68
+ };
69
+ fn.remove = (value)=>{
70
+ const values = [
71
+ ...innerSignal.peek()
72
+ ];
73
+ remove(values, value);
74
+ innerSignal(values);
75
+ };
76
+ fn.removeAt = (index)=>{
77
+ const values = [
78
+ ...innerSignal.peek()
79
+ ];
80
+ removeAt(values, index);
81
+ innerSignal(values);
82
+ };
83
+ fn.swap = (i, j)=>{
84
+ const values = [
85
+ ...innerSignal.peek()
86
+ ];
87
+ swap(values, i, j);
88
+ innerSignal(values);
89
+ };
90
+ fn.clear = ()=>{
91
+ innerSignal([]);
92
+ };
93
+ fn.peek = ()=>innerSignal.peek();
94
+ fn.subscribe = (fn)=>innerSignal.subscribe(fn);
95
+ return fn;
31
96
  }
32
- function s(e) {
33
- return typeof e == "function" && a in e;
97
+ /**
98
+ * Checks if the given value is an ArraySignal.
99
+ *
100
+ * @template T - The type of elements in the array signal.
101
+ * @param value - The value to check.
102
+ * @returns A boolean indicating whether the value is an ArraySignal.
103
+ */ export function isArraySignal(value) {
104
+ return typeof value === 'function' && arraySignalSymbol in value;
34
105
  }
35
- //#endregion
36
- export { o as arraySignal, s as isArraySignal };
106
+
107
+ //# sourceMappingURL=array-signal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/core/array-signal.ts"],"sourcesContent":["import { insertAt, remove, removeAt, swap } from '../utils/arrays';\n\nimport type { ReadonlySignal, Signal, Subscriber, Unsubscribe } from './signal';\nimport { signal } from './signal';\n\nconst arraySignalSymbol: unique symbol = Symbol('ArraySignal');\n\nexport type ReadonlyArraySignal<T> = ReadonlySignal<readonly T[]>;\n\nexport type ArraySignal<T> = Signal<readonly T[]> & {\n push(...values: T[]): void;\n insertAt(index: number, value: T): void;\n remove(value: T): void;\n removeAt(index: number): void;\n swap(i: number, j: number): void;\n clear(): void;\n [arraySignalSymbol]: boolean;\n};\n\n/**\n * Creates an `ArraySignal` which is a reactive signal for an array of values.\n *\n * @template T - The type of elements in the array.\n * @param {readonly T[]} [value=[]] - The initial value of the array.\n * @returns {ArraySignal<T>} A reactive signal for the array.\n *\n * @example\n * ```ts\n * const myArraySignal = arraySignal([1, 2, 3]);\n *\n * // Getter\n * const currentArray = myArraySignal();\n *\n * // Setter\n * myArraySignal([4, 5, 6]);\n *\n * // Push new values\n * myArraySignal.push(7, 8);\n *\n * // Insert value at specific index\n * myArraySignal.insertAt(1, 9);\n *\n * // Remove specific value\n * myArraySignal.remove(2);\n *\n * // Remove value at specific index\n * myArraySignal.removeAt(0);\n *\n * // Swap values at two indices\n * myArraySignal.swap(0, 1);\n *\n * // Clear the array\n * myArraySignal.clear();\n *\n * // Peek the current value without taking a dependency\n * const peekedArray = myArraySignal.peek();\n * ```\n */\nexport function arraySignal<T>(value: readonly T[] = []): ArraySignal<T> {\n const innerSignal = signal([...value] as readonly T[]);\n\n function fn(): readonly T[]; // Getter\n function fn(newValue: readonly T[]): void; // Setter\n function fn(newValue?: readonly T[]): readonly T[] | void {\n if (arguments.length === 1) {\n innerSignal([...(newValue as [])]);\n return;\n }\n return innerSignal();\n }\n\n fn[arraySignalSymbol] = true;\n\n fn.push = (...values: T[]): void => {\n innerSignal([...innerSignal.peek(), ...values]);\n };\n\n fn.insertAt = (index: number, value: T): void => {\n const values = [...innerSignal.peek()];\n insertAt(values, index, value);\n innerSignal(values);\n };\n\n fn.remove = (value: T): void => {\n const values = [...innerSignal.peek()];\n remove(values, value);\n innerSignal(values);\n };\n\n fn.removeAt = (index: number): void => {\n const values = [...innerSignal.peek()];\n removeAt(values, index);\n innerSignal(values);\n };\n\n fn.swap = (i: number, j: number): void => {\n const values = [...innerSignal.peek()];\n swap(values, i, j);\n innerSignal(values);\n };\n\n fn.clear = (): void => {\n innerSignal([]);\n };\n\n fn.peek = (): readonly T[] => innerSignal.peek();\n fn.subscribe = (fn: Subscriber<readonly T[]>): Unsubscribe =>\n innerSignal.subscribe(fn);\n\n return fn;\n}\n\n/**\n * Checks if the given value is an ArraySignal.\n *\n * @template T - The type of elements in the array signal.\n * @param value - The value to check.\n * @returns A boolean indicating whether the value is an ArraySignal.\n */\nexport function isArraySignal<T>(value: unknown): value is ArraySignal<T> {\n return typeof value === 'function' && arraySignalSymbol in value;\n}\n"],"names":["insertAt","remove","removeAt","swap","signal","arraySignalSymbol","Symbol","arraySignal","value","innerSignal","fn","newValue","arguments","length","push","values","peek","index","i","j","clear","subscribe","isArraySignal"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,MAAM,EAAEC,QAAQ,EAAEC,IAAI,QAAQ,qBAAkB;AAGnE,SAASC,MAAM,QAAQ,cAAW;AAElC,MAAMC,oBAAmCC,OAAO;AAchD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsCC,GACD,OAAO,SAASC,YAAeC,QAAsB,EAAE;IACrD,MAAMC,cAAcL,OAAO;WAAII;KAAM;IAIrC,SAASE,GAAGC,QAAuB;QACjC,IAAIC,UAAUC,MAAM,KAAK,GAAG;YAC1BJ,YAAY;mBAAKE;aAAgB;YACjC;QACF;QACA,OAAOF;IACT;IAEAC,EAAE,CAACL,kBAAkB,GAAG;IAExBK,GAAGI,IAAI,GAAG,CAAC,GAAGC;QACZN,YAAY;eAAIA,YAAYO,IAAI;eAAOD;SAAO;IAChD;IAEAL,GAAGV,QAAQ,GAAG,CAACiB,OAAeT;QAC5B,MAAMO,SAAS;eAAIN,YAAYO,IAAI;SAAG;QACtChB,SAASe,QAAQE,OAAOT;QACxBC,YAAYM;IACd;IAEAL,GAAGT,MAAM,GAAG,CAACO;QACX,MAAMO,SAAS;eAAIN,YAAYO,IAAI;SAAG;QACtCf,OAAOc,QAAQP;QACfC,YAAYM;IACd;IAEAL,GAAGR,QAAQ,GAAG,CAACe;QACb,MAAMF,SAAS;eAAIN,YAAYO,IAAI;SAAG;QACtCd,SAASa,QAAQE;QACjBR,YAAYM;IACd;IAEAL,GAAGP,IAAI,GAAG,CAACe,GAAWC;QACpB,MAAMJ,SAAS;eAAIN,YAAYO,IAAI;SAAG;QACtCb,KAAKY,QAAQG,GAAGC;QAChBV,YAAYM;IACd;IAEAL,GAAGU,KAAK,GAAG;QACTX,YAAY,EAAE;IAChB;IAEAC,GAAGM,IAAI,GAAG,IAAoBP,YAAYO,IAAI;IAC9CN,GAAGW,SAAS,GAAG,CAACX,KACdD,YAAYY,SAAS,CAACX;IAExB,OAAOA;AACT;AAEA;;;;;;CAMC,GACD,OAAO,SAASY,cAAiBd,KAAc;IAC7C,OAAO,OAAOA,UAAU,cAAcH,qBAAqBG;AAC7D"}
@@ -1,23 +1,23 @@
1
1
  /**
2
- * Executes a function within a batch, ensuring that signal updates are batched together (atomic operation).
3
- *
4
- * @param fn - The function to be executed within the batch.
5
- *
6
- * @example
7
- * ```ts
8
- * const givenName = signal('Jane');
9
- * const familyName = signal('Doe');
10
- * const fullName = computed(() => givenName() + ' ' + familyName());
11
- *
12
- * effect(() => console.log(fullName()));
13
- * // "Jane Doe" logged
14
- *
15
- * // Combines both signal writes into one atomic update.
16
- * batch(() => {
17
- * givenName('Foo');
18
- * familyName('Bar');
19
- * });
20
- * // "Foo Bar" logged
21
- * ```
22
- */
2
+ * Executes a function within a batch, ensuring that signal updates are batched together (atomic operation).
3
+ *
4
+ * @param fn - The function to be executed within the batch.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const givenName = signal('Jane');
9
+ * const familyName = signal('Doe');
10
+ * const fullName = computed(() => givenName() + ' ' + familyName());
11
+ *
12
+ * effect(() => console.log(fullName()));
13
+ * // "Jane Doe" logged
14
+ *
15
+ * // Combines both signal writes into one atomic update.
16
+ * batch(() => {
17
+ * givenName('Foo');
18
+ * familyName('Bar');
19
+ * });
20
+ * // "Foo Bar" logged
21
+ * ```
22
+ */
23
23
  export declare function batch(fn: VoidFunction): void;
@@ -1,7 +1,27 @@
1
- import { batch as e } from "@preact/signals-core";
2
- //#region libs/shared/signal-utils/src/core/batch.ts
3
- function t(t) {
4
- e(t);
1
+ import { batch as preactBatch } from "@preact/signals-core";
2
+ /**
3
+ * Executes a function within a batch, ensuring that signal updates are batched together (atomic operation).
4
+ *
5
+ * @param fn - The function to be executed within the batch.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const givenName = signal('Jane');
10
+ * const familyName = signal('Doe');
11
+ * const fullName = computed(() => givenName() + ' ' + familyName());
12
+ *
13
+ * effect(() => console.log(fullName()));
14
+ * // "Jane Doe" logged
15
+ *
16
+ * // Combines both signal writes into one atomic update.
17
+ * batch(() => {
18
+ * givenName('Foo');
19
+ * familyName('Bar');
20
+ * });
21
+ * // "Foo Bar" logged
22
+ * ```
23
+ */ export function batch(fn) {
24
+ preactBatch(fn);
5
25
  }
6
- //#endregion
7
- export { t as batch };
26
+
27
+ //# sourceMappingURL=batch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/core/batch.ts"],"sourcesContent":["import { batch as preactBatch } from '@preact/signals-core';\n\n/**\n * Executes a function within a batch, ensuring that signal updates are batched together (atomic operation).\n *\n * @param fn - The function to be executed within the batch.\n *\n * @example\n * ```ts\n * const givenName = signal('Jane');\n * const familyName = signal('Doe');\n * const fullName = computed(() => givenName() + ' ' + familyName());\n *\n * effect(() => console.log(fullName()));\n * // \"Jane Doe\" logged\n *\n * // Combines both signal writes into one atomic update.\n * batch(() => {\n * givenName('Foo');\n * familyName('Bar');\n * });\n * // \"Foo Bar\" logged\n * ```\n */\nexport function batch(fn: VoidFunction): void {\n preactBatch(fn);\n}\n"],"names":["batch","preactBatch","fn"],"mappings":"AAAA,SAASA,SAASC,WAAW,QAAQ,uBAAuB;AAE5D;;;;;;;;;;;;;;;;;;;;;CAqBC,GACD,OAAO,SAASD,MAAME,EAAgB;IACpCD,YAAYC;AACd"}
@@ -1,19 +1,19 @@
1
- import { ReadonlySignal } from './signal';
1
+ import type { ReadonlySignal } from "./signal";
2
2
  /**
3
- * Creates a computed signal that derives its value from the provided function.
4
- *
5
- * @template T - The type of the value returned by the computed function.
6
- * @param {() => T} fn - A function that returns the value for the computed signal.
7
- * @returns {ReadonlySignal<T>} A readonly signal that provides the computed value.
8
- *
9
- * @example
10
- * ```ts
11
- * const myNumber = signal(42);
12
- * const double = computed(() => myNumber() * 2);
13
- * double() // 84
14
- *
15
- * myNumber(43);
16
- * double() // 86
17
- * ```
18
- */
3
+ * Creates a computed signal that derives its value from the provided function.
4
+ *
5
+ * @template T - The type of the value returned by the computed function.
6
+ * @param {() => T} fn - A function that returns the value for the computed signal.
7
+ * @returns {ReadonlySignal<T>} A readonly signal that provides the computed value.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const myNumber = signal(42);
12
+ * const double = computed(() => myNumber() * 2);
13
+ * double() // 84
14
+ *
15
+ * myNumber(43);
16
+ * double() // 86
17
+ * ```
18
+ */
19
19
  export declare function computed<T>(fn: () => T): ReadonlySignal<T>;
@@ -1,11 +1,28 @@
1
- import { computed as e } from "@preact/signals-core";
2
- //#region libs/shared/signal-utils/src/core/computed.ts
3
- function t(t) {
4
- let n = e(t);
5
- function r() {
6
- return n.value;
7
- }
8
- return r.peek = () => n.peek(), r.subscribe = (e) => n.subscribe(e), r;
1
+ import { computed as preactComputed } from "@preact/signals-core";
2
+ /**
3
+ * Creates a computed signal that derives its value from the provided function.
4
+ *
5
+ * @template T - The type of the value returned by the computed function.
6
+ * @param {() => T} fn - A function that returns the value for the computed signal.
7
+ * @returns {ReadonlySignal<T>} A readonly signal that provides the computed value.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const myNumber = signal(42);
12
+ * const double = computed(() => myNumber() * 2);
13
+ * double() // 84
14
+ *
15
+ * myNumber(43);
16
+ * double() // 86
17
+ * ```
18
+ */ export function computed(fn) {
19
+ const innerComputed = preactComputed(fn);
20
+ function comp() {
21
+ return innerComputed.value;
22
+ }
23
+ comp.peek = ()=>innerComputed.peek();
24
+ comp.subscribe = (fn)=>innerComputed.subscribe(fn);
25
+ return comp;
9
26
  }
10
- //#endregion
11
- export { t as computed };
27
+
28
+ //# sourceMappingURL=computed.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/core/computed.ts"],"sourcesContent":["import { computed as preactComputed } from '@preact/signals-core';\n\nimport type { ReadonlySignal, Subscriber, Unsubscribe } from './signal';\n\n/**\n * Creates a computed signal that derives its value from the provided function.\n *\n * @template T - The type of the value returned by the computed function.\n * @param {() => T} fn - A function that returns the value for the computed signal.\n * @returns {ReadonlySignal<T>} A readonly signal that provides the computed value.\n *\n * @example\n * ```ts\n * const myNumber = signal(42);\n * const double = computed(() => myNumber() * 2);\n * double() // 84\n *\n * myNumber(43);\n * double() // 86\n * ```\n */\nexport function computed<T>(fn: () => T): ReadonlySignal<T> {\n const innerComputed = preactComputed(fn);\n function comp(): T {\n return innerComputed.value;\n }\n comp.peek = (): T => innerComputed.peek();\n comp.subscribe = (fn: Subscriber<T>): Unsubscribe =>\n innerComputed.subscribe(fn);\n return comp;\n}\n"],"names":["computed","preactComputed","fn","innerComputed","comp","value","peek","subscribe"],"mappings":"AAAA,SAASA,YAAYC,cAAc,QAAQ,uBAAuB;AAIlE;;;;;;;;;;;;;;;;CAgBC,GACD,OAAO,SAASD,SAAYE,EAAW;IACrC,MAAMC,gBAAgBF,eAAeC;IACrC,SAASE;QACP,OAAOD,cAAcE,KAAK;IAC5B;IACAD,KAAKE,IAAI,GAAG,IAASH,cAAcG,IAAI;IACvCF,KAAKG,SAAS,GAAG,CAACL,KAChBC,cAAcI,SAAS,CAACL;IAC1B,OAAOE;AACT"}
@@ -1,33 +1,33 @@
1
1
  /**
2
- * Calls the provided function, first initially, and when a signal dependency changes.
3
- *
4
- * The provided function can optionally return a cleanup function that will run only once.
5
- * It will run when the effect is disposed or right before the next invocation of provided function.
6
- *
7
- * @param fn
8
- * @returns A method to dispose the effect.
9
- *
10
- * @example
11
- * ```ts
12
- * const mySignal = signal(42);
13
- * effect(() => {
14
- * console.log(mySignal());
15
- * });
16
- * // 42 is logged
17
- *
18
- * mySignal(43);
19
- * // 43 is logged
20
- * ```
21
- *
22
- * @example
23
- * ```ts
24
- * // Cleanup
25
- * effect(() => {
26
- * addEventListener('click', handler);
27
- * return () => { removeEventListener('click', handler); };
28
- * });
29
- * ```
30
- */
2
+ * Calls the provided function, first initially, and when a signal dependency changes.
3
+ *
4
+ * The provided function can optionally return a cleanup function that will run only once.
5
+ * It will run when the effect is disposed or right before the next invocation of provided function.
6
+ *
7
+ * @param fn
8
+ * @returns A method to dispose the effect.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const mySignal = signal(42);
13
+ * effect(() => {
14
+ * console.log(mySignal());
15
+ * });
16
+ * // 42 is logged
17
+ *
18
+ * mySignal(43);
19
+ * // 43 is logged
20
+ * ```
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * // Cleanup
25
+ * effect(() => {
26
+ * addEventListener('click', handler);
27
+ * return () => { removeEventListener('click', handler); };
28
+ * });
29
+ * ```
30
+ */
31
31
  export declare function effect(fn: EffectFn): Dispose;
32
32
  type Dispose = VoidFunction;
33
33
  type EffectFn = () => void | Dispose;
@@ -1,7 +1,35 @@
1
- import { effect as e } from "@preact/signals-core";
2
- //#region libs/shared/signal-utils/src/core/effect.ts
3
- function t(t) {
4
- return e(t);
1
+ import { effect as preactEffect } from "@preact/signals-core";
2
+ /**
3
+ * Calls the provided function, first initially, and when a signal dependency changes.
4
+ *
5
+ * The provided function can optionally return a cleanup function that will run only once.
6
+ * It will run when the effect is disposed or right before the next invocation of provided function.
7
+ *
8
+ * @param fn
9
+ * @returns A method to dispose the effect.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const mySignal = signal(42);
14
+ * effect(() => {
15
+ * console.log(mySignal());
16
+ * });
17
+ * // 42 is logged
18
+ *
19
+ * mySignal(43);
20
+ * // 43 is logged
21
+ * ```
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * // Cleanup
26
+ * effect(() => {
27
+ * addEventListener('click', handler);
28
+ * return () => { removeEventListener('click', handler); };
29
+ * });
30
+ * ```
31
+ */ export function effect(fn) {
32
+ return preactEffect(fn);
5
33
  }
6
- //#endregion
7
- export { t as effect };
34
+
35
+ //# sourceMappingURL=effect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/core/effect.ts"],"sourcesContent":["import { effect as preactEffect } from '@preact/signals-core';\n\n/**\n * Calls the provided function, first initially, and when a signal dependency changes.\n *\n * The provided function can optionally return a cleanup function that will run only once.\n * It will run when the effect is disposed or right before the next invocation of provided function.\n *\n * @param fn\n * @returns A method to dispose the effect.\n *\n * @example\n * ```ts\n * const mySignal = signal(42);\n * effect(() => {\n * console.log(mySignal());\n * });\n * // 42 is logged\n *\n * mySignal(43);\n * // 43 is logged\n * ```\n *\n * @example\n * ```ts\n * // Cleanup\n * effect(() => {\n * addEventListener('click', handler);\n * return () => { removeEventListener('click', handler); };\n * });\n * ```\n */\nexport function effect(fn: EffectFn): Dispose {\n return preactEffect(fn);\n}\n\ntype Dispose = VoidFunction;\ntype EffectFn = () => void | Dispose;\n"],"names":["effect","preactEffect","fn"],"mappings":"AAAA,SAASA,UAAUC,YAAY,QAAQ,uBAAuB;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6BC,GACD,OAAO,SAASD,OAAOE,EAAY;IACjC,OAAOD,aAAaC;AACtB"}