@yaasl/react 0.11.0-alpha.1 → 0.11.0-alpha.3

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,29 +1,31 @@
1
1
  import type { Stateful } from "@yaasl/core";
2
- /** Use an atom's value in the react lifecycle.
2
+ /** Use an atom's value in the React lifecycle.
3
3
  *
4
4
  * @param atom Atom to be used.
5
+ * @param getServerSnapshot Function to retrieve a value that should be used for SSR.
5
6
  *
6
7
  * @returns The atom's value.
7
8
  **/
8
- export declare const useAtomValue: <ValueType>(atom: Stateful<ValueType>) => ValueType;
9
- /** Set an atom's value in the react lifecycle.
9
+ export declare const useAtomValue: <ValueType>(atom: Stateful<ValueType>, getServerSnapshot?: () => ValueType) => ValueType;
10
+ /** Set an atom's value in the React lifecycle.
10
11
  *
11
12
  * @param atom Atom to be used.
12
13
  *
13
14
  * @returns A setter function for the atom.
14
15
  **/
15
16
  export declare const useSetAtom: <ValueType>(atom: Stateful<ValueType>) => (next: import("react").SetStateAction<ValueType>) => void;
16
- /** Use an atom's initialization state in the react lifecycle.
17
+ /** Use an atom's initialization state in the React lifecycle.
17
18
  *
18
19
  * @param atom Atom to be used.
19
20
  *
20
21
  * @returns A boolean indicating if the atom has finished initializing yet.
21
22
  **/
22
23
  export declare const useAtomDidInit: <ValueType>(atom: Stateful<ValueType>) => boolean;
23
- /** Use an atom's value and setter in the react lifecycle.
24
+ /** Use an atom's value and setter in the React lifecycle.
24
25
  *
25
26
  * @param atom Atom to be used.
27
+ * @param getServerSnapshot Function to retrieve a value that should be used for SSR.
26
28
  *
27
29
  * @returns [value, setValue, didInit]
28
30
  **/
29
- export declare const useAtom: <ValueType>(atom: Stateful<ValueType>) => readonly [ValueType, (next: import("react").SetStateAction<ValueType>) => void, boolean];
31
+ export declare const useAtom: <ValueType>(atom: Stateful<ValueType>, getServerSnapshot?: () => ValueType) => readonly [ValueType, (next: import("react").SetStateAction<ValueType>) => void, boolean];
@@ -1,4 +1,4 @@
1
1
  import { SetStateAction } from "react";
2
- import type { Stateful } from "@yaasl/core";
3
- export declare const useStatefulValue: <ValueType>(stateful: Stateful<ValueType>) => ValueType;
2
+ import { Stateful } from "@yaasl/core";
3
+ export declare const useStatefulValue: <ValueType>(stateful: Stateful<ValueType>, getServerSnapshot?: () => ValueType) => ValueType;
4
4
  export declare const useSetStateful: <ValueType>(stateful: Stateful<ValueType>) => (next: SetStateAction<ValueType>) => void;
@@ -3,15 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useAtom = exports.useAtomDidInit = exports.useSetAtom = exports.useAtomValue = void 0;
4
4
  const react_1 = require("react");
5
5
  const useStateful_1 = require("./useStateful");
6
- /** Use an atom's value in the react lifecycle.
6
+ /** Use an atom's value in the React lifecycle.
7
7
  *
8
8
  * @param atom Atom to be used.
9
+ * @param getServerSnapshot Function to retrieve a value that should be used for SSR.
9
10
  *
10
11
  * @returns The atom's value.
11
12
  **/
12
- const useAtomValue = (atom) => (0, useStateful_1.useStatefulValue)(atom);
13
+ const useAtomValue = (atom, getServerSnapshot) => (0, useStateful_1.useStatefulValue)(atom, getServerSnapshot);
13
14
  exports.useAtomValue = useAtomValue;
14
- /** Set an atom's value in the react lifecycle.
15
+ /** Set an atom's value in the React lifecycle.
15
16
  *
16
17
  * @param atom Atom to be used.
17
18
  *
@@ -19,7 +20,7 @@ exports.useAtomValue = useAtomValue;
19
20
  **/
20
21
  const useSetAtom = (atom) => (0, useStateful_1.useSetStateful)(atom);
21
22
  exports.useSetAtom = useSetAtom;
22
- /** Use an atom's initialization state in the react lifecycle.
23
+ /** Use an atom's initialization state in the React lifecycle.
23
24
  *
24
25
  * @param atom Atom to be used.
25
26
  *
@@ -35,14 +36,15 @@ const useAtomDidInit = (atom) => {
35
36
  return didInit;
36
37
  };
37
38
  exports.useAtomDidInit = useAtomDidInit;
38
- /** Use an atom's value and setter in the react lifecycle.
39
+ /** Use an atom's value and setter in the React lifecycle.
39
40
  *
40
41
  * @param atom Atom to be used.
42
+ * @param getServerSnapshot Function to retrieve a value that should be used for SSR.
41
43
  *
42
44
  * @returns [value, setValue, didInit]
43
45
  **/
44
- const useAtom = (atom) => {
45
- const state = (0, exports.useAtomValue)(atom);
46
+ const useAtom = (atom, getServerSnapshot) => {
47
+ const state = (0, exports.useAtomValue)(atom, getServerSnapshot);
46
48
  const setState = (0, exports.useSetAtom)(atom);
47
49
  const didInit = (0, exports.useAtomDidInit)(atom);
48
50
  return [state, setState, didInit];
@@ -3,12 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useSetStateful = exports.useStatefulValue = void 0;
4
4
  const react_1 = require("react");
5
5
  const utils_1 = require("@yaasl/utils");
6
- const useStatefulValue = (stateful) => (0, react_1.useSyncExternalStore)(set => stateful.subscribe(set), () => stateful.get());
6
+ const useStatefulValue = (stateful, getServerSnapshot = () => stateful.get()) => (0, react_1.useSyncExternalStore)(set => stateful.subscribe(set), () => stateful.get(), getServerSnapshot);
7
7
  exports.useStatefulValue = useStatefulValue;
8
8
  const useSetStateful = (stateful) => (0, react_1.useCallback)((next) => {
9
9
  if (!("set" in stateful) || !(stateful.set instanceof Function)) {
10
10
  throw new Error((0, utils_1.consoleMessage)("Atom does not have a set method"));
11
11
  }
12
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
12
13
  stateful.set(next);
13
14
  }, [stateful]);
14
15
  exports.useSetStateful = useSetStateful;
@@ -1,20 +1,21 @@
1
1
  import { useState, useEffect } from "react";
2
2
  import { useSetStateful, useStatefulValue } from "./useStateful";
3
- /** Use an atom's value in the react lifecycle.
3
+ /** Use an atom's value in the React lifecycle.
4
4
  *
5
5
  * @param atom Atom to be used.
6
+ * @param getServerSnapshot Function to retrieve a value that should be used for SSR.
6
7
  *
7
8
  * @returns The atom's value.
8
9
  **/
9
- export const useAtomValue = (atom) => useStatefulValue(atom);
10
- /** Set an atom's value in the react lifecycle.
10
+ export const useAtomValue = (atom, getServerSnapshot) => useStatefulValue(atom, getServerSnapshot);
11
+ /** Set an atom's value in the React lifecycle.
11
12
  *
12
13
  * @param atom Atom to be used.
13
14
  *
14
15
  * @returns A setter function for the atom.
15
16
  **/
16
17
  export const useSetAtom = (atom) => useSetStateful(atom);
17
- /** Use an atom's initialization state in the react lifecycle.
18
+ /** Use an atom's initialization state in the React lifecycle.
18
19
  *
19
20
  * @param atom Atom to be used.
20
21
  *
@@ -29,14 +30,15 @@ export const useAtomDidInit = (atom) => {
29
30
  }, [atom.didInit]);
30
31
  return didInit;
31
32
  };
32
- /** Use an atom's value and setter in the react lifecycle.
33
+ /** Use an atom's value and setter in the React lifecycle.
33
34
  *
34
35
  * @param atom Atom to be used.
36
+ * @param getServerSnapshot Function to retrieve a value that should be used for SSR.
35
37
  *
36
38
  * @returns [value, setValue, didInit]
37
39
  **/
38
- export const useAtom = (atom) => {
39
- const state = useAtomValue(atom);
40
+ export const useAtom = (atom, getServerSnapshot) => {
41
+ const state = useAtomValue(atom, getServerSnapshot);
40
42
  const setState = useSetAtom(atom);
41
43
  const didInit = useAtomDidInit(atom);
42
44
  return [state, setState, didInit];
@@ -1,9 +1,10 @@
1
1
  import { useCallback, useSyncExternalStore } from "react";
2
2
  import { consoleMessage } from "@yaasl/utils";
3
- export const useStatefulValue = (stateful) => useSyncExternalStore(set => stateful.subscribe(set), () => stateful.get());
3
+ export const useStatefulValue = (stateful, getServerSnapshot = () => stateful.get()) => useSyncExternalStore(set => stateful.subscribe(set), () => stateful.get(), getServerSnapshot);
4
4
  export const useSetStateful = (stateful) => useCallback((next) => {
5
5
  if (!("set" in stateful) || !(stateful.set instanceof Function)) {
6
6
  throw new Error(consoleMessage("Atom does not have a set method"));
7
7
  }
8
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
8
9
  stateful.set(next);
9
10
  }, [stateful]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaasl/react",
3
- "version": "0.11.0-alpha.1",
3
+ "version": "0.11.0-alpha.3",
4
4
  "description": "yet another atomic store library (react)",
5
5
  "author": "PrettyCoffee",
6
6
  "license": "MIT",
@@ -40,23 +40,18 @@
40
40
  "validate": "run-s lint test build"
41
41
  },
42
42
  "peerDependencies": {
43
- "react": ">=17"
43
+ "react": "^18.0.0 || ^19.0.0"
44
44
  },
45
45
  "dependencies": {
46
- "@yaasl/core": "0.11.0-alpha.1",
47
- "@yaasl/utils": "0.11.0-alpha.1"
46
+ "@yaasl/core": "0.11.0-alpha.3",
47
+ "@yaasl/utils": "0.11.0-alpha.3"
48
48
  },
49
49
  "devDependencies": {
50
- "@testing-library/react": "^14.3.1",
51
- "@types/react": "^18.3.12",
50
+ "@testing-library/react": "^16.2.0",
51
+ "@types/react": "^18.3.18",
52
+ "react-dom": "^18.3.1",
52
53
  "react": "^18.3.1"
53
54
  },
54
- "eslintConfig": {
55
- "extends": [
56
- "../../.eslintrc",
57
- "@pretty-cozy/eslint-config/react"
58
- ]
59
- },
60
55
  "lint-staged": {
61
56
  "*.{ts,tsx}": [
62
57
  "npm run lint:fix"
package/tsconfig.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- }
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "include": ["./src"],
4
- "exclude": ["./**/*.test.ts"],
5
- "compilerOptions": {
6
- "declaration": false,
7
- "noEmit": false,
8
- }
9
- }