@open-pioneer/reactivity 2.4.0 → 3.0.0-dev.20250221111321
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 +19 -5
- package/README.md +6 -6
- package/index.js +1 -1
- package/index.js.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,29 +1,43 @@
|
|
|
1
1
|
# @open-pioneer/reactivity
|
|
2
2
|
|
|
3
|
+
## 3.0.0-dev.20250221111321
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 9477e54: Update dependencies
|
|
8
|
+
|
|
9
|
+
- React 19
|
|
10
|
+
- Vite 6
|
|
11
|
+
- FormatJS 3
|
|
12
|
+
- Open Pioneer Build Tools
|
|
13
|
+
- ...
|
|
14
|
+
|
|
15
|
+
For more details, see https://github.com/open-pioneer/trails-core-packages/pull/81
|
|
16
|
+
|
|
3
17
|
## 2.4.0
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
|
6
20
|
|
|
7
|
-
-
|
|
21
|
+
- 1b63ebe: Update dependencies
|
|
8
22
|
|
|
9
23
|
## 2.3.0
|
|
10
24
|
|
|
11
25
|
### Minor Changes
|
|
12
26
|
|
|
13
|
-
-
|
|
27
|
+
- 2fbaaa0: Use normal `dependencies` instead of `peerDependencies`. Peer dependencies have some usability issues (and bugs) when used at scale.
|
|
14
28
|
|
|
15
29
|
## 2.2.0
|
|
16
30
|
|
|
17
31
|
### Patch Changes
|
|
18
32
|
|
|
19
|
-
-
|
|
20
|
-
|
|
33
|
+
- 39dad46: Switch to a new versioning strategy.
|
|
34
|
+
From now on, packages released by this repository share a common version number.
|
|
21
35
|
|
|
22
36
|
## 0.1.0
|
|
23
37
|
|
|
24
38
|
### Minor Changes
|
|
25
39
|
|
|
26
|
-
-
|
|
40
|
+
- 5d3aafd: Initial release.
|
|
27
41
|
|
|
28
42
|
You can use the hook `useReactiveSnapshot` to write React components that make use of the reactivity API of [@conterra/reactivity-core](https://www.npmjs.com/package/@conterra/reactivity-core).
|
|
29
43
|
Quick example:
|
package/README.md
CHANGED
|
@@ -41,12 +41,12 @@ export function YourComponent({ model }: YourComponentProps) {
|
|
|
41
41
|
}
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
-
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
- `callback` is a function that accesses reactive values which are based on signals (see example below) and returns some computed _result_.
|
|
45
|
+
In the example above, we used the values of `firstName` and `lastName`.
|
|
46
|
+
The access to those values was tracked by `useReactiveSnapshot`; updates will result in the React component rendering with the updated `fullName`.
|
|
47
|
+
- `deps` are an array of React dependencies (such as props or local variables), very similar to `useEffect` or `useMemo`.
|
|
48
|
+
The hook cannot detect changes of values that are _not_ based on the reactivity API (such as react props), so they need to be listed here.
|
|
49
|
+
In this case we had to specify the `model` itself (it may change as well!) but not its properties.
|
|
50
50
|
|
|
51
51
|
To complete the example from above, here is the full source of a compatible `Model` implementation:
|
|
52
52
|
|
package/index.js
CHANGED
|
@@ -34,7 +34,7 @@ function useReactiveSnapshot(compute, deps) {
|
|
|
34
34
|
return snapshot;
|
|
35
35
|
}
|
|
36
36
|
function useComputedDeps(deps) {
|
|
37
|
-
const computedDeps = useRef();
|
|
37
|
+
const computedDeps = useRef(null);
|
|
38
38
|
if (computedDeps.current == null || !shallowEqual(computedDeps.current, deps)) {
|
|
39
39
|
computedDeps.current = deps ?? [];
|
|
40
40
|
}
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["index.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2023 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>();\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 return a.length === (b?.length ?? 0) && a.every((v, i) => v === b![i]);\n}\n"],"names":["reactive"],"mappings":";;;AA2CO,SAAS,YAAe,YAA2C,EAAA;AAEtE,EAAA,OAAO,QAAQ,MAAM,QAAA,CAAS,YAAY,CAAA,EAAG,EAAE,CAAA
|
|
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 return a.length === (b?.length ?? 0) && a.every((v, i) => v === b![i]);\n}\n"],"names":["reactive"],"mappings":";;;AA2CO,SAAS,YAAe,YAA2C,EAAA;AAEtE,EAAA,OAAO,QAAQ,MAAM,QAAA,CAAS,YAAY,CAAA,EAAG,EAAE,CAAA;AACnD;AA+BgB,SAAA,WAAA,CAAe,SAAkB,IAA2C,EAAA;AACxF,EAAM,MAAA,UAAA,GAAa,OAAO,OAAO,CAAA;AACjC,EAAA,UAAA,CAAW,OAAU,GAAA,OAAA;AAErB,EAAM,MAAA,YAAA,GAAe,gBAAg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AAA,CAAa,GAAmB,CAA4B,EAAA;AACjE,EAAA,IAAI,MAAM,CAAG,EAAA;AACT,IAAO,OAAA,IAAA;AAAA;AAEX,EAAA,OAAO,CAAE,CAAA,MAAA,MAAY,CAAG,EAAA,MAAA,IAAU,CAAM,CAAA,IAAA,CAAA,CAAE,KAAM,CAAA,CAAC,CAAG,EAAA,CAAA,KAAM,CAAM,KAAA,CAAA,CAAG,CAAC,CAAC,CAAA;AACzE;;;;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@open-pioneer/reactivity",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.0-dev.20250221111321",
|
|
5
5
|
"description": "Provides integration for the reactivity API.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"open-pioneer-trails"
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"directory": "src/packages/reactivity"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"react": "^
|
|
17
|
+
"react": "^19.0.0",
|
|
18
18
|
"@conterra/reactivity-core": "^0.4.3"
|
|
19
19
|
},
|
|
20
20
|
"exports": {
|