@yaasl/react 0.7.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.
- package/dist/@types/index.d.ts +3 -0
- package/dist/@types/useAtom.d.ts +32 -0
- package/dist/@types/useDerivedValue.d.ts +8 -0
- package/dist/@types/useStatefulValue.d.ts +2 -0
- package/dist/cjs/index.js +25 -0
- package/dist/cjs/useAtom.js +52 -0
- package/dist/cjs/useDerivedValue.js +12 -0
- package/dist/cjs/useStatefulValue.js +6 -0
- package/dist/mjs/index.js +3 -0
- package/dist/mjs/useAtom.js +45 -0
- package/dist/mjs/useDerivedValue.js +8 -0
- package/dist/mjs/useStatefulValue.js +2 -0
- package/package.json +66 -0
- package/tsconfig.json +3 -0
- package/tsconfig.node.json +9 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { SetStateAction } from "react";
|
|
2
|
+
import { Atom, Stateful } from "@yaasl/core";
|
|
3
|
+
/** Use an atom's value in the react lifecycle.
|
|
4
|
+
*
|
|
5
|
+
* @param atom Atom to be used.
|
|
6
|
+
*
|
|
7
|
+
* @returns A stateful value.
|
|
8
|
+
**/
|
|
9
|
+
export declare const useAtomValue: <ValueType>(atom: Stateful<ValueType>) => ValueType;
|
|
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 declare const useSetAtom: <ValueType>(atom: Atom<ValueType>) => (next: SetStateAction<ValueType>) => void;
|
|
17
|
+
/** Use an atom's initialization state in the react lifecycle.
|
|
18
|
+
*
|
|
19
|
+
* @param atom Atom to be used.
|
|
20
|
+
*
|
|
21
|
+
* @returns A boolean indicating if the atom has finished initializing yet.
|
|
22
|
+
**/
|
|
23
|
+
export declare const useAtomDidInit: <ValueType>(atom: Atom<ValueType>) => boolean;
|
|
24
|
+
/** Use an atom's value and setter in the react 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: Atom<ValueType>) => readonly [ValueType, (next: SetStateAction<ValueType>) => void, boolean];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Derive } from "@yaasl/core";
|
|
2
|
+
/** Use a derived value in the react lifecycle.
|
|
3
|
+
*
|
|
4
|
+
* @param derived Derived instance to be used.
|
|
5
|
+
*
|
|
6
|
+
* @returns A stateful value.
|
|
7
|
+
**/
|
|
8
|
+
export declare const useDerivedValue: <ValueType>(derived: Derive<ValueType>) => ValueType;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.useDerivedValue = exports.useAtomDidInit = exports.useAtomValue = exports.useSetAtom = exports.useAtom = void 0;
|
|
18
|
+
var useAtom_1 = require("./useAtom");
|
|
19
|
+
Object.defineProperty(exports, "useAtom", { enumerable: true, get: function () { return useAtom_1.useAtom; } });
|
|
20
|
+
Object.defineProperty(exports, "useSetAtom", { enumerable: true, get: function () { return useAtom_1.useSetAtom; } });
|
|
21
|
+
Object.defineProperty(exports, "useAtomValue", { enumerable: true, get: function () { return useAtom_1.useAtomValue; } });
|
|
22
|
+
Object.defineProperty(exports, "useAtomDidInit", { enumerable: true, get: function () { return useAtom_1.useAtomDidInit; } });
|
|
23
|
+
var useDerivedValue_1 = require("./useDerivedValue");
|
|
24
|
+
Object.defineProperty(exports, "useDerivedValue", { enumerable: true, get: function () { return useDerivedValue_1.useDerivedValue; } });
|
|
25
|
+
__exportStar(require("@yaasl/core"), exports);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useAtom = exports.useAtomDidInit = exports.useSetAtom = exports.useAtomValue = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const useStatefulValue_1 = require("./useStatefulValue");
|
|
6
|
+
/** Use an atom's value in the react lifecycle.
|
|
7
|
+
*
|
|
8
|
+
* @param atom Atom to be used.
|
|
9
|
+
*
|
|
10
|
+
* @returns A stateful value.
|
|
11
|
+
**/
|
|
12
|
+
const useAtomValue = (atom) => (0, useStatefulValue_1.useStatefulValue)(atom);
|
|
13
|
+
exports.useAtomValue = useAtomValue;
|
|
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, react_1.useCallback)((next) => atom.set(next), [atom]);
|
|
21
|
+
exports.useSetAtom = useSetAtom;
|
|
22
|
+
/** Use an atom's initialization state in the react lifecycle.
|
|
23
|
+
*
|
|
24
|
+
* @param atom Atom to be used.
|
|
25
|
+
*
|
|
26
|
+
* @returns A boolean indicating if the atom has finished initializing yet.
|
|
27
|
+
**/
|
|
28
|
+
const useAtomDidInit = (atom) => {
|
|
29
|
+
const [didInit, setDidInit] = (0, react_1.useState)(atom.didInit === true);
|
|
30
|
+
(0, react_1.useEffect)(() => {
|
|
31
|
+
if (typeof atom.didInit === "boolean")
|
|
32
|
+
return;
|
|
33
|
+
void atom.didInit.then(() => setDidInit(true));
|
|
34
|
+
}, [atom.didInit]);
|
|
35
|
+
return didInit;
|
|
36
|
+
};
|
|
37
|
+
exports.useAtomDidInit = useAtomDidInit;
|
|
38
|
+
/** Use an atom's value and setter in the react 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,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useDerivedValue = void 0;
|
|
4
|
+
const useStatefulValue_1 = require("./useStatefulValue");
|
|
5
|
+
/** Use a derived value in the react lifecycle.
|
|
6
|
+
*
|
|
7
|
+
* @param derived Derived instance to be used.
|
|
8
|
+
*
|
|
9
|
+
* @returns A stateful value.
|
|
10
|
+
**/
|
|
11
|
+
const useDerivedValue = (derived) => (0, useStatefulValue_1.useStatefulValue)(derived);
|
|
12
|
+
exports.useDerivedValue = useDerivedValue;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useStatefulValue = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const useStatefulValue = (atom) => (0, react_1.useSyncExternalStore)(set => atom.subscribe(set), () => atom.get());
|
|
6
|
+
exports.useStatefulValue = useStatefulValue;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useCallback, useState, useEffect } from "react";
|
|
2
|
+
import { useStatefulValue } from "./useStatefulValue";
|
|
3
|
+
/** Use an atom's value in the react lifecycle.
|
|
4
|
+
*
|
|
5
|
+
* @param atom Atom to be used.
|
|
6
|
+
*
|
|
7
|
+
* @returns A stateful value.
|
|
8
|
+
**/
|
|
9
|
+
export const useAtomValue = (atom) => useStatefulValue(atom);
|
|
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) => useCallback((next) => atom.set(next), [atom]);
|
|
17
|
+
/** Use an atom's initialization state in the react lifecycle.
|
|
18
|
+
*
|
|
19
|
+
* @param atom Atom to be used.
|
|
20
|
+
*
|
|
21
|
+
* @returns A boolean indicating if the atom has finished initializing yet.
|
|
22
|
+
**/
|
|
23
|
+
export const useAtomDidInit = (atom) => {
|
|
24
|
+
const [didInit, setDidInit] = useState(atom.didInit === true);
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (typeof atom.didInit === "boolean")
|
|
27
|
+
return;
|
|
28
|
+
void atom.didInit.then(() => setDidInit(true));
|
|
29
|
+
}, [atom.didInit]);
|
|
30
|
+
return didInit;
|
|
31
|
+
};
|
|
32
|
+
/** Use an atom's value and setter in the react 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,8 @@
|
|
|
1
|
+
import { useStatefulValue } from "./useStatefulValue";
|
|
2
|
+
/** Use a derived value in the react lifecycle.
|
|
3
|
+
*
|
|
4
|
+
* @param derived Derived instance to be used.
|
|
5
|
+
*
|
|
6
|
+
* @returns A stateful value.
|
|
7
|
+
**/
|
|
8
|
+
export const useDerivedValue = (derived) => useStatefulValue(derived);
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yaasl/react",
|
|
3
|
+
"version": "0.7.0-alpha.0",
|
|
4
|
+
"description": "yet another atomic store library (react)",
|
|
5
|
+
"author": "PrettyCoffee",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/PrettyCoffee/yaasl#readme",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"atom",
|
|
10
|
+
"store",
|
|
11
|
+
"react"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/PrettyCoffee/yaasl.git"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/PrettyCoffee/yaasl/issues"
|
|
19
|
+
},
|
|
20
|
+
"main": "./dist/cjs/index.js",
|
|
21
|
+
"types": "./dist/@types/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
"types": "./dist/@types/index.d.ts",
|
|
24
|
+
"import": "./dist/mjs/index.js",
|
|
25
|
+
"require": "./dist/cjs/index.js"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "run-s clean compile",
|
|
29
|
+
"clean": "rimraf ./dist",
|
|
30
|
+
"compile": "run-p compile:mjs compile:cjs compile:types",
|
|
31
|
+
"compile:mjs": "tsc -p ./tsconfig.node.json --outDir ./dist/mjs -m es2020 -t es2020",
|
|
32
|
+
"compile:cjs": "tsc -p ./tsconfig.node.json --outDir ./dist/cjs -m commonjs -t es2015",
|
|
33
|
+
"compile:types": "tsc -p ./tsconfig.node.json --outDir ./dist/@types --declaration --emitDeclarationOnly",
|
|
34
|
+
"test": "jest --testMatch ./**/*.test.*",
|
|
35
|
+
"test:watch": "npm run test -- --watchAll",
|
|
36
|
+
"lint": "eslint ./src",
|
|
37
|
+
"lint:fix": "eslint ./src --fix",
|
|
38
|
+
"validate": "run-s lint test build"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"react": ">=17"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@yaasl/core": "0.7.0-alpha.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@testing-library/react": "^14.2.1",
|
|
48
|
+
"@types/react": "^18.2.54",
|
|
49
|
+
"react": "^18.2.0"
|
|
50
|
+
},
|
|
51
|
+
"jest": {
|
|
52
|
+
"preset": "ts-jest",
|
|
53
|
+
"testEnvironment": "jsdom"
|
|
54
|
+
},
|
|
55
|
+
"eslintConfig": {
|
|
56
|
+
"extends": [
|
|
57
|
+
"../../.eslintrc",
|
|
58
|
+
"@pretty-cozy/eslint-config/react"
|
|
59
|
+
]
|
|
60
|
+
},
|
|
61
|
+
"lint-staged": {
|
|
62
|
+
"*.{ts,tsx}": [
|
|
63
|
+
"npm run lint:fix"
|
|
64
|
+
]
|
|
65
|
+
}
|
|
66
|
+
}
|
package/tsconfig.json
ADDED