@hyper-fetch/react 5.7.5 → 5.7.6-beta.1
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/index.cjs.js +54 -26
- package/dist/index.cjs.js.map +2 -2
- package/dist/index.d.ts +5 -3
- package/dist/index.esm.js +54 -26
- package/dist/index.esm.js.map +2 -2
- package/package.json +1 -1
- package/src/helpers/use-request-events/use-request-events.hooks.ts +16 -9
- package/src/helpers/use-request-events/use-request-events.types.ts +3 -2
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +26 -2
- package/src/helpers/use-tracked-state/use-tracked-state.types.ts +1 -0
- package/src/hooks/use-cache/use-cache.hooks.ts +4 -3
- package/src/hooks/use-fetch/use-fetch.hooks.ts +21 -11
- package/src/hooks/use-submit/use-submit.hooks.ts +5 -4
|
@@ -41,6 +41,7 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
41
41
|
|
|
42
42
|
const state = useRef<UseTrackedStateType<T>>(getInitialState(initialData, dispatcher, request));
|
|
43
43
|
const renderKeys = useRef<Array<keyof UseTrackedStateType<T>>>([]);
|
|
44
|
+
const isProcessingData = useRef("");
|
|
44
45
|
|
|
45
46
|
// ******************
|
|
46
47
|
// Utils
|
|
@@ -143,7 +144,7 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
143
144
|
extra: cacheData.extra,
|
|
144
145
|
retries: cacheData.retries,
|
|
145
146
|
timestamp: new Date(cacheData.timestamp),
|
|
146
|
-
loading:
|
|
147
|
+
loading: dispatcher.hasRunningRequests(queueKey),
|
|
147
148
|
};
|
|
148
149
|
|
|
149
150
|
const changedKeys = Object.keys(newStateValues).filter((key) => {
|
|
@@ -162,17 +163,40 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
162
163
|
renderKeyTrigger(changedKeys);
|
|
163
164
|
};
|
|
164
165
|
|
|
166
|
+
const setIsDataProcessing = ({
|
|
167
|
+
processingCacheKey,
|
|
168
|
+
isProcessing,
|
|
169
|
+
}: {
|
|
170
|
+
processingCacheKey: string;
|
|
171
|
+
isProcessing: boolean;
|
|
172
|
+
}) => {
|
|
173
|
+
if (isProcessing) {
|
|
174
|
+
isProcessingData.current = processingCacheKey;
|
|
175
|
+
}
|
|
176
|
+
// Do not turn off other keys processing
|
|
177
|
+
else if (isProcessingData.current === cacheKey) {
|
|
178
|
+
isProcessingData.current = "";
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const getIsDataProcessing = (processingCacheKey: string) => {
|
|
183
|
+
return isProcessingData.current === processingCacheKey;
|
|
184
|
+
};
|
|
185
|
+
|
|
165
186
|
const setCacheData = (
|
|
166
187
|
cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>,
|
|
167
188
|
): Promise<void> | void => {
|
|
189
|
+
setIsDataProcessing({ processingCacheKey: cacheKey, isProcessing: true });
|
|
168
190
|
const data = responseMapper ? responseMapper(cacheData) : cacheData;
|
|
169
191
|
|
|
170
192
|
if (data instanceof Promise) {
|
|
171
193
|
return (async () => {
|
|
172
194
|
const promiseData = await data;
|
|
173
195
|
handleCacheData({ ...cacheData, ...promiseData });
|
|
196
|
+
setIsDataProcessing({ processingCacheKey: cacheKey, isProcessing: false });
|
|
174
197
|
})();
|
|
175
198
|
}
|
|
199
|
+
setIsDataProcessing({ processingCacheKey: cacheKey, isProcessing: false });
|
|
176
200
|
return handleCacheData({ ...cacheData, ...data });
|
|
177
201
|
};
|
|
178
202
|
|
|
@@ -260,5 +284,5 @@ export const useTrackedState = <T extends RequestInstance>({
|
|
|
260
284
|
},
|
|
261
285
|
};
|
|
262
286
|
|
|
263
|
-
return [state.current, actions, { setRenderKey, setCacheData, getStaleStatus }];
|
|
287
|
+
return [state.current, actions, { setRenderKey, setCacheData, getStaleStatus, getIsDataProcessing }];
|
|
264
288
|
};
|
|
@@ -31,6 +31,7 @@ export type UseTrackedStateReturn<T extends RequestInstance> = [
|
|
|
31
31
|
setRenderKey: (renderKey: keyof UseTrackedStateType<T>) => void;
|
|
32
32
|
setCacheData: (cacheData: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>) => void;
|
|
33
33
|
getStaleStatus: () => boolean;
|
|
34
|
+
getIsDataProcessing: (cacheKey: string) => boolean;
|
|
34
35
|
},
|
|
35
36
|
];
|
|
36
37
|
|
|
@@ -28,7 +28,7 @@ export const useCache = <T extends RequestInstance>(
|
|
|
28
28
|
/**
|
|
29
29
|
* State handler with optimization for rerendering, that hooks into the cache state and dispatchers queues
|
|
30
30
|
*/
|
|
31
|
-
const [state, actions, { setRenderKey, setCacheData }] = useTrackedState<T>({
|
|
31
|
+
const [state, actions, { setRenderKey, setCacheData, getIsDataProcessing }] = useTrackedState<T>({
|
|
32
32
|
logger,
|
|
33
33
|
request,
|
|
34
34
|
dispatcher,
|
|
@@ -46,12 +46,13 @@ export const useCache = <T extends RequestInstance>(
|
|
|
46
46
|
request,
|
|
47
47
|
dispatcher,
|
|
48
48
|
setCacheData,
|
|
49
|
+
getIsDataProcessing,
|
|
49
50
|
});
|
|
50
51
|
|
|
51
|
-
const {
|
|
52
|
+
const { addCacheDataListener, addLifecycleListeners } = listeners;
|
|
52
53
|
|
|
53
54
|
const handleMountEvents = () => {
|
|
54
|
-
|
|
55
|
+
addCacheDataListener(request);
|
|
55
56
|
addLifecycleListeners(request);
|
|
56
57
|
};
|
|
57
58
|
|
|
@@ -36,6 +36,9 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
36
36
|
bounce = useFetchDefaultOptions.bounce,
|
|
37
37
|
bounceType = useFetchDefaultOptions.bounceType,
|
|
38
38
|
bounceTime = useFetchDefaultOptions.bounceTime,
|
|
39
|
+
// TODO: handle type error
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
41
|
+
// @ts-ignore
|
|
39
42
|
bounceTimeout = useFetchDefaultOptions.bounceTime,
|
|
40
43
|
deepCompare = useFetchDefaultOptions.deepCompare,
|
|
41
44
|
} = {
|
|
@@ -60,14 +63,15 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
60
63
|
/**
|
|
61
64
|
* State handler with optimization for re-rendering, that hooks into the cache state and dispatchers queues
|
|
62
65
|
*/
|
|
63
|
-
const [state, actions, { setRenderKey, setCacheData, getStaleStatus }] =
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
const [state, actions, { setRenderKey, setCacheData, getStaleStatus, getIsDataProcessing }] =
|
|
67
|
+
useTrackedState<RequestType>({
|
|
68
|
+
logger,
|
|
69
|
+
request,
|
|
70
|
+
dispatcher,
|
|
71
|
+
initialData,
|
|
72
|
+
deepCompare,
|
|
73
|
+
dependencyTracking,
|
|
74
|
+
});
|
|
71
75
|
|
|
72
76
|
/**
|
|
73
77
|
* Handles the data exchange with the core logic - responses, loading, downloading etc
|
|
@@ -78,9 +82,10 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
78
82
|
request,
|
|
79
83
|
dispatcher,
|
|
80
84
|
setCacheData,
|
|
85
|
+
getIsDataProcessing,
|
|
81
86
|
});
|
|
82
87
|
|
|
83
|
-
const {
|
|
88
|
+
const { addCacheDataListener, addLifecycleListeners, clearCacheDataListener } = listeners;
|
|
84
89
|
|
|
85
90
|
// ******************
|
|
86
91
|
// Fetching
|
|
@@ -89,6 +94,11 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
89
94
|
if (!disabled) {
|
|
90
95
|
logger.debug(`Fetching data`);
|
|
91
96
|
dispatcher.add(request);
|
|
97
|
+
const queue = dispatcher.getQueue(request.queueKey);
|
|
98
|
+
// We want fast initial loading state if not paused / disabled
|
|
99
|
+
if (!queue.stopped && queue.requests.length) {
|
|
100
|
+
actions.setLoading(true);
|
|
101
|
+
}
|
|
92
102
|
} else {
|
|
93
103
|
logger.debug(`Cannot add to fetch queue`, { disabled });
|
|
94
104
|
}
|
|
@@ -189,7 +199,7 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
189
199
|
// ******************
|
|
190
200
|
|
|
191
201
|
const handleMountEvents = () => {
|
|
192
|
-
|
|
202
|
+
addCacheDataListener(request);
|
|
193
203
|
addLifecycleListeners(request);
|
|
194
204
|
|
|
195
205
|
const focusUnmount = appManager.events.onFocus(() => {
|
|
@@ -214,7 +224,7 @@ export const useFetch = <RequestType extends RequestInstance>(
|
|
|
214
224
|
const invalidateUnmount = cache.events.onInvalidate(cacheKey, handleFetch);
|
|
215
225
|
|
|
216
226
|
const unmount = () => {
|
|
217
|
-
|
|
227
|
+
clearCacheDataListener();
|
|
218
228
|
focusUnmount();
|
|
219
229
|
blurUnmount();
|
|
220
230
|
onlineUnmount();
|
|
@@ -73,7 +73,7 @@ export const useSubmit = <RequestType extends RequestInstance>(
|
|
|
73
73
|
/**
|
|
74
74
|
* State handler with optimization for rerendering, that hooks into the cache state and dispatchers queues
|
|
75
75
|
*/
|
|
76
|
-
const [state, actions, { setRenderKey, setCacheData }] = useTrackedState<RequestType>({
|
|
76
|
+
const [state, actions, { setRenderKey, setCacheData, getIsDataProcessing }] = useTrackedState<RequestType>({
|
|
77
77
|
logger,
|
|
78
78
|
request,
|
|
79
79
|
dispatcher,
|
|
@@ -91,9 +91,10 @@ export const useSubmit = <RequestType extends RequestInstance>(
|
|
|
91
91
|
request,
|
|
92
92
|
dispatcher,
|
|
93
93
|
setCacheData,
|
|
94
|
+
getIsDataProcessing,
|
|
94
95
|
});
|
|
95
96
|
|
|
96
|
-
const {
|
|
97
|
+
const { addCacheDataListener, addLifecycleListeners } = listeners;
|
|
97
98
|
|
|
98
99
|
// ******************
|
|
99
100
|
// Submitting
|
|
@@ -119,7 +120,7 @@ export const useSubmit = <RequestType extends RequestInstance>(
|
|
|
119
120
|
}
|
|
120
121
|
|
|
121
122
|
const triggerRequest = () => {
|
|
122
|
-
|
|
123
|
+
addCacheDataListener(requestClone);
|
|
123
124
|
return sendRequest(requestClone, {
|
|
124
125
|
dispatcherType: "submit",
|
|
125
126
|
...submitOptions,
|
|
@@ -213,7 +214,7 @@ export const useSubmit = <RequestType extends RequestInstance>(
|
|
|
213
214
|
// ******************
|
|
214
215
|
|
|
215
216
|
useDidMount(() => {
|
|
216
|
-
|
|
217
|
+
addCacheDataListener(request);
|
|
217
218
|
});
|
|
218
219
|
|
|
219
220
|
return {
|