@yaasl/preact 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,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
|
@@ -7,27 +7,25 @@ const utils_1 = require("@yaasl/utils");
|
|
|
7
7
|
/** Compute a new value based on the state of an atom.
|
|
8
8
|
*
|
|
9
9
|
* @param atom Atom to be used.
|
|
10
|
-
* @param
|
|
10
|
+
* @param combiner Function to retrieve the new value.
|
|
11
11
|
* @param compare Function to compare the previous with a newer result. Defaults to a custom equality function.
|
|
12
12
|
*
|
|
13
13
|
* @returns The computed value.
|
|
14
14
|
**/
|
|
15
|
-
const useSelector = (atoms,
|
|
16
|
-
const
|
|
15
|
+
const useSelector = (atoms, combiner, compare) => {
|
|
16
|
+
const memoizedCombiner = (0, hooks_1.useRef)((0, utils_1.memoizeFunction)(combiner, compare));
|
|
17
17
|
(0, hooks_1.useEffect)(() => {
|
|
18
18
|
// eslint-disable-next-line react-compiler/react-compiler
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
memoizedCombiner.current.resultFn = combiner;
|
|
20
|
+
memoizedCombiner.current.compareResult = compare;
|
|
21
21
|
});
|
|
22
22
|
const subscribe = (onStoreChange) => {
|
|
23
|
-
const
|
|
24
|
-
const unsubscribers = atomArray.map(atom => atom.subscribe(onStoreChange));
|
|
23
|
+
const unsubscribers = (0, utils_1.toArray)(atoms).map(atom => atom.subscribe(onStoreChange));
|
|
25
24
|
return () => unsubscribers.forEach(fn => fn());
|
|
26
25
|
};
|
|
27
26
|
const getSnapshot = () => {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
return memoizedSelector.current(...args);
|
|
27
|
+
const args = (0, utils_1.toArray)(atoms).map(atom => atom.get());
|
|
28
|
+
return memoizedCombiner.current(...args);
|
|
31
29
|
};
|
|
32
30
|
return (0, compat_1.useSyncExternalStore)(subscribe, getSnapshot);
|
|
33
31
|
};
|
package/dist/esm/use-selector.js
CHANGED
|
@@ -1,30 +1,28 @@
|
|
|
1
1
|
import { useSyncExternalStore } from "preact/compat";
|
|
2
2
|
import { useRef, useEffect } from "preact/hooks";
|
|
3
|
-
import { memoizeFunction } from "@yaasl/utils";
|
|
3
|
+
import { memoizeFunction, toArray, } from "@yaasl/utils";
|
|
4
4
|
/** Compute a new value based on the state of an atom.
|
|
5
5
|
*
|
|
6
6
|
* @param atom Atom to be used.
|
|
7
|
-
* @param
|
|
7
|
+
* @param combiner Function to retrieve the new value.
|
|
8
8
|
* @param compare Function to compare the previous with a newer result. Defaults to a custom equality function.
|
|
9
9
|
*
|
|
10
10
|
* @returns The computed value.
|
|
11
11
|
**/
|
|
12
|
-
export const useSelector = (atoms,
|
|
13
|
-
const
|
|
12
|
+
export const useSelector = (atoms, combiner, compare) => {
|
|
13
|
+
const memoizedCombiner = useRef(memoizeFunction(combiner, compare));
|
|
14
14
|
useEffect(() => {
|
|
15
15
|
// eslint-disable-next-line react-compiler/react-compiler
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
memoizedCombiner.current.resultFn = combiner;
|
|
17
|
+
memoizedCombiner.current.compareResult = compare;
|
|
18
18
|
});
|
|
19
19
|
const subscribe = (onStoreChange) => {
|
|
20
|
-
const
|
|
21
|
-
const unsubscribers = atomArray.map(atom => atom.subscribe(onStoreChange));
|
|
20
|
+
const unsubscribers = toArray(atoms).map(atom => atom.subscribe(onStoreChange));
|
|
22
21
|
return () => unsubscribers.forEach(fn => fn());
|
|
23
22
|
};
|
|
24
23
|
const getSnapshot = () => {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
return memoizedSelector.current(...args);
|
|
24
|
+
const args = toArray(atoms).map(atom => atom.get());
|
|
25
|
+
return memoizedCombiner.current(...args);
|
|
28
26
|
};
|
|
29
27
|
return useSyncExternalStore(subscribe, getSnapshot);
|
|
30
28
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaasl/preact",
|
|
3
|
-
"version": "0.15.0-alpha.
|
|
3
|
+
"version": "0.15.0-alpha.1",
|
|
4
4
|
"description": "yet another atomic store library (preact)",
|
|
5
5
|
"author": "PrettyCoffee",
|
|
6
6
|
"license": "MIT",
|
|
@@ -40,14 +40,14 @@
|
|
|
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
|
"preact": ">=10"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@yaasl/core": "^0.15.0-alpha.
|
|
49
|
-
"@yaasl/utils": "^0.15.0-alpha.
|
|
50
|
-
"preact": "^10.28.
|
|
48
|
+
"@yaasl/core": "^0.15.0-alpha.1",
|
|
49
|
+
"@yaasl/utils": "^0.15.0-alpha.1",
|
|
50
|
+
"preact": "^10.28.2"
|
|
51
51
|
},
|
|
52
52
|
"lint-staged": {
|
|
53
53
|
"*.{ts,tsx}": [
|