@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 { Stateful } from "@yaasl/core";
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 selector Function to retrieve the new value.
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 Stateful | [Stateful, ...Stateful[]], TResult>(atoms: TAtoms, selector: (...states: InferValuesFromAtoms<TAtoms>) => TResult, compare?: (before: TResult, after: TResult) => boolean) => TResult;
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;
@@ -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 selector Function to retrieve the new value.
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, selector, compare) => {
16
- const memoizedSelector = (0, hooks_1.useRef)((0, utils_1.memoizeFunction)(selector, compare));
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
- memoizedSelector.current.resultFn = selector;
20
- memoizedSelector.current.compareResult = compare;
19
+ memoizedCombiner.current.resultFn = combiner;
20
+ memoizedCombiner.current.compareResult = compare;
21
21
  });
22
22
  const subscribe = (onStoreChange) => {
23
- const atomArray = [atoms].flat();
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 atomArray = [atoms].flat();
29
- const args = atomArray.map(atom => atom.get());
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
  };
@@ -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 selector Function to retrieve the new value.
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, selector, compare) => {
13
- const memoizedSelector = useRef(memoizeFunction(selector, compare));
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
- memoizedSelector.current.resultFn = selector;
17
- memoizedSelector.current.compareResult = compare;
16
+ memoizedCombiner.current.resultFn = combiner;
17
+ memoizedCombiner.current.compareResult = compare;
18
18
  });
19
19
  const subscribe = (onStoreChange) => {
20
- const atomArray = [atoms].flat();
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 atomArray = [atoms].flat();
26
- const args = atomArray.map(atom => atom.get());
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.0",
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.0",
44
- "@yaasl/utils": "^0.15.0-alpha.0",
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.0",
49
- "@yaasl/utils": "^0.15.0-alpha.0",
50
- "preact": "^10.28.0"
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}": [