@yaasl/react 0.15.0-alpha.0 → 0.15.0-alpha.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,11 +1,12 @@
|
|
|
1
1
|
import type { Stateful } from "@yaasl/core";
|
|
2
|
+
import type { Subscribable } from "@yaasl/utils";
|
|
2
3
|
/** Use an atom's value in the React lifecycle.
|
|
3
4
|
*
|
|
4
5
|
* @param atom Atom to be used.
|
|
5
6
|
*
|
|
6
7
|
* @returns The atom's value.
|
|
7
8
|
**/
|
|
8
|
-
export declare const useAtom: <ValueType>(atom:
|
|
9
|
+
export declare const useAtom: <ValueType>(atom: Subscribable<ValueType>) => ValueType;
|
|
9
10
|
/** Use an atom's initialization state in the React lifecycle.
|
|
10
11
|
*
|
|
11
12
|
* @param atom Atom to be used.
|
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type InferValuesFromAtoms<TAtoms, TValues extends unknown[] = []> = TAtoms extends Stateful<infer TValue> ? [...TValues, TValue] : TAtoms extends [Stateful<infer Value>, ...infer Rest] ? InferValuesFromAtoms<Rest, [...TValues, Value]> : TValues;
|
|
1
|
+
import { type Subscribable, type InferValues } from "@yaasl/utils";
|
|
3
2
|
/** Compute a new value based on the state of an atom.
|
|
4
3
|
*
|
|
5
4
|
* @param atom Atom to be used.
|
|
6
|
-
* @param
|
|
5
|
+
* @param combiner Function to retrieve the new value.
|
|
7
6
|
* @param compare Function to compare the previous with a newer result. Defaults to a custom equality function.
|
|
8
7
|
*
|
|
9
8
|
* @returns The computed value.
|
|
10
9
|
**/
|
|
11
|
-
export declare const useSelector: <TAtoms extends
|
|
12
|
-
export {};
|
|
10
|
+
export declare const useSelector: <TAtoms extends Subscribable | [Subscribable, ...Subscribable[]], TResult>(atoms: TAtoms, combiner: (...states: InferValues<TAtoms>) => TResult, compare?: (before: TResult, after: TResult) => boolean) => TResult;
|
package/dist/cjs/use-selector.js
CHANGED
|
@@ -6,26 +6,24 @@ const utils_1 = require("@yaasl/utils");
|
|
|
6
6
|
/** Compute a new value based on the state of an atom.
|
|
7
7
|
*
|
|
8
8
|
* @param atom Atom to be used.
|
|
9
|
-
* @param
|
|
9
|
+
* @param combiner Function to retrieve the new value.
|
|
10
10
|
* @param compare Function to compare the previous with a newer result. Defaults to a custom equality function.
|
|
11
11
|
*
|
|
12
12
|
* @returns The computed value.
|
|
13
13
|
**/
|
|
14
|
-
const useSelector = (atoms,
|
|
15
|
-
const
|
|
14
|
+
const useSelector = (atoms, combiner, compare) => {
|
|
15
|
+
const memoizedCombiner = (0, react_1.useRef)((0, utils_1.memoizeFunction)(combiner, compare));
|
|
16
16
|
(0, react_1.useEffect)(() => {
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
memoizedCombiner.current.resultFn = combiner;
|
|
18
|
+
memoizedCombiner.current.compareResult = compare;
|
|
19
19
|
});
|
|
20
20
|
const subscribe = (onStoreChange) => {
|
|
21
|
-
const
|
|
22
|
-
const unsubscribers = atomArray.map(atom => atom.subscribe(onStoreChange));
|
|
21
|
+
const unsubscribers = (0, utils_1.toArray)(atoms).map(atom => atom.subscribe(onStoreChange));
|
|
23
22
|
return () => unsubscribers.forEach(fn => fn());
|
|
24
23
|
};
|
|
25
24
|
const getSnapshot = () => {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
return memoizedSelector.current(...args);
|
|
25
|
+
const args = (0, utils_1.toArray)(atoms).map(atom => atom.get());
|
|
26
|
+
return memoizedCombiner.current(...args);
|
|
29
27
|
};
|
|
30
28
|
return (0, react_1.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
|
|
31
29
|
};
|
package/dist/esm/use-selector.js
CHANGED
|
@@ -1,28 +1,26 @@
|
|
|
1
1
|
import { useEffect, useRef, useSyncExternalStore } from "react";
|
|
2
|
-
import { memoizeFunction } from "@yaasl/utils";
|
|
2
|
+
import { memoizeFunction, toArray, } from "@yaasl/utils";
|
|
3
3
|
/** Compute a new value based on the state of an atom.
|
|
4
4
|
*
|
|
5
5
|
* @param atom Atom to be used.
|
|
6
|
-
* @param
|
|
6
|
+
* @param combiner Function to retrieve the new value.
|
|
7
7
|
* @param compare Function to compare the previous with a newer result. Defaults to a custom equality function.
|
|
8
8
|
*
|
|
9
9
|
* @returns The computed value.
|
|
10
10
|
**/
|
|
11
|
-
export const useSelector = (atoms,
|
|
12
|
-
const
|
|
11
|
+
export const useSelector = (atoms, combiner, compare) => {
|
|
12
|
+
const memoizedCombiner = useRef(memoizeFunction(combiner, compare));
|
|
13
13
|
useEffect(() => {
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
memoizedCombiner.current.resultFn = combiner;
|
|
15
|
+
memoizedCombiner.current.compareResult = compare;
|
|
16
16
|
});
|
|
17
17
|
const subscribe = (onStoreChange) => {
|
|
18
|
-
const
|
|
19
|
-
const unsubscribers = atomArray.map(atom => atom.subscribe(onStoreChange));
|
|
18
|
+
const unsubscribers = toArray(atoms).map(atom => atom.subscribe(onStoreChange));
|
|
20
19
|
return () => unsubscribers.forEach(fn => fn());
|
|
21
20
|
};
|
|
22
21
|
const getSnapshot = () => {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
return memoizedSelector.current(...args);
|
|
22
|
+
const args = toArray(atoms).map(atom => atom.get());
|
|
23
|
+
return memoizedCombiner.current(...args);
|
|
26
24
|
};
|
|
27
25
|
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
28
26
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaasl/react",
|
|
3
|
-
"version": "0.15.0-alpha.
|
|
3
|
+
"version": "0.15.0-alpha.1",
|
|
4
4
|
"description": "yet another atomic store library (react)",
|
|
5
5
|
"author": "PrettyCoffee",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,15 +40,15 @@
|
|
|
40
40
|
"validate": "run-s lint test build"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@yaasl/core": "^0.15.0-alpha.
|
|
44
|
-
"@yaasl/utils": "^0.15.0-alpha.
|
|
43
|
+
"@yaasl/core": "^0.15.0-alpha.1",
|
|
44
|
+
"@yaasl/utils": "^0.15.0-alpha.1",
|
|
45
45
|
"react": "^18.0.0 || ^19.0.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@testing-library/react": "^16.3.1",
|
|
49
|
-
"@types/react": "^19.2.
|
|
50
|
-
"@yaasl/core": "^0.15.0-alpha.
|
|
51
|
-
"@yaasl/utils": "^0.15.0-alpha.
|
|
49
|
+
"@types/react": "^19.2.8",
|
|
50
|
+
"@yaasl/core": "^0.15.0-alpha.1",
|
|
51
|
+
"@yaasl/utils": "^0.15.0-alpha.1",
|
|
52
52
|
"react": "^19.2.3",
|
|
53
53
|
"react-dom": "^19.2.3"
|
|
54
54
|
},
|