@yaasl/react 0.14.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.
- package/dist/@types/index.d.ts +2 -1
- package/dist/@types/use-atom.d.ts +2 -15
- package/dist/@types/use-selector.d.ts +10 -0
- package/dist/cjs/index.js +3 -3
- package/dist/cjs/use-atom.js +3 -25
- package/dist/cjs/use-selector.js +30 -0
- package/dist/esm/index.js +2 -1
- package/dist/esm/use-atom.js +2 -22
- package/dist/esm/use-selector.js +26 -0
- package/package.json +6 -6
- package/dist/@types/use-stateful.d.ts +0 -4
- package/dist/cjs/use-stateful.js +0 -15
- package/dist/esm/use-stateful.js +0 -10
package/dist/@types/index.d.ts
CHANGED
|
@@ -1,18 +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
|
|
9
|
-
/** Set an atom's value in the React lifecycle.
|
|
10
|
-
*
|
|
11
|
-
* @param atom Atom to be used.
|
|
12
|
-
*
|
|
13
|
-
* @returns A setter function for the atom.
|
|
14
|
-
**/
|
|
15
|
-
export declare const useSetAtom: <ValueType>(atom: Stateful<ValueType>) => (next: import("react").SetStateAction<ValueType>) => void;
|
|
9
|
+
export declare const useAtom: <ValueType>(atom: Subscribable<ValueType>) => ValueType;
|
|
16
10
|
/** Use an atom's initialization state in the React lifecycle.
|
|
17
11
|
*
|
|
18
12
|
* @param atom Atom to be used.
|
|
@@ -20,10 +14,3 @@ export declare const useSetAtom: <ValueType>(atom: Stateful<ValueType>) => (next
|
|
|
20
14
|
* @returns A boolean indicating if the atom has finished initializing yet.
|
|
21
15
|
**/
|
|
22
16
|
export declare const useAtomDidInit: <ValueType>(atom: Stateful<ValueType>) => boolean;
|
|
23
|
-
/** Use an atom's value and setter in the React lifecycle.
|
|
24
|
-
*
|
|
25
|
-
* @param atom Atom to be used.
|
|
26
|
-
*
|
|
27
|
-
* @returns [value, setValue, didInit]
|
|
28
|
-
**/
|
|
29
|
-
export declare const useAtom: <ValueType>(atom: Stateful<ValueType>) => readonly [ValueType, (next: import("react").SetStateAction<ValueType>) => void, boolean];
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Subscribable, type InferValues } from "@yaasl/utils";
|
|
2
|
+
/** Compute a new value based on the state of an atom.
|
|
3
|
+
*
|
|
4
|
+
* @param atom Atom to be used.
|
|
5
|
+
* @param combiner Function to retrieve the new value.
|
|
6
|
+
* @param compare Function to compare the previous with a newer result. Defaults to a custom equality function.
|
|
7
|
+
*
|
|
8
|
+
* @returns The computed value.
|
|
9
|
+
**/
|
|
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/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.
|
|
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);
|
package/dist/cjs/use-atom.js
CHANGED
|
@@ -1,24 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.useAtomDidInit = exports.useAtom = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
-
const use_stateful_1 = require("./use-stateful");
|
|
6
5
|
/** Use an atom's value in the React lifecycle.
|
|
7
6
|
*
|
|
8
7
|
* @param atom Atom to be used.
|
|
9
8
|
*
|
|
10
9
|
* @returns The atom's value.
|
|
11
10
|
**/
|
|
12
|
-
const
|
|
13
|
-
exports.
|
|
14
|
-
/** Set an atom's value in the React 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;
|
|
11
|
+
const useAtom = (atom) => (0, react_1.useSyncExternalStore)(set => atom.subscribe(set), () => atom.get(), () => atom.get());
|
|
12
|
+
exports.useAtom = useAtom;
|
|
22
13
|
/** Use an atom's initialization state in the React lifecycle.
|
|
23
14
|
*
|
|
24
15
|
* @param atom Atom to be used.
|
|
@@ -35,16 +26,3 @@ const useAtomDidInit = (atom) => {
|
|
|
35
26
|
return didInit;
|
|
36
27
|
};
|
|
37
28
|
exports.useAtomDidInit = useAtomDidInit;
|
|
38
|
-
/** Use an atom's value and setter in the React lifecycle.
|
|
39
|
-
*
|
|
40
|
-
* @param atom Atom to be used.
|
|
41
|
-
*
|
|
42
|
-
* @returns [value, setValue, didInit]
|
|
43
|
-
**/
|
|
44
|
-
const useAtom = (atom) => {
|
|
45
|
-
const state = (0, exports.useAtomValue)(atom);
|
|
46
|
-
const setState = (0, exports.useSetAtom)(atom);
|
|
47
|
-
const didInit = (0, exports.useAtomDidInit)(atom);
|
|
48
|
-
return [state, setState, didInit];
|
|
49
|
-
};
|
|
50
|
-
exports.useAtom = useAtom;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useSelector = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const utils_1 = require("@yaasl/utils");
|
|
6
|
+
/** Compute a new value based on the state of an atom.
|
|
7
|
+
*
|
|
8
|
+
* @param atom Atom to be used.
|
|
9
|
+
* @param combiner Function to retrieve the new value.
|
|
10
|
+
* @param compare Function to compare the previous with a newer result. Defaults to a custom equality function.
|
|
11
|
+
*
|
|
12
|
+
* @returns The computed value.
|
|
13
|
+
**/
|
|
14
|
+
const useSelector = (atoms, combiner, compare) => {
|
|
15
|
+
const memoizedCombiner = (0, react_1.useRef)((0, utils_1.memoizeFunction)(combiner, compare));
|
|
16
|
+
(0, react_1.useEffect)(() => {
|
|
17
|
+
memoizedCombiner.current.resultFn = combiner;
|
|
18
|
+
memoizedCombiner.current.compareResult = compare;
|
|
19
|
+
});
|
|
20
|
+
const subscribe = (onStoreChange) => {
|
|
21
|
+
const unsubscribers = (0, utils_1.toArray)(atoms).map(atom => atom.subscribe(onStoreChange));
|
|
22
|
+
return () => unsubscribers.forEach(fn => fn());
|
|
23
|
+
};
|
|
24
|
+
const getSnapshot = () => {
|
|
25
|
+
const args = (0, utils_1.toArray)(atoms).map(atom => atom.get());
|
|
26
|
+
return memoizedCombiner.current(...args);
|
|
27
|
+
};
|
|
28
|
+
return (0, react_1.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
|
|
29
|
+
};
|
|
30
|
+
exports.useSelector = useSelector;
|
package/dist/esm/index.js
CHANGED
package/dist/esm/use-atom.js
CHANGED
|
@@ -1,19 +1,11 @@
|
|
|
1
|
-
import { useState, useEffect } from "react";
|
|
2
|
-
import { useSetStateful, useStatefulValue } from "./use-stateful";
|
|
1
|
+
import { useState, useEffect, useSyncExternalStore } from "react";
|
|
3
2
|
/** Use an atom's value in the React lifecycle.
|
|
4
3
|
*
|
|
5
4
|
* @param atom Atom to be used.
|
|
6
5
|
*
|
|
7
6
|
* @returns The atom's value.
|
|
8
7
|
**/
|
|
9
|
-
export const
|
|
10
|
-
/** Set an atom's value in the React 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);
|
|
8
|
+
export const useAtom = (atom) => useSyncExternalStore(set => atom.subscribe(set), () => atom.get(), () => atom.get());
|
|
17
9
|
/** Use an atom's initialization state in the React lifecycle.
|
|
18
10
|
*
|
|
19
11
|
* @param atom Atom to be used.
|
|
@@ -29,15 +21,3 @@ export const useAtomDidInit = (atom) => {
|
|
|
29
21
|
}, [atom.didInit]);
|
|
30
22
|
return didInit;
|
|
31
23
|
};
|
|
32
|
-
/** Use an atom's value and setter in the React lifecycle.
|
|
33
|
-
*
|
|
34
|
-
* @param atom Atom to be used.
|
|
35
|
-
*
|
|
36
|
-
* @returns [value, setValue, didInit]
|
|
37
|
-
**/
|
|
38
|
-
export const useAtom = (atom) => {
|
|
39
|
-
const state = useAtomValue(atom);
|
|
40
|
-
const setState = useSetAtom(atom);
|
|
41
|
-
const didInit = useAtomDidInit(atom);
|
|
42
|
-
return [state, setState, didInit];
|
|
43
|
-
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useEffect, useRef, useSyncExternalStore } from "react";
|
|
2
|
+
import { memoizeFunction, toArray, } from "@yaasl/utils";
|
|
3
|
+
/** Compute a new value based on the state of an atom.
|
|
4
|
+
*
|
|
5
|
+
* @param atom Atom to be used.
|
|
6
|
+
* @param combiner 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 const useSelector = (atoms, combiner, compare) => {
|
|
12
|
+
const memoizedCombiner = useRef(memoizeFunction(combiner, compare));
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
memoizedCombiner.current.resultFn = combiner;
|
|
15
|
+
memoizedCombiner.current.compareResult = compare;
|
|
16
|
+
});
|
|
17
|
+
const subscribe = (onStoreChange) => {
|
|
18
|
+
const unsubscribers = toArray(atoms).map(atom => atom.subscribe(onStoreChange));
|
|
19
|
+
return () => unsubscribers.forEach(fn => fn());
|
|
20
|
+
};
|
|
21
|
+
const getSnapshot = () => {
|
|
22
|
+
const args = toArray(atoms).map(atom => atom.get());
|
|
23
|
+
return memoizedCombiner.current(...args);
|
|
24
|
+
};
|
|
25
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
26
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaasl/react",
|
|
3
|
-
"version": "0.
|
|
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.
|
|
44
|
-
"@yaasl/utils": "^0.
|
|
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.
|
|
51
|
-
"@yaasl/utils": "^0.
|
|
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
|
},
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { SetStateAction } from "react";
|
|
2
|
-
import type { Stateful } from "@yaasl/core";
|
|
3
|
-
export declare const useStatefulValue: <ValueType>(stateful: Stateful<ValueType>) => ValueType;
|
|
4
|
-
export declare const useSetStateful: <ValueType>(stateful: Stateful<ValueType>) => (next: SetStateAction<ValueType>) => void;
|
package/dist/cjs/use-stateful.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useSetStateful = exports.useStatefulValue = void 0;
|
|
4
|
-
const react_1 = require("react");
|
|
5
|
-
const utils_1 = require("@yaasl/utils");
|
|
6
|
-
const useStatefulValue = (stateful) => (0, react_1.useSyncExternalStore)(set => stateful.subscribe(set), () => stateful.get(), () => stateful.get());
|
|
7
|
-
exports.useStatefulValue = useStatefulValue;
|
|
8
|
-
const useSetStateful = (stateful) => (0, react_1.useCallback)((next) => {
|
|
9
|
-
if (!("set" in stateful) || !(stateful.set instanceof Function)) {
|
|
10
|
-
throw new Error((0, utils_1.consoleMessage)("Atom does not have a set method"));
|
|
11
|
-
}
|
|
12
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
13
|
-
stateful.set(next);
|
|
14
|
-
}, [stateful]);
|
|
15
|
-
exports.useSetStateful = useSetStateful;
|
package/dist/esm/use-stateful.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { useCallback, useSyncExternalStore } from "react";
|
|
2
|
-
import { consoleMessage } from "@yaasl/utils";
|
|
3
|
-
export const useStatefulValue = (stateful) => useSyncExternalStore(set => stateful.subscribe(set), () => stateful.get(), () => stateful.get());
|
|
4
|
-
export const useSetStateful = (stateful) => useCallback((next) => {
|
|
5
|
-
if (!("set" in stateful) || !(stateful.set instanceof Function)) {
|
|
6
|
-
throw new Error(consoleMessage("Atom does not have a set method"));
|
|
7
|
-
}
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
9
|
-
stateful.set(next);
|
|
10
|
-
}, [stateful]);
|