ccstate-react 3.0.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/src/useGet.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { useSyncExternalStore } from 'react';
2
+ import { useStore } from './provider';
3
+ import { command } from 'ccstate';
4
+ import type { Computed, State } from 'ccstate';
5
+
6
+ export function useGet<T>(atom: State<T> | Computed<T>) {
7
+ const store = useStore();
8
+ return useSyncExternalStore(
9
+ (fn) => {
10
+ const ctrl = new AbortController();
11
+ store.sub(atom, command(fn), { signal: ctrl.signal });
12
+ return () => {
13
+ ctrl.abort();
14
+ };
15
+ },
16
+ () => {
17
+ return store.get(atom);
18
+ },
19
+ );
20
+ }
@@ -0,0 +1,69 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { useGet } from './useGet';
3
+ import type { Computed, State } from 'ccstate';
4
+
5
+ type Loadable<T> =
6
+ | {
7
+ state: 'loading';
8
+ }
9
+ | {
10
+ state: 'hasData';
11
+ data: T;
12
+ }
13
+ | {
14
+ state: 'hasError';
15
+ error: unknown;
16
+ };
17
+
18
+ function useLoadableInternal<T>(
19
+ atom: State<Promise<T>> | Computed<Promise<T>>,
20
+ keepLastResolved: boolean,
21
+ ): Loadable<T> {
22
+ const promise = useGet(atom);
23
+ const [promiseResult, setPromiseResult] = useState<Loadable<T>>({
24
+ state: 'loading',
25
+ });
26
+
27
+ useEffect(() => {
28
+ const ctrl = new AbortController();
29
+ const signal = ctrl.signal;
30
+
31
+ if (!keepLastResolved) {
32
+ setPromiseResult({
33
+ state: 'loading',
34
+ });
35
+ }
36
+
37
+ void promise
38
+ .then((ret) => {
39
+ if (signal.aborted) return;
40
+
41
+ setPromiseResult({
42
+ state: 'hasData',
43
+ data: ret,
44
+ });
45
+ })
46
+ .catch((error: unknown) => {
47
+ if (signal.aborted) return;
48
+
49
+ setPromiseResult({
50
+ state: 'hasError',
51
+ error,
52
+ });
53
+ });
54
+
55
+ return () => {
56
+ ctrl.abort();
57
+ };
58
+ }, [promise]);
59
+
60
+ return promiseResult;
61
+ }
62
+
63
+ export function useLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T> {
64
+ return useLoadableInternal(atom, false);
65
+ }
66
+
67
+ export function useLastLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T> {
68
+ return useLoadableInternal(atom, true);
69
+ }
@@ -0,0 +1,12 @@
1
+ import { useLastLoadable, useLoadable } from './useLoadable';
2
+ import type { Computed, State } from 'ccstate';
3
+
4
+ export function useResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined {
5
+ const loadable = useLoadable(atom);
6
+ return loadable.state === 'hasData' ? loadable.data : undefined;
7
+ }
8
+
9
+ export function useLastResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined {
10
+ const loadable = useLastLoadable(atom);
11
+ return loadable.state === 'hasData' ? loadable.data : undefined;
12
+ }
package/src/useSet.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { useStore } from './provider';
2
+ import type { Command, Updater, State } from 'ccstate';
3
+
4
+ export function useSet<T>(atom: State<T>): (value: T | Updater<T>) => void;
5
+ export function useSet<T, ARGS extends unknown[]>(atom: Command<T, ARGS>): (...args: ARGS) => T;
6
+ export function useSet<T, ARGS extends unknown[]>(
7
+ atom: State<T> | Command<T, ARGS>,
8
+ ): ((value: T | Updater<T>) => void) | ((...args: ARGS) => T) {
9
+ const store = useStore();
10
+
11
+ if ('write' in atom) {
12
+ return (...args: ARGS): T => {
13
+ const ret = store.set(atom, ...args);
14
+
15
+ return ret;
16
+ };
17
+ }
18
+
19
+ return (value: T | Updater<T>) => {
20
+ store.set(atom, value);
21
+ };
22
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.options.json",
3
+ "compilerOptions": {
4
+ "lib": ["ES2020", "DOM"],
5
+ "jsx": "react-jsx"
6
+ },
7
+ "include": ["src/**/*", "vitest.config.ts"]
8
+ }
@@ -0,0 +1,3 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({});