@open-pioneer/reactivity 4.5.0-dev.20260226113620 → 4.5.0-dev.20260311110502
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/CHANGELOG.md +4 -1
- package/index.d.ts +40 -3
- package/index.js +10 -7
- package/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
# @open-pioneer/reactivity
|
|
2
2
|
|
|
3
|
-
## 4.5.0-dev.
|
|
3
|
+
## 4.5.0-dev.20260311110502
|
|
4
4
|
|
|
5
5
|
### Minor Changes
|
|
6
6
|
|
|
7
7
|
- 4e76893: Update Chakra UI to 3.32.0
|
|
8
|
+
- 19b26dc: Allow configuration of dispatch timing in `useReactiveSnapshot` and `useReactive`.
|
|
9
|
+
|
|
10
|
+
This is intended as a fix for https://github.com/open-pioneer/trails-core-packages/issues/125.
|
|
8
11
|
|
|
9
12
|
## 4.4.0
|
|
10
13
|
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DispatchType, Reactive, ReadonlyReactive } from "@conterra/reactivity-core";
|
|
2
2
|
import { DependencyList } from "react";
|
|
3
3
|
/**
|
|
4
4
|
* Creates a new signal with the initial value `undefined`.
|
|
@@ -59,14 +59,30 @@ export declare function useReactive<T>(initialValue: T): Reactive<T>;
|
|
|
59
59
|
* @see {@link useReactiveSnapshot()}
|
|
60
60
|
*/
|
|
61
61
|
export declare function useComputed<T>(compute: () => T, deps: DependencyList): ReadonlyReactive<T>;
|
|
62
|
+
/**
|
|
63
|
+
* Advanced options for `useReactiveSnapshot`.
|
|
64
|
+
*
|
|
65
|
+
* Note that objects should either be memoized (`useMemo` etc.) or constants (see {@link DISPATCH_SYNC})).
|
|
66
|
+
*/
|
|
67
|
+
export interface ReactiveHookOptions {
|
|
68
|
+
dispatch?: DispatchType;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Switches from async dispatch (the default) to sync dispatch.
|
|
72
|
+
*
|
|
73
|
+
* This is helpful when using reactive values for input fields, focus management or layout behavior.
|
|
74
|
+
*/
|
|
75
|
+
export declare const DISPATCH_SYNC: ReactiveHookOptions;
|
|
62
76
|
/**
|
|
63
77
|
* Watches the given reactive object and returns its current value.
|
|
64
78
|
*
|
|
65
79
|
* > NOTE: The return value of this hook should be considered read-only.
|
|
66
80
|
*
|
|
81
|
+
* > NOTE: The `options` parameter (if used) should be memoized or constant.
|
|
82
|
+
*
|
|
67
83
|
* @see {@link useComputed()}
|
|
68
84
|
*/
|
|
69
|
-
export declare function useReactiveValue<T>(reactive: ReadonlyReactive<T
|
|
85
|
+
export declare function useReactiveValue<T>(reactive: ReadonlyReactive<T>, options?: ReactiveHookOptions): T;
|
|
70
86
|
/**
|
|
71
87
|
* Evaluates the `compute` function and returns its result.
|
|
72
88
|
*
|
|
@@ -76,11 +92,15 @@ export declare function useReactiveValue<T>(reactive: ReadonlyReactive<T>): T;
|
|
|
76
92
|
*
|
|
77
93
|
* > NOTE: The return value of this hook should be considered read-only.
|
|
78
94
|
*
|
|
95
|
+
* > NOTE: The `options` parameter (if used) should be memoized or constant.
|
|
96
|
+
*
|
|
79
97
|
* @example
|
|
80
98
|
*
|
|
81
99
|
* Simple Example:
|
|
82
100
|
*
|
|
83
101
|
* ```jsx
|
|
102
|
+
* import { useReactiveSnapshot } from "@open-pioneer/reactivity";
|
|
103
|
+
*
|
|
84
104
|
* // A reactive signal from anywhere (global data, props, ...).
|
|
85
105
|
* const name = reactive("User");
|
|
86
106
|
*
|
|
@@ -95,6 +115,8 @@ export declare function useReactiveValue<T>(reactive: ReadonlyReactive<T>): T;
|
|
|
95
115
|
* Multiple values, with props:
|
|
96
116
|
*
|
|
97
117
|
* ```jsx
|
|
118
|
+
* import { useReactiveSnapshot } from "@open-pioneer/reactivity";
|
|
119
|
+
*
|
|
98
120
|
* function YourComponent({firstName, lastName}) {
|
|
99
121
|
* // You can use complex expressions in your compute function,
|
|
100
122
|
* // just like in a computed signal.
|
|
@@ -111,7 +133,22 @@ export declare function useReactiveValue<T>(reactive: ReadonlyReactive<T>): T;
|
|
|
111
133
|
* }
|
|
112
134
|
* ```
|
|
113
135
|
*
|
|
136
|
+
* @example
|
|
137
|
+
*
|
|
138
|
+
* Changing the default timing:
|
|
139
|
+
*
|
|
140
|
+
* ```jsx
|
|
141
|
+
* import { useReactiveSnapshot, DISPATCH_SYNC } from "@open-pioneer/reactivity";
|
|
142
|
+
*
|
|
143
|
+
* function YourComponent(props) {
|
|
144
|
+
* // DISPATCH_SYNC rerenders us faster than the default timing.
|
|
145
|
+
* // This is useful for values used in input fields, in layout operations or focus management.
|
|
146
|
+
* // See also https://github.com/open-pioneer/trails-core-packages/issues/125
|
|
147
|
+
* const nameSnapshot = useReactiveSnapshot(() => props.reactiveModel.value, [props.reactiveModel], DISPATCH_SYNC);
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
114
151
|
* @see
|
|
115
152
|
* This hook is based on {@link useComputed()} and {@link useReactiveValue()}.
|
|
116
153
|
*/
|
|
117
|
-
export declare function useReactiveSnapshot<T>(compute: () => T, deps: DependencyList): T;
|
|
154
|
+
export declare function useReactiveSnapshot<T>(compute: () => T, deps: DependencyList, options?: ReactiveHookOptions): T;
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { reactive, computed,
|
|
1
|
+
import { reactive, computed, watchValue } from '@conterra/reactivity-core';
|
|
2
2
|
import { useMemo, useRef, useCallback, useSyncExternalStore, useDebugValue } from 'react';
|
|
3
3
|
|
|
4
4
|
function useReactive(initialValue) {
|
|
@@ -12,24 +12,27 @@ function useComputed(compute, deps) {
|
|
|
12
12
|
return computed(() => computeRef.current());
|
|
13
13
|
}, [computedDeps]);
|
|
14
14
|
}
|
|
15
|
-
|
|
15
|
+
const DISPATCH_SYNC = Object.freeze({ dispatch: "sync" });
|
|
16
|
+
function useReactiveValue(reactive2, options) {
|
|
16
17
|
const getSnapshot = useCallback(() => {
|
|
17
18
|
return reactive2.peek();
|
|
18
19
|
}, [reactive2]);
|
|
19
20
|
const subscribe = useCallback(
|
|
20
21
|
(cb) => {
|
|
21
|
-
const handle =
|
|
22
|
+
const handle = watchValue(() => reactive2.value, cb, {
|
|
23
|
+
dispatch: options?.dispatch ?? "async"
|
|
24
|
+
});
|
|
22
25
|
return () => handle.destroy();
|
|
23
26
|
},
|
|
24
|
-
[reactive2]
|
|
27
|
+
[reactive2, options?.dispatch]
|
|
25
28
|
);
|
|
26
29
|
const value = useSyncExternalStore(subscribe, getSnapshot);
|
|
27
30
|
useDebugValue(value);
|
|
28
31
|
return value;
|
|
29
32
|
}
|
|
30
|
-
function useReactiveSnapshot(compute, deps) {
|
|
33
|
+
function useReactiveSnapshot(compute, deps, options) {
|
|
31
34
|
const computedSnapshot = useComputed(compute, deps);
|
|
32
|
-
const snapshot = useReactiveValue(computedSnapshot);
|
|
35
|
+
const snapshot = useReactiveValue(computedSnapshot, options);
|
|
33
36
|
useDebugValue(snapshot);
|
|
34
37
|
return snapshot;
|
|
35
38
|
}
|
|
@@ -47,5 +50,5 @@ function shallowEqual(a, b) {
|
|
|
47
50
|
return a.length === (b?.length ?? 0) && a.every((v, i) => v === b[i]);
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
export { useComputed, useReactive, useReactiveSnapshot, useReactiveValue };
|
|
53
|
+
export { DISPATCH_SYNC, useComputed, useReactive, useReactiveSnapshot, useReactiveValue };
|
|
51
54
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport { ReadonlyReactive, Reactive, computed, reactive, watch } from \"@conterra/reactivity-core\";\nimport {\n DependencyList,\n useCallback,\n useDebugValue,\n useMemo,\n useRef,\n useSyncExternalStore\n} from \"react\";\n\n/**\n * Creates a new signal with the initial value `undefined`.\n *\n * The initial value will only be applied on the very first call to this hook.\n * The same signal instance will be returned when this hook is called on the next render.\n * This is very similar (in concept) to React's builtin `useState` hook.\n *\n * @example\n *\n * ```jsx\n * function YourComponent() {\n * const mySignal = useReactive();\n * const someEventHandler = () => {\n * // Update the value after some event\n * mySignal.value = \"new value\";\n * };\n *\n * // Always returns the current value, re-renders if necessary.\n * const currentValue = useReactiveValue(mySignal);\n * }\n * ```\n */\nexport function useReactive<T>(): Reactive<T | undefined>;\n\n/**\n * Creates a new signal with the given initial value.\n *\n * @see {@link useReactive()}.\n */\nexport function useReactive<T>(initialValue: T): Reactive<T>;\n\nexport function useReactive<T>(initialValue?: T): Reactive<T | undefined> {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n return useMemo(() => reactive(initialValue), []);\n}\n\n/**\n * Creates a new computed signal that evaluates the given `compute` function.\n *\n * The hook returns the same signal for as long as `deps` remains the same.\n * When `deps` change, a new signal will be created.\n *\n * @example\n *\n * Defining a simple computed value in your react component:\n *\n * ```jsx\n * function MyComponent(props) {\n * // Creates a computed value based on the given function.\n * // The computed value will be re-created whenever one of the listed dependencies changes.\n * // While the computed value can track all its _reactive_ dependencies automatically (`.value` reads etc.)\n * // the reactive value itself used here might change (different props) - this cannot be detected automatically.\n * const computed = useComputed(() => props.someReactive.value * 2, [props.someReactive]);\n *\n * // Watches on `computed` and returns its current value.\n * const currentValue = useReactiveValue(computed);\n * }\n * ```\n *\n * > If the body of your compute function depends on non-reactive values (such as react props or state),\n * > list those values in the `deps` array.\n *\n * @see {@link useReactiveValue()}\n * @see {@link useReactiveSnapshot()}\n */\nexport function useComputed<T>(compute: () => T, deps: DependencyList): ReadonlyReactive<T> {\n const computeRef = useRef(compute);\n computeRef.current = compute;\n\n const computedDeps = useComputedDeps(deps);\n return useMemo(() => {\n void computedDeps; // not really used, but computed must be recreated when this value changes\n return computed(() => computeRef.current());\n }, [computedDeps]);\n}\n\n/**\n * Watches the given reactive object and returns its current value.\n *\n * > NOTE: The return value of this hook should be considered read-only.\n *\n * @see {@link useComputed()}\n */\nexport function useReactiveValue<T>(reactive: ReadonlyReactive<T>): T {\n const getSnapshot = useCallback(() => {\n return reactive.peek();\n }, [reactive]);\n const subscribe = useCallback(\n (cb: () => void) => {\n const handle = watch(() => [reactive.value], cb);\n return () => handle.destroy();\n },\n [reactive]\n );\n const value = useSyncExternalStore(subscribe, getSnapshot);\n useDebugValue(value);\n return value;\n}\n\n/**\n * Evaluates the `compute` function and returns its result.\n *\n * The body of the compute function is tracked: it\n * will be re-evaluated whenever its reactive dependencies change.\n * The outer react component will re-render whenever `compute` returns a new value.\n *\n * > NOTE: The return value of this hook should be considered read-only.\n *\n * @example\n *\n * Simple Example:\n *\n * ```jsx\n * // A reactive signal from anywhere (global data, props, ...).\n * const name = reactive(\"User\");\n *\n * function YourComponent() {\n * const nameSnapshot = useReactiveSnapshot(() => name.value);\n * // snapshot is automatically up to date\n * }\n * ```\n *\n * @example\n *\n * Multiple values, with props:\n *\n * ```jsx\n * function YourComponent({firstName, lastName}) {\n * // You can use complex expressions in your compute function,\n * // just like in a computed signal.\n * // React-dependencies used in the callback must be listed in the second argument,\n * // just like when using `useMemo()` or `useEffect`.\n * const snapshot = useReactiveSnapshot(() => {\n * return {\n * firstName: firstName.value,\n * lastName: lastName.value,\n * fullName: `${firstName.value} ${lastName.value}`\n * };\n * }, [firstName, lastName]);\n * // snapshot.firstName, snapshot.lastName, snapshot.fullName are available and up to date\n * }\n * ```\n *\n * @see\n * This hook is based on {@link useComputed()} and {@link useReactiveValue()}.\n */\nexport function useReactiveSnapshot<T>(compute: () => T, deps: DependencyList): T {\n // We create an internal computed value that executes the compute function;\n // this way the implementation of `compute` is automatically tracked.\n // getSnapshot and subscribe simply watch the current value of that computed value.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const computedSnapshot = useComputed(compute, deps);\n const snapshot = useReactiveValue(computedSnapshot);\n useDebugValue(snapshot);\n return snapshot;\n}\n\nfunction useComputedDeps(deps: DependencyList): DependencyList {\n const computedDeps = useRef<DependencyList>(null);\n if (computedDeps.current == null || !shallowEqual(computedDeps.current, deps)) {\n computedDeps.current = deps ?? [];\n }\n return computedDeps.current;\n}\n\nfunction shallowEqual(a: DependencyList, b: DependencyList): boolean {\n if (a === b) {\n return true;\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return a.length === (b?.length ?? 0) && a.every((v, i) => v === b![i]);\n}\n"],"names":["reactive"],"mappings":";;;AA2CO,SAAS,YAAe,YAAA,EAA2C;AAEtE,EAAA,OAAO,QAAQ,MAAM,QAAA,CAAS,YAAY,CAAA,EAAG,EAAE,CAAA;AACnD;AA+BO,SAAS,WAAA,CAAe,SAAkB,IAAA,EAA2C;AACxF,EAAA,MAAM,UAAA,GAAa,OAAO,OAAO,CAAA;AACjC,EAAA,UAAA,CAAW,OAAA,GAAU,OAAA;AAErB,EAAA,MAAM,YAAA,GAAe,gBAAgB,IAAI,CAAA;AACzC,EAAA,OAAO,QAAQ,MAAM;AAEjB,IAAA,OAAO,QAAA,CAAS,MAAM,UAAA,CAAW,OAAA,EAAS,CAAA;AAAA,EAC9C,CAAA,EAAG,CAAC,YAAY,CAAC,CAAA;AACrB;AASO,SAAS,iBAAoBA,SAAAA,EAAkC;AAClE,EAAA,MAAM,WAAA,GAAc,YAAY,MAAM;AAClC,IAAA,OAAOA,UAAS,IAAA,EAAK;AAAA,EACzB,CAAA,EAAG,CAACA,SAAQ,CAAC,CAAA;AACb,EAAA,MAAM,SAAA,GAAY,WAAA;AAAA,IACd,CAAC,EAAA,KAAmB;AAChB,MAAA,MAAM,SAAS,KAAA,CAAM,MAAM,CAACA,SAAAA,CAAS,KAAK,GAAG,EAAE,CAAA;AAC/C,MAAA,OAAO,MAAM,OAAO,OAAA,EAAQ;AAAA,IAChC,CAAA;AAAA,IACA,CAACA,SAAQ;AAAA,GACb;AACA,EAAA,MAAM,KAAA,GAAQ,oBAAA,CAAqB,SAAA,EAAW,WAAW,CAAA;AACzD,EAAA,aAAA,CAAc,KAAK,CAAA;AACnB,EAAA,OAAO,KAAA;AACX;AAiDO,SAAS,mBAAA,CAAuB,SAAkB,IAAA,EAAyB;AAK9E,EAAA,MAAM,gBAAA,GAAmB,WAAA,CAAY,OAAA,EAAS,IAAI,CAAA;AAClD,EAAA,MAAM,QAAA,GAAW,iBAAiB,gBAAgB,CAAA;AAClD,EAAA,aAAA,CAAc,QAAQ,CAAA;AACtB,EAAA,OAAO,QAAA;AACX;AAEA,SAAS,gBAAgB,IAAA,EAAsC;AAC3D,EAAA,MAAM,YAAA,GAAe,OAAuB,IAAI,CAAA;AAChD,EAAA,IAAI,YAAA,CAAa,WAAW,IAAA,IAAQ,CAAC,aAAa,YAAA,CAAa,OAAA,EAAS,IAAI,CAAA,EAAG;AAC3E,IAAA,YAAA,CAAa,OAAA,GAAU,QAAQ,EAAC;AAAA,EACpC;AACA,EAAA,OAAO,YAAA,CAAa,OAAA;AACxB;AAEA,SAAS,YAAA,CAAa,GAAmB,CAAA,EAA4B;AACjE,EAAA,IAAI,MAAM,CAAA,EAAG;AACT,IAAA,OAAO,IAAA;AAAA,EACX;AAEA,EAAA,OAAO,CAAA,CAAE,MAAA,MAAY,CAAA,EAAG,MAAA,IAAU,CAAA,CAAA,IAAM,CAAA,CAAE,KAAA,CAAM,CAAC,CAAA,EAAG,CAAA,KAAM,CAAA,KAAM,CAAA,CAAG,CAAC,CAAC,CAAA;AACzE;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023-2025 Open Pioneer project (https://github.com/open-pioneer)\n// SPDX-License-Identifier: Apache-2.0\nimport {\n DispatchType,\n Reactive,\n ReadonlyReactive,\n computed,\n reactive,\n watchValue\n} from \"@conterra/reactivity-core\";\nimport {\n DependencyList,\n useCallback,\n useDebugValue,\n useMemo,\n useRef,\n useSyncExternalStore\n} from \"react\";\n\n/**\n * Creates a new signal with the initial value `undefined`.\n *\n * The initial value will only be applied on the very first call to this hook.\n * The same signal instance will be returned when this hook is called on the next render.\n * This is very similar (in concept) to React's builtin `useState` hook.\n *\n * @example\n *\n * ```jsx\n * function YourComponent() {\n * const mySignal = useReactive();\n * const someEventHandler = () => {\n * // Update the value after some event\n * mySignal.value = \"new value\";\n * };\n *\n * // Always returns the current value, re-renders if necessary.\n * const currentValue = useReactiveValue(mySignal);\n * }\n * ```\n */\nexport function useReactive<T>(): Reactive<T | undefined>;\n\n/**\n * Creates a new signal with the given initial value.\n *\n * @see {@link useReactive()}.\n */\nexport function useReactive<T>(initialValue: T): Reactive<T>;\n\nexport function useReactive<T>(initialValue?: T): Reactive<T | undefined> {\n // eslint-disable-next-line react-hooks/exhaustive-deps\n return useMemo(() => reactive(initialValue), []);\n}\n\n/**\n * Creates a new computed signal that evaluates the given `compute` function.\n *\n * The hook returns the same signal for as long as `deps` remains the same.\n * When `deps` change, a new signal will be created.\n *\n * @example\n *\n * Defining a simple computed value in your react component:\n *\n * ```jsx\n * function MyComponent(props) {\n * // Creates a computed value based on the given function.\n * // The computed value will be re-created whenever one of the listed dependencies changes.\n * // While the computed value can track all its _reactive_ dependencies automatically (`.value` reads etc.)\n * // the reactive value itself used here might change (different props) - this cannot be detected automatically.\n * const computed = useComputed(() => props.someReactive.value * 2, [props.someReactive]);\n *\n * // Watches on `computed` and returns its current value.\n * const currentValue = useReactiveValue(computed);\n * }\n * ```\n *\n * > If the body of your compute function depends on non-reactive values (such as react props or state),\n * > list those values in the `deps` array.\n *\n * @see {@link useReactiveValue()}\n * @see {@link useReactiveSnapshot()}\n */\nexport function useComputed<T>(compute: () => T, deps: DependencyList): ReadonlyReactive<T> {\n const computeRef = useRef(compute);\n computeRef.current = compute;\n\n const computedDeps = useComputedDeps(deps);\n return useMemo(() => {\n void computedDeps; // not really used, but computed must be recreated when this value changes\n return computed(() => computeRef.current());\n }, [computedDeps]);\n}\n\n/**\n * Advanced options for `useReactiveSnapshot`.\n *\n * Note that objects should either be memoized (`useMemo` etc.) or constants (see {@link DISPATCH_SYNC})).\n */\nexport interface ReactiveHookOptions {\n dispatch?: DispatchType;\n}\n\n/**\n * Switches from async dispatch (the default) to sync dispatch.\n *\n * This is helpful when using reactive values for input fields, focus management or layout behavior.\n */\nexport const DISPATCH_SYNC: ReactiveHookOptions = Object.freeze({ dispatch: \"sync\" });\n\n/**\n * Watches the given reactive object and returns its current value.\n *\n * > NOTE: The return value of this hook should be considered read-only.\n *\n * > NOTE: The `options` parameter (if used) should be memoized or constant.\n *\n * @see {@link useComputed()}\n */\nexport function useReactiveValue<T>(\n reactive: ReadonlyReactive<T>,\n options?: ReactiveHookOptions\n): T {\n const getSnapshot = useCallback(() => {\n return reactive.peek();\n }, [reactive]);\n const subscribe = useCallback(\n (cb: () => void) => {\n const handle = watchValue(() => reactive.value, cb, {\n dispatch: options?.dispatch ?? \"async\"\n });\n return () => handle.destroy();\n },\n [reactive, options?.dispatch]\n );\n const value = useSyncExternalStore(subscribe, getSnapshot);\n useDebugValue(value);\n return value;\n}\n\n/**\n * Evaluates the `compute` function and returns its result.\n *\n * The body of the compute function is tracked: it\n * will be re-evaluated whenever its reactive dependencies change.\n * The outer react component will re-render whenever `compute` returns a new value.\n *\n * > NOTE: The return value of this hook should be considered read-only.\n *\n * > NOTE: The `options` parameter (if used) should be memoized or constant.\n *\n * @example\n *\n * Simple Example:\n *\n * ```jsx\n * import { useReactiveSnapshot } from \"@open-pioneer/reactivity\";\n *\n * // A reactive signal from anywhere (global data, props, ...).\n * const name = reactive(\"User\");\n *\n * function YourComponent() {\n * const nameSnapshot = useReactiveSnapshot(() => name.value);\n * // snapshot is automatically up to date\n * }\n * ```\n *\n * @example\n *\n * Multiple values, with props:\n *\n * ```jsx\n * import { useReactiveSnapshot } from \"@open-pioneer/reactivity\";\n *\n * function YourComponent({firstName, lastName}) {\n * // You can use complex expressions in your compute function,\n * // just like in a computed signal.\n * // React-dependencies used in the callback must be listed in the second argument,\n * // just like when using `useMemo()` or `useEffect`.\n * const snapshot = useReactiveSnapshot(() => {\n * return {\n * firstName: firstName.value,\n * lastName: lastName.value,\n * fullName: `${firstName.value} ${lastName.value}`\n * };\n * }, [firstName, lastName]);\n * // snapshot.firstName, snapshot.lastName, snapshot.fullName are available and up to date\n * }\n * ```\n *\n * @example\n *\n * Changing the default timing:\n *\n * ```jsx\n * import { useReactiveSnapshot, DISPATCH_SYNC } from \"@open-pioneer/reactivity\";\n *\n * function YourComponent(props) {\n * // DISPATCH_SYNC rerenders us faster than the default timing.\n * // This is useful for values used in input fields, in layout operations or focus management.\n * // See also https://github.com/open-pioneer/trails-core-packages/issues/125\n * const nameSnapshot = useReactiveSnapshot(() => props.reactiveModel.value, [props.reactiveModel], DISPATCH_SYNC);\n * }\n * ```\n *\n * @see\n * This hook is based on {@link useComputed()} and {@link useReactiveValue()}.\n */\nexport function useReactiveSnapshot<T>(\n compute: () => T,\n deps: DependencyList,\n options?: ReactiveHookOptions\n): T {\n // We create an internal computed value that executes the compute function;\n // this way the implementation of `compute` is automatically tracked.\n // getSnapshot and subscribe simply watch the current value of that computed value.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n const computedSnapshot = useComputed(compute, deps);\n const snapshot = useReactiveValue(computedSnapshot, options);\n useDebugValue(snapshot);\n return snapshot;\n}\n\nfunction useComputedDeps(deps: DependencyList): DependencyList {\n const computedDeps = useRef<DependencyList>(null);\n if (computedDeps.current == null || !shallowEqual(computedDeps.current, deps)) {\n computedDeps.current = deps ?? [];\n }\n return computedDeps.current;\n}\n\nfunction shallowEqual(a: DependencyList, b: DependencyList): boolean {\n if (a === b) {\n return true;\n }\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return a.length === (b?.length ?? 0) && a.every((v, i) => v === b![i]);\n}\n"],"names":["reactive"],"mappings":";;;AAkDO,SAAS,YAAe,YAAA,EAA2C;AAEtE,EAAA,OAAO,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