@yaasl/preact 0.14.0 → 0.15.0-alpha.0

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,2 +1,3 @@
1
- export { useAtom, useSetAtom, useAtomValue, useAtomDidInit } from "./use-atom";
1
+ export { useAtom, useAtomDidInit } from "./use-atom";
2
+ export { useSelector } from "./use-selector";
2
3
  export * from "@yaasl/core";
@@ -1,19 +1,11 @@
1
1
  import type { Stateful } from "@yaasl/core";
2
- import type { Updater } from "@yaasl/utils";
3
2
  /** Use an atom's value in the preact lifecycle.
4
3
  *
5
4
  * @param atom Atom to be used.
6
5
  *
7
6
  * @returns A stateful value.
8
7
  **/
9
- export declare const useAtomValue: <ValueType>(atom: Stateful<ValueType>) => ValueType;
10
- /** Set an atom's value in the preact lifecycle.
11
- *
12
- * @param atom Atom to be used.
13
- *
14
- * @returns A setter function for the atom.
15
- **/
16
- export declare const useSetAtom: <ValueType>(atom: Stateful<ValueType>) => ((next: Updater<ValueType>) => void);
8
+ export declare const useAtom: <ValueType>(atom: Stateful<ValueType>) => ValueType;
17
9
  /** Use an atom's initialization state in the preact lifecycle.
18
10
  *
19
11
  * @param atom Atom to be used.
@@ -21,12 +13,3 @@ export declare const useSetAtom: <ValueType>(atom: Stateful<ValueType>) => ((nex
21
13
  * @returns A boolean indicating if the atom has finished initializing yet.
22
14
  **/
23
15
  export declare const useAtomDidInit: <ValueType>(atom: Stateful<ValueType>) => boolean;
24
- /** Use an atom's value and setter in the preact lifecycle.
25
- *
26
- * **Note:** Use `useAtomValue` or `useSetAtom` to use value or setter separately.
27
- *
28
- * @param atom Atom to be used.
29
- *
30
- * @returns [value, setValue, didInit]
31
- **/
32
- export declare const useAtom: <ValueType>(atom: Stateful<ValueType>) => readonly [ValueType, (next: Updater<ValueType>) => void, boolean];
@@ -0,0 +1,12 @@
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;
3
+ /** Compute a new value based on the state of an atom.
4
+ *
5
+ * @param atom Atom to be used.
6
+ * @param selector Function to retrieve the new value.
7
+ * @param compare Function to compare the previous with a newer result. Defaults to a custom equality function.
8
+ *
9
+ * @returns The computed value.
10
+ **/
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 {};
package/dist/cjs/index.js CHANGED
@@ -14,10 +14,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.useAtomDidInit = exports.useAtomValue = exports.useSetAtom = exports.useAtom = void 0;
17
+ exports.useSelector = exports.useAtomDidInit = exports.useAtom = void 0;
18
18
  var use_atom_1 = require("./use-atom");
19
19
  Object.defineProperty(exports, "useAtom", { enumerable: true, get: function () { return use_atom_1.useAtom; } });
20
- Object.defineProperty(exports, "useSetAtom", { enumerable: true, get: function () { return use_atom_1.useSetAtom; } });
21
- Object.defineProperty(exports, "useAtomValue", { enumerable: true, get: function () { return use_atom_1.useAtomValue; } });
22
20
  Object.defineProperty(exports, "useAtomDidInit", { enumerable: true, get: function () { return use_atom_1.useAtomDidInit; } });
21
+ var use_selector_1 = require("./use-selector");
22
+ Object.defineProperty(exports, "useSelector", { enumerable: true, get: function () { return use_selector_1.useSelector; } });
23
23
  __exportStar(require("@yaasl/core"), exports);
@@ -1,24 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useAtom = exports.useAtomDidInit = exports.useSetAtom = exports.useAtomValue = void 0;
3
+ exports.useAtomDidInit = exports.useAtom = void 0;
4
+ const compat_1 = require("preact/compat");
4
5
  const hooks_1 = require("preact/hooks");
5
- const use_stateful_1 = require("./use-stateful");
6
6
  /** Use an atom's value in the preact lifecycle.
7
7
  *
8
8
  * @param atom Atom to be used.
9
9
  *
10
10
  * @returns A stateful value.
11
11
  **/
12
- const useAtomValue = (atom) => (0, use_stateful_1.useStatefulValue)(atom);
13
- exports.useAtomValue = useAtomValue;
14
- /** Set an atom's value in the preact lifecycle.
15
- *
16
- * @param atom Atom to be used.
17
- *
18
- * @returns A setter function for the atom.
19
- **/
20
- const useSetAtom = (atom) => (0, use_stateful_1.useSetStateful)(atom);
21
- exports.useSetAtom = useSetAtom;
12
+ const useAtom = (atom) => (0, compat_1.useSyncExternalStore)(set => atom.subscribe(set), () => atom.get());
13
+ exports.useAtom = useAtom;
22
14
  /** Use an atom's initialization state in the preact lifecycle.
23
15
  *
24
16
  * @param atom Atom to be used.
@@ -35,18 +27,3 @@ const useAtomDidInit = (atom) => {
35
27
  return didInit;
36
28
  };
37
29
  exports.useAtomDidInit = useAtomDidInit;
38
- /** Use an atom's value and setter in the preact lifecycle.
39
- *
40
- * **Note:** Use `useAtomValue` or `useSetAtom` to use value or setter separately.
41
- *
42
- * @param atom Atom to be used.
43
- *
44
- * @returns [value, setValue, didInit]
45
- **/
46
- const useAtom = (atom) => {
47
- const state = (0, exports.useAtomValue)(atom);
48
- const setState = (0, exports.useSetAtom)(atom);
49
- const didInit = (0, exports.useAtomDidInit)(atom);
50
- return [state, setState, didInit];
51
- };
52
- exports.useAtom = useAtom;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useSelector = void 0;
4
+ const compat_1 = require("preact/compat");
5
+ const hooks_1 = require("preact/hooks");
6
+ const utils_1 = require("@yaasl/utils");
7
+ /** Compute a new value based on the state of an atom.
8
+ *
9
+ * @param atom Atom to be used.
10
+ * @param selector Function to retrieve the new value.
11
+ * @param compare Function to compare the previous with a newer result. Defaults to a custom equality function.
12
+ *
13
+ * @returns The computed value.
14
+ **/
15
+ const useSelector = (atoms, selector, compare) => {
16
+ const memoizedSelector = (0, hooks_1.useRef)((0, utils_1.memoizeFunction)(selector, compare));
17
+ (0, hooks_1.useEffect)(() => {
18
+ // eslint-disable-next-line react-compiler/react-compiler
19
+ memoizedSelector.current.resultFn = selector;
20
+ memoizedSelector.current.compareResult = compare;
21
+ });
22
+ const subscribe = (onStoreChange) => {
23
+ const atomArray = [atoms].flat();
24
+ const unsubscribers = atomArray.map(atom => atom.subscribe(onStoreChange));
25
+ return () => unsubscribers.forEach(fn => fn());
26
+ };
27
+ const getSnapshot = () => {
28
+ const atomArray = [atoms].flat();
29
+ const args = atomArray.map(atom => atom.get());
30
+ return memoizedSelector.current(...args);
31
+ };
32
+ return (0, compat_1.useSyncExternalStore)(subscribe, getSnapshot);
33
+ };
34
+ exports.useSelector = useSelector;
package/dist/esm/index.js CHANGED
@@ -1,2 +1,3 @@
1
- export { useAtom, useSetAtom, useAtomValue, useAtomDidInit } from "./use-atom";
1
+ export { useAtom, useAtomDidInit } from "./use-atom";
2
+ export { useSelector } from "./use-selector";
2
3
  export * from "@yaasl/core";
@@ -1,19 +1,12 @@
1
+ import { useSyncExternalStore } from "preact/compat";
1
2
  import { useEffect, useState } from "preact/hooks";
2
- import { useStatefulValue, useSetStateful } from "./use-stateful";
3
3
  /** Use an atom's value in the preact lifecycle.
4
4
  *
5
5
  * @param atom Atom to be used.
6
6
  *
7
7
  * @returns A stateful value.
8
8
  **/
9
- export const useAtomValue = (atom) => useStatefulValue(atom);
10
- /** Set an atom's value in the preact lifecycle.
11
- *
12
- * @param atom Atom to be used.
13
- *
14
- * @returns A setter function for the atom.
15
- **/
16
- export const useSetAtom = (atom) => useSetStateful(atom);
9
+ export const useAtom = (atom) => useSyncExternalStore(set => atom.subscribe(set), () => atom.get());
17
10
  /** Use an atom's initialization state in the preact lifecycle.
18
11
  *
19
12
  * @param atom Atom to be used.
@@ -29,17 +22,3 @@ export const useAtomDidInit = (atom) => {
29
22
  }, [atom.didInit]);
30
23
  return didInit;
31
24
  };
32
- /** Use an atom's value and setter in the preact lifecycle.
33
- *
34
- * **Note:** Use `useAtomValue` or `useSetAtom` to use value or setter separately.
35
- *
36
- * @param atom Atom to be used.
37
- *
38
- * @returns [value, setValue, didInit]
39
- **/
40
- export const useAtom = (atom) => {
41
- const state = useAtomValue(atom);
42
- const setState = useSetAtom(atom);
43
- const didInit = useAtomDidInit(atom);
44
- return [state, setState, didInit];
45
- };
@@ -0,0 +1,30 @@
1
+ import { useSyncExternalStore } from "preact/compat";
2
+ import { useRef, useEffect } from "preact/hooks";
3
+ import { memoizeFunction } from "@yaasl/utils";
4
+ /** Compute a new value based on the state of an atom.
5
+ *
6
+ * @param atom Atom to be used.
7
+ * @param selector Function to retrieve the new value.
8
+ * @param compare Function to compare the previous with a newer result. Defaults to a custom equality function.
9
+ *
10
+ * @returns The computed value.
11
+ **/
12
+ export const useSelector = (atoms, selector, compare) => {
13
+ const memoizedSelector = useRef(memoizeFunction(selector, compare));
14
+ useEffect(() => {
15
+ // eslint-disable-next-line react-compiler/react-compiler
16
+ memoizedSelector.current.resultFn = selector;
17
+ memoizedSelector.current.compareResult = compare;
18
+ });
19
+ const subscribe = (onStoreChange) => {
20
+ const atomArray = [atoms].flat();
21
+ const unsubscribers = atomArray.map(atom => atom.subscribe(onStoreChange));
22
+ return () => unsubscribers.forEach(fn => fn());
23
+ };
24
+ const getSnapshot = () => {
25
+ const atomArray = [atoms].flat();
26
+ const args = atomArray.map(atom => atom.get());
27
+ return memoizedSelector.current(...args);
28
+ };
29
+ return useSyncExternalStore(subscribe, getSnapshot);
30
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaasl/preact",
3
- "version": "0.14.0",
3
+ "version": "0.15.0-alpha.0",
4
4
  "description": "yet another atomic store library (preact)",
5
5
  "author": "PrettyCoffee",
6
6
  "license": "MIT",
@@ -40,13 +40,13 @@
40
40
  "validate": "run-s lint test build"
41
41
  },
42
42
  "peerDependencies": {
43
- "@yaasl/core": "^0.14.0",
44
- "@yaasl/utils": "^0.14.0",
43
+ "@yaasl/core": "^0.15.0-alpha.0",
44
+ "@yaasl/utils": "^0.15.0-alpha.0",
45
45
  "preact": ">=10"
46
46
  },
47
47
  "devDependencies": {
48
- "@yaasl/core": "^0.14.0",
49
- "@yaasl/utils": "^0.14.0",
48
+ "@yaasl/core": "^0.15.0-alpha.0",
49
+ "@yaasl/utils": "^0.15.0-alpha.0",
50
50
  "preact": "^10.28.0"
51
51
  },
52
52
  "lint-staged": {
@@ -1,4 +0,0 @@
1
- import type { Stateful } from "@yaasl/core";
2
- import { Updater } from "@yaasl/utils";
3
- export declare const useStatefulValue: <ValueType>(stateful: Stateful<ValueType>) => ValueType;
4
- export declare const useSetStateful: <State>(stateful: Stateful<State>) => (next: Updater<State>) => void;
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useSetStateful = exports.useStatefulValue = void 0;
4
- const compat_1 = require("preact/compat");
5
- const hooks_1 = require("preact/hooks");
6
- const utils_1 = require("@yaasl/utils");
7
- const useStatefulValue = (stateful) => (0, compat_1.useSyncExternalStore)(set => stateful.subscribe(set), () => stateful.get());
8
- exports.useStatefulValue = useStatefulValue;
9
- const useSetStateful = (stateful) => (0, hooks_1.useCallback)((next) => {
10
- if (!("set" in stateful) || !(stateful.set instanceof Function)) {
11
- throw new Error((0, utils_1.consoleMessage)("Atom does not have a set method"));
12
- }
13
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
14
- stateful.set(next);
15
- }, [stateful]);
16
- exports.useSetStateful = useSetStateful;
@@ -1,11 +0,0 @@
1
- import { useSyncExternalStore } from "preact/compat";
2
- import { useCallback } from "preact/hooks";
3
- import { consoleMessage } from "@yaasl/utils";
4
- export const useStatefulValue = (stateful) => useSyncExternalStore(set => stateful.subscribe(set), () => stateful.get());
5
- export const useSetStateful = (stateful) => useCallback((next) => {
6
- if (!("set" in stateful) || !(stateful.set instanceof Function)) {
7
- throw new Error(consoleMessage("Atom does not have a set method"));
8
- }
9
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
10
- stateful.set(next);
11
- }, [stateful]);