ccstate-react 4.13.0 → 5.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/CHANGELOG.md +17 -0
- package/dist/experimental.cjs +0 -20
- package/dist/experimental.d.cts +2 -3
- package/dist/experimental.d.ts +2 -3
- package/dist/experimental.js +3 -22
- package/dist/index.cjs +56 -125
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +57 -126
- package/package.json +3 -3
- package/src/__tests__/get-and-set.test.tsx +22 -13
- package/src/__tests__/inline-atom.test.tsx +6 -61
- package/src/__tests__/loadable.test.tsx +14 -6
- package/src/__tests__/resolved.test.tsx +6 -1
- package/src/experimental.ts +1 -1
- package/src/provider.ts +1 -2
- package/src/useGet.ts +19 -6
- package/src/useInlineAtom.ts +2 -11
- package/src/useLoadable.ts +74 -95
- package/src/useResolved.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# ccstate-react
|
|
2
2
|
|
|
3
|
+
## 5.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 2fdba09: feat: provide watch method to replace sub
|
|
8
|
+
|
|
9
|
+
### Minor Changes
|
|
10
|
+
|
|
11
|
+
- 211f5b5: refactor: better promise rejection handling
|
|
12
|
+
- 52c52fd: refactor: remove defaultStore
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- Updated dependencies [2fdba09]
|
|
17
|
+
- Updated dependencies [52c52fd]
|
|
18
|
+
- ccstate@5.0.0
|
|
19
|
+
|
|
3
20
|
## 4.13.0
|
|
4
21
|
|
|
5
22
|
### Minor Changes
|
package/dist/experimental.cjs
CHANGED
|
@@ -3,16 +3,6 @@
|
|
|
3
3
|
var ccstate = require('ccstate');
|
|
4
4
|
var react = require('react');
|
|
5
5
|
|
|
6
|
-
var StoreContext = react.createContext(null);
|
|
7
|
-
StoreContext.Provider;
|
|
8
|
-
function useStore() {
|
|
9
|
-
var store = react.useContext(StoreContext);
|
|
10
|
-
if (!store) {
|
|
11
|
-
return ccstate.getDefaultStore();
|
|
12
|
-
}
|
|
13
|
-
return store;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
6
|
function useRefFactory(factory) {
|
|
17
7
|
var ref = react.useRef(null);
|
|
18
8
|
if (!ref.current) {
|
|
@@ -46,17 +36,7 @@ function useCommand() {
|
|
|
46
36
|
return ccstate.command.apply(void 0, args);
|
|
47
37
|
});
|
|
48
38
|
}
|
|
49
|
-
function useSub() {
|
|
50
|
-
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
|
51
|
-
args[_key4] = arguments[_key4];
|
|
52
|
-
}
|
|
53
|
-
var store = useStore();
|
|
54
|
-
react.useEffect(function () {
|
|
55
|
-
return store.sub.apply(store, args);
|
|
56
|
-
}, []);
|
|
57
|
-
}
|
|
58
39
|
|
|
59
40
|
exports.useCCState = useCCState;
|
|
60
41
|
exports.useCommand = useCommand;
|
|
61
42
|
exports.useComputed = useComputed;
|
|
62
|
-
exports.useSub = useSub;
|
package/dist/experimental.d.cts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { state, State, computed, Computed, command, Command
|
|
1
|
+
import { state, State, computed, Computed, command, Command } from 'ccstate';
|
|
2
2
|
|
|
3
3
|
declare function useCCState<T>(...args: Parameters<typeof state<T>>): State<T>;
|
|
4
4
|
declare function useComputed<T>(...args: Parameters<typeof computed<T>>): Computed<T>;
|
|
5
5
|
declare function useCommand<T, Args extends unknown[]>(...args: Parameters<typeof command<T, Args>>): Command<T, Args>;
|
|
6
|
-
declare function useSub(...args: Parameters<Subscribe>): void;
|
|
7
6
|
|
|
8
|
-
export { useCCState, useCommand, useComputed
|
|
7
|
+
export { useCCState, useCommand, useComputed };
|
package/dist/experimental.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { state, State, computed, Computed, command, Command
|
|
1
|
+
import { state, State, computed, Computed, command, Command } from 'ccstate';
|
|
2
2
|
|
|
3
3
|
declare function useCCState<T>(...args: Parameters<typeof state<T>>): State<T>;
|
|
4
4
|
declare function useComputed<T>(...args: Parameters<typeof computed<T>>): Computed<T>;
|
|
5
5
|
declare function useCommand<T, Args extends unknown[]>(...args: Parameters<typeof command<T, Args>>): Command<T, Args>;
|
|
6
|
-
declare function useSub(...args: Parameters<Subscribe>): void;
|
|
7
6
|
|
|
8
|
-
export { useCCState, useCommand, useComputed
|
|
7
|
+
export { useCCState, useCommand, useComputed };
|
package/dist/experimental.js
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
var StoreContext = createContext(null);
|
|
5
|
-
StoreContext.Provider;
|
|
6
|
-
function useStore() {
|
|
7
|
-
var store = useContext(StoreContext);
|
|
8
|
-
if (!store) {
|
|
9
|
-
return getDefaultStore();
|
|
10
|
-
}
|
|
11
|
-
return store;
|
|
12
|
-
}
|
|
1
|
+
import { state, computed, command } from 'ccstate';
|
|
2
|
+
import { useRef } from 'react';
|
|
13
3
|
|
|
14
4
|
function useRefFactory(factory) {
|
|
15
5
|
var ref = useRef(null);
|
|
@@ -44,14 +34,5 @@ function useCommand() {
|
|
|
44
34
|
return command.apply(void 0, args);
|
|
45
35
|
});
|
|
46
36
|
}
|
|
47
|
-
function useSub() {
|
|
48
|
-
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
|
49
|
-
args[_key4] = arguments[_key4];
|
|
50
|
-
}
|
|
51
|
-
var store = useStore();
|
|
52
|
-
useEffect(function () {
|
|
53
|
-
return store.sub.apply(store, args);
|
|
54
|
-
}, []);
|
|
55
|
-
}
|
|
56
37
|
|
|
57
|
-
export { useCCState, useCommand, useComputed
|
|
38
|
+
export { useCCState, useCommand, useComputed };
|
package/dist/index.cjs
CHANGED
|
@@ -1,23 +1,32 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var react = require('react');
|
|
4
|
-
var ccstate = require('ccstate');
|
|
5
4
|
|
|
6
5
|
var StoreContext = react.createContext(null);
|
|
7
6
|
var StoreProvider = StoreContext.Provider;
|
|
8
7
|
function useStore() {
|
|
9
8
|
var store = react.useContext(StoreContext);
|
|
10
9
|
if (!store) {
|
|
11
|
-
|
|
10
|
+
throw new Error('useStore must be used within a StoreProvider');
|
|
12
11
|
}
|
|
13
12
|
return store;
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
function useGet(atom) {
|
|
17
16
|
var store = useStore();
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
var onChange = react.useRef(function (fn) {
|
|
18
|
+
var controller = new AbortController();
|
|
19
|
+
store.watch(function (get) {
|
|
20
|
+
get(atom);
|
|
21
|
+
fn();
|
|
22
|
+
}, {
|
|
23
|
+
signal: controller.signal
|
|
24
|
+
});
|
|
25
|
+
return function () {
|
|
26
|
+
controller.abort();
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
return react.useSyncExternalStore(onChange.current, function () {
|
|
21
30
|
return store.get(atom);
|
|
22
31
|
});
|
|
23
32
|
}
|
|
@@ -34,132 +43,54 @@ function useSet(signal) {
|
|
|
34
43
|
}, [store, signal]);
|
|
35
44
|
}
|
|
36
45
|
|
|
37
|
-
function
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return n;
|
|
41
|
-
}
|
|
42
|
-
function _arrayWithHoles(r) {
|
|
43
|
-
if (Array.isArray(r)) return r;
|
|
44
|
-
}
|
|
45
|
-
function _iterableToArrayLimit(r, l) {
|
|
46
|
-
var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
47
|
-
if (null != t) {
|
|
48
|
-
var e,
|
|
49
|
-
n,
|
|
50
|
-
i,
|
|
51
|
-
u,
|
|
52
|
-
a = [],
|
|
53
|
-
f = !0,
|
|
54
|
-
o = !1;
|
|
55
|
-
try {
|
|
56
|
-
if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
|
|
57
|
-
} catch (r) {
|
|
58
|
-
o = !0, n = r;
|
|
59
|
-
} finally {
|
|
60
|
-
try {
|
|
61
|
-
if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
|
|
62
|
-
} finally {
|
|
63
|
-
if (o) throw n;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return a;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
function _nonIterableRest() {
|
|
70
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
71
|
-
}
|
|
72
|
-
function _slicedToArray(r, e) {
|
|
73
|
-
return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
|
|
74
|
-
}
|
|
75
|
-
function _unsupportedIterableToArray(r, a) {
|
|
76
|
-
if (r) {
|
|
77
|
-
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
78
|
-
var t = {}.toString.call(r).slice(8, -1);
|
|
79
|
-
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Handles a specific behavior of useSyncExternalStore. In React, there are situations where the getSnapshot function of
|
|
85
|
-
* useSyncExternalStore executes, but the Render function doesn't execute.
|
|
86
|
-
*
|
|
87
|
-
* This can cause the promise generated in that round to not be caught, and userspace has no opportunity to handle this
|
|
88
|
-
* promise. Therefore, this issue needs to be handled in useGetPromise.
|
|
89
|
-
*
|
|
90
|
-
* @param atom
|
|
91
|
-
* @returns
|
|
92
|
-
*/
|
|
93
|
-
function useGetPromise(atom) {
|
|
94
|
-
var store = useStore();
|
|
95
|
-
var lastPromise = react.useRef(undefined);
|
|
96
|
-
var promiseProcessed = react.useRef(false);
|
|
97
|
-
var promise = react.useSyncExternalStore(function (fn) {
|
|
98
|
-
return store.sub(atom, ccstate.command(fn));
|
|
99
|
-
}, function () {
|
|
100
|
-
var val = store.get(atom);
|
|
101
|
-
|
|
102
|
-
// If the last promise is not processed and the current value is a promise,
|
|
103
|
-
// we need to silence the last promise to avoid unhandled rejections.
|
|
104
|
-
if (lastPromise.current !== undefined && lastPromise.current !== val && !promiseProcessed.current) {
|
|
105
|
-
lastPromise.current["catch"](function () {
|
|
106
|
-
return void 0;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
if (lastPromise.current !== val) {
|
|
110
|
-
promiseProcessed.current = false;
|
|
111
|
-
lastPromise.current = val instanceof Promise ? val : undefined;
|
|
112
|
-
}
|
|
113
|
-
return val;
|
|
46
|
+
function useLoadableInternal(promise$, keepLastResolved) {
|
|
47
|
+
var promiseResult = react.useRef({
|
|
48
|
+
state: 'loading'
|
|
114
49
|
});
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
_useGetPromise2 = _slicedToArray(_useGetPromise, 2),
|
|
122
|
-
promise = _useGetPromise2[0],
|
|
123
|
-
setPromiseProcessed = _useGetPromise2[1];
|
|
124
|
-
var _useState = react.useState({
|
|
125
|
-
state: 'loading'
|
|
126
|
-
}),
|
|
127
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
128
|
-
promiseResult = _useState2[0],
|
|
129
|
-
setPromiseResult = _useState2[1];
|
|
130
|
-
react.useEffect(function () {
|
|
131
|
-
if (!(promise instanceof Promise)) {
|
|
132
|
-
setPromiseResult({
|
|
133
|
-
state: 'hasData',
|
|
134
|
-
data: promise
|
|
135
|
-
});
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
var cancelled = false;
|
|
139
|
-
if (!keepLastResolved) {
|
|
140
|
-
setPromiseResult({
|
|
141
|
-
state: 'loading'
|
|
142
|
-
});
|
|
50
|
+
var store = useStore();
|
|
51
|
+
var subStore = react.useCallback(function (fn) {
|
|
52
|
+
function updateResult(result, signal) {
|
|
53
|
+
if (signal.aborted) return;
|
|
54
|
+
promiseResult.current = result;
|
|
55
|
+
fn();
|
|
143
56
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
57
|
+
var controller = new AbortController();
|
|
58
|
+
store.watch(function (get, _ref) {
|
|
59
|
+
var signal = _ref.signal;
|
|
60
|
+
var promise = get(promise$);
|
|
61
|
+
if (!(promise instanceof Promise)) {
|
|
62
|
+
updateResult({
|
|
63
|
+
state: 'hasData',
|
|
64
|
+
data: promise
|
|
65
|
+
}, signal);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (!keepLastResolved) {
|
|
69
|
+
updateResult({
|
|
70
|
+
state: 'loading'
|
|
71
|
+
}, signal);
|
|
72
|
+
}
|
|
73
|
+
promise.then(function (ret) {
|
|
74
|
+
updateResult({
|
|
75
|
+
state: 'hasData',
|
|
76
|
+
data: ret
|
|
77
|
+
}, signal);
|
|
78
|
+
}, function (error) {
|
|
79
|
+
updateResult({
|
|
80
|
+
state: 'hasError',
|
|
81
|
+
error: error
|
|
82
|
+
}, signal);
|
|
156
83
|
});
|
|
84
|
+
}, {
|
|
85
|
+
signal: controller.signal
|
|
157
86
|
});
|
|
158
87
|
return function () {
|
|
159
|
-
|
|
88
|
+
controller.abort();
|
|
160
89
|
};
|
|
161
|
-
}, [promise]);
|
|
162
|
-
return
|
|
90
|
+
}, [store, promise$]);
|
|
91
|
+
return react.useSyncExternalStore(subStore, function () {
|
|
92
|
+
return promiseResult.current;
|
|
93
|
+
});
|
|
163
94
|
}
|
|
164
95
|
function useLoadable(atom) {
|
|
165
96
|
return useLoadableInternal(atom, false);
|
package/dist/index.d.cts
CHANGED
|
@@ -15,13 +15,13 @@ type Loadable<T> = {
|
|
|
15
15
|
state: 'loading';
|
|
16
16
|
} | {
|
|
17
17
|
state: 'hasData';
|
|
18
|
-
data: T
|
|
18
|
+
data: Awaited<T>;
|
|
19
19
|
} | {
|
|
20
20
|
state: 'hasError';
|
|
21
21
|
error: unknown;
|
|
22
22
|
};
|
|
23
|
-
declare function useLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<
|
|
24
|
-
declare function useLastLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<
|
|
23
|
+
declare function useLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<T>;
|
|
24
|
+
declare function useLastLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<T>;
|
|
25
25
|
|
|
26
26
|
declare const StoreProvider: react.Provider<Store | null>;
|
|
27
27
|
|
package/dist/index.d.ts
CHANGED
|
@@ -15,13 +15,13 @@ type Loadable<T> = {
|
|
|
15
15
|
state: 'loading';
|
|
16
16
|
} | {
|
|
17
17
|
state: 'hasData';
|
|
18
|
-
data: T
|
|
18
|
+
data: Awaited<T>;
|
|
19
19
|
} | {
|
|
20
20
|
state: 'hasError';
|
|
21
21
|
error: unknown;
|
|
22
22
|
};
|
|
23
|
-
declare function useLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<
|
|
24
|
-
declare function useLastLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<
|
|
23
|
+
declare function useLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<T>;
|
|
24
|
+
declare function useLastLoadable<T>(atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>): Loadable<T>;
|
|
25
25
|
|
|
26
26
|
declare const StoreProvider: react.Provider<Store | null>;
|
|
27
27
|
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,30 @@
|
|
|
1
|
-
import { createContext, useContext, useSyncExternalStore, useCallback
|
|
2
|
-
import { getDefaultStore, command } from 'ccstate';
|
|
1
|
+
import { createContext, useContext, useRef, useSyncExternalStore, useCallback } from 'react';
|
|
3
2
|
|
|
4
3
|
var StoreContext = createContext(null);
|
|
5
4
|
var StoreProvider = StoreContext.Provider;
|
|
6
5
|
function useStore() {
|
|
7
6
|
var store = useContext(StoreContext);
|
|
8
7
|
if (!store) {
|
|
9
|
-
|
|
8
|
+
throw new Error('useStore must be used within a StoreProvider');
|
|
10
9
|
}
|
|
11
10
|
return store;
|
|
12
11
|
}
|
|
13
12
|
|
|
14
13
|
function useGet(atom) {
|
|
15
14
|
var store = useStore();
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
var onChange = useRef(function (fn) {
|
|
16
|
+
var controller = new AbortController();
|
|
17
|
+
store.watch(function (get) {
|
|
18
|
+
get(atom);
|
|
19
|
+
fn();
|
|
20
|
+
}, {
|
|
21
|
+
signal: controller.signal
|
|
22
|
+
});
|
|
23
|
+
return function () {
|
|
24
|
+
controller.abort();
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
return useSyncExternalStore(onChange.current, function () {
|
|
19
28
|
return store.get(atom);
|
|
20
29
|
});
|
|
21
30
|
}
|
|
@@ -32,132 +41,54 @@ function useSet(signal) {
|
|
|
32
41
|
}, [store, signal]);
|
|
33
42
|
}
|
|
34
43
|
|
|
35
|
-
function
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return n;
|
|
39
|
-
}
|
|
40
|
-
function _arrayWithHoles(r) {
|
|
41
|
-
if (Array.isArray(r)) return r;
|
|
42
|
-
}
|
|
43
|
-
function _iterableToArrayLimit(r, l) {
|
|
44
|
-
var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
45
|
-
if (null != t) {
|
|
46
|
-
var e,
|
|
47
|
-
n,
|
|
48
|
-
i,
|
|
49
|
-
u,
|
|
50
|
-
a = [],
|
|
51
|
-
f = !0,
|
|
52
|
-
o = !1;
|
|
53
|
-
try {
|
|
54
|
-
if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
|
|
55
|
-
} catch (r) {
|
|
56
|
-
o = !0, n = r;
|
|
57
|
-
} finally {
|
|
58
|
-
try {
|
|
59
|
-
if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
|
|
60
|
-
} finally {
|
|
61
|
-
if (o) throw n;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return a;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function _nonIterableRest() {
|
|
68
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
69
|
-
}
|
|
70
|
-
function _slicedToArray(r, e) {
|
|
71
|
-
return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
|
|
72
|
-
}
|
|
73
|
-
function _unsupportedIterableToArray(r, a) {
|
|
74
|
-
if (r) {
|
|
75
|
-
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
76
|
-
var t = {}.toString.call(r).slice(8, -1);
|
|
77
|
-
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Handles a specific behavior of useSyncExternalStore. In React, there are situations where the getSnapshot function of
|
|
83
|
-
* useSyncExternalStore executes, but the Render function doesn't execute.
|
|
84
|
-
*
|
|
85
|
-
* This can cause the promise generated in that round to not be caught, and userspace has no opportunity to handle this
|
|
86
|
-
* promise. Therefore, this issue needs to be handled in useGetPromise.
|
|
87
|
-
*
|
|
88
|
-
* @param atom
|
|
89
|
-
* @returns
|
|
90
|
-
*/
|
|
91
|
-
function useGetPromise(atom) {
|
|
92
|
-
var store = useStore();
|
|
93
|
-
var lastPromise = useRef(undefined);
|
|
94
|
-
var promiseProcessed = useRef(false);
|
|
95
|
-
var promise = useSyncExternalStore(function (fn) {
|
|
96
|
-
return store.sub(atom, command(fn));
|
|
97
|
-
}, function () {
|
|
98
|
-
var val = store.get(atom);
|
|
99
|
-
|
|
100
|
-
// If the last promise is not processed and the current value is a promise,
|
|
101
|
-
// we need to silence the last promise to avoid unhandled rejections.
|
|
102
|
-
if (lastPromise.current !== undefined && lastPromise.current !== val && !promiseProcessed.current) {
|
|
103
|
-
lastPromise.current["catch"](function () {
|
|
104
|
-
return void 0;
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
if (lastPromise.current !== val) {
|
|
108
|
-
promiseProcessed.current = false;
|
|
109
|
-
lastPromise.current = val instanceof Promise ? val : undefined;
|
|
110
|
-
}
|
|
111
|
-
return val;
|
|
44
|
+
function useLoadableInternal(promise$, keepLastResolved) {
|
|
45
|
+
var promiseResult = useRef({
|
|
46
|
+
state: 'loading'
|
|
112
47
|
});
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
_useGetPromise2 = _slicedToArray(_useGetPromise, 2),
|
|
120
|
-
promise = _useGetPromise2[0],
|
|
121
|
-
setPromiseProcessed = _useGetPromise2[1];
|
|
122
|
-
var _useState = useState({
|
|
123
|
-
state: 'loading'
|
|
124
|
-
}),
|
|
125
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
126
|
-
promiseResult = _useState2[0],
|
|
127
|
-
setPromiseResult = _useState2[1];
|
|
128
|
-
useEffect(function () {
|
|
129
|
-
if (!(promise instanceof Promise)) {
|
|
130
|
-
setPromiseResult({
|
|
131
|
-
state: 'hasData',
|
|
132
|
-
data: promise
|
|
133
|
-
});
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
var cancelled = false;
|
|
137
|
-
if (!keepLastResolved) {
|
|
138
|
-
setPromiseResult({
|
|
139
|
-
state: 'loading'
|
|
140
|
-
});
|
|
48
|
+
var store = useStore();
|
|
49
|
+
var subStore = useCallback(function (fn) {
|
|
50
|
+
function updateResult(result, signal) {
|
|
51
|
+
if (signal.aborted) return;
|
|
52
|
+
promiseResult.current = result;
|
|
53
|
+
fn();
|
|
141
54
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
55
|
+
var controller = new AbortController();
|
|
56
|
+
store.watch(function (get, _ref) {
|
|
57
|
+
var signal = _ref.signal;
|
|
58
|
+
var promise = get(promise$);
|
|
59
|
+
if (!(promise instanceof Promise)) {
|
|
60
|
+
updateResult({
|
|
61
|
+
state: 'hasData',
|
|
62
|
+
data: promise
|
|
63
|
+
}, signal);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!keepLastResolved) {
|
|
67
|
+
updateResult({
|
|
68
|
+
state: 'loading'
|
|
69
|
+
}, signal);
|
|
70
|
+
}
|
|
71
|
+
promise.then(function (ret) {
|
|
72
|
+
updateResult({
|
|
73
|
+
state: 'hasData',
|
|
74
|
+
data: ret
|
|
75
|
+
}, signal);
|
|
76
|
+
}, function (error) {
|
|
77
|
+
updateResult({
|
|
78
|
+
state: 'hasError',
|
|
79
|
+
error: error
|
|
80
|
+
}, signal);
|
|
154
81
|
});
|
|
82
|
+
}, {
|
|
83
|
+
signal: controller.signal
|
|
155
84
|
});
|
|
156
85
|
return function () {
|
|
157
|
-
|
|
86
|
+
controller.abort();
|
|
158
87
|
};
|
|
159
|
-
}, [promise]);
|
|
160
|
-
return
|
|
88
|
+
}, [store, promise$]);
|
|
89
|
+
return useSyncExternalStore(subStore, function () {
|
|
90
|
+
return promiseResult.current;
|
|
91
|
+
});
|
|
161
92
|
}
|
|
162
93
|
function useLoadable(atom) {
|
|
163
94
|
return useLoadableInternal(atom, false);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ccstate-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.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": "^
|
|
28
|
+
"ccstate": "^5.0.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": "^
|
|
58
|
+
"ccstate": "^5.0.0"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"build": "rollup -c",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { render, cleanup, screen } from '@testing-library/react';
|
|
2
2
|
import userEvent from '@testing-library/user-event';
|
|
3
3
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
-
import { computed, createStore, command, state, createDebugStore
|
|
4
|
+
import { computed, createStore, command, state, createDebugStore } from 'ccstate';
|
|
5
5
|
import { StoreProvider, useGet, useSet } from '..';
|
|
6
6
|
import { StrictMode, useState } from 'react';
|
|
7
7
|
import '@testing-library/jest-dom/vitest';
|
|
@@ -198,21 +198,21 @@ describe('react', () => {
|
|
|
198
198
|
expect(await screen.findByText('1')).toBeInTheDocument();
|
|
199
199
|
});
|
|
200
200
|
|
|
201
|
-
it('
|
|
201
|
+
it('throw error if no store provide', () => {
|
|
202
202
|
const count$ = state(0);
|
|
203
|
-
getDefaultStore().set(count$, 10);
|
|
204
203
|
|
|
205
204
|
function App() {
|
|
206
205
|
const count = useGet(count$);
|
|
207
206
|
return <div>{count}</div>;
|
|
208
207
|
}
|
|
209
208
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
<
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
209
|
+
expect(() => {
|
|
210
|
+
render(
|
|
211
|
+
<StrictMode>
|
|
212
|
+
<App />
|
|
213
|
+
</StrictMode>,
|
|
214
|
+
);
|
|
215
|
+
}).toThrowError('useStore must be used within a StoreProvider');
|
|
216
216
|
});
|
|
217
217
|
|
|
218
218
|
it('will unmount when component cleanup', async () => {
|
|
@@ -221,7 +221,7 @@ describe('react', () => {
|
|
|
221
221
|
|
|
222
222
|
function App() {
|
|
223
223
|
const ret = useGet(base$);
|
|
224
|
-
return <div>{ret}</div>;
|
|
224
|
+
return <div>ret:{ret}</div>;
|
|
225
225
|
}
|
|
226
226
|
|
|
227
227
|
function Container() {
|
|
@@ -252,12 +252,16 @@ describe('react', () => {
|
|
|
252
252
|
);
|
|
253
253
|
|
|
254
254
|
const user = userEvent.setup();
|
|
255
|
-
|
|
255
|
+
|
|
256
|
+
expect(screen.getByText('ret:0')).toBeInTheDocument();
|
|
256
257
|
const button = screen.getByText('hide');
|
|
258
|
+
|
|
259
|
+
expect(store.getReadDependents(base$)).toHaveLength(2);
|
|
260
|
+
|
|
257
261
|
expect(button).toBeInTheDocument();
|
|
258
262
|
await user.click(button);
|
|
259
263
|
expect(await screen.findByText('unmounted')).toBeInTheDocument();
|
|
260
|
-
expect(store.
|
|
264
|
+
expect(store.getReadDependents(base$)).toHaveLength(1);
|
|
261
265
|
});
|
|
262
266
|
});
|
|
263
267
|
|
|
@@ -287,7 +291,12 @@ it('useSet should be stable', () => {
|
|
|
287
291
|
return <div>Render</div>;
|
|
288
292
|
}
|
|
289
293
|
|
|
290
|
-
|
|
294
|
+
const store = createStore();
|
|
295
|
+
render(
|
|
296
|
+
<StoreProvider value={store}>
|
|
297
|
+
<Container />
|
|
298
|
+
</StoreProvider>,
|
|
299
|
+
);
|
|
291
300
|
|
|
292
301
|
expect(trace).toHaveBeenCalledTimes(2);
|
|
293
302
|
expect(trace.mock.calls[0][0]).toBe(trace.mock.calls[1][0]);
|
|
@@ -4,8 +4,8 @@ import '@testing-library/jest-dom/vitest';
|
|
|
4
4
|
import { screen, cleanup, render } from '@testing-library/react';
|
|
5
5
|
import { StrictMode } from 'react';
|
|
6
6
|
import userEvent from '@testing-library/user-event';
|
|
7
|
-
import { useCCState, useCommand, useComputed
|
|
8
|
-
import {
|
|
7
|
+
import { useCCState, useCommand, useComputed } from '../experimental';
|
|
8
|
+
import { createStore } from 'ccstate';
|
|
9
9
|
|
|
10
10
|
afterEach(() => {
|
|
11
11
|
cleanup();
|
|
@@ -62,9 +62,12 @@ it('use atom in React component', async () => {
|
|
|
62
62
|
);
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
const store = createStore();
|
|
65
66
|
render(
|
|
66
67
|
<StrictMode>
|
|
67
|
-
<
|
|
68
|
+
<StoreProvider value={store}>
|
|
69
|
+
<Counter />
|
|
70
|
+
</StoreProvider>
|
|
68
71
|
</StrictMode>,
|
|
69
72
|
);
|
|
70
73
|
expect(screen.getByText('count: 0')).toBeInTheDocument();
|
|
@@ -79,61 +82,3 @@ it('use atom in React component', async () => {
|
|
|
79
82
|
expect(screen.getByText('count: 4')).toBeInTheDocument();
|
|
80
83
|
expect(screen.getByText('double: 8')).toBeInTheDocument();
|
|
81
84
|
});
|
|
82
|
-
|
|
83
|
-
it('use sub in React component', async () => {
|
|
84
|
-
function Counter() {
|
|
85
|
-
const count$ = useCCState(0, {
|
|
86
|
-
debugLabel: 'count$',
|
|
87
|
-
});
|
|
88
|
-
const double$ = useCCState(0, {
|
|
89
|
-
debugLabel: 'double$',
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
const updateDouble$ = useCommand(
|
|
93
|
-
({ get, set }) => {
|
|
94
|
-
const double = get(count$) * 2;
|
|
95
|
-
set(double$, double);
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
debugLabel: 'updateDouble$',
|
|
99
|
-
},
|
|
100
|
-
);
|
|
101
|
-
useSub(count$, updateDouble$);
|
|
102
|
-
|
|
103
|
-
const count = useGet(count$);
|
|
104
|
-
const double = useGet(double$);
|
|
105
|
-
const setCount = useSet(count$);
|
|
106
|
-
|
|
107
|
-
return (
|
|
108
|
-
<>
|
|
109
|
-
<div>count: {String(count)}</div>
|
|
110
|
-
<div>double: {String(double)}</div>
|
|
111
|
-
<button
|
|
112
|
-
onClick={() => {
|
|
113
|
-
setCount((prev) => prev + 1);
|
|
114
|
-
}}
|
|
115
|
-
>
|
|
116
|
-
Increment
|
|
117
|
-
</button>
|
|
118
|
-
</>
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const store = createDebugStore();
|
|
123
|
-
render(
|
|
124
|
-
<StrictMode>
|
|
125
|
-
<StoreProvider value={store}>
|
|
126
|
-
<Counter />
|
|
127
|
-
</StoreProvider>
|
|
128
|
-
</StrictMode>,
|
|
129
|
-
);
|
|
130
|
-
expect(screen.getByText('count: 0')).toBeInTheDocument();
|
|
131
|
-
|
|
132
|
-
const button = screen.getByText('Increment');
|
|
133
|
-
await user.click(button);
|
|
134
|
-
expect(screen.getByText('count: 1')).toBeInTheDocument();
|
|
135
|
-
expect(await screen.findByText('double: 2')).toBeInTheDocument();
|
|
136
|
-
|
|
137
|
-
cleanup();
|
|
138
|
-
expect(store.getSubscribeGraph()).toEqual([]);
|
|
139
|
-
});
|
|
@@ -539,7 +539,12 @@ it('useLoadable accept sync computed', async () => {
|
|
|
539
539
|
return <div>{base.state}</div>;
|
|
540
540
|
}
|
|
541
541
|
|
|
542
|
-
|
|
542
|
+
const store = createStore();
|
|
543
|
+
render(
|
|
544
|
+
<StoreProvider value={store}>
|
|
545
|
+
<App />
|
|
546
|
+
</StoreProvider>,
|
|
547
|
+
);
|
|
543
548
|
|
|
544
549
|
expect(await screen.findByText('hasData')).toBeInTheDocument();
|
|
545
550
|
});
|
|
@@ -605,10 +610,10 @@ test('useLoadable should catch errors', () => {
|
|
|
605
610
|
get(reload$);
|
|
606
611
|
|
|
607
612
|
const p = Promise.resolve();
|
|
608
|
-
const
|
|
609
|
-
vi.spyOn(p, '
|
|
613
|
+
const originalThen = p.then.bind(p);
|
|
614
|
+
vi.spyOn(p, 'then').mockImplementation((...args) => {
|
|
610
615
|
traceCatch();
|
|
611
|
-
return
|
|
616
|
+
return originalThen(...args);
|
|
612
617
|
});
|
|
613
618
|
return p;
|
|
614
619
|
});
|
|
@@ -628,9 +633,12 @@ test('useLoadable should catch errors', () => {
|
|
|
628
633
|
</StoreProvider>
|
|
629
634
|
</StrictMode>,
|
|
630
635
|
);
|
|
636
|
+
expect(traceCatch).toHaveBeenCalledTimes(2); // strict mode renders twice
|
|
631
637
|
|
|
632
638
|
store.set(reload$, (x) => x + 1);
|
|
633
|
-
|
|
639
|
+
expect(traceCatch).toHaveBeenCalledTimes(3);
|
|
634
640
|
|
|
635
|
-
|
|
641
|
+
store.set(reload$, (x) => x + 1);
|
|
642
|
+
store.set(reload$, (x) => x + 1);
|
|
643
|
+
expect(traceCatch).toHaveBeenCalledTimes(5);
|
|
636
644
|
});
|
|
@@ -109,7 +109,12 @@ it('useResolved accept sync computed', async () => {
|
|
|
109
109
|
return <div>{base}</div>;
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
|
|
112
|
+
const store = createStore();
|
|
113
|
+
render(
|
|
114
|
+
<StoreProvider value={store}>
|
|
115
|
+
<App />
|
|
116
|
+
</StoreProvider>,
|
|
117
|
+
);
|
|
113
118
|
|
|
114
119
|
expect(await screen.findByText('0')).toBeInTheDocument();
|
|
115
120
|
});
|
package/src/experimental.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { useCCState, useComputed, useCommand
|
|
1
|
+
export { useCCState, useComputed, useCommand } from './useInlineAtom';
|
package/src/provider.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createContext, useContext } from 'react';
|
|
2
|
-
import { getDefaultStore } from 'ccstate';
|
|
3
2
|
import type { Store } from 'ccstate';
|
|
4
3
|
|
|
5
4
|
const StoreContext = createContext<Store | null>(null);
|
|
@@ -10,7 +9,7 @@ export function useStore(): Store {
|
|
|
10
9
|
const store = useContext(StoreContext);
|
|
11
10
|
|
|
12
11
|
if (!store) {
|
|
13
|
-
|
|
12
|
+
throw new Error('useStore must be used within a StoreProvider');
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
return store;
|
package/src/useGet.ts
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
|
-
import { useSyncExternalStore } from 'react';
|
|
1
|
+
import { useRef, useSyncExternalStore } from 'react';
|
|
2
2
|
import { useStore } from './provider';
|
|
3
|
-
|
|
3
|
+
|
|
4
4
|
import type { Computed, State } from 'ccstate';
|
|
5
5
|
|
|
6
6
|
export function useGet<T>(atom: State<T> | Computed<T>) {
|
|
7
7
|
const store = useStore();
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const onChange = useRef((fn: () => void) => {
|
|
9
|
+
const controller = new AbortController();
|
|
10
|
+
store.watch(
|
|
11
|
+
(get) => {
|
|
12
|
+
get(atom);
|
|
13
|
+
fn();
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
signal: controller.signal,
|
|
17
|
+
},
|
|
18
|
+
);
|
|
19
|
+
return () => {
|
|
20
|
+
controller.abort();
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
return useSyncExternalStore(onChange.current, () => store.get(atom));
|
|
12
25
|
}
|
package/src/useInlineAtom.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { command, computed, state, type Command, type Computed, type State
|
|
2
|
-
import {
|
|
3
|
-
import { useStore } from './provider';
|
|
1
|
+
import { command, computed, state, type Command, type Computed, type State } from 'ccstate';
|
|
2
|
+
import { useRef } from 'react';
|
|
4
3
|
|
|
5
4
|
function useRefFactory<T>(factory: () => T): T {
|
|
6
5
|
const ref = useRef<T | null>(null);
|
|
@@ -30,11 +29,3 @@ export function useCommand<T, Args extends unknown[]>(...args: Parameters<typeof
|
|
|
30
29
|
return command(...args);
|
|
31
30
|
});
|
|
32
31
|
}
|
|
33
|
-
|
|
34
|
-
export function useSub(...args: Parameters<Subscribe>) {
|
|
35
|
-
const store = useStore();
|
|
36
|
-
|
|
37
|
-
useEffect(() => {
|
|
38
|
-
return store.sub(...args);
|
|
39
|
-
}, []);
|
|
40
|
-
}
|
package/src/useLoadable.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useCallback, useRef, useSyncExternalStore } from 'react';
|
|
2
|
+
import { type Computed, type State } from 'ccstate';
|
|
3
3
|
import { useStore } from './provider';
|
|
4
4
|
|
|
5
5
|
type Loadable<T> =
|
|
@@ -8,120 +8,99 @@ type Loadable<T> =
|
|
|
8
8
|
}
|
|
9
9
|
| {
|
|
10
10
|
state: 'hasData';
|
|
11
|
-
data: T
|
|
11
|
+
data: Awaited<T>;
|
|
12
12
|
}
|
|
13
13
|
| {
|
|
14
14
|
state: 'hasError';
|
|
15
15
|
error: unknown;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
* useSyncExternalStore executes, but the Render function doesn't execute.
|
|
21
|
-
*
|
|
22
|
-
* This can cause the promise generated in that round to not be caught, and userspace has no opportunity to handle this
|
|
23
|
-
* promise. Therefore, this issue needs to be handled in useGetPromise.
|
|
24
|
-
*
|
|
25
|
-
* @param atom
|
|
26
|
-
* @returns
|
|
27
|
-
*/
|
|
28
|
-
function useGetPromise<T, U extends Promise<T> | T>(atom: State<U> | Computed<U>): [U, () => void] {
|
|
29
|
-
const store = useStore();
|
|
30
|
-
const lastPromise = useRef<Promise<unknown> | undefined>(undefined);
|
|
31
|
-
const promiseProcessed = useRef(false);
|
|
32
|
-
|
|
33
|
-
const promise = useSyncExternalStore(
|
|
34
|
-
(fn) => store.sub(atom, command(fn)),
|
|
35
|
-
() => {
|
|
36
|
-
const val = store.get(atom);
|
|
37
|
-
|
|
38
|
-
// If the last promise is not processed and the current value is a promise,
|
|
39
|
-
// we need to silence the last promise to avoid unhandled rejections.
|
|
40
|
-
if (lastPromise.current !== undefined && lastPromise.current !== val && !promiseProcessed.current) {
|
|
41
|
-
lastPromise.current.catch(() => void 0);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (lastPromise.current !== val) {
|
|
45
|
-
promiseProcessed.current = false;
|
|
46
|
-
lastPromise.current = val instanceof Promise ? val : undefined;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return val;
|
|
50
|
-
},
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
return [
|
|
54
|
-
promise,
|
|
55
|
-
() => {
|
|
56
|
-
promiseProcessed.current = true;
|
|
57
|
-
},
|
|
58
|
-
];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function useLoadableInternal<T>(
|
|
62
|
-
atom: State<Promise<T> | T> | Computed<Promise<T> | T>,
|
|
18
|
+
function useLoadableInternal<T, U extends Promise<Awaited<T>> | Awaited<T>>(
|
|
19
|
+
promise$: State<U> | Computed<U>,
|
|
63
20
|
keepLastResolved: boolean,
|
|
64
21
|
): Loadable<T> {
|
|
65
|
-
const
|
|
66
|
-
const [promiseResult, setPromiseResult] = useState<Loadable<T>>({
|
|
22
|
+
const promiseResult = useRef<Loadable<T>>({
|
|
67
23
|
state: 'loading',
|
|
68
24
|
});
|
|
69
25
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
let cancelled = false;
|
|
81
|
-
|
|
82
|
-
if (!keepLastResolved) {
|
|
83
|
-
setPromiseResult({
|
|
84
|
-
state: 'loading',
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
setPromiseProcessed();
|
|
89
|
-
|
|
90
|
-
promise.then(
|
|
91
|
-
(ret) => {
|
|
92
|
-
if (cancelled) return;
|
|
93
|
-
|
|
94
|
-
setPromiseResult({
|
|
95
|
-
state: 'hasData',
|
|
96
|
-
data: ret,
|
|
97
|
-
});
|
|
98
|
-
},
|
|
99
|
-
(error: unknown) => {
|
|
100
|
-
if (cancelled) return;
|
|
101
|
-
|
|
102
|
-
setPromiseResult({
|
|
103
|
-
state: 'hasError',
|
|
104
|
-
error,
|
|
105
|
-
});
|
|
106
|
-
},
|
|
107
|
-
);
|
|
26
|
+
const store = useStore();
|
|
27
|
+
const subStore = useCallback(
|
|
28
|
+
(fn: () => void) => {
|
|
29
|
+
function updateResult(result: Loadable<T>, signal: AbortSignal) {
|
|
30
|
+
if (signal.aborted) return;
|
|
31
|
+
promiseResult.current = result;
|
|
32
|
+
fn();
|
|
33
|
+
}
|
|
108
34
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
35
|
+
const controller = new AbortController();
|
|
36
|
+
|
|
37
|
+
store.watch(
|
|
38
|
+
(get, { signal }) => {
|
|
39
|
+
const promise: Promise<Awaited<T>> | Awaited<T> = get(promise$);
|
|
40
|
+
if (!(promise instanceof Promise)) {
|
|
41
|
+
updateResult(
|
|
42
|
+
{
|
|
43
|
+
state: 'hasData',
|
|
44
|
+
data: promise,
|
|
45
|
+
},
|
|
46
|
+
signal,
|
|
47
|
+
);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!keepLastResolved) {
|
|
52
|
+
updateResult(
|
|
53
|
+
{
|
|
54
|
+
state: 'loading',
|
|
55
|
+
},
|
|
56
|
+
signal,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
promise.then(
|
|
61
|
+
(ret) => {
|
|
62
|
+
updateResult(
|
|
63
|
+
{
|
|
64
|
+
state: 'hasData',
|
|
65
|
+
data: ret,
|
|
66
|
+
},
|
|
67
|
+
signal,
|
|
68
|
+
);
|
|
69
|
+
},
|
|
70
|
+
(error: unknown) => {
|
|
71
|
+
updateResult(
|
|
72
|
+
{
|
|
73
|
+
state: 'hasError',
|
|
74
|
+
error,
|
|
75
|
+
},
|
|
76
|
+
signal,
|
|
77
|
+
);
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
signal: controller.signal,
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
return () => {
|
|
87
|
+
controller.abort();
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
[store, promise$],
|
|
91
|
+
);
|
|
113
92
|
|
|
114
|
-
return promiseResult;
|
|
93
|
+
return useSyncExternalStore(subStore, () => promiseResult.current);
|
|
115
94
|
}
|
|
116
95
|
|
|
117
96
|
export function useLoadable<T>(
|
|
118
97
|
atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>,
|
|
119
|
-
): Loadable<
|
|
98
|
+
): Loadable<T> {
|
|
120
99
|
return useLoadableInternal(atom, false);
|
|
121
100
|
}
|
|
122
101
|
|
|
123
102
|
export function useLastLoadable<T>(
|
|
124
103
|
atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>,
|
|
125
|
-
): Loadable<
|
|
104
|
+
): Loadable<T> {
|
|
126
105
|
return useLoadableInternal(atom, true);
|
|
127
106
|
}
|
package/src/useResolved.ts
CHANGED
|
@@ -4,13 +4,13 @@ import type { Computed, State } from 'ccstate';
|
|
|
4
4
|
export function useResolved<T>(
|
|
5
5
|
atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>,
|
|
6
6
|
): Awaited<T> | undefined {
|
|
7
|
-
const loadable = useLoadable(atom);
|
|
7
|
+
const loadable = useLoadable<T>(atom);
|
|
8
8
|
return loadable.state === 'hasData' ? loadable.data : undefined;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export function useLastResolved<T>(
|
|
12
12
|
atom: State<Promise<Awaited<T>> | Awaited<T>> | Computed<Promise<Awaited<T>> | Awaited<T>>,
|
|
13
13
|
): Awaited<T> | undefined {
|
|
14
|
-
const loadable = useLastLoadable(atom);
|
|
14
|
+
const loadable = useLastLoadable<T>(atom);
|
|
15
15
|
return loadable.state === 'hasData' ? loadable.data : undefined;
|
|
16
16
|
}
|