@hyper-fetch/react 5.7.4 → 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
package/dist/index.esm.js
CHANGED
|
@@ -52,7 +52,8 @@ var useRequestEvents = ({
|
|
|
52
52
|
dispatcher,
|
|
53
53
|
logger,
|
|
54
54
|
actions,
|
|
55
|
-
setCacheData
|
|
55
|
+
setCacheData,
|
|
56
|
+
getIsDataProcessing
|
|
56
57
|
}) => {
|
|
57
58
|
const { responseMapper } = request;
|
|
58
59
|
const { cache, requestManager } = request.client;
|
|
@@ -99,9 +100,12 @@ var useRequestEvents = ({
|
|
|
99
100
|
}
|
|
100
101
|
(_e = onFinishedCallback.current) == null ? void 0 : _e.call(onFinishedCallback, { response, request: cmd, details });
|
|
101
102
|
};
|
|
102
|
-
const handleGetLoadingEvent = (
|
|
103
|
+
const handleGetLoadingEvent = (req) => {
|
|
103
104
|
return ({ loading }) => {
|
|
104
|
-
const
|
|
105
|
+
const isProcessing = getIsDataProcessing(req.cacheKey);
|
|
106
|
+
if (isProcessing)
|
|
107
|
+
return;
|
|
108
|
+
const canDisableLoading = !loading && !dispatcher.hasRunningRequests(req.queueKey);
|
|
105
109
|
if (loading || canDisableLoading) {
|
|
106
110
|
actions.setLoading(loading, false);
|
|
107
111
|
}
|
|
@@ -142,13 +146,13 @@ var useRequestEvents = ({
|
|
|
142
146
|
const handleRemove = ({ requestId }) => {
|
|
143
147
|
removeLifecycleListener(requestId);
|
|
144
148
|
};
|
|
145
|
-
const
|
|
149
|
+
const clearCacheDataListener = () => {
|
|
146
150
|
var _a;
|
|
147
151
|
(_a = dataEvents.current) == null ? void 0 : _a.unmount();
|
|
148
152
|
dataEvents.current = null;
|
|
149
153
|
};
|
|
150
|
-
const
|
|
151
|
-
const loadingUnmount = requestManager.events.onLoading(req.queueKey, handleGetLoadingEvent(req
|
|
154
|
+
const addCacheDataListener = (req) => {
|
|
155
|
+
const loadingUnmount = requestManager.events.onLoading(req.queueKey, handleGetLoadingEvent(req));
|
|
152
156
|
const getResponseUnmount = cache.events.onData(
|
|
153
157
|
req.cacheKey,
|
|
154
158
|
setCacheData
|
|
@@ -157,7 +161,7 @@ var useRequestEvents = ({
|
|
|
157
161
|
loadingUnmount();
|
|
158
162
|
getResponseUnmount();
|
|
159
163
|
};
|
|
160
|
-
|
|
164
|
+
clearCacheDataListener();
|
|
161
165
|
dataEvents.current = { unmount };
|
|
162
166
|
return unmount;
|
|
163
167
|
};
|
|
@@ -208,7 +212,7 @@ var useRequestEvents = ({
|
|
|
208
212
|
};
|
|
209
213
|
useWillUnmount(() => {
|
|
210
214
|
clearLifecycleListeners();
|
|
211
|
-
|
|
215
|
+
clearCacheDataListener();
|
|
212
216
|
});
|
|
213
217
|
return [
|
|
214
218
|
{
|
|
@@ -242,8 +246,8 @@ var useRequestEvents = ({
|
|
|
242
246
|
}
|
|
243
247
|
},
|
|
244
248
|
{
|
|
245
|
-
|
|
246
|
-
|
|
249
|
+
addCacheDataListener,
|
|
250
|
+
clearCacheDataListener,
|
|
247
251
|
addLifecycleListeners,
|
|
248
252
|
removeLifecycleListener,
|
|
249
253
|
clearLifecycleListeners
|
|
@@ -406,6 +410,7 @@ var useTrackedState = ({
|
|
|
406
410
|
const forceUpdate = useForceUpdate();
|
|
407
411
|
const state = useRef2(getInitialState(initialData, dispatcher, request));
|
|
408
412
|
const renderKeys = useRef2([]);
|
|
413
|
+
const isProcessingData = useRef2("");
|
|
409
414
|
const getStaleStatus = () => {
|
|
410
415
|
const cacheData = cache.get(cacheKey);
|
|
411
416
|
return !cacheData || isStaleCacheData(cacheTime, cacheData == null ? void 0 : cacheData.timestamp);
|
|
@@ -466,7 +471,7 @@ var useTrackedState = ({
|
|
|
466
471
|
extra: cacheData.extra,
|
|
467
472
|
retries: cacheData.retries,
|
|
468
473
|
timestamp: new Date(cacheData.timestamp),
|
|
469
|
-
loading:
|
|
474
|
+
loading: dispatcher.hasRunningRequests(queueKey)
|
|
470
475
|
};
|
|
471
476
|
const changedKeys = Object.keys(newStateValues).filter((key) => {
|
|
472
477
|
const keyValue = key;
|
|
@@ -477,14 +482,30 @@ var useTrackedState = ({
|
|
|
477
482
|
state.current = __spreadValues(__spreadValues({}, state.current), newStateValues);
|
|
478
483
|
renderKeyTrigger(changedKeys);
|
|
479
484
|
};
|
|
485
|
+
const setIsDataProcessing = ({
|
|
486
|
+
processingCacheKey,
|
|
487
|
+
isProcessing
|
|
488
|
+
}) => {
|
|
489
|
+
if (isProcessing) {
|
|
490
|
+
isProcessingData.current = processingCacheKey;
|
|
491
|
+
} else if (isProcessingData.current === cacheKey) {
|
|
492
|
+
isProcessingData.current = "";
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
const getIsDataProcessing = (processingCacheKey) => {
|
|
496
|
+
return isProcessingData.current === processingCacheKey;
|
|
497
|
+
};
|
|
480
498
|
const setCacheData = (cacheData) => {
|
|
499
|
+
setIsDataProcessing({ processingCacheKey: cacheKey, isProcessing: true });
|
|
481
500
|
const data = responseMapper ? responseMapper(cacheData) : cacheData;
|
|
482
501
|
if (data instanceof Promise) {
|
|
483
502
|
return (() => __async(void 0, null, function* () {
|
|
484
503
|
const promiseData = yield data;
|
|
485
504
|
handleCacheData(__spreadValues(__spreadValues({}, cacheData), promiseData));
|
|
505
|
+
setIsDataProcessing({ processingCacheKey: cacheKey, isProcessing: false });
|
|
486
506
|
}))();
|
|
487
507
|
}
|
|
508
|
+
setIsDataProcessing({ processingCacheKey: cacheKey, isProcessing: false });
|
|
488
509
|
return handleCacheData(__spreadValues(__spreadValues({}, cacheData), data));
|
|
489
510
|
};
|
|
490
511
|
const actions = {
|
|
@@ -566,7 +587,7 @@ var useTrackedState = ({
|
|
|
566
587
|
}
|
|
567
588
|
}
|
|
568
589
|
};
|
|
569
|
-
return [state.current, actions, { setRenderKey, setCacheData, getStaleStatus }];
|
|
590
|
+
return [state.current, actions, { setRenderKey, setCacheData, getStaleStatus, getIsDataProcessing }];
|
|
570
591
|
};
|
|
571
592
|
|
|
572
593
|
// src/helpers/use-socket-state/use-socket-state.hooks.ts
|
|
@@ -738,7 +759,7 @@ var useFetch = (request, options = useFetchDefaultOptions) => {
|
|
|
738
759
|
const logger = useRef4(loggerManager.init("useFetch")).current;
|
|
739
760
|
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
740
761
|
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
741
|
-
const [state, actions, { setRenderKey, setCacheData, getStaleStatus }] = useTrackedState({
|
|
762
|
+
const [state, actions, { setRenderKey, setCacheData, getStaleStatus, getIsDataProcessing }] = useTrackedState({
|
|
742
763
|
logger,
|
|
743
764
|
request,
|
|
744
765
|
dispatcher,
|
|
@@ -751,13 +772,18 @@ var useFetch = (request, options = useFetchDefaultOptions) => {
|
|
|
751
772
|
actions,
|
|
752
773
|
request,
|
|
753
774
|
dispatcher,
|
|
754
|
-
setCacheData
|
|
775
|
+
setCacheData,
|
|
776
|
+
getIsDataProcessing
|
|
755
777
|
});
|
|
756
|
-
const {
|
|
778
|
+
const { addCacheDataListener, addLifecycleListeners, clearCacheDataListener } = listeners;
|
|
757
779
|
const handleFetch = () => {
|
|
758
780
|
if (!disabled) {
|
|
759
781
|
logger.debug(`Fetching data`);
|
|
760
782
|
dispatcher.add(request);
|
|
783
|
+
const queue = dispatcher.getQueue(request.queueKey);
|
|
784
|
+
if (!queue.stopped && queue.requests.length) {
|
|
785
|
+
actions.setLoading(true);
|
|
786
|
+
}
|
|
761
787
|
} else {
|
|
762
788
|
logger.debug(`Cannot add to fetch queue`, { disabled });
|
|
763
789
|
}
|
|
@@ -823,7 +849,7 @@ var useFetch = (request, options = useFetchDefaultOptions) => {
|
|
|
823
849
|
}
|
|
824
850
|
};
|
|
825
851
|
const handleMountEvents = () => {
|
|
826
|
-
|
|
852
|
+
addCacheDataListener(request);
|
|
827
853
|
addLifecycleListeners(request);
|
|
828
854
|
const focusUnmount = appManager.events.onFocus(() => {
|
|
829
855
|
if (refetchOnFocus) {
|
|
@@ -845,7 +871,7 @@ var useFetch = (request, options = useFetchDefaultOptions) => {
|
|
|
845
871
|
});
|
|
846
872
|
const invalidateUnmount = cache.events.onInvalidate(cacheKey, handleFetch);
|
|
847
873
|
const unmount = () => {
|
|
848
|
-
|
|
874
|
+
clearCacheDataListener();
|
|
849
875
|
focusUnmount();
|
|
850
876
|
blurUnmount();
|
|
851
877
|
onlineUnmount();
|
|
@@ -955,7 +981,7 @@ var useSubmit = (request, options = useSubmitDefaultOptions) => {
|
|
|
955
981
|
const bounceResolver = useRef5(() => null);
|
|
956
982
|
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
957
983
|
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
958
|
-
const [state, actions, { setRenderKey, setCacheData }] = useTrackedState({
|
|
984
|
+
const [state, actions, { setRenderKey, setCacheData, getIsDataProcessing }] = useTrackedState({
|
|
959
985
|
logger,
|
|
960
986
|
request,
|
|
961
987
|
dispatcher,
|
|
@@ -968,9 +994,10 @@ var useSubmit = (request, options = useSubmitDefaultOptions) => {
|
|
|
968
994
|
actions,
|
|
969
995
|
request,
|
|
970
996
|
dispatcher,
|
|
971
|
-
setCacheData
|
|
997
|
+
setCacheData,
|
|
998
|
+
getIsDataProcessing
|
|
972
999
|
});
|
|
973
|
-
const {
|
|
1000
|
+
const { addCacheDataListener, addLifecycleListeners } = listeners;
|
|
974
1001
|
const handleSubmit = (submitOptions) => {
|
|
975
1002
|
const requestClone = request.clone(submitOptions);
|
|
976
1003
|
if (disabled) {
|
|
@@ -983,7 +1010,7 @@ var useSubmit = (request, options = useSubmitDefaultOptions) => {
|
|
|
983
1010
|
});
|
|
984
1011
|
}
|
|
985
1012
|
const triggerRequest = () => {
|
|
986
|
-
|
|
1013
|
+
addCacheDataListener(requestClone);
|
|
987
1014
|
return sendRequest(requestClone, __spreadProps(__spreadValues({
|
|
988
1015
|
dispatcherType: "submit"
|
|
989
1016
|
}, submitOptions), {
|
|
@@ -1045,7 +1072,7 @@ var useSubmit = (request, options = useSubmitDefaultOptions) => {
|
|
|
1045
1072
|
onSubmitAbort: callbacks.onAbort
|
|
1046
1073
|
};
|
|
1047
1074
|
useDidMount3(() => {
|
|
1048
|
-
|
|
1075
|
+
addCacheDataListener(request);
|
|
1049
1076
|
});
|
|
1050
1077
|
return __spreadProps(__spreadValues(__spreadValues({
|
|
1051
1078
|
submit: handleSubmit,
|
|
@@ -1179,7 +1206,7 @@ var useCache = (request, options = useCacheDefaultOptions) => {
|
|
|
1179
1206
|
const updateKey = JSON.stringify(request.toJSON());
|
|
1180
1207
|
const [globalConfig] = useConfigProvider();
|
|
1181
1208
|
const { dependencyTracking, initialData, deepCompare } = __spreadValues(__spreadValues(__spreadValues({}, useCacheDefaultOptions), globalConfig.useCacheConfig), options);
|
|
1182
|
-
const [state, actions, { setRenderKey, setCacheData }] = useTrackedState({
|
|
1209
|
+
const [state, actions, { setRenderKey, setCacheData, getIsDataProcessing }] = useTrackedState({
|
|
1183
1210
|
logger,
|
|
1184
1211
|
request,
|
|
1185
1212
|
dispatcher,
|
|
@@ -1192,11 +1219,12 @@ var useCache = (request, options = useCacheDefaultOptions) => {
|
|
|
1192
1219
|
actions,
|
|
1193
1220
|
request,
|
|
1194
1221
|
dispatcher,
|
|
1195
|
-
setCacheData
|
|
1222
|
+
setCacheData,
|
|
1223
|
+
getIsDataProcessing
|
|
1196
1224
|
});
|
|
1197
|
-
const {
|
|
1225
|
+
const { addCacheDataListener, addLifecycleListeners } = listeners;
|
|
1198
1226
|
const handleMountEvents = () => {
|
|
1199
|
-
|
|
1227
|
+
addCacheDataListener(request);
|
|
1200
1228
|
addLifecycleListeners(request);
|
|
1201
1229
|
};
|
|
1202
1230
|
useDidUpdate5(handleMountEvents, [updateKey], true);
|