@hyper-fetch/react 9.6.0 → 9.6.2
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/__tests__/features/helpers/use-request-events/use-request-events.async-mapper.spec.ts +243 -0
- package/__tests__/features/helpers/use-tracked-state/use-tacked-state.actions.spec.ts +1 -1
- package/__tests__/features/helpers/use-tracked-state/use-tracked-state.keys.spec.ts +294 -0
- package/__tests__/features/helpers/utils.spec.ts +127 -0
- package/__tests__/features/provider/provider.config.spec.tsx +172 -0
- package/__tests__/features/use-app-manager/use-app-manager.events.spec.tsx +497 -0
- package/__tests__/features/use-cache/use-cache.state.spec.tsx +326 -0
- package/__tests__/features/use-emitter/use-emitter.lifecycle.spec.tsx +417 -0
- package/__tests__/features/use-fetch/use-fetch.bounce-timers.spec.ts +199 -0
- package/__tests__/features/use-fetch/use-fetch.cache-events.spec.ts +212 -0
- package/__tests__/features/use-fetch/use-fetch.callbacks.spec.tsx +359 -0
- package/__tests__/features/use-fetch/use-fetch.cancel.spec.ts +6 -3
- package/__tests__/features/use-fetch/use-fetch.debounce.spec.ts +72 -80
- package/__tests__/features/use-fetch/use-fetch.hydration.spec.tsx +126 -0
- package/__tests__/features/use-fetch/use-fetch.infinite.spec.ts +146 -0
- package/__tests__/features/use-fetch/use-fetch.initial-state.spec.ts +340 -0
- package/__tests__/features/use-fetch/use-fetch.keys.spec.tsx +559 -0
- package/__tests__/features/use-fetch/use-fetch.lifecycle.spec.ts +348 -0
- package/__tests__/features/use-fetch/use-fetch.mounting.spec.tsx +411 -0
- package/__tests__/features/use-fetch/use-fetch.pagination.spec.ts +177 -0
- package/__tests__/features/use-fetch/use-fetch.refresh-interval.spec.ts +355 -0
- package/__tests__/features/use-fetch/use-fetch.refresh.spec.ts +36 -13
- package/__tests__/features/use-fetch/use-fetch.response-order.spec.ts +105 -0
- package/__tests__/features/use-fetch/use-fetch.retry.spec.ts +235 -0
- package/__tests__/features/use-fetch/use-fetch.return-types.spec.ts +186 -0
- package/__tests__/features/use-fetch/use-fetch.scope.spec.ts +333 -0
- package/__tests__/features/use-fetch/use-fetch.ssr.spec.tsx +195 -0
- package/__tests__/features/use-fetch/use-fetch.suspense-edge.spec.tsx +391 -0
- package/__tests__/features/use-fetch/use-fetch.throwing-callbacks.spec.tsx +112 -0
- package/__tests__/features/use-fetch/use-fetch.tracking.spec.tsx +364 -0
- package/__tests__/features/use-fetch/use-fetch.utils.spec.ts +11 -2
- package/__tests__/features/use-fetch/use-fetch.window-events.spec.ts +300 -0
- package/__tests__/features/use-infinite-fetch/use-infinite-fetch.parity.spec.tsx +1027 -0
- package/__tests__/features/use-infinite-fetch/use-infinite-fetch.resubscribe.spec.tsx +267 -0
- package/__tests__/features/use-infinite-fetch/use-infinite-fetch.typescript.spec.ts +64 -1
- package/__tests__/features/use-is-fetching/use-is-fetching.base.spec.tsx +19 -9
- package/__tests__/features/use-is-fetching/use-is-fetching.lifecycle.spec.tsx +301 -0
- package/__tests__/features/use-listener/use-listener.lifecycle.spec.tsx +507 -0
- package/__tests__/features/use-listener/use-listener.ssr.spec.tsx +89 -0
- package/__tests__/features/use-queue/use-queue.controls.spec.tsx +593 -0
- package/__tests__/features/use-queue/use-queue.list.spec.tsx +560 -0
- package/__tests__/features/use-queue/use-queue.progress.spec.tsx +650 -0
- package/__tests__/features/use-submit/use-submit.abort.spec.tsx +270 -0
- package/__tests__/features/use-submit/use-submit.actions.spec.tsx +446 -0
- package/__tests__/features/use-submit/use-submit.arguments.spec.tsx +732 -0
- package/__tests__/features/use-submit/use-submit.bounce.spec.tsx +644 -0
- package/__tests__/features/use-submit/use-submit.callbacks.spec.tsx +860 -0
- package/__tests__/features/use-submit/use-submit.chaining.spec.tsx +167 -0
- package/__tests__/features/use-submit/use-submit.concurrency.spec.ts +2 -1
- package/__tests__/features/use-submit/use-submit.debounce.spec.ts +100 -124
- package/__tests__/features/use-submit/use-submit.double-submit.spec.tsx +552 -0
- package/__tests__/features/use-submit/use-submit.offline.spec.tsx +285 -0
- package/__tests__/features/use-submit/use-submit.optimistic-cache.spec.tsx +619 -0
- package/__tests__/features/use-submit/use-submit.optimistic-settlement.spec.ts +8 -1
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +5 -1
- package/__tests__/features/use-submit/use-submit.retry.spec.ts +51 -35
- package/__tests__/features/use-submit/use-submit.state.spec.tsx +1025 -0
- package/__tests__/features/use-submit/use-submit.submit-types.spec.ts +194 -0
- package/__tests__/features/use-submit/use-submit.submitting.spec.tsx +180 -0
- package/__tests__/features/use-submit/use-submit.unmount.spec.ts +2 -1
- package/__tests__/utils/gate.utils.ts +103 -0
- package/__tests__/utils/gated-client.utils.ts +158 -0
- package/__tests__/utils/helpers.utils.ts +19 -0
- package/__tests__/utils/use-queue.utils.ts +8 -4
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -1
- package/dist/helpers/use-request-events/use-request-events.types.d.ts +1 -1
- package/dist/helpers/use-request-events/use-request-events.types.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +8 -0
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -1
- package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -1
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.hooks.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts +3 -0
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.store.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts +5 -3
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.types.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts +11 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts.map +1 -1
- package/dist/hooks/use-listener/use-listener.hooks.d.ts.map +1 -1
- package/dist/hooks/use-queue/use-queue.hooks.d.ts.map +1 -1
- package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -1
- package/dist/index.mjs +633 -347
- package/dist/index.mjs.map +1 -1
- package/dist/provider/provider.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/helpers/use-request-events/use-request-events.hooks.ts +147 -32
- package/src/helpers/use-request-events/use-request-events.types.ts +1 -1
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +148 -101
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +26 -5
- package/src/hooks/use-cache/use-cache.hooks.ts +20 -5
- package/src/hooks/use-fetch/use-fetch.hooks.ts +194 -113
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.hooks.ts +55 -26
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.store.ts +35 -29
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.types.ts +5 -3
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts +25 -1
- package/src/hooks/use-listener/use-listener.hooks.ts +2 -1
- package/src/hooks/use-queue/use-queue.hooks.ts +85 -60
- package/src/hooks/use-submit/use-submit.hooks.ts +163 -53
- package/src/provider/provider.tsx +9 -1
- package/__tests__/features/use-fetch/use-fetch.pagination.ts +0 -36
- package/__tests__/features/use-fetch/use-fetch.retry.ts +0 -38
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import React, { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
-
import { Request, Time, getCachedPages, getErrorMessage, getHasNextPage, getHasPreviousPage, getInitialPageParam, getNextPageParam, getPageItems, getPageRequest, getPageTotal, getPaginationMode, getPreviousPageParam, getRequestDispatcher, mapResponseForSend, parseResponse, replaceEqualDeep, runResponseMapper, scopeKey, sendRequest, stringifyKey } from "@hyper-fetch/core";
|
|
2
|
+
import { Request, Time, getCachedPages, getErrorMessage, getHasNextPage, getHasPreviousPage, getInitialPageParam, getNextPageParam, getPageItems, getPageRequest, getPageTotal, getPaginationMode, getPreviousPageParam, getRequestDispatcher, mapResponseForSend, parseResponse, replaceEqualDeep, runResponseMapper, runUserCode, scopeKey, sendRequest, stringifyKey } from "@hyper-fetch/core";
|
|
3
3
|
import { jsxDEV } from "react/jsx-dev-runtime";
|
|
4
4
|
import { getDeliveryKey } from "@hyper-fetch/sockets";
|
|
5
5
|
//#region ../../node_modules/.pnpm/@better-hooks+lifecycle@2.0.0_react@19.2.5/node_modules/@better-hooks/lifecycle/dist/index.esm.js
|
|
6
|
-
var
|
|
7
|
-
var u = useRef(!1);
|
|
8
|
-
useEffect((function() {
|
|
9
|
-
if (!u.current) return u.current = !0, t();
|
|
10
|
-
}), []);
|
|
11
|
-
}, i = function(t, u, c) {
|
|
6
|
+
var i = function(t, u, c) {
|
|
12
7
|
void 0 === c && (c = !1);
|
|
13
8
|
var e = useRef(c);
|
|
14
9
|
useEffect((function() {
|
|
@@ -144,6 +139,10 @@ var isSameScope = (scope, otherScope) => {
|
|
|
144
139
|
*/
|
|
145
140
|
var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, getIsDataProcessing }) => {
|
|
146
141
|
const { cache, requestManager } = request.client;
|
|
142
|
+
const requestRef = useRef(request);
|
|
143
|
+
requestRef.current = request;
|
|
144
|
+
const dispatcherRef = useRef(dispatcher);
|
|
145
|
+
dispatcherRef.current = dispatcher;
|
|
147
146
|
const onSuccessCallback = useRef(null);
|
|
148
147
|
const onErrorCallback = useRef(null);
|
|
149
148
|
const onAbortCallback = useRef(null);
|
|
@@ -166,8 +165,12 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
166
165
|
value.unmount();
|
|
167
166
|
});
|
|
168
167
|
events.clear();
|
|
168
|
+
pendingRequests.current.clear();
|
|
169
169
|
};
|
|
170
170
|
const optimisticResultsRef = useRef(/* @__PURE__ */ new Map());
|
|
171
|
+
const settledRequests = useRef(/* @__PURE__ */ new Set());
|
|
172
|
+
const pendingRequests = useRef(/* @__PURE__ */ new Set());
|
|
173
|
+
const listenedQueryKey = useRef(null);
|
|
171
174
|
const handleResponseCallbacks = (values) => {
|
|
172
175
|
const { success } = values.response;
|
|
173
176
|
const { isOffline, isCanceled, willRetry } = values.details;
|
|
@@ -213,39 +216,87 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
213
216
|
return ({ loading, request: eventRequest }) => {
|
|
214
217
|
if (!isSameScope(eventRequest.scope, req.scope)) return;
|
|
215
218
|
if (getIsDataProcessing(req.cacheKey)) return;
|
|
216
|
-
const canDisableLoading = !loading && !dispatcher.hasRunningRequests(scopeKey(req.queryKey, req.scope));
|
|
219
|
+
const canDisableLoading = !loading && !dispatcher.hasRunningRequests(scopeKey(req.queryKey, req.scope)) && pendingRequests.current.size === 0;
|
|
217
220
|
if (loading || canDisableLoading) actions.setLoading(loading);
|
|
218
221
|
};
|
|
219
222
|
};
|
|
220
|
-
const handleDownloadProgress = (
|
|
221
|
-
|
|
223
|
+
const handleDownloadProgress = (req) => {
|
|
224
|
+
return (data) => {
|
|
225
|
+
if (!isSameScope(data.request?.scope, req.scope)) return;
|
|
226
|
+
onDownloadProgressCallback.current?.(data);
|
|
227
|
+
};
|
|
222
228
|
};
|
|
223
|
-
const handleUploadProgress = (
|
|
224
|
-
|
|
229
|
+
const handleUploadProgress = (req) => {
|
|
230
|
+
return (data) => {
|
|
231
|
+
if (!isSameScope(data.request?.scope, req.scope)) return;
|
|
232
|
+
onUploadProgressCallback.current?.(data);
|
|
233
|
+
};
|
|
225
234
|
};
|
|
226
|
-
const handleRequestStart = () => {
|
|
235
|
+
const handleRequestStart = (req) => {
|
|
227
236
|
return (details) => {
|
|
237
|
+
if (!isSameScope(details.request?.scope, req.scope)) return;
|
|
228
238
|
onRequestStartCallback.current?.(details);
|
|
229
239
|
};
|
|
230
240
|
};
|
|
231
|
-
const handleResponseStart = () => {
|
|
241
|
+
const handleResponseStart = (req) => {
|
|
232
242
|
return (details) => {
|
|
243
|
+
if (!isSameScope(details.request?.scope, req.scope)) return;
|
|
233
244
|
onResponseStartCallback.current?.(details);
|
|
234
245
|
};
|
|
235
246
|
};
|
|
236
|
-
const handleResponse = () => {
|
|
247
|
+
const handleResponse = (req) => {
|
|
237
248
|
return (values) => {
|
|
238
|
-
|
|
249
|
+
if (!isSameScope(values.request?.scope, req.scope)) return;
|
|
250
|
+
pendingRequests.current.delete(values.requestId);
|
|
251
|
+
const { success } = values.response;
|
|
252
|
+
const { isCanceled, isOffline, willRetry } = values.details;
|
|
253
|
+
if (!(!success && !isCanceled && (willRetry || !!req.offline && isOffline))) settledRequests.current.add(values.requestId);
|
|
254
|
+
const queryKey = scopeKey(req.queryKey, req.scope);
|
|
255
|
+
if (pendingRequests.current.size === 0 && !dispatcher.hasRunningRequests(queryKey) && listenedQueryKey.current !== null && listenedQueryKey.current !== queryKey) actions.setLoading(dispatcher.hasRunningRequests(listenedQueryKey.current));
|
|
256
|
+
const data = runResponseMapper(req, values.response);
|
|
239
257
|
if (data instanceof Promise) return (async () => {
|
|
258
|
+
const response = await data;
|
|
259
|
+
if (!isMounted.current) return;
|
|
240
260
|
handleResponseCallbacks({
|
|
241
261
|
...values,
|
|
242
|
-
response
|
|
262
|
+
response
|
|
243
263
|
});
|
|
244
264
|
})();
|
|
245
|
-
return handleResponseCallbacks(
|
|
265
|
+
return handleResponseCallbacks({
|
|
266
|
+
...values,
|
|
267
|
+
response: data || values.response
|
|
268
|
+
});
|
|
246
269
|
};
|
|
247
270
|
};
|
|
248
|
-
const handleRemove = ({ requestId }) => {
|
|
271
|
+
const handleRemove = ({ requestId, request: removedRequest }) => {
|
|
272
|
+
if (!settledRequests.current.has(requestId) && lifecycleEvents.current.has(requestId)) {
|
|
273
|
+
const timestamp = Date.now();
|
|
274
|
+
handleResponseCallbacks({
|
|
275
|
+
request: removedRequest,
|
|
276
|
+
requestId,
|
|
277
|
+
response: {
|
|
278
|
+
data: null,
|
|
279
|
+
error: getErrorMessage("deleted"),
|
|
280
|
+
status: null,
|
|
281
|
+
success: false,
|
|
282
|
+
extra: removedRequest.client.adapter.defaultExtra,
|
|
283
|
+
requestTimestamp: timestamp,
|
|
284
|
+
responseTimestamp: timestamp
|
|
285
|
+
},
|
|
286
|
+
details: {
|
|
287
|
+
retries: 0,
|
|
288
|
+
isCanceled: true,
|
|
289
|
+
isOffline: false,
|
|
290
|
+
willRetry: false,
|
|
291
|
+
addedTimestamp: timestamp,
|
|
292
|
+
triggerTimestamp: timestamp,
|
|
293
|
+
requestTimestamp: timestamp,
|
|
294
|
+
responseTimestamp: timestamp
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
settledRequests.current.delete(requestId);
|
|
299
|
+
pendingRequests.current.delete(requestId);
|
|
249
300
|
optimisticResultsRef.current.delete(requestId);
|
|
250
301
|
removeLifecycleListener(requestId);
|
|
251
302
|
};
|
|
@@ -259,6 +310,7 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
259
310
|
const getResponseUnmount = cache.events.onDataByKey(req.cacheKey, (cacheData) => {
|
|
260
311
|
if (!isSameScope(cacheData.scope, req.scope)) return;
|
|
261
312
|
setCacheData(cacheData);
|
|
313
|
+
if (pendingRequests.current.size > 0) actions.setLoading(true);
|
|
262
314
|
});
|
|
263
315
|
const unmount = () => {
|
|
264
316
|
loadingUnmount();
|
|
@@ -266,6 +318,7 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
266
318
|
};
|
|
267
319
|
clearCacheDataListener();
|
|
268
320
|
dataEvents.current = { unmount };
|
|
321
|
+
listenedQueryKey.current = scopeKey(req.queryKey, req.scope);
|
|
269
322
|
return unmount;
|
|
270
323
|
};
|
|
271
324
|
const addLifecycleListeners = (req, requestId, optimisticResult) => {
|
|
@@ -277,11 +330,11 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
277
330
|
if (!requestId) {
|
|
278
331
|
clearLifecycleListeners();
|
|
279
332
|
const { queryKey, cacheKey } = req;
|
|
280
|
-
const requestStartUnmount = requestManager.events.onRequestStartByQueue(queryKey, handleRequestStart());
|
|
281
|
-
const responseStartUnmount = requestManager.events.onResponseStartByQueue(queryKey, handleResponseStart());
|
|
282
|
-
const uploadUnmount = requestManager.events.onUploadProgressByQueue(queryKey, handleUploadProgress);
|
|
283
|
-
const downloadUnmount = requestManager.events.onDownloadProgressByQueue(queryKey, handleDownloadProgress);
|
|
284
|
-
const responseUnmount = requestManager.events.onResponseByCache(cacheKey, handleResponse());
|
|
333
|
+
const requestStartUnmount = requestManager.events.onRequestStartByQueue(queryKey, handleRequestStart(req));
|
|
334
|
+
const responseStartUnmount = requestManager.events.onResponseStartByQueue(queryKey, handleResponseStart(req));
|
|
335
|
+
const uploadUnmount = requestManager.events.onUploadProgressByQueue(queryKey, handleUploadProgress(req));
|
|
336
|
+
const downloadUnmount = requestManager.events.onDownloadProgressByQueue(queryKey, handleDownloadProgress(req));
|
|
337
|
+
const responseUnmount = requestManager.events.onResponseByCache(cacheKey, handleResponse(req));
|
|
285
338
|
const unmount = () => {
|
|
286
339
|
downloadUnmount();
|
|
287
340
|
uploadUnmount();
|
|
@@ -296,12 +349,17 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
296
349
|
* useSubmit handles requesting by requestIds, this makes it possible to track single requests
|
|
297
350
|
*/
|
|
298
351
|
if (optimisticResult) optimisticResultsRef.current.set(requestId, optimisticResult);
|
|
352
|
+
pendingRequests.current.add(requestId);
|
|
353
|
+
const onRequestStart = handleRequestStart(req);
|
|
299
354
|
const requestRemove = requestManager.events.onRemoveById(requestId, handleRemove);
|
|
300
|
-
const requestStartUnmount = requestManager.events.onRequestStartById(requestId,
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
const
|
|
355
|
+
const requestStartUnmount = requestManager.events.onRequestStartById(requestId, (details) => {
|
|
356
|
+
pendingRequests.current.add(requestId);
|
|
357
|
+
onRequestStart(details);
|
|
358
|
+
});
|
|
359
|
+
const responseStartUnmount = requestManager.events.onResponseStartById(requestId, handleResponseStart(req));
|
|
360
|
+
const responseUnmount = requestManager.events.onResponseById(requestId, handleResponse(req));
|
|
361
|
+
const uploadUnmount = requestManager.events.onUploadProgressById(requestId, handleUploadProgress(req));
|
|
362
|
+
const downloadUnmount = requestManager.events.onDownloadProgressById(requestId, handleDownloadProgress(req));
|
|
305
363
|
const unmount = () => {
|
|
306
364
|
requestRemove();
|
|
307
365
|
downloadUnmount();
|
|
@@ -314,12 +372,14 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
314
372
|
return unmount;
|
|
315
373
|
};
|
|
316
374
|
const abort = () => {
|
|
317
|
-
const
|
|
318
|
-
|
|
375
|
+
const currentRequest = requestRef.current;
|
|
376
|
+
const currentDispatcher = dispatcherRef.current;
|
|
377
|
+
const ak = scopeKey(currentRequest.abortKey, currentRequest.scope);
|
|
378
|
+
currentDispatcher.getAllRunningRequests().forEach((requestData) => {
|
|
319
379
|
const reqAk = scopeKey(requestData.request.abortKey, requestData.request.scope);
|
|
320
380
|
if (reqAk === ak) {
|
|
321
381
|
const qk = scopeKey(requestData.request.queryKey, requestData.request.scope);
|
|
322
|
-
|
|
382
|
+
currentDispatcher.delete(qk, requestData.requestId, reqAk);
|
|
323
383
|
}
|
|
324
384
|
});
|
|
325
385
|
};
|
|
@@ -330,7 +390,8 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
330
390
|
clearLifecycleListeners();
|
|
331
391
|
clearCacheDataListener();
|
|
332
392
|
});
|
|
333
|
-
|
|
393
|
+
const callbacksRef = useRef(null);
|
|
394
|
+
if (!callbacksRef.current) callbacksRef.current = {
|
|
334
395
|
abort,
|
|
335
396
|
onSuccess: (callback) => {
|
|
336
397
|
onSuccessCallback.current = callback;
|
|
@@ -359,7 +420,8 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
359
420
|
onUploadProgress: (callback) => {
|
|
360
421
|
onUploadProgressCallback.current = callback;
|
|
361
422
|
}
|
|
362
|
-
}
|
|
423
|
+
};
|
|
424
|
+
return [callbacksRef.current, {
|
|
363
425
|
addCacheDataListener,
|
|
364
426
|
clearCacheDataListener,
|
|
365
427
|
addLifecycleListeners,
|
|
@@ -613,11 +675,21 @@ var getCacheKeyIdentity = (cacheKey) => {
|
|
|
613
675
|
return cacheKey.substring(0, lastUnderscoreIndex);
|
|
614
676
|
};
|
|
615
677
|
/**
|
|
678
|
+
* Identity of the requested resource - method and endpoint with resolved params, without the query params.
|
|
679
|
+
* Two requests with the same identity ask for the same resource (e.g. another page or filter of one list).
|
|
680
|
+
*/
|
|
681
|
+
var getRequestIdentity = (request) => {
|
|
682
|
+
return `${request.scope ?? ""}_${String(request.method)}_${String(request.endpoint)}`;
|
|
683
|
+
};
|
|
684
|
+
/**
|
|
616
685
|
* Determines whether state should be cleared when the cache key changes.
|
|
617
686
|
*
|
|
618
687
|
* - `"clean"` — always clear
|
|
619
688
|
* - `"preserve"` — never clear
|
|
620
689
|
* - `"auto"` — clear when the resource identity changed (URL params), preserve when only query params changed
|
|
690
|
+
*
|
|
691
|
+
* In the "auto" mode the identity is read from the cache keys, which is not reliable for custom cache keys
|
|
692
|
+
* and for query params containing underscores - compare the requests with `getRequestIdentity` when they are available.
|
|
621
693
|
*/
|
|
622
694
|
var getShouldClearState = (mode, oldCacheKey, newCacheKey) => {
|
|
623
695
|
if (oldCacheKey === newCacheKey) return false;
|
|
@@ -722,11 +794,16 @@ var getInitialState = ({ initialResponse, dispatcher, request, disabled, revalid
|
|
|
722
794
|
var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, dependencyTracking, keepPreviousData = "auto", disabled, revalidate, select, structuralSharing = true }) => {
|
|
723
795
|
const { client, cacheKey, queryKey, staleTime } = request;
|
|
724
796
|
const { cache } = client;
|
|
797
|
+
const scopedCacheKey = scopeKey(cacheKey, request.scope);
|
|
798
|
+
const scopedQueryKey = scopeKey(queryKey, request.scope);
|
|
799
|
+
const identity = getRequestIdentity(request);
|
|
725
800
|
const hydration = useHydration();
|
|
726
801
|
const state = useRef(null);
|
|
727
802
|
const renderKeys = useRef([]);
|
|
728
803
|
const isProcessingData = useRef("");
|
|
729
|
-
const previousCacheKey = useRef(
|
|
804
|
+
const previousCacheKey = useRef(scopedCacheKey);
|
|
805
|
+
const previousIdentity = useRef(identity);
|
|
806
|
+
const isFreshState = useRef(true);
|
|
730
807
|
const selectRef = useRef(select);
|
|
731
808
|
selectRef.current = select;
|
|
732
809
|
const appliedSelect = useRef(select);
|
|
@@ -758,6 +835,22 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
758
835
|
disabled,
|
|
759
836
|
hydratedEntry
|
|
760
837
|
}));
|
|
838
|
+
} else if (previousCacheKey.current !== scopedCacheKey) {
|
|
839
|
+
const shouldClear = keepPreviousData === "auto" ? previousIdentity.current !== identity : getShouldClearState(keepPreviousData, previousCacheKey.current, scopedCacheKey);
|
|
840
|
+
previousCacheKey.current = scopedCacheKey;
|
|
841
|
+
previousIdentity.current = identity;
|
|
842
|
+
isFreshState.current = !!cache.get(scopedCacheKey) || shouldClear;
|
|
843
|
+
if (isFreshState.current) {
|
|
844
|
+
const hydratedEntry = hydration?.get(client, scopedCacheKey);
|
|
845
|
+
state.current = getSelectedState(getInitialState({
|
|
846
|
+
initialResponse,
|
|
847
|
+
dispatcher,
|
|
848
|
+
request,
|
|
849
|
+
disabled,
|
|
850
|
+
revalidate,
|
|
851
|
+
hydratedEntry
|
|
852
|
+
}));
|
|
853
|
+
}
|
|
761
854
|
}
|
|
762
855
|
if (selectError.current) {
|
|
763
856
|
const { error } = selectError.current;
|
|
@@ -783,8 +876,9 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
783
876
|
listenerRef.current?.();
|
|
784
877
|
};
|
|
785
878
|
const getStaleStatus = () => {
|
|
786
|
-
const cacheData = cache.get(
|
|
787
|
-
|
|
879
|
+
const cacheData = cache.get(scopedCacheKey);
|
|
880
|
+
const isInvalidated = cacheData?.staleTime === 0;
|
|
881
|
+
return !cacheData || isInvalidated || isStaleCacheData(staleTime, cacheData?.responseTimestamp);
|
|
788
882
|
};
|
|
789
883
|
const renderKeyTrigger = (keys) => {
|
|
790
884
|
if (renderKeys.current.some((renderKey) => keys.includes(renderKey))) emitChange();
|
|
@@ -793,21 +887,10 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
793
887
|
if (!renderKeys.current.includes(renderKey)) renderKeys.current.push(renderKey);
|
|
794
888
|
};
|
|
795
889
|
i(() => {
|
|
796
|
-
const
|
|
797
|
-
|
|
798
|
-
const cacheState = getValidCacheData(request, initialResponse, cache.get(scopeKey(cacheKey, request.scope)));
|
|
799
|
-
if (getShouldClearState(keepPreviousData, oldKey, cacheKey) && !cacheState) {
|
|
800
|
-
state.current = getSelectedState(getInitialState({
|
|
801
|
-
initialResponse,
|
|
802
|
-
dispatcher,
|
|
803
|
-
request,
|
|
804
|
-
disabled,
|
|
805
|
-
revalidate
|
|
806
|
-
}));
|
|
807
|
-
renderKeyTrigger(Object.keys(state.current));
|
|
808
|
-
} else if (cacheState) {
|
|
890
|
+
const cacheState = cache.get(scopedCacheKey) || (isFreshState.current ? getValidCacheData(request, initialResponse, null) : null);
|
|
891
|
+
if (cacheState) {
|
|
809
892
|
state.current.loading = getIsInitiallyLoading({
|
|
810
|
-
queryKey:
|
|
893
|
+
queryKey: scopedQueryKey,
|
|
811
894
|
dispatcher,
|
|
812
895
|
disabled,
|
|
813
896
|
revalidate,
|
|
@@ -815,7 +898,7 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
815
898
|
});
|
|
816
899
|
setCacheData(cacheState);
|
|
817
900
|
}
|
|
818
|
-
}, [
|
|
901
|
+
}, [scopedCacheKey, scopedQueryKey], true);
|
|
819
902
|
i(() => {
|
|
820
903
|
const handleDependencyTracking = () => {
|
|
821
904
|
if (!dependencyTracking) Object.keys(state.current).forEach((key) => setRenderKey(key));
|
|
@@ -854,7 +937,7 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
854
937
|
retries: cacheData.retries,
|
|
855
938
|
responseTimestamp: new Date(cacheData.responseTimestamp),
|
|
856
939
|
requestTimestamp: new Date(cacheData.requestTimestamp),
|
|
857
|
-
loading: dispatcher.hasRunningRequests(
|
|
940
|
+
loading: dispatcher.hasRunningRequests(scopedQueryKey)
|
|
858
941
|
};
|
|
859
942
|
const nextState = { ...state.current };
|
|
860
943
|
const changedKeys = Object.keys(newStateValues).filter((key) => {
|
|
@@ -898,78 +981,85 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
898
981
|
...data
|
|
899
982
|
});
|
|
900
983
|
};
|
|
984
|
+
const actionsRef = useRef(null);
|
|
985
|
+
if (!actionsRef.current) actionsRef.current = {
|
|
986
|
+
setData: (data) => {
|
|
987
|
+
isLocalData.current = true;
|
|
988
|
+
state.current.data = data instanceof Function ? data(state.current.data ?? null) : data;
|
|
989
|
+
renderKeyTrigger(["data"]);
|
|
990
|
+
},
|
|
991
|
+
setError: (error) => {
|
|
992
|
+
state.current.error = error instanceof Function ? error(state.current.error ?? null) : error;
|
|
993
|
+
renderKeyTrigger(["error"]);
|
|
994
|
+
},
|
|
995
|
+
setLoading: (loading) => {
|
|
996
|
+
const value = loading instanceof Function ? loading(state.current.loading) : loading;
|
|
997
|
+
if (value === state.current.loading) return;
|
|
998
|
+
state.current.loading = value;
|
|
999
|
+
renderKeyTrigger(["loading"]);
|
|
1000
|
+
},
|
|
1001
|
+
setStatus: (status) => {
|
|
1002
|
+
const value = status instanceof Function ? status(state.current.status ?? null) : status;
|
|
1003
|
+
if (value === state.current.status) return;
|
|
1004
|
+
state.current.status = value;
|
|
1005
|
+
renderKeyTrigger(["status"]);
|
|
1006
|
+
},
|
|
1007
|
+
setSuccess: (success) => {
|
|
1008
|
+
const value = success instanceof Function ? success(state.current.success || false) : success;
|
|
1009
|
+
if (value === state.current.success) return;
|
|
1010
|
+
state.current.success = value;
|
|
1011
|
+
renderKeyTrigger(["success"]);
|
|
1012
|
+
},
|
|
1013
|
+
setExtra: (extra) => {
|
|
1014
|
+
const value = extra instanceof Function ? extra(state.current.extra) : extra;
|
|
1015
|
+
if (value === state.current.extra) return;
|
|
1016
|
+
state.current.extra = value;
|
|
1017
|
+
renderKeyTrigger(["extra"]);
|
|
1018
|
+
},
|
|
1019
|
+
setRetries: (retries) => {
|
|
1020
|
+
const value = retries instanceof Function ? retries(state.current.retries || 0) : retries;
|
|
1021
|
+
if (value === state.current.retries) return;
|
|
1022
|
+
state.current.retries = value;
|
|
1023
|
+
renderKeyTrigger(["retries"]);
|
|
1024
|
+
},
|
|
1025
|
+
setResponseTimestamp: (timestamp) => {
|
|
1026
|
+
if ((timestamp instanceof Function ? timestamp(state.current.responseTimestamp) : timestamp) === state.current.responseTimestamp) return;
|
|
1027
|
+
const getTimestamp = (prev) => {
|
|
1028
|
+
return timestamp instanceof Function ? timestamp(prev ? new Date(prev) : null) : timestamp;
|
|
1029
|
+
};
|
|
1030
|
+
state.current.responseTimestamp = getTimestamp(state.current.responseTimestamp);
|
|
1031
|
+
renderKeyTrigger(["responseTimestamp"]);
|
|
1032
|
+
},
|
|
1033
|
+
setRequestTimestamp: (timestamp) => {
|
|
1034
|
+
if ((timestamp instanceof Function ? timestamp(state.current.requestTimestamp) : timestamp) === state.current.requestTimestamp) return;
|
|
1035
|
+
const getTimestamp = (prev) => {
|
|
1036
|
+
return timestamp instanceof Function ? timestamp(prev ? new Date(prev) : null) : timestamp;
|
|
1037
|
+
};
|
|
1038
|
+
state.current.requestTimestamp = getTimestamp(state.current.requestTimestamp);
|
|
1039
|
+
renderKeyTrigger(["requestTimestamp"]);
|
|
1040
|
+
},
|
|
1041
|
+
clearState: () => {
|
|
1042
|
+
rawData.current = null;
|
|
1043
|
+
isLocalData.current = false;
|
|
1044
|
+
responseDetails.current = null;
|
|
1045
|
+
state.current = {
|
|
1046
|
+
data: null,
|
|
1047
|
+
error: null,
|
|
1048
|
+
loading: false,
|
|
1049
|
+
status: null,
|
|
1050
|
+
success: false,
|
|
1051
|
+
extra: client.adapter.defaultExtra,
|
|
1052
|
+
retries: 0,
|
|
1053
|
+
responseTimestamp: null,
|
|
1054
|
+
requestTimestamp: null
|
|
1055
|
+
};
|
|
1056
|
+
renderKeyTrigger(Object.keys(state.current));
|
|
1057
|
+
}
|
|
1058
|
+
};
|
|
1059
|
+
const actions = actionsRef.current;
|
|
901
1060
|
return [
|
|
902
1061
|
state.current,
|
|
903
|
-
|
|
904
|
-
setData: (data) => {
|
|
905
|
-
isLocalData.current = true;
|
|
906
|
-
state.current.data = data instanceof Function ? data(state.current.data || null) : data;
|
|
907
|
-
renderKeyTrigger(["data"]);
|
|
908
|
-
},
|
|
909
|
-
setError: (error) => {
|
|
910
|
-
state.current.error = error instanceof Function ? error(state.current.error || null) : error;
|
|
911
|
-
renderKeyTrigger(["error"]);
|
|
912
|
-
},
|
|
913
|
-
setLoading: (loading) => {
|
|
914
|
-
const value = loading instanceof Function ? loading(state.current.loading) : loading;
|
|
915
|
-
if (value === state.current.loading) return;
|
|
916
|
-
state.current.loading = value;
|
|
917
|
-
renderKeyTrigger(["loading"]);
|
|
918
|
-
},
|
|
919
|
-
setStatus: (status) => {
|
|
920
|
-
if ((status instanceof Function ? status(state.current.status) : status) === state.current.status) return;
|
|
921
|
-
state.current.status = status instanceof Function ? status(state.current.status || null) : status;
|
|
922
|
-
renderKeyTrigger(["status"]);
|
|
923
|
-
},
|
|
924
|
-
setSuccess: (success) => {
|
|
925
|
-
if ((success instanceof Function ? success(state.current.success || false) : success) === state.current.success) return;
|
|
926
|
-
state.current.success = success instanceof Function ? success(state.current.success || false) : success;
|
|
927
|
-
renderKeyTrigger(["success"]);
|
|
928
|
-
},
|
|
929
|
-
setExtra: (extra) => {
|
|
930
|
-
if ((extra instanceof Function ? extra(state.current.extra) : extra) === state.current.extra) return;
|
|
931
|
-
state.current.extra = extra instanceof Function ? extra(state.current.extra) : extra;
|
|
932
|
-
renderKeyTrigger(["extra"]);
|
|
933
|
-
},
|
|
934
|
-
setRetries: (retries) => {
|
|
935
|
-
if ((retries instanceof Function ? retries(state.current.retries || 0) : retries) === state.current.retries) return;
|
|
936
|
-
state.current.retries = retries instanceof Function ? retries(state.current.retries || 0) : retries;
|
|
937
|
-
renderKeyTrigger(["retries"]);
|
|
938
|
-
},
|
|
939
|
-
setResponseTimestamp: (timestamp) => {
|
|
940
|
-
if ((timestamp instanceof Function ? timestamp(state.current.responseTimestamp) : timestamp) === state.current.responseTimestamp) return;
|
|
941
|
-
const getTimestamp = (prev) => {
|
|
942
|
-
return timestamp instanceof Function ? timestamp(prev ? new Date(prev) : null) : timestamp;
|
|
943
|
-
};
|
|
944
|
-
state.current.responseTimestamp = getTimestamp(state.current.responseTimestamp);
|
|
945
|
-
renderKeyTrigger(["responseTimestamp"]);
|
|
946
|
-
},
|
|
947
|
-
setRequestTimestamp: (timestamp) => {
|
|
948
|
-
if ((timestamp instanceof Function ? timestamp(state.current.requestTimestamp) : timestamp) === state.current.requestTimestamp) return;
|
|
949
|
-
const getTimestamp = (prev) => {
|
|
950
|
-
return timestamp instanceof Function ? timestamp(prev ? new Date(prev) : null) : timestamp;
|
|
951
|
-
};
|
|
952
|
-
state.current.requestTimestamp = getTimestamp(state.current.requestTimestamp);
|
|
953
|
-
renderKeyTrigger(["requestTimestamp"]);
|
|
954
|
-
},
|
|
955
|
-
clearState: () => {
|
|
956
|
-
rawData.current = null;
|
|
957
|
-
isLocalData.current = false;
|
|
958
|
-
responseDetails.current = null;
|
|
959
|
-
state.current = {
|
|
960
|
-
data: null,
|
|
961
|
-
error: null,
|
|
962
|
-
loading: false,
|
|
963
|
-
status: null,
|
|
964
|
-
success: false,
|
|
965
|
-
extra: null,
|
|
966
|
-
retries: 0,
|
|
967
|
-
responseTimestamp: null,
|
|
968
|
-
requestTimestamp: null
|
|
969
|
-
};
|
|
970
|
-
renderKeyTrigger(Object.keys(state.current));
|
|
971
|
-
}
|
|
972
|
-
},
|
|
1062
|
+
actions,
|
|
973
1063
|
{
|
|
974
1064
|
setRenderKey,
|
|
975
1065
|
setCacheData,
|
|
@@ -1143,6 +1233,11 @@ var ConfigContext = React.createContext({
|
|
|
1143
1233
|
*/
|
|
1144
1234
|
var Provider = ({ children, config }) => {
|
|
1145
1235
|
const [currentConfig, setConfig] = useState(config || {});
|
|
1236
|
+
const previousConfig = useRef(config);
|
|
1237
|
+
if (!isEqual(previousConfig.current, config)) {
|
|
1238
|
+
previousConfig.current = config;
|
|
1239
|
+
setConfig(config || {});
|
|
1240
|
+
}
|
|
1146
1241
|
const value = useMemo(() => {
|
|
1147
1242
|
return {
|
|
1148
1243
|
config: currentConfig,
|
|
@@ -1154,7 +1249,7 @@ var Provider = ({ children, config }) => {
|
|
|
1154
1249
|
children
|
|
1155
1250
|
}, void 0, false, {
|
|
1156
1251
|
fileName: _jsxFileName,
|
|
1157
|
-
lineNumber:
|
|
1252
|
+
lineNumber: 31,
|
|
1158
1253
|
columnNumber: 10
|
|
1159
1254
|
}, void 0);
|
|
1160
1255
|
};
|
|
@@ -1167,20 +1262,41 @@ var useProvider = () => {
|
|
|
1167
1262
|
};
|
|
1168
1263
|
//#endregion
|
|
1169
1264
|
//#region src/hooks/use-fetch/use-fetch.hooks.ts
|
|
1265
|
+
/**
|
|
1266
|
+
* Suspended components lose their state, so the pending promises and the responses that are not stored in the cache
|
|
1267
|
+
* have to live outside of them. They are kept per cache instance - every client (and every cleared client)
|
|
1268
|
+
* has its own store, so responses never leak between the clients.
|
|
1269
|
+
*/
|
|
1170
1270
|
var suspenseStores = /* @__PURE__ */ new WeakMap();
|
|
1171
|
-
var
|
|
1172
|
-
|
|
1173
|
-
let store = suspenseStores.get(client);
|
|
1271
|
+
var getSuspenseStore = (cache) => {
|
|
1272
|
+
let store = suspenseStores.get(cache);
|
|
1174
1273
|
if (!store) {
|
|
1175
1274
|
store = {
|
|
1176
1275
|
promises: /* @__PURE__ */ new Map(),
|
|
1177
1276
|
results: /* @__PURE__ */ new Map()
|
|
1178
1277
|
};
|
|
1179
|
-
suspenseStores.set(
|
|
1278
|
+
suspenseStores.set(cache, store);
|
|
1180
1279
|
}
|
|
1181
1280
|
return store;
|
|
1182
1281
|
};
|
|
1183
1282
|
/**
|
|
1283
|
+
* Data received by the suspended component is not fetched again when it mounts right after.
|
|
1284
|
+
* Responses waiting for the suspended component are dropped after this time - the component is gone.
|
|
1285
|
+
*/
|
|
1286
|
+
var suspenseFreshTime = 1e3;
|
|
1287
|
+
/**
|
|
1288
|
+
* Options passed as `undefined` (e.g. forwarded optional props) fall back to the defaults instead of overriding them
|
|
1289
|
+
*/
|
|
1290
|
+
var mergeOptions$1 = (...sources) => {
|
|
1291
|
+
const merged = {};
|
|
1292
|
+
sources.forEach((source) => {
|
|
1293
|
+
Object.entries(source || {}).forEach(([key, value]) => {
|
|
1294
|
+
if (value !== void 0) merged[key] = value;
|
|
1295
|
+
});
|
|
1296
|
+
});
|
|
1297
|
+
return merged;
|
|
1298
|
+
};
|
|
1299
|
+
/**
|
|
1184
1300
|
* This hook aims to retrieve data from the server. It automatically fetches on mount and
|
|
1185
1301
|
* refetches based on dependencies, with support for caching, polling, and suspense.
|
|
1186
1302
|
* @param request Request instance
|
|
@@ -1189,11 +1305,7 @@ var getSuspenseStore = (client) => {
|
|
|
1189
1305
|
*/
|
|
1190
1306
|
var useFetch = (request, options) => {
|
|
1191
1307
|
const { config: globalConfig } = useProvider();
|
|
1192
|
-
const { suspense, dependencies, disabled, dependencyTracking, revalidate, initialResponse, keepPreviousData, refresh, refreshTime, refetchBlurred, refetchOnBlur, refetchOnFocus, refetchOnReconnect, bounce, bounceType, bounceTime, bounceTimeout, deepCompare, structuralSharing, throwOnError } =
|
|
1193
|
-
...useFetchDefaultOptions,
|
|
1194
|
-
...globalConfig.useFetchConfig,
|
|
1195
|
-
...options
|
|
1196
|
-
};
|
|
1308
|
+
const { suspense, dependencies, disabled, dependencyTracking, revalidate, initialResponse, keepPreviousData, refresh, refreshTime, refetchBlurred, refetchOnBlur, refetchOnFocus, refetchOnReconnect, bounce, bounceType, bounceTime, bounceTimeout, deepCompare, structuralSharing, throwOnError } = mergeOptions$1(useFetchDefaultOptions, globalConfig.useFetchConfig, options);
|
|
1197
1309
|
const updateKey = JSON.stringify(request.toJSON());
|
|
1198
1310
|
const requestDebounce = f({ delay: bounceTime });
|
|
1199
1311
|
const requestThrottle = p({
|
|
@@ -1203,8 +1315,10 @@ var useFetch = (request, options) => {
|
|
|
1203
1315
|
const refreshDebounce = f({ delay: refreshTime });
|
|
1204
1316
|
const { cacheKey, queryKey, client } = request;
|
|
1205
1317
|
const { cache, fetchDispatcher: dispatcher, appManager, loggerManager } = client;
|
|
1206
|
-
const
|
|
1207
|
-
const
|
|
1318
|
+
const scopedCacheKey = scopeKey(cacheKey, request.scope);
|
|
1319
|
+
const scopedQueryKey = scopeKey(queryKey, request.scope);
|
|
1320
|
+
const fetchDependencies = useRef(null);
|
|
1321
|
+
const isSuspenseResolved = useRef(false);
|
|
1208
1322
|
const logger = useRef(loggerManager.initialize(client, "useFetch")).current;
|
|
1209
1323
|
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
1210
1324
|
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
@@ -1257,57 +1371,71 @@ var useFetch = (request, options) => {
|
|
|
1257
1371
|
return;
|
|
1258
1372
|
}
|
|
1259
1373
|
refreshDebounce.debounce(() => {
|
|
1260
|
-
|
|
1261
|
-
const scopedQueryKey = scopeKey(request.queryKey, request.scope);
|
|
1262
|
-
const isFetching = dispatcher.hasRunningRequests(scopedQueryKey);
|
|
1263
|
-
const isQueued = dispatcher.getIsActiveQueue(scopedQueryKey);
|
|
1264
|
-
const isActive = isFetching || isQueued;
|
|
1265
|
-
if (isBlurred && refetchBlurred && !isActive || !isBlurred && !isActive) {
|
|
1266
|
-
handleFetch();
|
|
1267
|
-
logger.debug({
|
|
1268
|
-
title: `Performing refresh request`,
|
|
1269
|
-
type: "system",
|
|
1270
|
-
extra: { request }
|
|
1271
|
-
});
|
|
1272
|
-
}
|
|
1273
|
-
handleRefresh();
|
|
1374
|
+
latest.current.refresh();
|
|
1274
1375
|
});
|
|
1275
1376
|
}
|
|
1276
|
-
const
|
|
1277
|
-
|
|
1377
|
+
const performRefresh = () => {
|
|
1378
|
+
const isBlurred = !appManager.isFocused;
|
|
1379
|
+
const isFetching = dispatcher.hasRunningRequests(scopedQueryKey);
|
|
1380
|
+
const isQueued = dispatcher.getIsActiveQueue(scopedQueryKey);
|
|
1381
|
+
const isActive = isFetching || isQueued;
|
|
1382
|
+
if (isBlurred && refetchBlurred && !isActive || !isBlurred && !isActive) {
|
|
1383
|
+
handleFetch();
|
|
1384
|
+
logger.debug({
|
|
1385
|
+
title: `Performing refresh request`,
|
|
1386
|
+
type: "system",
|
|
1387
|
+
extra: { request }
|
|
1388
|
+
});
|
|
1389
|
+
}
|
|
1278
1390
|
handleRefresh();
|
|
1279
1391
|
};
|
|
1280
|
-
const
|
|
1281
|
-
|
|
1392
|
+
const handleRefetch = () => {
|
|
1393
|
+
handleFetch({ force: true });
|
|
1394
|
+
handleRefresh();
|
|
1282
1395
|
};
|
|
1283
1396
|
/**
|
|
1284
|
-
*
|
|
1285
|
-
*
|
|
1286
|
-
* applied by other suspense-enabled libraries.
|
|
1397
|
+
* Events and timers are registered once and live longer than a single render.
|
|
1398
|
+
* This ref gives them the handlers and options from the latest render.
|
|
1287
1399
|
*/
|
|
1288
|
-
const
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1400
|
+
const latest = useRef({
|
|
1401
|
+
disabled,
|
|
1402
|
+
refetchOnFocus,
|
|
1403
|
+
refetchOnBlur,
|
|
1404
|
+
refetchOnReconnect,
|
|
1405
|
+
fetch: handleFetch,
|
|
1406
|
+
refresh: performRefresh,
|
|
1407
|
+
restartRefresh: handleRefresh,
|
|
1408
|
+
refetch: handleRefetch
|
|
1409
|
+
});
|
|
1410
|
+
latest.current = {
|
|
1411
|
+
disabled,
|
|
1412
|
+
refetchOnFocus,
|
|
1413
|
+
refetchOnBlur,
|
|
1414
|
+
refetchOnReconnect,
|
|
1415
|
+
fetch: handleFetch,
|
|
1416
|
+
refresh: performRefresh,
|
|
1417
|
+
restartRefresh: handleRefresh,
|
|
1418
|
+
refetch: handleRefetch
|
|
1419
|
+
};
|
|
1420
|
+
const refetch = useCallback(() => {
|
|
1421
|
+
latest.current.refetch();
|
|
1422
|
+
}, []);
|
|
1423
|
+
const getIsFetchingIdentity = () => {
|
|
1424
|
+
return dispatcher.getRunningRequests(scopedQueryKey).some((running) => running.request.cacheKey === cacheKey);
|
|
1293
1425
|
};
|
|
1294
1426
|
const initialFetchData = () => {
|
|
1295
|
-
if (getHasJustResolvedSuspense()) return;
|
|
1296
1427
|
const hasStaleData = getStaleStatus();
|
|
1297
1428
|
const isFetching = getIsFetchingIdentity();
|
|
1298
|
-
|
|
1429
|
+
const cacheTimestamp = cache.get(scopedCacheKey)?.responseTimestamp;
|
|
1430
|
+
const isFreshSuspenseData = suspense && (isSuspenseResolved.current || typeof cacheTimestamp === "number" && Date.now() - cacheTimestamp < suspenseFreshTime);
|
|
1431
|
+
isSuspenseResolved.current = false;
|
|
1432
|
+
if ((revalidate || hasStaleData) && !isFetching && !isFreshSuspenseData) handleFetch();
|
|
1299
1433
|
};
|
|
1300
1434
|
const updateFetchData = () => {
|
|
1301
1435
|
const hasStaleData = getStaleStatus();
|
|
1302
|
-
|
|
1303
|
-
/**
|
|
1304
|
-
* This is a hack to avoid double rendering in React 18
|
|
1305
|
-
* It renders initial mount event and allow us to consume only hook updates
|
|
1306
|
-
*/
|
|
1307
|
-
if (!ignoreReact18DoubleRender.current && shouldUpdate)
|
|
1436
|
+
if (!revalidate ? hasStaleData : true)
|
|
1308
1437
|
/**
|
|
1309
|
-
*
|
|
1310
|
-
* This way it will not wait for debouncing but fetch data right away
|
|
1438
|
+
* Only the updates are bounced - the initial request is sent right away
|
|
1311
1439
|
*/
|
|
1312
1440
|
if (bounce) {
|
|
1313
1441
|
logger.debug({
|
|
@@ -1318,33 +1446,53 @@ var useFetch = (request, options) => {
|
|
|
1318
1446
|
request
|
|
1319
1447
|
}
|
|
1320
1448
|
});
|
|
1321
|
-
bounceFunction(() =>
|
|
1449
|
+
bounceFunction(() => latest.current.fetch());
|
|
1322
1450
|
} else handleFetch();
|
|
1323
|
-
|
|
1451
|
+
};
|
|
1452
|
+
/**
|
|
1453
|
+
* Effects can run again without any change - on StrictMode remount or when the hidden <Activity> is revealed.
|
|
1454
|
+
* We compare the dependencies with the ones already handled, to fetch once on mount and once per change.
|
|
1455
|
+
*/
|
|
1456
|
+
const handleFetchLifecycle = () => {
|
|
1457
|
+
const currentDependencies = [
|
|
1458
|
+
updateKey,
|
|
1459
|
+
disabled,
|
|
1460
|
+
...dependencies
|
|
1461
|
+
];
|
|
1462
|
+
const previousDependencies = fetchDependencies.current;
|
|
1463
|
+
fetchDependencies.current = currentDependencies;
|
|
1464
|
+
if (!previousDependencies) {
|
|
1465
|
+
initialFetchData();
|
|
1466
|
+
return;
|
|
1467
|
+
}
|
|
1468
|
+
if (previousDependencies.length !== currentDependencies.length || currentDependencies.some((dependency, index) => !Object.is(dependency, previousDependencies[index]))) updateFetchData();
|
|
1324
1469
|
};
|
|
1325
1470
|
const handleMountEvents = () => {
|
|
1326
1471
|
addCacheDataListener(request);
|
|
1327
1472
|
addLifecycleListeners(request);
|
|
1328
1473
|
const focusUnmount = appManager.events.onFocus(() => {
|
|
1329
|
-
if (refetchOnFocus && !disabled) {
|
|
1330
|
-
|
|
1331
|
-
|
|
1474
|
+
if (latest.current.refetchOnFocus && !latest.current.disabled) {
|
|
1475
|
+
latest.current.fetch();
|
|
1476
|
+
latest.current.restartRefresh();
|
|
1332
1477
|
}
|
|
1333
1478
|
});
|
|
1334
1479
|
const blurUnmount = appManager.events.onBlur(() => {
|
|
1335
|
-
if (refetchOnBlur && !disabled) {
|
|
1336
|
-
|
|
1337
|
-
|
|
1480
|
+
if (latest.current.refetchOnBlur && !latest.current.disabled) {
|
|
1481
|
+
latest.current.fetch();
|
|
1482
|
+
latest.current.restartRefresh();
|
|
1338
1483
|
}
|
|
1339
1484
|
});
|
|
1340
1485
|
const onlineUnmount = appManager.events.onOnline(() => {
|
|
1341
|
-
if (refetchOnReconnect && !disabled) {
|
|
1342
|
-
|
|
1343
|
-
|
|
1486
|
+
if (latest.current.refetchOnReconnect && !latest.current.disabled) {
|
|
1487
|
+
latest.current.fetch();
|
|
1488
|
+
latest.current.restartRefresh();
|
|
1344
1489
|
}
|
|
1345
1490
|
});
|
|
1346
|
-
const
|
|
1347
|
-
|
|
1491
|
+
const handleCacheChange = (changedKey) => {
|
|
1492
|
+
if (changedKey === scopedCacheKey) latest.current.fetch();
|
|
1493
|
+
};
|
|
1494
|
+
const invalidateUnmount = cache.events.onInvalidateByKey(cacheKey, handleCacheChange);
|
|
1495
|
+
const deletionUnmount = cache.events.onDeleteByKey(cacheKey, handleCacheChange);
|
|
1348
1496
|
const unmount = () => {
|
|
1349
1497
|
clearCacheDataListener();
|
|
1350
1498
|
focusUnmount();
|
|
@@ -1362,12 +1510,9 @@ var useFetch = (request, options) => {
|
|
|
1362
1510
|
i(handleMountEvents, [updateKey, disabled], true);
|
|
1363
1511
|
/**
|
|
1364
1512
|
* Initial fetch triggered once data is stale or we use the refetch strategy
|
|
1513
|
+
* and the fetching logic for updates handling
|
|
1365
1514
|
*/
|
|
1366
|
-
|
|
1367
|
-
/**
|
|
1368
|
-
* Fetching logic for updates handling
|
|
1369
|
-
*/
|
|
1370
|
-
i(updateFetchData, [
|
|
1515
|
+
i(handleFetchLifecycle, [
|
|
1371
1516
|
updateKey,
|
|
1372
1517
|
disabled,
|
|
1373
1518
|
...dependencies
|
|
@@ -1382,28 +1527,28 @@ var useFetch = (request, options) => {
|
|
|
1382
1527
|
refresh,
|
|
1383
1528
|
refreshTime
|
|
1384
1529
|
], true);
|
|
1385
|
-
/**
|
|
1386
|
-
* Reset the ignore flag for React 18 strict mode
|
|
1387
|
-
*/
|
|
1388
|
-
d$1(() => {
|
|
1389
|
-
ignoreReact18DoubleRender.current = true;
|
|
1390
|
-
});
|
|
1391
1530
|
if (suspense && !disabled) {
|
|
1392
|
-
const
|
|
1393
|
-
const
|
|
1394
|
-
const suspenseResult =
|
|
1531
|
+
const store = getSuspenseStore(cache);
|
|
1532
|
+
const storedResult = store.results.get(scopedCacheKey);
|
|
1533
|
+
const suspenseResult = storedResult && Date.now() - storedResult.receivedTimestamp <= suspenseFreshTime ? storedResult : null;
|
|
1534
|
+
if (storedResult && !suspenseResult) store.results.delete(scopedCacheKey);
|
|
1395
1535
|
if (suspenseResult) {
|
|
1396
|
-
|
|
1397
|
-
|
|
1536
|
+
queueMicrotask(() => {
|
|
1537
|
+
if (store.results.get(scopedCacheKey) === suspenseResult) store.results.delete(scopedCacheKey);
|
|
1538
|
+
});
|
|
1539
|
+
isSuspenseResolved.current = true;
|
|
1398
1540
|
if (suspenseResult.data !== null) state.data = selectData(suspenseResult.data);
|
|
1399
1541
|
if (suspenseResult.error !== null) state.error = suspenseResult.error;
|
|
1400
1542
|
if (suspenseResult.status !== null) state.status = suspenseResult.status;
|
|
1401
1543
|
if (suspenseResult.extra !== null) state.extra = suspenseResult.extra;
|
|
1402
1544
|
state.success = suspenseResult.success;
|
|
1403
|
-
state.loading = dispatcher.hasRunningRequests(
|
|
1545
|
+
state.loading = dispatcher.hasRunningRequests(scopedQueryKey);
|
|
1546
|
+
state.retries = suspenseResult.retries ?? state.retries;
|
|
1547
|
+
state.responseTimestamp = new Date(suspenseResult.responseTimestamp);
|
|
1548
|
+
state.requestTimestamp = new Date(suspenseResult.requestTimestamp);
|
|
1404
1549
|
}
|
|
1405
|
-
if (!(state.data !== null || state.error !== null)) {
|
|
1406
|
-
let entry =
|
|
1550
|
+
if (!(state.data !== null || state.error !== null || state.responseTimestamp !== null)) {
|
|
1551
|
+
let entry = store.promises.get(scopedCacheKey);
|
|
1407
1552
|
if (!entry) {
|
|
1408
1553
|
let resolvePromise;
|
|
1409
1554
|
const promise = new Promise((r) => {
|
|
@@ -1411,32 +1556,35 @@ var useFetch = (request, options) => {
|
|
|
1411
1556
|
});
|
|
1412
1557
|
const unsubscribe = cache.events.onDataByKey(cacheKey, (cacheData) => {
|
|
1413
1558
|
if (!isSameScope(cacheData.scope, request.scope)) return;
|
|
1414
|
-
|
|
1559
|
+
store.results.set(scopedCacheKey, {
|
|
1415
1560
|
data: cacheData.data ?? null,
|
|
1416
1561
|
error: cacheData.error ?? null,
|
|
1417
1562
|
status: cacheData.status ?? null,
|
|
1418
1563
|
extra: cacheData.extra ?? null,
|
|
1419
|
-
success: cacheData.success ?? false
|
|
1564
|
+
success: cacheData.success ?? false,
|
|
1565
|
+
retries: cacheData.retries ?? 0,
|
|
1566
|
+
responseTimestamp: cacheData.responseTimestamp ?? Date.now(),
|
|
1567
|
+
requestTimestamp: cacheData.requestTimestamp ?? Date.now(),
|
|
1568
|
+
receivedTimestamp: Date.now()
|
|
1420
1569
|
});
|
|
1421
1570
|
resolvePromise();
|
|
1422
1571
|
unsubscribe();
|
|
1423
|
-
|
|
1572
|
+
store.promises.delete(scopedCacheKey);
|
|
1424
1573
|
});
|
|
1425
1574
|
entry = {
|
|
1426
1575
|
promise,
|
|
1427
1576
|
resolve: resolvePromise,
|
|
1428
1577
|
cleanup: unsubscribe
|
|
1429
1578
|
};
|
|
1430
|
-
|
|
1579
|
+
store.promises.set(scopedCacheKey, entry);
|
|
1431
1580
|
}
|
|
1432
|
-
const scopedQueryKey = scopeKey(queryKey, request.scope);
|
|
1433
1581
|
if (!dispatcher.hasRunningRequests(scopedQueryKey) && !dispatcher.getIsActiveQueue(scopedQueryKey)) dispatcher.add(request);
|
|
1434
1582
|
throw entry.promise;
|
|
1435
1583
|
}
|
|
1436
|
-
const entry =
|
|
1584
|
+
const entry = store.promises.get(scopedCacheKey);
|
|
1437
1585
|
if (entry) {
|
|
1438
1586
|
entry.cleanup();
|
|
1439
|
-
|
|
1587
|
+
store.promises.delete(scopedCacheKey);
|
|
1440
1588
|
}
|
|
1441
1589
|
}
|
|
1442
1590
|
if (throwOnError) {
|
|
@@ -1595,9 +1743,50 @@ var loadPage = async (options) => {
|
|
|
1595
1743
|
return sendPage(options);
|
|
1596
1744
|
};
|
|
1597
1745
|
//#endregion
|
|
1598
|
-
//#region src/hooks/use-infinite-fetch/use-infinite-fetch.
|
|
1599
|
-
|
|
1746
|
+
//#region src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts
|
|
1747
|
+
/**
|
|
1748
|
+
* Identity of a list. Everything describing the request takes part in it, so a changed filter, sort, page size
|
|
1749
|
+
* or URL param starts a new list without anything having to be listed by hand.
|
|
1750
|
+
*/
|
|
1751
|
+
var getInfiniteKey = (request, pagination, maxPages) => {
|
|
1752
|
+
return JSON.stringify([
|
|
1753
|
+
request.toJSON(),
|
|
1754
|
+
pagination,
|
|
1755
|
+
maxPages ?? null
|
|
1756
|
+
], (_key, value) => {
|
|
1757
|
+
return typeof value === "function" ? value.toString() : value;
|
|
1758
|
+
});
|
|
1759
|
+
};
|
|
1760
|
+
/**
|
|
1761
|
+
* Keeps the first occurrence of every item
|
|
1762
|
+
*/
|
|
1763
|
+
var getUniqueItems = (items, uniqueBy) => {
|
|
1764
|
+
if (uniqueBy === void 0) return items;
|
|
1765
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1766
|
+
return items.filter((item) => {
|
|
1767
|
+
const identity = typeof uniqueBy === "function" ? uniqueBy({ item }) : item?.[uniqueBy];
|
|
1768
|
+
if (seen.has(identity)) return false;
|
|
1769
|
+
seen.add(identity);
|
|
1770
|
+
return true;
|
|
1771
|
+
});
|
|
1772
|
+
};
|
|
1773
|
+
/**
|
|
1774
|
+
* Decides if the previous list stays on screen while the new one loads its first page
|
|
1775
|
+
*/
|
|
1776
|
+
var getShouldKeepPreviousList = (mode, previous, current) => {
|
|
1777
|
+
if (mode === "auto") return previous.method === current.method && previous.endpoint === current.endpoint;
|
|
1778
|
+
return mode === "preserve";
|
|
1779
|
+
};
|
|
1780
|
+
/**
|
|
1781
|
+
* Tells whether nothing changed between two states of a list, so nobody has to be notified
|
|
1782
|
+
*/
|
|
1783
|
+
var isSameSnapshot = (a, b) => {
|
|
1784
|
+
return Object.keys(a).every((key) => a[key] === b[key]);
|
|
1785
|
+
};
|
|
1600
1786
|
var stores = /* @__PURE__ */ new WeakMap();
|
|
1787
|
+
/**
|
|
1788
|
+
* Stores of the lists shown with the client, by the key of the list
|
|
1789
|
+
*/
|
|
1601
1790
|
var getStores = (client) => {
|
|
1602
1791
|
let clientStores = stores.get(client);
|
|
1603
1792
|
if (!clientStores) {
|
|
@@ -1606,9 +1795,9 @@ var getStores = (client) => {
|
|
|
1606
1795
|
}
|
|
1607
1796
|
return clientStores;
|
|
1608
1797
|
};
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1798
|
+
//#endregion
|
|
1799
|
+
//#region src/hooks/use-infinite-fetch/use-infinite-fetch.store.ts
|
|
1800
|
+
var unusedStoreTime = 1e3;
|
|
1612
1801
|
/**
|
|
1613
1802
|
* State of a single list. Hooks showing the same list share one store, so they stay in sync and never request
|
|
1614
1803
|
* the same page twice. Pages are ordinary requests under their own cache keys - nothing extra is stored.
|
|
@@ -1626,6 +1815,7 @@ var InfiniteStore = class {
|
|
|
1626
1815
|
refetchedKeys = /* @__PURE__ */ new Set();
|
|
1627
1816
|
unmountCacheListeners = null;
|
|
1628
1817
|
unusedTimer = null;
|
|
1818
|
+
enabledHooks = 0;
|
|
1629
1819
|
started = false;
|
|
1630
1820
|
settled = false;
|
|
1631
1821
|
firstFetch = Promise.resolve(null);
|
|
@@ -1660,6 +1850,13 @@ var InfiniteStore = class {
|
|
|
1660
1850
|
this.pageListeners.delete(listener);
|
|
1661
1851
|
};
|
|
1662
1852
|
};
|
|
1853
|
+
/** Counts the hooks allowed to fetch - changes of the cache refetch the list only for them */
|
|
1854
|
+
enable = () => {
|
|
1855
|
+
this.enabledHooks += 1;
|
|
1856
|
+
return () => {
|
|
1857
|
+
this.enabledHooks -= 1;
|
|
1858
|
+
};
|
|
1859
|
+
};
|
|
1663
1860
|
/** Callbacks can capture values of the latest render */
|
|
1664
1861
|
setPagination = (pagination) => {
|
|
1665
1862
|
this.options.pagination = pagination;
|
|
@@ -1669,7 +1866,10 @@ var InfiniteStore = class {
|
|
|
1669
1866
|
* Loads the list when it is first needed. Safe to call many times.
|
|
1670
1867
|
*/
|
|
1671
1868
|
start = ({ revalidate } = {}) => {
|
|
1672
|
-
if (this.started)
|
|
1869
|
+
if (this.started) {
|
|
1870
|
+
if (!this.running && this.queue.length === 0 && this.pages.length > 0 && (revalidate || this.getHasStalePages())) this.revalidate();
|
|
1871
|
+
return this.firstFetch;
|
|
1872
|
+
}
|
|
1673
1873
|
this.started = true;
|
|
1674
1874
|
if (this.pages.length === 0) this.firstFetch = this.schedule("initial");
|
|
1675
1875
|
else if (revalidate || this.getHasStalePages()) this.revalidate();
|
|
@@ -1841,11 +2041,17 @@ var InfiniteStore = class {
|
|
|
1841
2041
|
refetchAtOnce = async (operation, current) => {
|
|
1842
2042
|
const results = await Promise.all(current.map((page) => this.fetchPage(operation, page.param, true)));
|
|
1843
2043
|
if (operation.cancelled) return null;
|
|
1844
|
-
const
|
|
1845
|
-
this.pages = current.map((page, index) => {
|
|
2044
|
+
const pages = current.map((page, index) => {
|
|
1846
2045
|
const fetched = results[index];
|
|
1847
2046
|
return fetched?.response.success ? this.createPage(fetched) : page;
|
|
1848
2047
|
});
|
|
2048
|
+
const end = pages.findIndex((page, index) => {
|
|
2049
|
+
return results[index]?.response.success && !getHasNextPage(this.getContext(page, pages.slice(0, index + 1)));
|
|
2050
|
+
});
|
|
2051
|
+
const length = end === -1 ? pages.length : end + 1;
|
|
2052
|
+
const kept = results.slice(0, length);
|
|
2053
|
+
const last = kept.find((fetched) => fetched && !fetched.response.success) ?? kept.findLast(Boolean);
|
|
2054
|
+
this.pages = pages.slice(0, length);
|
|
1849
2055
|
if (last) this.setResult(last.response);
|
|
1850
2056
|
return last?.response ?? null;
|
|
1851
2057
|
};
|
|
@@ -1927,8 +2133,12 @@ var InfiniteStore = class {
|
|
|
1927
2133
|
return null;
|
|
1928
2134
|
};
|
|
1929
2135
|
getHasStalePages = () => {
|
|
1930
|
-
return this.pages.some((
|
|
1931
|
-
const
|
|
2136
|
+
return this.pages.some(({ request, responseTimestamp }) => {
|
|
2137
|
+
const kept = request.cache ? null : {
|
|
2138
|
+
staleTime: request.staleTime,
|
|
2139
|
+
responseTimestamp
|
|
2140
|
+
};
|
|
2141
|
+
const value = this.readValue(request) ?? kept;
|
|
1932
2142
|
return !value || isStaleCacheData(value.staleTime, value.responseTimestamp);
|
|
1933
2143
|
});
|
|
1934
2144
|
};
|
|
@@ -2013,6 +2223,8 @@ var InfiniteStore = class {
|
|
|
2013
2223
|
}
|
|
2014
2224
|
};
|
|
2015
2225
|
setResult = (response) => {
|
|
2226
|
+
this.nextPageError = false;
|
|
2227
|
+
this.previousPageError = false;
|
|
2016
2228
|
this.result = {
|
|
2017
2229
|
error: response.success ? null : response.error,
|
|
2018
2230
|
status: response.status,
|
|
@@ -2078,7 +2290,7 @@ var InfiniteStore = class {
|
|
|
2078
2290
|
this.hasInvalidation = true;
|
|
2079
2291
|
queueMicrotask(() => {
|
|
2080
2292
|
this.hasInvalidation = false;
|
|
2081
|
-
if (this.
|
|
2293
|
+
if (this.enabledHooks > 0) this.refetch();
|
|
2082
2294
|
});
|
|
2083
2295
|
};
|
|
2084
2296
|
createSnapshot = () => {
|
|
@@ -2131,41 +2343,6 @@ var getInfiniteStore = (key, options) => {
|
|
|
2131
2343
|
return store;
|
|
2132
2344
|
};
|
|
2133
2345
|
//#endregion
|
|
2134
|
-
//#region src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts
|
|
2135
|
-
/**
|
|
2136
|
-
* Identity of a list. Everything describing the request takes part in it, so a changed filter, sort, page size
|
|
2137
|
-
* or URL param starts a new list without anything having to be listed by hand.
|
|
2138
|
-
*/
|
|
2139
|
-
var getInfiniteKey = (request, pagination, maxPages) => {
|
|
2140
|
-
return JSON.stringify([
|
|
2141
|
-
request.toJSON(),
|
|
2142
|
-
pagination,
|
|
2143
|
-
maxPages ?? null
|
|
2144
|
-
], (_key, value) => {
|
|
2145
|
-
return typeof value === "function" ? value.toString() : value;
|
|
2146
|
-
});
|
|
2147
|
-
};
|
|
2148
|
-
/**
|
|
2149
|
-
* Keeps the first occurrence of every item
|
|
2150
|
-
*/
|
|
2151
|
-
var getUniqueItems = (items, uniqueBy) => {
|
|
2152
|
-
if (uniqueBy === void 0) return items;
|
|
2153
|
-
const seen = /* @__PURE__ */ new Set();
|
|
2154
|
-
return items.filter((item) => {
|
|
2155
|
-
const identity = typeof uniqueBy === "function" ? uniqueBy({ item }) : item?.[uniqueBy];
|
|
2156
|
-
if (seen.has(identity)) return false;
|
|
2157
|
-
seen.add(identity);
|
|
2158
|
-
return true;
|
|
2159
|
-
});
|
|
2160
|
-
};
|
|
2161
|
-
/**
|
|
2162
|
-
* Decides if the previous list stays on screen while the new one loads its first page
|
|
2163
|
-
*/
|
|
2164
|
-
var getShouldKeepPreviousList = (mode, previous, current) => {
|
|
2165
|
-
if (mode === "auto") return previous.method === current.method && previous.endpoint === current.endpoint;
|
|
2166
|
-
return mode === "preserve";
|
|
2167
|
-
};
|
|
2168
|
-
//#endregion
|
|
2169
2346
|
//#region src/hooks/use-infinite-fetch/use-infinite-fetch.hooks.ts
|
|
2170
2347
|
var trackedSources = {
|
|
2171
2348
|
items: ["items", "loading"],
|
|
@@ -2296,11 +2473,20 @@ var useInfiniteFetch = (request, options) => {
|
|
|
2296
2473
|
callbacks.current.onReset?.();
|
|
2297
2474
|
}
|
|
2298
2475
|
if (disabled) return;
|
|
2299
|
-
if (bounce && previous && previous !== store && !store.hasPages()) bounceFunction(() =>
|
|
2476
|
+
if (bounce && previous && previous !== store && !store.hasPages()) bounceFunction(() => {
|
|
2477
|
+
if (mountedStore.current === store && !latest.current.disabled) store.start({ revalidate: latest.current.revalidate });
|
|
2478
|
+
});
|
|
2300
2479
|
else store.start({ revalidate });
|
|
2301
2480
|
}, [store, disabled]);
|
|
2302
|
-
|
|
2303
|
-
if (
|
|
2481
|
+
useEffect(() => {
|
|
2482
|
+
if (disabled) return;
|
|
2483
|
+
return store.enable();
|
|
2484
|
+
}, [store, disabled]);
|
|
2485
|
+
const usedDependencies = useRef(dependencies);
|
|
2486
|
+
useEffect(() => {
|
|
2487
|
+
const previous = usedDependencies.current;
|
|
2488
|
+
usedDependencies.current = dependencies;
|
|
2489
|
+
if ((previous.length !== dependencies.length || previous.some((value, index) => !Object.is(value, dependencies[index]))) && !disabled) store.refetch();
|
|
2304
2490
|
}, dependencies);
|
|
2305
2491
|
useEffect(() => {
|
|
2306
2492
|
const { appManager } = client;
|
|
@@ -2343,15 +2529,6 @@ var useInfiniteFetch = (request, options) => {
|
|
|
2343
2529
|
const list = isPreviousData ? previous.snapshot : snapshot;
|
|
2344
2530
|
const isServer = client.mode === "server" || typeof window === "undefined";
|
|
2345
2531
|
if (suspense && !disabled && !isServer && snapshot.loading && !isPreviousData) throw store.start({ revalidate });
|
|
2346
|
-
if (throwOnError) {
|
|
2347
|
-
usedKeys.add("error");
|
|
2348
|
-
usedKeys.add("isFetching");
|
|
2349
|
-
const error = snapshot.error;
|
|
2350
|
-
if ((typeof throwOnError === "function" ? !!error && throwOnError(error) : !!error) && !snapshot.isFetching) {
|
|
2351
|
-
store.evictSoon();
|
|
2352
|
-
throw error;
|
|
2353
|
-
}
|
|
2354
|
-
}
|
|
2355
2532
|
const uniqueByRef = useRef(uniqueBy);
|
|
2356
2533
|
uniqueByRef.current = uniqueBy;
|
|
2357
2534
|
const uniqueByKey = typeof uniqueBy === "function" ? uniqueBy.toString() : uniqueBy;
|
|
@@ -2360,13 +2537,26 @@ var useInfiniteFetch = (request, options) => {
|
|
|
2360
2537
|
}, [list.items, uniqueByKey]);
|
|
2361
2538
|
const selectedRef = useRef(null);
|
|
2362
2539
|
let items = uniqueItems;
|
|
2363
|
-
|
|
2540
|
+
let selectError = null;
|
|
2541
|
+
if (select) try {
|
|
2364
2542
|
const selected = select({
|
|
2365
2543
|
items: uniqueItems,
|
|
2366
2544
|
pages: list.data
|
|
2367
2545
|
});
|
|
2368
2546
|
items = structuralSharing ? replaceEqualDeep(selectedRef.current, selected) : selected;
|
|
2369
2547
|
selectedRef.current = items;
|
|
2548
|
+
} catch (error) {
|
|
2549
|
+
selectError = error;
|
|
2550
|
+
items = selectedRef.current;
|
|
2551
|
+
}
|
|
2552
|
+
const error = selectError ?? snapshot.error;
|
|
2553
|
+
if (throwOnError) {
|
|
2554
|
+
usedKeys.add("error");
|
|
2555
|
+
usedKeys.add("isFetching");
|
|
2556
|
+
if ((typeof throwOnError === "function" ? !!error && throwOnError(error) : !!error) && !snapshot.isFetching) {
|
|
2557
|
+
store.evictSoon();
|
|
2558
|
+
throw error;
|
|
2559
|
+
}
|
|
2370
2560
|
}
|
|
2371
2561
|
const actions = useMemo(() => ({
|
|
2372
2562
|
fetchNextPage: () => store.fetchNextPage(),
|
|
@@ -2405,10 +2595,10 @@ var useInfiniteFetch = (request, options) => {
|
|
|
2405
2595
|
isFetchNextPageError: snapshot.isFetchNextPageError,
|
|
2406
2596
|
isFetchPreviousPageError: snapshot.isFetchPreviousPageError,
|
|
2407
2597
|
isPreviousData,
|
|
2408
|
-
error
|
|
2598
|
+
error,
|
|
2409
2599
|
status: snapshot.status,
|
|
2410
2600
|
extra: snapshot.extra,
|
|
2411
|
-
success: snapshot.success,
|
|
2601
|
+
success: !selectError && snapshot.success,
|
|
2412
2602
|
bounce: getBounceData(bounceData),
|
|
2413
2603
|
...actions
|
|
2414
2604
|
}, trackedKeys, (usedKey) => usedKeys.add(usedKey));
|
|
@@ -2446,8 +2636,10 @@ var useSubmit = (request, options) => {
|
|
|
2446
2636
|
interval: bounceTime,
|
|
2447
2637
|
timeout: "bounceTimeout" in mergedOptions ? mergedOptions.bounceTimeout : bounceTime
|
|
2448
2638
|
});
|
|
2449
|
-
const
|
|
2639
|
+
const forceUpdate = f$1();
|
|
2640
|
+
const bounceResolver = useRef(null);
|
|
2450
2641
|
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
2642
|
+
const isBounceActive = bounceData.active;
|
|
2451
2643
|
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
2452
2644
|
/**
|
|
2453
2645
|
* State handler with optimization for rerendering, that hooks into the cache state and dispatchers queues
|
|
@@ -2473,9 +2665,33 @@ var useSubmit = (request, options) => {
|
|
|
2473
2665
|
setCacheData,
|
|
2474
2666
|
getIsDataProcessing
|
|
2475
2667
|
});
|
|
2476
|
-
const { addCacheDataListener, addLifecycleListeners } = listeners;
|
|
2668
|
+
const { addCacheDataListener, addLifecycleListeners, removeLifecycleListener } = listeners;
|
|
2669
|
+
/**
|
|
2670
|
+
* Response for the submit calls which did not send any request. The submit promise never rejects,
|
|
2671
|
+
* so the reason is returned as an error response.
|
|
2672
|
+
*/
|
|
2673
|
+
const getNotSentResponse = (message) => {
|
|
2674
|
+
const timestamp = Date.now();
|
|
2675
|
+
return {
|
|
2676
|
+
data: null,
|
|
2677
|
+
error: new Error(message),
|
|
2678
|
+
status: null,
|
|
2679
|
+
success: false,
|
|
2680
|
+
extra: request.client.adapter.defaultExtra,
|
|
2681
|
+
requestTimestamp: timestamp,
|
|
2682
|
+
responseTimestamp: timestamp
|
|
2683
|
+
};
|
|
2684
|
+
};
|
|
2685
|
+
/**
|
|
2686
|
+
* Bounced submit calls wait for the request to be triggered. When it will not happen anymore (bounce got reset,
|
|
2687
|
+
* call got dropped or the hook unmounted) we have to resolve them - the submit promise can never stay pending.
|
|
2688
|
+
*/
|
|
2689
|
+
const resolveBouncedCalls = (message) => {
|
|
2690
|
+
const resolvePending = bounceResolver.current;
|
|
2691
|
+
bounceResolver.current = null;
|
|
2692
|
+
resolvePending?.(getNotSentResponse(message));
|
|
2693
|
+
};
|
|
2477
2694
|
const handleSubmit = (submitOptions) => {
|
|
2478
|
-
const requestClone = request.clone(submitOptions);
|
|
2479
2695
|
if (disabled) {
|
|
2480
2696
|
logger.warning({
|
|
2481
2697
|
title: `Cannot submit request`,
|
|
@@ -2485,21 +2701,22 @@ var useSubmit = (request, options) => {
|
|
|
2485
2701
|
submitOptions
|
|
2486
2702
|
}
|
|
2487
2703
|
});
|
|
2488
|
-
return Promise.resolve(
|
|
2489
|
-
data: null,
|
|
2490
|
-
error: /* @__PURE__ */ new Error("Cannot submit request. Option 'disabled' is enabled"),
|
|
2491
|
-
status: null,
|
|
2492
|
-
extra: request.client.adapter.defaultExtra
|
|
2493
|
-
});
|
|
2704
|
+
return Promise.resolve(getNotSentResponse("Cannot submit request. Option 'disabled' is enabled"));
|
|
2494
2705
|
}
|
|
2706
|
+
const requestClone = request.clone(submitOptions);
|
|
2495
2707
|
const triggerRequest = () => {
|
|
2496
2708
|
addCacheDataListener(requestClone);
|
|
2497
2709
|
return sendRequest(requestClone, {
|
|
2498
2710
|
dispatcherType: "submit",
|
|
2499
2711
|
...submitOptions,
|
|
2500
2712
|
onBeforeSent: (data) => {
|
|
2713
|
+
removeLifecycleListener(data.requestId);
|
|
2501
2714
|
addLifecycleListeners(requestClone, data.requestId, data.mutationContext);
|
|
2502
2715
|
submitOptions?.onBeforeSent?.(data);
|
|
2716
|
+
},
|
|
2717
|
+
onResponse: (data) => {
|
|
2718
|
+
removeLifecycleListener(data.requestId);
|
|
2719
|
+
submitOptions?.onResponse?.(data);
|
|
2503
2720
|
}
|
|
2504
2721
|
});
|
|
2505
2722
|
};
|
|
@@ -2516,35 +2733,81 @@ var useSubmit = (request, options) => {
|
|
|
2516
2733
|
if (bounce) {
|
|
2517
2734
|
const bouncedResolve = bounceResolver.current;
|
|
2518
2735
|
bounceResolver.current = (value) => {
|
|
2519
|
-
bouncedResolve(value);
|
|
2736
|
+
bouncedResolve?.(value);
|
|
2520
2737
|
resolve(value);
|
|
2521
2738
|
};
|
|
2739
|
+
let isTriggered = false;
|
|
2522
2740
|
bounceFunction(async () => {
|
|
2741
|
+
isTriggered = true;
|
|
2523
2742
|
const callback = bounceResolver.current;
|
|
2524
|
-
bounceResolver.current =
|
|
2525
|
-
|
|
2743
|
+
bounceResolver.current = null;
|
|
2744
|
+
const value = await triggerRequest();
|
|
2745
|
+
callback?.(value);
|
|
2526
2746
|
});
|
|
2747
|
+
if (!isTriggered && !bounceData.active) resolveBouncedCalls("Request was not sent. Submit call got dropped by the throttle");
|
|
2748
|
+
if (bounceData.active !== isBounceActive) forceUpdate();
|
|
2527
2749
|
} else resolve(await triggerRequest());
|
|
2528
2750
|
};
|
|
2529
2751
|
performSubmit();
|
|
2530
2752
|
});
|
|
2531
2753
|
};
|
|
2532
|
-
const
|
|
2754
|
+
const handleRefetch = () => {
|
|
2533
2755
|
cache.invalidate(request);
|
|
2534
2756
|
};
|
|
2535
|
-
const
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2757
|
+
const latest = useRef({
|
|
2758
|
+
handleSubmit,
|
|
2759
|
+
handleRefetch,
|
|
2760
|
+
resolveBouncedCalls
|
|
2761
|
+
});
|
|
2762
|
+
latest.current = {
|
|
2763
|
+
handleSubmit,
|
|
2764
|
+
handleRefetch,
|
|
2765
|
+
resolveBouncedCalls
|
|
2766
|
+
};
|
|
2767
|
+
const submit = useCallback(((submitOptions) => latest.current.handleSubmit(submitOptions)), []);
|
|
2768
|
+
const refetch = useCallback(() => latest.current.handleRefetch(), []);
|
|
2769
|
+
const resetBounce = () => {
|
|
2770
|
+
const wasActive = bounceData.active;
|
|
2771
|
+
bounceData.reset();
|
|
2772
|
+
resolveBouncedCalls("Request was not sent. Bounce got reset before triggering the request");
|
|
2773
|
+
if (wasActive) forceUpdate();
|
|
2774
|
+
};
|
|
2775
|
+
const handlers = useMemo(() => {
|
|
2776
|
+
/**
|
|
2777
|
+
* Callbacks run inside of the request lifecycle. Error thrown by one of them would break it - the request
|
|
2778
|
+
* would never finish and it would block the queue. Such error is thrown outside of the lifecycle instead,
|
|
2779
|
+
* the same way as the errors of every other callback, listener and plugin.
|
|
2780
|
+
*/
|
|
2781
|
+
const protect = (callback) => {
|
|
2782
|
+
return (...args) => {
|
|
2783
|
+
runUserCode(() => {
|
|
2784
|
+
callback(...args);
|
|
2785
|
+
});
|
|
2786
|
+
};
|
|
2787
|
+
};
|
|
2788
|
+
return {
|
|
2789
|
+
onSubmitSuccess: (callback) => callbacks.onSuccess(protect(callback)),
|
|
2790
|
+
onSubmitError: (callback) => callbacks.onError(protect(callback)),
|
|
2791
|
+
onSubmitFinished: (callback) => callbacks.onFinished(protect(callback)),
|
|
2792
|
+
onSubmitRequestStart: (callback) => callbacks.onRequestStart(protect(callback)),
|
|
2793
|
+
onSubmitResponseStart: (callback) => callbacks.onResponseStart(protect(callback)),
|
|
2794
|
+
onSubmitDownloadProgress: (callback) => callbacks.onDownloadProgress(protect(callback)),
|
|
2795
|
+
onSubmitUploadProgress: (callback) => callbacks.onUploadProgress(protect(callback)),
|
|
2796
|
+
onSubmitOfflineError: (callback) => callbacks.onOfflineError(protect(callback)),
|
|
2797
|
+
onSubmitAbort: (callback) => callbacks.onAbort(protect(callback))
|
|
2798
|
+
};
|
|
2799
|
+
}, [callbacks]);
|
|
2546
2800
|
useEffect(() => {
|
|
2547
2801
|
addCacheDataListener(request);
|
|
2802
|
+
}, [
|
|
2803
|
+
request.cacheKey,
|
|
2804
|
+
request.queryKey,
|
|
2805
|
+
request.scope
|
|
2806
|
+
]);
|
|
2807
|
+
useEffect(() => {
|
|
2808
|
+
return () => {
|
|
2809
|
+
latest.current.resolveBouncedCalls("Request was not sent. Hook got unmounted before triggering the request");
|
|
2810
|
+
};
|
|
2548
2811
|
}, []);
|
|
2549
2812
|
const setSubmitRenderKey = (key) => {
|
|
2550
2813
|
setRenderKey(key === "submitting" ? "loading" : key);
|
|
@@ -2561,7 +2824,7 @@ var useSubmit = (request, options) => {
|
|
|
2561
2824
|
"requestTimestamp"
|
|
2562
2825
|
];
|
|
2563
2826
|
return createTrackedProxy({
|
|
2564
|
-
submit
|
|
2827
|
+
submit,
|
|
2565
2828
|
data: state.data,
|
|
2566
2829
|
error: state.error,
|
|
2567
2830
|
submitting: state.loading,
|
|
@@ -2574,7 +2837,10 @@ var useSubmit = (request, options) => {
|
|
|
2574
2837
|
abort: callbacks.abort,
|
|
2575
2838
|
...actions,
|
|
2576
2839
|
...handlers,
|
|
2577
|
-
bounce:
|
|
2840
|
+
bounce: {
|
|
2841
|
+
...getBounceData(bounceData),
|
|
2842
|
+
reset: resetBounce
|
|
2843
|
+
},
|
|
2578
2844
|
refetch
|
|
2579
2845
|
}, trackedKeys, setSubmitRenderKey);
|
|
2580
2846
|
};
|
|
@@ -2596,6 +2862,14 @@ var canUpdate = (item) => {
|
|
|
2596
2862
|
if (item.success || item.failed) return false;
|
|
2597
2863
|
return true;
|
|
2598
2864
|
};
|
|
2865
|
+
var getInitialProgress = () => ({
|
|
2866
|
+
progress: 0,
|
|
2867
|
+
timeLeft: 0,
|
|
2868
|
+
sizeLeft: 0,
|
|
2869
|
+
total: 0,
|
|
2870
|
+
loaded: 0,
|
|
2871
|
+
startTimestamp: 0
|
|
2872
|
+
});
|
|
2599
2873
|
/**
|
|
2600
2874
|
* This hook allows you to monitor and control dispatcher request queues for a given request,
|
|
2601
2875
|
* including starting, stopping, pausing, and inspecting pending/in-flight items.
|
|
@@ -2615,43 +2889,39 @@ var useQueue = (request, options) => {
|
|
|
2615
2889
|
const queryKey = scopeKey(rawQueryKey, scope);
|
|
2616
2890
|
const { requestManager } = client;
|
|
2617
2891
|
const [dispatcher] = getRequestDispatcher(request, dispatcherType);
|
|
2618
|
-
const
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
sizeLeft: 0,
|
|
2639
|
-
total: 0,
|
|
2640
|
-
loaded: 0,
|
|
2641
|
-
startTimestamp: 0
|
|
2642
|
-
},
|
|
2643
|
-
stopRequest: () => dispatcher.stopRequest(queryKey, req.requestId),
|
|
2644
|
-
startRequest: () => dispatcher.startRequest(queryKey, req.requestId),
|
|
2645
|
-
deleteRequest: () => dispatcher.delete(queryKey, req.requestId, abortKey)
|
|
2892
|
+
const createRequestsArray = useCallback((queueElements, prevRequests = []) => {
|
|
2893
|
+
const previous = new Map(prevRequests.map((el) => [el.requestId, el]));
|
|
2894
|
+
const current = new Map(queueElements.map((req) => {
|
|
2895
|
+
const tracked = previous.get(req.requestId);
|
|
2896
|
+
const item = tracked ? {
|
|
2897
|
+
...tracked,
|
|
2898
|
+
...req
|
|
2899
|
+
} : {
|
|
2900
|
+
failed: false,
|
|
2901
|
+
canceled: false,
|
|
2902
|
+
removed: false,
|
|
2903
|
+
success: false,
|
|
2904
|
+
...req,
|
|
2905
|
+
downloading: getInitialProgress(),
|
|
2906
|
+
uploading: getInitialProgress(),
|
|
2907
|
+
stopRequest: () => dispatcher.stopRequest(queryKey, req.requestId),
|
|
2908
|
+
startRequest: () => dispatcher.startRequest(queryKey, req.requestId),
|
|
2909
|
+
deleteRequest: () => dispatcher.delete(queryKey, req.requestId, abortKey)
|
|
2910
|
+
};
|
|
2911
|
+
return [req.requestId, item];
|
|
2646
2912
|
}));
|
|
2647
|
-
if (keepFinishedRequests
|
|
2648
|
-
|
|
2913
|
+
if (!keepFinishedRequests) return [...current.values()];
|
|
2914
|
+
const kept = prevRequests.map((el) => current.get(el.requestId) || el);
|
|
2915
|
+
const added = [...current.values()].filter((el) => !previous.has(el.requestId));
|
|
2916
|
+
return [...kept, ...added];
|
|
2649
2917
|
}, [
|
|
2650
2918
|
abortKey,
|
|
2651
2919
|
dispatcher,
|
|
2652
2920
|
queryKey,
|
|
2653
2921
|
keepFinishedRequests
|
|
2654
2922
|
]);
|
|
2923
|
+
const [stopped, setStopped] = useState(() => dispatcher.getQueue(queryKey).stopped);
|
|
2924
|
+
const [requests, setRequests] = useState(() => createRequestsArray(dispatcher.getQueue(queryKey).requests));
|
|
2655
2925
|
const mergePayloadType = useCallback((requestId, data) => {
|
|
2656
2926
|
setRequests((prev) => prev.map((el) => {
|
|
2657
2927
|
if (el.requestId === requestId && canUpdate(el)) return {
|
|
@@ -2682,15 +2952,18 @@ var useQueue = (request, options) => {
|
|
|
2682
2952
|
const mountEvents = () => {
|
|
2683
2953
|
const unmountChange = dispatcher.events.onQueueChangeByKey(queryKey, updateQueueState);
|
|
2684
2954
|
const unmountStatus = dispatcher.events.onQueueStatusChangeByKey(queryKey, updateQueueState);
|
|
2685
|
-
const
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2955
|
+
const unmountResponse = requestManager.events.onResponse(({ requestId, response, details }) => {
|
|
2956
|
+
setRequests((prev) => prev.map((el) => {
|
|
2957
|
+
if (el.requestId !== requestId) return el;
|
|
2958
|
+
if (response.success) return {
|
|
2959
|
+
...el,
|
|
2960
|
+
success: true
|
|
2961
|
+
};
|
|
2962
|
+
return !!details?.willRetry || !!details?.isCanceled || !!details?.isOffline && !!el.request.offline ? el : {
|
|
2963
|
+
...el,
|
|
2964
|
+
failed: true
|
|
2965
|
+
};
|
|
2966
|
+
}));
|
|
2694
2967
|
});
|
|
2695
2968
|
const unmountCanceled = requestManager.events.onAbort(({ requestId }) => {
|
|
2696
2969
|
setRequests((prev) => prev.map((el) => el.requestId === requestId ? {
|
|
@@ -2698,6 +2971,12 @@ var useQueue = (request, options) => {
|
|
|
2698
2971
|
canceled: true
|
|
2699
2972
|
} : el));
|
|
2700
2973
|
});
|
|
2974
|
+
const unmountStarted = requestManager.events.onRequestStart(({ requestId }) => {
|
|
2975
|
+
setRequests((prev) => prev.map((el) => el.requestId === requestId && el.canceled ? {
|
|
2976
|
+
...el,
|
|
2977
|
+
canceled: false
|
|
2978
|
+
} : el));
|
|
2979
|
+
});
|
|
2701
2980
|
const unmountRemoved = requestManager.events.onRemove(({ requestId }) => {
|
|
2702
2981
|
setRequests((prev) => prev.map((el) => el.requestId === requestId && canUpdate(el) ? {
|
|
2703
2982
|
...el,
|
|
@@ -2729,8 +3008,9 @@ var useQueue = (request, options) => {
|
|
|
2729
3008
|
unmountChange();
|
|
2730
3009
|
unmountDownload();
|
|
2731
3010
|
unmountUpload();
|
|
2732
|
-
|
|
3011
|
+
unmountResponse();
|
|
2733
3012
|
unmountCanceled();
|
|
3013
|
+
unmountStarted();
|
|
2734
3014
|
unmountRemoved();
|
|
2735
3015
|
};
|
|
2736
3016
|
return unmount;
|
|
@@ -2741,10 +3021,6 @@ var useQueue = (request, options) => {
|
|
|
2741
3021
|
queryKey
|
|
2742
3022
|
]);
|
|
2743
3023
|
useEffect(mountEvents, [
|
|
2744
|
-
stopped,
|
|
2745
|
-
requests,
|
|
2746
|
-
setRequests,
|
|
2747
|
-
setStopped,
|
|
2748
3024
|
queryKey,
|
|
2749
3025
|
dispatcher.events,
|
|
2750
3026
|
requestManager.events,
|
|
@@ -2768,6 +3044,18 @@ var useQueueDefaultOptions = {
|
|
|
2768
3044
|
};
|
|
2769
3045
|
//#endregion
|
|
2770
3046
|
//#region src/hooks/use-cache/use-cache.hooks.ts
|
|
3047
|
+
/**
|
|
3048
|
+
* Options passed as `undefined` (e.g. forwarded optional props) fall back to the defaults instead of overriding them
|
|
3049
|
+
*/
|
|
3050
|
+
var mergeOptions = (...sources) => {
|
|
3051
|
+
const merged = {};
|
|
3052
|
+
sources.forEach((source) => {
|
|
3053
|
+
Object.entries(source || {}).forEach(([key, value]) => {
|
|
3054
|
+
if (value !== void 0) merged[key] = value;
|
|
3055
|
+
});
|
|
3056
|
+
});
|
|
3057
|
+
return merged;
|
|
3058
|
+
};
|
|
2771
3059
|
/** Subscribe to cache updates for a given request without triggering network activity. */
|
|
2772
3060
|
var useCache = (request, options) => {
|
|
2773
3061
|
const { cacheKey, client } = request;
|
|
@@ -2776,11 +3064,7 @@ var useCache = (request, options) => {
|
|
|
2776
3064
|
const [dispatcher] = getRequestDispatcher(request);
|
|
2777
3065
|
const updateKey = JSON.stringify(request.toJSON());
|
|
2778
3066
|
const { config: globalConfig } = useProvider();
|
|
2779
|
-
const { dependencyTracking, initialResponse, deepCompare, structuralSharing } =
|
|
2780
|
-
...useCacheDefaultOptions,
|
|
2781
|
-
...globalConfig.useCacheConfig,
|
|
2782
|
-
...options
|
|
2783
|
-
};
|
|
3067
|
+
const { dependencyTracking, initialResponse, deepCompare, structuralSharing } = mergeOptions(useCacheDefaultOptions, globalConfig.useCacheConfig, options);
|
|
2784
3068
|
/**
|
|
2785
3069
|
* State handler with optimization for rerendering, that hooks into the cache state and dispatchers queues
|
|
2786
3070
|
*/
|
|
@@ -2791,6 +3075,7 @@ var useCache = (request, options) => {
|
|
|
2791
3075
|
initialResponse,
|
|
2792
3076
|
deepCompare,
|
|
2793
3077
|
dependencyTracking,
|
|
3078
|
+
keepPreviousData: "clean",
|
|
2794
3079
|
select: options?.select,
|
|
2795
3080
|
structuralSharing
|
|
2796
3081
|
});
|
|
@@ -3044,6 +3329,7 @@ var useListener = (listener, options) => {
|
|
|
3044
3329
|
i(() => {
|
|
3045
3330
|
listen();
|
|
3046
3331
|
}, [
|
|
3332
|
+
listener.socket,
|
|
3047
3333
|
listener.topic,
|
|
3048
3334
|
JSON.stringify(listener.options),
|
|
3049
3335
|
getDeliveryKey(listener.delivery)
|
|
@@ -3199,6 +3485,6 @@ var useIsSubmitting = (client, options) => {
|
|
|
3199
3485
|
return useRunningRequestsCount(client.submitDispatcher, options);
|
|
3200
3486
|
};
|
|
3201
3487
|
//#endregion
|
|
3202
|
-
export { Hydrate, Provider, createTrackedProxy, getBounceData, getDetailsState, getInitialState, getIsInitiallyLoading, getRefreshTime, getShouldClearState, getTimestamp, getValidCacheData, initialSocketState, initialState, isEmpty, isEqual, isStaleCacheData, useAppManager, useCache, useCacheDefaultOptions, useEmitter, useFetch, useFetchDefaultOptions, useInfiniteFetch, useInfiniteFetchDefaultOptions, useIsFetching, useIsSubmitting, useListener, useProvider, useQueue, useQueueDefaultOptions, useRequestEvents, useSocketState, useStream, useSubmit, useSubmitDefaultOptions, useTrackedState };
|
|
3488
|
+
export { Hydrate, Provider, createTrackedProxy, getBounceData, getDetailsState, getInitialState, getIsInitiallyLoading, getRefreshTime, getRequestIdentity, getShouldClearState, getTimestamp, getValidCacheData, initialSocketState, initialState, isEmpty, isEqual, isStaleCacheData, useAppManager, useCache, useCacheDefaultOptions, useEmitter, useFetch, useFetchDefaultOptions, useInfiniteFetch, useInfiniteFetchDefaultOptions, useIsFetching, useIsSubmitting, useListener, useProvider, useQueue, useQueueDefaultOptions, useRequestEvents, useSocketState, useStream, useSubmit, useSubmitDefaultOptions, useTrackedState };
|
|
3203
3489
|
|
|
3204
3490
|
//# sourceMappingURL=index.mjs.map
|