ccstate-react 4.9.0 → 4.11.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/CHANGELOG.md +21 -0
- package/dist/index.cjs +9 -2
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +9 -2
- package/package.json +3 -3
- package/src/__tests__/loadable.test.tsx +13 -0
- package/src/__tests__/resolved.test.tsx +13 -0
- package/src/useLoadable.ts +23 -8
- package/src/useResolved.ts +6 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# ccstate-react
|
|
2
2
|
|
|
3
|
+
## 4.11.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 81795a2: remove asyncGetSettled$
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- ccstate@4.11.0
|
|
12
|
+
|
|
13
|
+
## 4.10.0
|
|
14
|
+
|
|
15
|
+
### Minor Changes
|
|
16
|
+
|
|
17
|
+
- e00d4af: feat: useLoadable support non-promise signals
|
|
18
|
+
- e00d4af: feat: collect floating promise in async computed
|
|
19
|
+
|
|
20
|
+
### Patch Changes
|
|
21
|
+
|
|
22
|
+
- ccstate@4.10.0
|
|
23
|
+
|
|
3
24
|
## 4.9.0
|
|
4
25
|
|
|
5
26
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -95,6 +95,13 @@ function useLoadableInternal(atom, keepLastResolved) {
|
|
|
95
95
|
promiseResult = _useState2[0],
|
|
96
96
|
setPromiseResult = _useState2[1];
|
|
97
97
|
react.useEffect(function () {
|
|
98
|
+
if (!(promise instanceof Promise)) {
|
|
99
|
+
setPromiseResult({
|
|
100
|
+
state: 'hasData',
|
|
101
|
+
data: promise
|
|
102
|
+
});
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
98
105
|
var ctrl = new AbortController();
|
|
99
106
|
var signal = ctrl.signal;
|
|
100
107
|
if (!keepLastResolved) {
|
|
@@ -102,13 +109,13 @@ function useLoadableInternal(atom, keepLastResolved) {
|
|
|
102
109
|
state: 'loading'
|
|
103
110
|
});
|
|
104
111
|
}
|
|
105
|
-
|
|
112
|
+
promise.then(function (ret) {
|
|
106
113
|
if (signal.aborted) return;
|
|
107
114
|
setPromiseResult({
|
|
108
115
|
state: 'hasData',
|
|
109
116
|
data: ret
|
|
110
117
|
});
|
|
111
|
-
}
|
|
118
|
+
}, function (error) {
|
|
112
119
|
if (signal.aborted) return;
|
|
113
120
|
setPromiseResult({
|
|
114
121
|
state: 'hasError',
|
package/dist/index.d.cts
CHANGED
|
@@ -8,8 +8,8 @@ type CommandInvoker<T, CommandArgs extends unknown[]> = (...args: CommandArgs) =
|
|
|
8
8
|
declare function useSet<T>(signal: State<T>): ValueSetter<T>;
|
|
9
9
|
declare function useSet<T, CommandArgs extends unknown[]>(signal: Command<T, CommandArgs>): CommandInvoker<T, CommandArgs>;
|
|
10
10
|
|
|
11
|
-
declare function useResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
|
|
12
|
-
declare function useLastResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
|
|
11
|
+
declare function useResolved<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Awaited<T> | undefined;
|
|
12
|
+
declare function useLastResolved<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Awaited<T> | undefined;
|
|
13
13
|
|
|
14
14
|
type Loadable<T> = {
|
|
15
15
|
state: 'loading';
|
|
@@ -20,8 +20,8 @@ type Loadable<T> = {
|
|
|
20
20
|
state: 'hasError';
|
|
21
21
|
error: unknown;
|
|
22
22
|
};
|
|
23
|
-
declare function useLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T
|
|
24
|
-
declare function useLastLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T
|
|
23
|
+
declare function useLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<Awaited<T>>;
|
|
24
|
+
declare function useLastLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<Awaited<T>>;
|
|
25
25
|
|
|
26
26
|
declare const StoreProvider: react.Provider<Store | null>;
|
|
27
27
|
|
package/dist/index.d.ts
CHANGED
|
@@ -8,8 +8,8 @@ type CommandInvoker<T, CommandArgs extends unknown[]> = (...args: CommandArgs) =
|
|
|
8
8
|
declare function useSet<T>(signal: State<T>): ValueSetter<T>;
|
|
9
9
|
declare function useSet<T, CommandArgs extends unknown[]>(signal: Command<T, CommandArgs>): CommandInvoker<T, CommandArgs>;
|
|
10
10
|
|
|
11
|
-
declare function useResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
|
|
12
|
-
declare function useLastResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): T | undefined;
|
|
11
|
+
declare function useResolved<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Awaited<T> | undefined;
|
|
12
|
+
declare function useLastResolved<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Awaited<T> | undefined;
|
|
13
13
|
|
|
14
14
|
type Loadable<T> = {
|
|
15
15
|
state: 'loading';
|
|
@@ -20,8 +20,8 @@ type Loadable<T> = {
|
|
|
20
20
|
state: 'hasError';
|
|
21
21
|
error: unknown;
|
|
22
22
|
};
|
|
23
|
-
declare function useLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T
|
|
24
|
-
declare function useLastLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Loadable<T
|
|
23
|
+
declare function useLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<Awaited<T>>;
|
|
24
|
+
declare function useLastLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<Awaited<T>>;
|
|
25
25
|
|
|
26
26
|
declare const StoreProvider: react.Provider<Store | null>;
|
|
27
27
|
|
package/dist/index.js
CHANGED
|
@@ -93,6 +93,13 @@ function useLoadableInternal(atom, keepLastResolved) {
|
|
|
93
93
|
promiseResult = _useState2[0],
|
|
94
94
|
setPromiseResult = _useState2[1];
|
|
95
95
|
useEffect(function () {
|
|
96
|
+
if (!(promise instanceof Promise)) {
|
|
97
|
+
setPromiseResult({
|
|
98
|
+
state: 'hasData',
|
|
99
|
+
data: promise
|
|
100
|
+
});
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
96
103
|
var ctrl = new AbortController();
|
|
97
104
|
var signal = ctrl.signal;
|
|
98
105
|
if (!keepLastResolved) {
|
|
@@ -100,13 +107,13 @@ function useLoadableInternal(atom, keepLastResolved) {
|
|
|
100
107
|
state: 'loading'
|
|
101
108
|
});
|
|
102
109
|
}
|
|
103
|
-
|
|
110
|
+
promise.then(function (ret) {
|
|
104
111
|
if (signal.aborted) return;
|
|
105
112
|
setPromiseResult({
|
|
106
113
|
state: 'hasData',
|
|
107
114
|
data: ret
|
|
108
115
|
});
|
|
109
|
-
}
|
|
116
|
+
}, function (error) {
|
|
110
117
|
if (signal.aborted) return;
|
|
111
118
|
setPromiseResult({
|
|
112
119
|
state: 'hasError',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ccstate-react",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.11.0",
|
|
4
4
|
"description": "CCState React Hooks",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"react": ">=17.0.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"ccstate": "^4.
|
|
28
|
+
"ccstate": "^4.11.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependenciesMeta": {
|
|
31
31
|
"@types/react": {
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"shx": "^0.3.4",
|
|
56
56
|
"signal-timers": "^1.0.4",
|
|
57
57
|
"vitest": "^2.1.8",
|
|
58
|
-
"ccstate": "^4.
|
|
58
|
+
"ccstate": "^4.11.0"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"build": "rollup -c",
|
|
@@ -530,3 +530,16 @@ it('use lastLoadable will will not use old promise error if new promise is made'
|
|
|
530
530
|
await delay(0);
|
|
531
531
|
expect(screen.getByText('num2')).toBeInTheDocument();
|
|
532
532
|
});
|
|
533
|
+
|
|
534
|
+
it('useLoadable accept sync computed', async () => {
|
|
535
|
+
const base$ = state(0);
|
|
536
|
+
function App() {
|
|
537
|
+
const base = useLoadable(base$);
|
|
538
|
+
|
|
539
|
+
return <div>{base.state}</div>;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
render(<App />);
|
|
543
|
+
|
|
544
|
+
expect(await screen.findByText('hasData')).toBeInTheDocument();
|
|
545
|
+
});
|
|
@@ -100,3 +100,16 @@ it('use lastLoadable should not update when new promise pending', async () => {
|
|
|
100
100
|
await delay(0);
|
|
101
101
|
expect(screen.getByText('num2')).toBeInTheDocument();
|
|
102
102
|
});
|
|
103
|
+
|
|
104
|
+
it('useResolved accept sync computed', async () => {
|
|
105
|
+
const base$ = state(0);
|
|
106
|
+
function App() {
|
|
107
|
+
const base = useResolved(base$);
|
|
108
|
+
|
|
109
|
+
return <div>{base}</div>;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
render(<App />);
|
|
113
|
+
|
|
114
|
+
expect(await screen.findByText('0')).toBeInTheDocument();
|
|
115
|
+
});
|
package/src/useLoadable.ts
CHANGED
|
@@ -16,15 +16,25 @@ type Loadable<T> =
|
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
function useLoadableInternal<T>(
|
|
19
|
-
atom: State<Promise<T
|
|
19
|
+
atom: State<Promise<T> | T> | Computed<Promise<T> | T>,
|
|
20
20
|
keepLastResolved: boolean,
|
|
21
21
|
): Loadable<T> {
|
|
22
22
|
const promise = useGet(atom);
|
|
23
|
+
|
|
23
24
|
const [promiseResult, setPromiseResult] = useState<Loadable<T>>({
|
|
24
25
|
state: 'loading',
|
|
25
26
|
});
|
|
26
27
|
|
|
27
28
|
useEffect(() => {
|
|
29
|
+
if (!(promise instanceof Promise)) {
|
|
30
|
+
setPromiseResult({
|
|
31
|
+
state: 'hasData',
|
|
32
|
+
data: promise,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
28
38
|
const ctrl = new AbortController();
|
|
29
39
|
const signal = ctrl.signal;
|
|
30
40
|
|
|
@@ -34,23 +44,24 @@ function useLoadableInternal<T>(
|
|
|
34
44
|
});
|
|
35
45
|
}
|
|
36
46
|
|
|
37
|
-
|
|
38
|
-
|
|
47
|
+
promise.then(
|
|
48
|
+
(ret) => {
|
|
39
49
|
if (signal.aborted) return;
|
|
40
50
|
|
|
41
51
|
setPromiseResult({
|
|
42
52
|
state: 'hasData',
|
|
43
53
|
data: ret,
|
|
44
54
|
});
|
|
45
|
-
}
|
|
46
|
-
|
|
55
|
+
},
|
|
56
|
+
(error: unknown) => {
|
|
47
57
|
if (signal.aborted) return;
|
|
48
58
|
|
|
49
59
|
setPromiseResult({
|
|
50
60
|
state: 'hasError',
|
|
51
61
|
error,
|
|
52
62
|
});
|
|
53
|
-
}
|
|
63
|
+
},
|
|
64
|
+
);
|
|
54
65
|
|
|
55
66
|
return () => {
|
|
56
67
|
ctrl.abort();
|
|
@@ -60,10 +71,14 @@ function useLoadableInternal<T>(
|
|
|
60
71
|
return promiseResult;
|
|
61
72
|
}
|
|
62
73
|
|
|
63
|
-
export function useLoadable<T>(
|
|
74
|
+
export function useLoadable<T>(
|
|
75
|
+
atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>,
|
|
76
|
+
): Loadable<Awaited<T>> {
|
|
64
77
|
return useLoadableInternal(atom, false);
|
|
65
78
|
}
|
|
66
79
|
|
|
67
|
-
export function useLastLoadable<T>(
|
|
80
|
+
export function useLastLoadable<T>(
|
|
81
|
+
atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>,
|
|
82
|
+
): Loadable<Awaited<T>> {
|
|
68
83
|
return useLoadableInternal(atom, true);
|
|
69
84
|
}
|
package/src/useResolved.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { useLastLoadable, useLoadable } from './useLoadable';
|
|
2
2
|
import type { Computed, State } from 'ccstate';
|
|
3
3
|
|
|
4
|
-
export function useResolved<T>(
|
|
4
|
+
export function useResolved<T>(
|
|
5
|
+
atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>,
|
|
6
|
+
): Awaited<T> | undefined {
|
|
5
7
|
const loadable = useLoadable(atom);
|
|
6
8
|
return loadable.state === 'hasData' ? loadable.data : undefined;
|
|
7
9
|
}
|
|
8
10
|
|
|
9
|
-
export function useLastResolved<T>(
|
|
11
|
+
export function useLastResolved<T>(
|
|
12
|
+
atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>,
|
|
13
|
+
): Awaited<T> | undefined {
|
|
10
14
|
const loadable = useLastLoadable(atom);
|
|
11
15
|
return loadable.state === 'hasData' ? loadable.data : undefined;
|
|
12
16
|
}
|