@hyper-fetch/react 9.6.1 → 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-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.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.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.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.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.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 +6 -0
- package/__tests__/features/use-submit/use-submit.optimistic.spec.ts +5 -1
- 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__/utils/gate.utils.ts +103 -0
- package/__tests__/utils/gated-client.utils.ts +158 -0
- package/__tests__/utils/helpers.utils.ts +19 -0
- 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 +630 -346
- 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 +141 -28
- 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,29 +216,44 @@ 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 () => {
|
|
240
258
|
const response = await data;
|
|
241
259
|
if (!isMounted.current) return;
|
|
@@ -244,10 +262,41 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
244
262
|
response
|
|
245
263
|
});
|
|
246
264
|
})();
|
|
247
|
-
return handleResponseCallbacks(
|
|
265
|
+
return handleResponseCallbacks({
|
|
266
|
+
...values,
|
|
267
|
+
response: data || values.response
|
|
268
|
+
});
|
|
248
269
|
};
|
|
249
270
|
};
|
|
250
|
-
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);
|
|
251
300
|
optimisticResultsRef.current.delete(requestId);
|
|
252
301
|
removeLifecycleListener(requestId);
|
|
253
302
|
};
|
|
@@ -261,6 +310,7 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
261
310
|
const getResponseUnmount = cache.events.onDataByKey(req.cacheKey, (cacheData) => {
|
|
262
311
|
if (!isSameScope(cacheData.scope, req.scope)) return;
|
|
263
312
|
setCacheData(cacheData);
|
|
313
|
+
if (pendingRequests.current.size > 0) actions.setLoading(true);
|
|
264
314
|
});
|
|
265
315
|
const unmount = () => {
|
|
266
316
|
loadingUnmount();
|
|
@@ -268,6 +318,7 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
268
318
|
};
|
|
269
319
|
clearCacheDataListener();
|
|
270
320
|
dataEvents.current = { unmount };
|
|
321
|
+
listenedQueryKey.current = scopeKey(req.queryKey, req.scope);
|
|
271
322
|
return unmount;
|
|
272
323
|
};
|
|
273
324
|
const addLifecycleListeners = (req, requestId, optimisticResult) => {
|
|
@@ -279,11 +330,11 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
279
330
|
if (!requestId) {
|
|
280
331
|
clearLifecycleListeners();
|
|
281
332
|
const { queryKey, cacheKey } = req;
|
|
282
|
-
const requestStartUnmount = requestManager.events.onRequestStartByQueue(queryKey, handleRequestStart());
|
|
283
|
-
const responseStartUnmount = requestManager.events.onResponseStartByQueue(queryKey, handleResponseStart());
|
|
284
|
-
const uploadUnmount = requestManager.events.onUploadProgressByQueue(queryKey, handleUploadProgress);
|
|
285
|
-
const downloadUnmount = requestManager.events.onDownloadProgressByQueue(queryKey, handleDownloadProgress);
|
|
286
|
-
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));
|
|
287
338
|
const unmount = () => {
|
|
288
339
|
downloadUnmount();
|
|
289
340
|
uploadUnmount();
|
|
@@ -298,12 +349,17 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
298
349
|
* useSubmit handles requesting by requestIds, this makes it possible to track single requests
|
|
299
350
|
*/
|
|
300
351
|
if (optimisticResult) optimisticResultsRef.current.set(requestId, optimisticResult);
|
|
352
|
+
pendingRequests.current.add(requestId);
|
|
353
|
+
const onRequestStart = handleRequestStart(req);
|
|
301
354
|
const requestRemove = requestManager.events.onRemoveById(requestId, handleRemove);
|
|
302
|
-
const requestStartUnmount = requestManager.events.onRequestStartById(requestId,
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
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));
|
|
307
363
|
const unmount = () => {
|
|
308
364
|
requestRemove();
|
|
309
365
|
downloadUnmount();
|
|
@@ -316,12 +372,14 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
316
372
|
return unmount;
|
|
317
373
|
};
|
|
318
374
|
const abort = () => {
|
|
319
|
-
const
|
|
320
|
-
|
|
375
|
+
const currentRequest = requestRef.current;
|
|
376
|
+
const currentDispatcher = dispatcherRef.current;
|
|
377
|
+
const ak = scopeKey(currentRequest.abortKey, currentRequest.scope);
|
|
378
|
+
currentDispatcher.getAllRunningRequests().forEach((requestData) => {
|
|
321
379
|
const reqAk = scopeKey(requestData.request.abortKey, requestData.request.scope);
|
|
322
380
|
if (reqAk === ak) {
|
|
323
381
|
const qk = scopeKey(requestData.request.queryKey, requestData.request.scope);
|
|
324
|
-
|
|
382
|
+
currentDispatcher.delete(qk, requestData.requestId, reqAk);
|
|
325
383
|
}
|
|
326
384
|
});
|
|
327
385
|
};
|
|
@@ -332,7 +390,8 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
332
390
|
clearLifecycleListeners();
|
|
333
391
|
clearCacheDataListener();
|
|
334
392
|
});
|
|
335
|
-
|
|
393
|
+
const callbacksRef = useRef(null);
|
|
394
|
+
if (!callbacksRef.current) callbacksRef.current = {
|
|
336
395
|
abort,
|
|
337
396
|
onSuccess: (callback) => {
|
|
338
397
|
onSuccessCallback.current = callback;
|
|
@@ -361,7 +420,8 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
361
420
|
onUploadProgress: (callback) => {
|
|
362
421
|
onUploadProgressCallback.current = callback;
|
|
363
422
|
}
|
|
364
|
-
}
|
|
423
|
+
};
|
|
424
|
+
return [callbacksRef.current, {
|
|
365
425
|
addCacheDataListener,
|
|
366
426
|
clearCacheDataListener,
|
|
367
427
|
addLifecycleListeners,
|
|
@@ -615,11 +675,21 @@ var getCacheKeyIdentity = (cacheKey) => {
|
|
|
615
675
|
return cacheKey.substring(0, lastUnderscoreIndex);
|
|
616
676
|
};
|
|
617
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
|
+
/**
|
|
618
685
|
* Determines whether state should be cleared when the cache key changes.
|
|
619
686
|
*
|
|
620
687
|
* - `"clean"` — always clear
|
|
621
688
|
* - `"preserve"` — never clear
|
|
622
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.
|
|
623
693
|
*/
|
|
624
694
|
var getShouldClearState = (mode, oldCacheKey, newCacheKey) => {
|
|
625
695
|
if (oldCacheKey === newCacheKey) return false;
|
|
@@ -724,11 +794,16 @@ var getInitialState = ({ initialResponse, dispatcher, request, disabled, revalid
|
|
|
724
794
|
var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, dependencyTracking, keepPreviousData = "auto", disabled, revalidate, select, structuralSharing = true }) => {
|
|
725
795
|
const { client, cacheKey, queryKey, staleTime } = request;
|
|
726
796
|
const { cache } = client;
|
|
797
|
+
const scopedCacheKey = scopeKey(cacheKey, request.scope);
|
|
798
|
+
const scopedQueryKey = scopeKey(queryKey, request.scope);
|
|
799
|
+
const identity = getRequestIdentity(request);
|
|
727
800
|
const hydration = useHydration();
|
|
728
801
|
const state = useRef(null);
|
|
729
802
|
const renderKeys = useRef([]);
|
|
730
803
|
const isProcessingData = useRef("");
|
|
731
|
-
const previousCacheKey = useRef(
|
|
804
|
+
const previousCacheKey = useRef(scopedCacheKey);
|
|
805
|
+
const previousIdentity = useRef(identity);
|
|
806
|
+
const isFreshState = useRef(true);
|
|
732
807
|
const selectRef = useRef(select);
|
|
733
808
|
selectRef.current = select;
|
|
734
809
|
const appliedSelect = useRef(select);
|
|
@@ -760,6 +835,22 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
760
835
|
disabled,
|
|
761
836
|
hydratedEntry
|
|
762
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
|
+
}
|
|
763
854
|
}
|
|
764
855
|
if (selectError.current) {
|
|
765
856
|
const { error } = selectError.current;
|
|
@@ -785,8 +876,9 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
785
876
|
listenerRef.current?.();
|
|
786
877
|
};
|
|
787
878
|
const getStaleStatus = () => {
|
|
788
|
-
const cacheData = cache.get(
|
|
789
|
-
|
|
879
|
+
const cacheData = cache.get(scopedCacheKey);
|
|
880
|
+
const isInvalidated = cacheData?.staleTime === 0;
|
|
881
|
+
return !cacheData || isInvalidated || isStaleCacheData(staleTime, cacheData?.responseTimestamp);
|
|
790
882
|
};
|
|
791
883
|
const renderKeyTrigger = (keys) => {
|
|
792
884
|
if (renderKeys.current.some((renderKey) => keys.includes(renderKey))) emitChange();
|
|
@@ -795,21 +887,10 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
795
887
|
if (!renderKeys.current.includes(renderKey)) renderKeys.current.push(renderKey);
|
|
796
888
|
};
|
|
797
889
|
i(() => {
|
|
798
|
-
const
|
|
799
|
-
|
|
800
|
-
const cacheState = getValidCacheData(request, initialResponse, cache.get(scopeKey(cacheKey, request.scope)));
|
|
801
|
-
if (getShouldClearState(keepPreviousData, oldKey, cacheKey) && !cacheState) {
|
|
802
|
-
state.current = getSelectedState(getInitialState({
|
|
803
|
-
initialResponse,
|
|
804
|
-
dispatcher,
|
|
805
|
-
request,
|
|
806
|
-
disabled,
|
|
807
|
-
revalidate
|
|
808
|
-
}));
|
|
809
|
-
renderKeyTrigger(Object.keys(state.current));
|
|
810
|
-
} else if (cacheState) {
|
|
890
|
+
const cacheState = cache.get(scopedCacheKey) || (isFreshState.current ? getValidCacheData(request, initialResponse, null) : null);
|
|
891
|
+
if (cacheState) {
|
|
811
892
|
state.current.loading = getIsInitiallyLoading({
|
|
812
|
-
queryKey:
|
|
893
|
+
queryKey: scopedQueryKey,
|
|
813
894
|
dispatcher,
|
|
814
895
|
disabled,
|
|
815
896
|
revalidate,
|
|
@@ -817,7 +898,7 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
817
898
|
});
|
|
818
899
|
setCacheData(cacheState);
|
|
819
900
|
}
|
|
820
|
-
}, [
|
|
901
|
+
}, [scopedCacheKey, scopedQueryKey], true);
|
|
821
902
|
i(() => {
|
|
822
903
|
const handleDependencyTracking = () => {
|
|
823
904
|
if (!dependencyTracking) Object.keys(state.current).forEach((key) => setRenderKey(key));
|
|
@@ -856,7 +937,7 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
856
937
|
retries: cacheData.retries,
|
|
857
938
|
responseTimestamp: new Date(cacheData.responseTimestamp),
|
|
858
939
|
requestTimestamp: new Date(cacheData.requestTimestamp),
|
|
859
|
-
loading: dispatcher.hasRunningRequests(
|
|
940
|
+
loading: dispatcher.hasRunningRequests(scopedQueryKey)
|
|
860
941
|
};
|
|
861
942
|
const nextState = { ...state.current };
|
|
862
943
|
const changedKeys = Object.keys(newStateValues).filter((key) => {
|
|
@@ -900,78 +981,85 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
900
981
|
...data
|
|
901
982
|
});
|
|
902
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;
|
|
903
1060
|
return [
|
|
904
1061
|
state.current,
|
|
905
|
-
|
|
906
|
-
setData: (data) => {
|
|
907
|
-
isLocalData.current = true;
|
|
908
|
-
state.current.data = data instanceof Function ? data(state.current.data || null) : data;
|
|
909
|
-
renderKeyTrigger(["data"]);
|
|
910
|
-
},
|
|
911
|
-
setError: (error) => {
|
|
912
|
-
state.current.error = error instanceof Function ? error(state.current.error || null) : error;
|
|
913
|
-
renderKeyTrigger(["error"]);
|
|
914
|
-
},
|
|
915
|
-
setLoading: (loading) => {
|
|
916
|
-
const value = loading instanceof Function ? loading(state.current.loading) : loading;
|
|
917
|
-
if (value === state.current.loading) return;
|
|
918
|
-
state.current.loading = value;
|
|
919
|
-
renderKeyTrigger(["loading"]);
|
|
920
|
-
},
|
|
921
|
-
setStatus: (status) => {
|
|
922
|
-
if ((status instanceof Function ? status(state.current.status) : status) === state.current.status) return;
|
|
923
|
-
state.current.status = status instanceof Function ? status(state.current.status || null) : status;
|
|
924
|
-
renderKeyTrigger(["status"]);
|
|
925
|
-
},
|
|
926
|
-
setSuccess: (success) => {
|
|
927
|
-
if ((success instanceof Function ? success(state.current.success || false) : success) === state.current.success) return;
|
|
928
|
-
state.current.success = success instanceof Function ? success(state.current.success || false) : success;
|
|
929
|
-
renderKeyTrigger(["success"]);
|
|
930
|
-
},
|
|
931
|
-
setExtra: (extra) => {
|
|
932
|
-
if ((extra instanceof Function ? extra(state.current.extra) : extra) === state.current.extra) return;
|
|
933
|
-
state.current.extra = extra instanceof Function ? extra(state.current.extra) : extra;
|
|
934
|
-
renderKeyTrigger(["extra"]);
|
|
935
|
-
},
|
|
936
|
-
setRetries: (retries) => {
|
|
937
|
-
if ((retries instanceof Function ? retries(state.current.retries || 0) : retries) === state.current.retries) return;
|
|
938
|
-
state.current.retries = retries instanceof Function ? retries(state.current.retries || 0) : retries;
|
|
939
|
-
renderKeyTrigger(["retries"]);
|
|
940
|
-
},
|
|
941
|
-
setResponseTimestamp: (timestamp) => {
|
|
942
|
-
if ((timestamp instanceof Function ? timestamp(state.current.responseTimestamp) : timestamp) === state.current.responseTimestamp) return;
|
|
943
|
-
const getTimestamp = (prev) => {
|
|
944
|
-
return timestamp instanceof Function ? timestamp(prev ? new Date(prev) : null) : timestamp;
|
|
945
|
-
};
|
|
946
|
-
state.current.responseTimestamp = getTimestamp(state.current.responseTimestamp);
|
|
947
|
-
renderKeyTrigger(["responseTimestamp"]);
|
|
948
|
-
},
|
|
949
|
-
setRequestTimestamp: (timestamp) => {
|
|
950
|
-
if ((timestamp instanceof Function ? timestamp(state.current.requestTimestamp) : timestamp) === state.current.requestTimestamp) return;
|
|
951
|
-
const getTimestamp = (prev) => {
|
|
952
|
-
return timestamp instanceof Function ? timestamp(prev ? new Date(prev) : null) : timestamp;
|
|
953
|
-
};
|
|
954
|
-
state.current.requestTimestamp = getTimestamp(state.current.requestTimestamp);
|
|
955
|
-
renderKeyTrigger(["requestTimestamp"]);
|
|
956
|
-
},
|
|
957
|
-
clearState: () => {
|
|
958
|
-
rawData.current = null;
|
|
959
|
-
isLocalData.current = false;
|
|
960
|
-
responseDetails.current = null;
|
|
961
|
-
state.current = {
|
|
962
|
-
data: null,
|
|
963
|
-
error: null,
|
|
964
|
-
loading: false,
|
|
965
|
-
status: null,
|
|
966
|
-
success: false,
|
|
967
|
-
extra: null,
|
|
968
|
-
retries: 0,
|
|
969
|
-
responseTimestamp: null,
|
|
970
|
-
requestTimestamp: null
|
|
971
|
-
};
|
|
972
|
-
renderKeyTrigger(Object.keys(state.current));
|
|
973
|
-
}
|
|
974
|
-
},
|
|
1062
|
+
actions,
|
|
975
1063
|
{
|
|
976
1064
|
setRenderKey,
|
|
977
1065
|
setCacheData,
|
|
@@ -1145,6 +1233,11 @@ var ConfigContext = React.createContext({
|
|
|
1145
1233
|
*/
|
|
1146
1234
|
var Provider = ({ children, config }) => {
|
|
1147
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
|
+
}
|
|
1148
1241
|
const value = useMemo(() => {
|
|
1149
1242
|
return {
|
|
1150
1243
|
config: currentConfig,
|
|
@@ -1156,7 +1249,7 @@ var Provider = ({ children, config }) => {
|
|
|
1156
1249
|
children
|
|
1157
1250
|
}, void 0, false, {
|
|
1158
1251
|
fileName: _jsxFileName,
|
|
1159
|
-
lineNumber:
|
|
1252
|
+
lineNumber: 31,
|
|
1160
1253
|
columnNumber: 10
|
|
1161
1254
|
}, void 0);
|
|
1162
1255
|
};
|
|
@@ -1169,20 +1262,41 @@ var useProvider = () => {
|
|
|
1169
1262
|
};
|
|
1170
1263
|
//#endregion
|
|
1171
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
|
+
*/
|
|
1172
1270
|
var suspenseStores = /* @__PURE__ */ new WeakMap();
|
|
1173
|
-
var
|
|
1174
|
-
|
|
1175
|
-
let store = suspenseStores.get(client);
|
|
1271
|
+
var getSuspenseStore = (cache) => {
|
|
1272
|
+
let store = suspenseStores.get(cache);
|
|
1176
1273
|
if (!store) {
|
|
1177
1274
|
store = {
|
|
1178
1275
|
promises: /* @__PURE__ */ new Map(),
|
|
1179
1276
|
results: /* @__PURE__ */ new Map()
|
|
1180
1277
|
};
|
|
1181
|
-
suspenseStores.set(
|
|
1278
|
+
suspenseStores.set(cache, store);
|
|
1182
1279
|
}
|
|
1183
1280
|
return store;
|
|
1184
1281
|
};
|
|
1185
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
|
+
/**
|
|
1186
1300
|
* This hook aims to retrieve data from the server. It automatically fetches on mount and
|
|
1187
1301
|
* refetches based on dependencies, with support for caching, polling, and suspense.
|
|
1188
1302
|
* @param request Request instance
|
|
@@ -1191,11 +1305,7 @@ var getSuspenseStore = (client) => {
|
|
|
1191
1305
|
*/
|
|
1192
1306
|
var useFetch = (request, options) => {
|
|
1193
1307
|
const { config: globalConfig } = useProvider();
|
|
1194
|
-
const { suspense, dependencies, disabled, dependencyTracking, revalidate, initialResponse, keepPreviousData, refresh, refreshTime, refetchBlurred, refetchOnBlur, refetchOnFocus, refetchOnReconnect, bounce, bounceType, bounceTime, bounceTimeout, deepCompare, structuralSharing, throwOnError } =
|
|
1195
|
-
...useFetchDefaultOptions,
|
|
1196
|
-
...globalConfig.useFetchConfig,
|
|
1197
|
-
...options
|
|
1198
|
-
};
|
|
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);
|
|
1199
1309
|
const updateKey = JSON.stringify(request.toJSON());
|
|
1200
1310
|
const requestDebounce = f({ delay: bounceTime });
|
|
1201
1311
|
const requestThrottle = p({
|
|
@@ -1205,8 +1315,10 @@ var useFetch = (request, options) => {
|
|
|
1205
1315
|
const refreshDebounce = f({ delay: refreshTime });
|
|
1206
1316
|
const { cacheKey, queryKey, client } = request;
|
|
1207
1317
|
const { cache, fetchDispatcher: dispatcher, appManager, loggerManager } = client;
|
|
1208
|
-
const
|
|
1209
|
-
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);
|
|
1210
1322
|
const logger = useRef(loggerManager.initialize(client, "useFetch")).current;
|
|
1211
1323
|
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
1212
1324
|
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
@@ -1259,57 +1371,71 @@ var useFetch = (request, options) => {
|
|
|
1259
1371
|
return;
|
|
1260
1372
|
}
|
|
1261
1373
|
refreshDebounce.debounce(() => {
|
|
1262
|
-
|
|
1263
|
-
const scopedQueryKey = scopeKey(request.queryKey, request.scope);
|
|
1264
|
-
const isFetching = dispatcher.hasRunningRequests(scopedQueryKey);
|
|
1265
|
-
const isQueued = dispatcher.getIsActiveQueue(scopedQueryKey);
|
|
1266
|
-
const isActive = isFetching || isQueued;
|
|
1267
|
-
if (isBlurred && refetchBlurred && !isActive || !isBlurred && !isActive) {
|
|
1268
|
-
handleFetch();
|
|
1269
|
-
logger.debug({
|
|
1270
|
-
title: `Performing refresh request`,
|
|
1271
|
-
type: "system",
|
|
1272
|
-
extra: { request }
|
|
1273
|
-
});
|
|
1274
|
-
}
|
|
1275
|
-
handleRefresh();
|
|
1374
|
+
latest.current.refresh();
|
|
1276
1375
|
});
|
|
1277
1376
|
}
|
|
1278
|
-
const
|
|
1279
|
-
|
|
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
|
+
}
|
|
1280
1390
|
handleRefresh();
|
|
1281
1391
|
};
|
|
1282
|
-
const
|
|
1283
|
-
|
|
1392
|
+
const handleRefetch = () => {
|
|
1393
|
+
handleFetch({ force: true });
|
|
1394
|
+
handleRefresh();
|
|
1284
1395
|
};
|
|
1285
1396
|
/**
|
|
1286
|
-
*
|
|
1287
|
-
*
|
|
1288
|
-
* 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.
|
|
1289
1399
|
*/
|
|
1290
|
-
const
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
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);
|
|
1295
1425
|
};
|
|
1296
1426
|
const initialFetchData = () => {
|
|
1297
|
-
if (getHasJustResolvedSuspense()) return;
|
|
1298
1427
|
const hasStaleData = getStaleStatus();
|
|
1299
1428
|
const isFetching = getIsFetchingIdentity();
|
|
1300
|
-
|
|
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();
|
|
1301
1433
|
};
|
|
1302
1434
|
const updateFetchData = () => {
|
|
1303
1435
|
const hasStaleData = getStaleStatus();
|
|
1304
|
-
|
|
1305
|
-
/**
|
|
1306
|
-
* This is a hack to avoid double rendering in React 18
|
|
1307
|
-
* It renders initial mount event and allow us to consume only hook updates
|
|
1308
|
-
*/
|
|
1309
|
-
if (!ignoreReact18DoubleRender.current && shouldUpdate)
|
|
1436
|
+
if (!revalidate ? hasStaleData : true)
|
|
1310
1437
|
/**
|
|
1311
|
-
*
|
|
1312
|
-
* 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
|
|
1313
1439
|
*/
|
|
1314
1440
|
if (bounce) {
|
|
1315
1441
|
logger.debug({
|
|
@@ -1320,33 +1446,53 @@ var useFetch = (request, options) => {
|
|
|
1320
1446
|
request
|
|
1321
1447
|
}
|
|
1322
1448
|
});
|
|
1323
|
-
bounceFunction(() =>
|
|
1449
|
+
bounceFunction(() => latest.current.fetch());
|
|
1324
1450
|
} else handleFetch();
|
|
1325
|
-
|
|
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();
|
|
1326
1469
|
};
|
|
1327
1470
|
const handleMountEvents = () => {
|
|
1328
1471
|
addCacheDataListener(request);
|
|
1329
1472
|
addLifecycleListeners(request);
|
|
1330
1473
|
const focusUnmount = appManager.events.onFocus(() => {
|
|
1331
|
-
if (refetchOnFocus && !disabled) {
|
|
1332
|
-
|
|
1333
|
-
|
|
1474
|
+
if (latest.current.refetchOnFocus && !latest.current.disabled) {
|
|
1475
|
+
latest.current.fetch();
|
|
1476
|
+
latest.current.restartRefresh();
|
|
1334
1477
|
}
|
|
1335
1478
|
});
|
|
1336
1479
|
const blurUnmount = appManager.events.onBlur(() => {
|
|
1337
|
-
if (refetchOnBlur && !disabled) {
|
|
1338
|
-
|
|
1339
|
-
|
|
1480
|
+
if (latest.current.refetchOnBlur && !latest.current.disabled) {
|
|
1481
|
+
latest.current.fetch();
|
|
1482
|
+
latest.current.restartRefresh();
|
|
1340
1483
|
}
|
|
1341
1484
|
});
|
|
1342
1485
|
const onlineUnmount = appManager.events.onOnline(() => {
|
|
1343
|
-
if (refetchOnReconnect && !disabled) {
|
|
1344
|
-
|
|
1345
|
-
|
|
1486
|
+
if (latest.current.refetchOnReconnect && !latest.current.disabled) {
|
|
1487
|
+
latest.current.fetch();
|
|
1488
|
+
latest.current.restartRefresh();
|
|
1346
1489
|
}
|
|
1347
1490
|
});
|
|
1348
|
-
const
|
|
1349
|
-
|
|
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);
|
|
1350
1496
|
const unmount = () => {
|
|
1351
1497
|
clearCacheDataListener();
|
|
1352
1498
|
focusUnmount();
|
|
@@ -1364,12 +1510,9 @@ var useFetch = (request, options) => {
|
|
|
1364
1510
|
i(handleMountEvents, [updateKey, disabled], true);
|
|
1365
1511
|
/**
|
|
1366
1512
|
* Initial fetch triggered once data is stale or we use the refetch strategy
|
|
1513
|
+
* and the fetching logic for updates handling
|
|
1367
1514
|
*/
|
|
1368
|
-
|
|
1369
|
-
/**
|
|
1370
|
-
* Fetching logic for updates handling
|
|
1371
|
-
*/
|
|
1372
|
-
i(updateFetchData, [
|
|
1515
|
+
i(handleFetchLifecycle, [
|
|
1373
1516
|
updateKey,
|
|
1374
1517
|
disabled,
|
|
1375
1518
|
...dependencies
|
|
@@ -1384,28 +1527,28 @@ var useFetch = (request, options) => {
|
|
|
1384
1527
|
refresh,
|
|
1385
1528
|
refreshTime
|
|
1386
1529
|
], true);
|
|
1387
|
-
/**
|
|
1388
|
-
* Reset the ignore flag for React 18 strict mode
|
|
1389
|
-
*/
|
|
1390
|
-
d$1(() => {
|
|
1391
|
-
ignoreReact18DoubleRender.current = true;
|
|
1392
|
-
});
|
|
1393
1530
|
if (suspense && !disabled) {
|
|
1394
|
-
const
|
|
1395
|
-
const
|
|
1396
|
-
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);
|
|
1397
1535
|
if (suspenseResult) {
|
|
1398
|
-
|
|
1399
|
-
|
|
1536
|
+
queueMicrotask(() => {
|
|
1537
|
+
if (store.results.get(scopedCacheKey) === suspenseResult) store.results.delete(scopedCacheKey);
|
|
1538
|
+
});
|
|
1539
|
+
isSuspenseResolved.current = true;
|
|
1400
1540
|
if (suspenseResult.data !== null) state.data = selectData(suspenseResult.data);
|
|
1401
1541
|
if (suspenseResult.error !== null) state.error = suspenseResult.error;
|
|
1402
1542
|
if (suspenseResult.status !== null) state.status = suspenseResult.status;
|
|
1403
1543
|
if (suspenseResult.extra !== null) state.extra = suspenseResult.extra;
|
|
1404
1544
|
state.success = suspenseResult.success;
|
|
1405
|
-
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);
|
|
1406
1549
|
}
|
|
1407
|
-
if (!(state.data !== null || state.error !== null)) {
|
|
1408
|
-
let entry =
|
|
1550
|
+
if (!(state.data !== null || state.error !== null || state.responseTimestamp !== null)) {
|
|
1551
|
+
let entry = store.promises.get(scopedCacheKey);
|
|
1409
1552
|
if (!entry) {
|
|
1410
1553
|
let resolvePromise;
|
|
1411
1554
|
const promise = new Promise((r) => {
|
|
@@ -1413,32 +1556,35 @@ var useFetch = (request, options) => {
|
|
|
1413
1556
|
});
|
|
1414
1557
|
const unsubscribe = cache.events.onDataByKey(cacheKey, (cacheData) => {
|
|
1415
1558
|
if (!isSameScope(cacheData.scope, request.scope)) return;
|
|
1416
|
-
|
|
1559
|
+
store.results.set(scopedCacheKey, {
|
|
1417
1560
|
data: cacheData.data ?? null,
|
|
1418
1561
|
error: cacheData.error ?? null,
|
|
1419
1562
|
status: cacheData.status ?? null,
|
|
1420
1563
|
extra: cacheData.extra ?? null,
|
|
1421
|
-
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()
|
|
1422
1569
|
});
|
|
1423
1570
|
resolvePromise();
|
|
1424
1571
|
unsubscribe();
|
|
1425
|
-
|
|
1572
|
+
store.promises.delete(scopedCacheKey);
|
|
1426
1573
|
});
|
|
1427
1574
|
entry = {
|
|
1428
1575
|
promise,
|
|
1429
1576
|
resolve: resolvePromise,
|
|
1430
1577
|
cleanup: unsubscribe
|
|
1431
1578
|
};
|
|
1432
|
-
|
|
1579
|
+
store.promises.set(scopedCacheKey, entry);
|
|
1433
1580
|
}
|
|
1434
|
-
const scopedQueryKey = scopeKey(queryKey, request.scope);
|
|
1435
1581
|
if (!dispatcher.hasRunningRequests(scopedQueryKey) && !dispatcher.getIsActiveQueue(scopedQueryKey)) dispatcher.add(request);
|
|
1436
1582
|
throw entry.promise;
|
|
1437
1583
|
}
|
|
1438
|
-
const entry =
|
|
1584
|
+
const entry = store.promises.get(scopedCacheKey);
|
|
1439
1585
|
if (entry) {
|
|
1440
1586
|
entry.cleanup();
|
|
1441
|
-
|
|
1587
|
+
store.promises.delete(scopedCacheKey);
|
|
1442
1588
|
}
|
|
1443
1589
|
}
|
|
1444
1590
|
if (throwOnError) {
|
|
@@ -1597,9 +1743,50 @@ var loadPage = async (options) => {
|
|
|
1597
1743
|
return sendPage(options);
|
|
1598
1744
|
};
|
|
1599
1745
|
//#endregion
|
|
1600
|
-
//#region src/hooks/use-infinite-fetch/use-infinite-fetch.
|
|
1601
|
-
|
|
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
|
+
};
|
|
1602
1786
|
var stores = /* @__PURE__ */ new WeakMap();
|
|
1787
|
+
/**
|
|
1788
|
+
* Stores of the lists shown with the client, by the key of the list
|
|
1789
|
+
*/
|
|
1603
1790
|
var getStores = (client) => {
|
|
1604
1791
|
let clientStores = stores.get(client);
|
|
1605
1792
|
if (!clientStores) {
|
|
@@ -1608,9 +1795,9 @@ var getStores = (client) => {
|
|
|
1608
1795
|
}
|
|
1609
1796
|
return clientStores;
|
|
1610
1797
|
};
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1798
|
+
//#endregion
|
|
1799
|
+
//#region src/hooks/use-infinite-fetch/use-infinite-fetch.store.ts
|
|
1800
|
+
var unusedStoreTime = 1e3;
|
|
1614
1801
|
/**
|
|
1615
1802
|
* State of a single list. Hooks showing the same list share one store, so they stay in sync and never request
|
|
1616
1803
|
* the same page twice. Pages are ordinary requests under their own cache keys - nothing extra is stored.
|
|
@@ -1628,6 +1815,7 @@ var InfiniteStore = class {
|
|
|
1628
1815
|
refetchedKeys = /* @__PURE__ */ new Set();
|
|
1629
1816
|
unmountCacheListeners = null;
|
|
1630
1817
|
unusedTimer = null;
|
|
1818
|
+
enabledHooks = 0;
|
|
1631
1819
|
started = false;
|
|
1632
1820
|
settled = false;
|
|
1633
1821
|
firstFetch = Promise.resolve(null);
|
|
@@ -1662,6 +1850,13 @@ var InfiniteStore = class {
|
|
|
1662
1850
|
this.pageListeners.delete(listener);
|
|
1663
1851
|
};
|
|
1664
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
|
+
};
|
|
1665
1860
|
/** Callbacks can capture values of the latest render */
|
|
1666
1861
|
setPagination = (pagination) => {
|
|
1667
1862
|
this.options.pagination = pagination;
|
|
@@ -1671,7 +1866,10 @@ var InfiniteStore = class {
|
|
|
1671
1866
|
* Loads the list when it is first needed. Safe to call many times.
|
|
1672
1867
|
*/
|
|
1673
1868
|
start = ({ revalidate } = {}) => {
|
|
1674
|
-
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
|
+
}
|
|
1675
1873
|
this.started = true;
|
|
1676
1874
|
if (this.pages.length === 0) this.firstFetch = this.schedule("initial");
|
|
1677
1875
|
else if (revalidate || this.getHasStalePages()) this.revalidate();
|
|
@@ -1843,11 +2041,17 @@ var InfiniteStore = class {
|
|
|
1843
2041
|
refetchAtOnce = async (operation, current) => {
|
|
1844
2042
|
const results = await Promise.all(current.map((page) => this.fetchPage(operation, page.param, true)));
|
|
1845
2043
|
if (operation.cancelled) return null;
|
|
1846
|
-
const
|
|
1847
|
-
this.pages = current.map((page, index) => {
|
|
2044
|
+
const pages = current.map((page, index) => {
|
|
1848
2045
|
const fetched = results[index];
|
|
1849
2046
|
return fetched?.response.success ? this.createPage(fetched) : page;
|
|
1850
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);
|
|
1851
2055
|
if (last) this.setResult(last.response);
|
|
1852
2056
|
return last?.response ?? null;
|
|
1853
2057
|
};
|
|
@@ -1929,8 +2133,12 @@ var InfiniteStore = class {
|
|
|
1929
2133
|
return null;
|
|
1930
2134
|
};
|
|
1931
2135
|
getHasStalePages = () => {
|
|
1932
|
-
return this.pages.some((
|
|
1933
|
-
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;
|
|
1934
2142
|
return !value || isStaleCacheData(value.staleTime, value.responseTimestamp);
|
|
1935
2143
|
});
|
|
1936
2144
|
};
|
|
@@ -2015,6 +2223,8 @@ var InfiniteStore = class {
|
|
|
2015
2223
|
}
|
|
2016
2224
|
};
|
|
2017
2225
|
setResult = (response) => {
|
|
2226
|
+
this.nextPageError = false;
|
|
2227
|
+
this.previousPageError = false;
|
|
2018
2228
|
this.result = {
|
|
2019
2229
|
error: response.success ? null : response.error,
|
|
2020
2230
|
status: response.status,
|
|
@@ -2080,7 +2290,7 @@ var InfiniteStore = class {
|
|
|
2080
2290
|
this.hasInvalidation = true;
|
|
2081
2291
|
queueMicrotask(() => {
|
|
2082
2292
|
this.hasInvalidation = false;
|
|
2083
|
-
if (this.
|
|
2293
|
+
if (this.enabledHooks > 0) this.refetch();
|
|
2084
2294
|
});
|
|
2085
2295
|
};
|
|
2086
2296
|
createSnapshot = () => {
|
|
@@ -2133,41 +2343,6 @@ var getInfiniteStore = (key, options) => {
|
|
|
2133
2343
|
return store;
|
|
2134
2344
|
};
|
|
2135
2345
|
//#endregion
|
|
2136
|
-
//#region src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts
|
|
2137
|
-
/**
|
|
2138
|
-
* Identity of a list. Everything describing the request takes part in it, so a changed filter, sort, page size
|
|
2139
|
-
* or URL param starts a new list without anything having to be listed by hand.
|
|
2140
|
-
*/
|
|
2141
|
-
var getInfiniteKey = (request, pagination, maxPages) => {
|
|
2142
|
-
return JSON.stringify([
|
|
2143
|
-
request.toJSON(),
|
|
2144
|
-
pagination,
|
|
2145
|
-
maxPages ?? null
|
|
2146
|
-
], (_key, value) => {
|
|
2147
|
-
return typeof value === "function" ? value.toString() : value;
|
|
2148
|
-
});
|
|
2149
|
-
};
|
|
2150
|
-
/**
|
|
2151
|
-
* Keeps the first occurrence of every item
|
|
2152
|
-
*/
|
|
2153
|
-
var getUniqueItems = (items, uniqueBy) => {
|
|
2154
|
-
if (uniqueBy === void 0) return items;
|
|
2155
|
-
const seen = /* @__PURE__ */ new Set();
|
|
2156
|
-
return items.filter((item) => {
|
|
2157
|
-
const identity = typeof uniqueBy === "function" ? uniqueBy({ item }) : item?.[uniqueBy];
|
|
2158
|
-
if (seen.has(identity)) return false;
|
|
2159
|
-
seen.add(identity);
|
|
2160
|
-
return true;
|
|
2161
|
-
});
|
|
2162
|
-
};
|
|
2163
|
-
/**
|
|
2164
|
-
* Decides if the previous list stays on screen while the new one loads its first page
|
|
2165
|
-
*/
|
|
2166
|
-
var getShouldKeepPreviousList = (mode, previous, current) => {
|
|
2167
|
-
if (mode === "auto") return previous.method === current.method && previous.endpoint === current.endpoint;
|
|
2168
|
-
return mode === "preserve";
|
|
2169
|
-
};
|
|
2170
|
-
//#endregion
|
|
2171
2346
|
//#region src/hooks/use-infinite-fetch/use-infinite-fetch.hooks.ts
|
|
2172
2347
|
var trackedSources = {
|
|
2173
2348
|
items: ["items", "loading"],
|
|
@@ -2298,11 +2473,20 @@ var useInfiniteFetch = (request, options) => {
|
|
|
2298
2473
|
callbacks.current.onReset?.();
|
|
2299
2474
|
}
|
|
2300
2475
|
if (disabled) return;
|
|
2301
|
-
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
|
+
});
|
|
2302
2479
|
else store.start({ revalidate });
|
|
2303
2480
|
}, [store, disabled]);
|
|
2304
|
-
|
|
2305
|
-
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();
|
|
2306
2490
|
}, dependencies);
|
|
2307
2491
|
useEffect(() => {
|
|
2308
2492
|
const { appManager } = client;
|
|
@@ -2345,15 +2529,6 @@ var useInfiniteFetch = (request, options) => {
|
|
|
2345
2529
|
const list = isPreviousData ? previous.snapshot : snapshot;
|
|
2346
2530
|
const isServer = client.mode === "server" || typeof window === "undefined";
|
|
2347
2531
|
if (suspense && !disabled && !isServer && snapshot.loading && !isPreviousData) throw store.start({ revalidate });
|
|
2348
|
-
if (throwOnError) {
|
|
2349
|
-
usedKeys.add("error");
|
|
2350
|
-
usedKeys.add("isFetching");
|
|
2351
|
-
const error = snapshot.error;
|
|
2352
|
-
if ((typeof throwOnError === "function" ? !!error && throwOnError(error) : !!error) && !snapshot.isFetching) {
|
|
2353
|
-
store.evictSoon();
|
|
2354
|
-
throw error;
|
|
2355
|
-
}
|
|
2356
|
-
}
|
|
2357
2532
|
const uniqueByRef = useRef(uniqueBy);
|
|
2358
2533
|
uniqueByRef.current = uniqueBy;
|
|
2359
2534
|
const uniqueByKey = typeof uniqueBy === "function" ? uniqueBy.toString() : uniqueBy;
|
|
@@ -2362,13 +2537,26 @@ var useInfiniteFetch = (request, options) => {
|
|
|
2362
2537
|
}, [list.items, uniqueByKey]);
|
|
2363
2538
|
const selectedRef = useRef(null);
|
|
2364
2539
|
let items = uniqueItems;
|
|
2365
|
-
|
|
2540
|
+
let selectError = null;
|
|
2541
|
+
if (select) try {
|
|
2366
2542
|
const selected = select({
|
|
2367
2543
|
items: uniqueItems,
|
|
2368
2544
|
pages: list.data
|
|
2369
2545
|
});
|
|
2370
2546
|
items = structuralSharing ? replaceEqualDeep(selectedRef.current, selected) : selected;
|
|
2371
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
|
+
}
|
|
2372
2560
|
}
|
|
2373
2561
|
const actions = useMemo(() => ({
|
|
2374
2562
|
fetchNextPage: () => store.fetchNextPage(),
|
|
@@ -2407,10 +2595,10 @@ var useInfiniteFetch = (request, options) => {
|
|
|
2407
2595
|
isFetchNextPageError: snapshot.isFetchNextPageError,
|
|
2408
2596
|
isFetchPreviousPageError: snapshot.isFetchPreviousPageError,
|
|
2409
2597
|
isPreviousData,
|
|
2410
|
-
error
|
|
2598
|
+
error,
|
|
2411
2599
|
status: snapshot.status,
|
|
2412
2600
|
extra: snapshot.extra,
|
|
2413
|
-
success: snapshot.success,
|
|
2601
|
+
success: !selectError && snapshot.success,
|
|
2414
2602
|
bounce: getBounceData(bounceData),
|
|
2415
2603
|
...actions
|
|
2416
2604
|
}, trackedKeys, (usedKey) => usedKeys.add(usedKey));
|
|
@@ -2448,8 +2636,10 @@ var useSubmit = (request, options) => {
|
|
|
2448
2636
|
interval: bounceTime,
|
|
2449
2637
|
timeout: "bounceTimeout" in mergedOptions ? mergedOptions.bounceTimeout : bounceTime
|
|
2450
2638
|
});
|
|
2451
|
-
const
|
|
2639
|
+
const forceUpdate = f$1();
|
|
2640
|
+
const bounceResolver = useRef(null);
|
|
2452
2641
|
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
2642
|
+
const isBounceActive = bounceData.active;
|
|
2453
2643
|
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
2454
2644
|
/**
|
|
2455
2645
|
* State handler with optimization for rerendering, that hooks into the cache state and dispatchers queues
|
|
@@ -2475,9 +2665,33 @@ var useSubmit = (request, options) => {
|
|
|
2475
2665
|
setCacheData,
|
|
2476
2666
|
getIsDataProcessing
|
|
2477
2667
|
});
|
|
2478
|
-
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
|
+
};
|
|
2479
2694
|
const handleSubmit = (submitOptions) => {
|
|
2480
|
-
const requestClone = request.clone(submitOptions);
|
|
2481
2695
|
if (disabled) {
|
|
2482
2696
|
logger.warning({
|
|
2483
2697
|
title: `Cannot submit request`,
|
|
@@ -2487,21 +2701,22 @@ var useSubmit = (request, options) => {
|
|
|
2487
2701
|
submitOptions
|
|
2488
2702
|
}
|
|
2489
2703
|
});
|
|
2490
|
-
return Promise.resolve(
|
|
2491
|
-
data: null,
|
|
2492
|
-
error: /* @__PURE__ */ new Error("Cannot submit request. Option 'disabled' is enabled"),
|
|
2493
|
-
status: null,
|
|
2494
|
-
extra: request.client.adapter.defaultExtra
|
|
2495
|
-
});
|
|
2704
|
+
return Promise.resolve(getNotSentResponse("Cannot submit request. Option 'disabled' is enabled"));
|
|
2496
2705
|
}
|
|
2706
|
+
const requestClone = request.clone(submitOptions);
|
|
2497
2707
|
const triggerRequest = () => {
|
|
2498
2708
|
addCacheDataListener(requestClone);
|
|
2499
2709
|
return sendRequest(requestClone, {
|
|
2500
2710
|
dispatcherType: "submit",
|
|
2501
2711
|
...submitOptions,
|
|
2502
2712
|
onBeforeSent: (data) => {
|
|
2713
|
+
removeLifecycleListener(data.requestId);
|
|
2503
2714
|
addLifecycleListeners(requestClone, data.requestId, data.mutationContext);
|
|
2504
2715
|
submitOptions?.onBeforeSent?.(data);
|
|
2716
|
+
},
|
|
2717
|
+
onResponse: (data) => {
|
|
2718
|
+
removeLifecycleListener(data.requestId);
|
|
2719
|
+
submitOptions?.onResponse?.(data);
|
|
2505
2720
|
}
|
|
2506
2721
|
});
|
|
2507
2722
|
};
|
|
@@ -2518,35 +2733,81 @@ var useSubmit = (request, options) => {
|
|
|
2518
2733
|
if (bounce) {
|
|
2519
2734
|
const bouncedResolve = bounceResolver.current;
|
|
2520
2735
|
bounceResolver.current = (value) => {
|
|
2521
|
-
bouncedResolve(value);
|
|
2736
|
+
bouncedResolve?.(value);
|
|
2522
2737
|
resolve(value);
|
|
2523
2738
|
};
|
|
2739
|
+
let isTriggered = false;
|
|
2524
2740
|
bounceFunction(async () => {
|
|
2741
|
+
isTriggered = true;
|
|
2525
2742
|
const callback = bounceResolver.current;
|
|
2526
|
-
bounceResolver.current =
|
|
2527
|
-
|
|
2743
|
+
bounceResolver.current = null;
|
|
2744
|
+
const value = await triggerRequest();
|
|
2745
|
+
callback?.(value);
|
|
2528
2746
|
});
|
|
2747
|
+
if (!isTriggered && !bounceData.active) resolveBouncedCalls("Request was not sent. Submit call got dropped by the throttle");
|
|
2748
|
+
if (bounceData.active !== isBounceActive) forceUpdate();
|
|
2529
2749
|
} else resolve(await triggerRequest());
|
|
2530
2750
|
};
|
|
2531
2751
|
performSubmit();
|
|
2532
2752
|
});
|
|
2533
2753
|
};
|
|
2534
|
-
const
|
|
2754
|
+
const handleRefetch = () => {
|
|
2535
2755
|
cache.invalidate(request);
|
|
2536
2756
|
};
|
|
2537
|
-
const
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
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]);
|
|
2548
2800
|
useEffect(() => {
|
|
2549
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
|
+
};
|
|
2550
2811
|
}, []);
|
|
2551
2812
|
const setSubmitRenderKey = (key) => {
|
|
2552
2813
|
setRenderKey(key === "submitting" ? "loading" : key);
|
|
@@ -2563,7 +2824,7 @@ var useSubmit = (request, options) => {
|
|
|
2563
2824
|
"requestTimestamp"
|
|
2564
2825
|
];
|
|
2565
2826
|
return createTrackedProxy({
|
|
2566
|
-
submit
|
|
2827
|
+
submit,
|
|
2567
2828
|
data: state.data,
|
|
2568
2829
|
error: state.error,
|
|
2569
2830
|
submitting: state.loading,
|
|
@@ -2576,7 +2837,10 @@ var useSubmit = (request, options) => {
|
|
|
2576
2837
|
abort: callbacks.abort,
|
|
2577
2838
|
...actions,
|
|
2578
2839
|
...handlers,
|
|
2579
|
-
bounce:
|
|
2840
|
+
bounce: {
|
|
2841
|
+
...getBounceData(bounceData),
|
|
2842
|
+
reset: resetBounce
|
|
2843
|
+
},
|
|
2580
2844
|
refetch
|
|
2581
2845
|
}, trackedKeys, setSubmitRenderKey);
|
|
2582
2846
|
};
|
|
@@ -2598,6 +2862,14 @@ var canUpdate = (item) => {
|
|
|
2598
2862
|
if (item.success || item.failed) return false;
|
|
2599
2863
|
return true;
|
|
2600
2864
|
};
|
|
2865
|
+
var getInitialProgress = () => ({
|
|
2866
|
+
progress: 0,
|
|
2867
|
+
timeLeft: 0,
|
|
2868
|
+
sizeLeft: 0,
|
|
2869
|
+
total: 0,
|
|
2870
|
+
loaded: 0,
|
|
2871
|
+
startTimestamp: 0
|
|
2872
|
+
});
|
|
2601
2873
|
/**
|
|
2602
2874
|
* This hook allows you to monitor and control dispatcher request queues for a given request,
|
|
2603
2875
|
* including starting, stopping, pausing, and inspecting pending/in-flight items.
|
|
@@ -2617,43 +2889,39 @@ var useQueue = (request, options) => {
|
|
|
2617
2889
|
const queryKey = scopeKey(rawQueryKey, scope);
|
|
2618
2890
|
const { requestManager } = client;
|
|
2619
2891
|
const [dispatcher] = getRequestDispatcher(request, dispatcherType);
|
|
2620
|
-
const
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
sizeLeft: 0,
|
|
2641
|
-
total: 0,
|
|
2642
|
-
loaded: 0,
|
|
2643
|
-
startTimestamp: 0
|
|
2644
|
-
},
|
|
2645
|
-
stopRequest: () => dispatcher.stopRequest(queryKey, req.requestId),
|
|
2646
|
-
startRequest: () => dispatcher.startRequest(queryKey, req.requestId),
|
|
2647
|
-
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];
|
|
2648
2912
|
}));
|
|
2649
|
-
if (keepFinishedRequests
|
|
2650
|
-
|
|
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];
|
|
2651
2917
|
}, [
|
|
2652
2918
|
abortKey,
|
|
2653
2919
|
dispatcher,
|
|
2654
2920
|
queryKey,
|
|
2655
2921
|
keepFinishedRequests
|
|
2656
2922
|
]);
|
|
2923
|
+
const [stopped, setStopped] = useState(() => dispatcher.getQueue(queryKey).stopped);
|
|
2924
|
+
const [requests, setRequests] = useState(() => createRequestsArray(dispatcher.getQueue(queryKey).requests));
|
|
2657
2925
|
const mergePayloadType = useCallback((requestId, data) => {
|
|
2658
2926
|
setRequests((prev) => prev.map((el) => {
|
|
2659
2927
|
if (el.requestId === requestId && canUpdate(el)) return {
|
|
@@ -2684,15 +2952,18 @@ var useQueue = (request, options) => {
|
|
|
2684
2952
|
const mountEvents = () => {
|
|
2685
2953
|
const unmountChange = dispatcher.events.onQueueChangeByKey(queryKey, updateQueueState);
|
|
2686
2954
|
const unmountStatus = dispatcher.events.onQueueStatusChangeByKey(queryKey, updateQueueState);
|
|
2687
|
-
const
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
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
|
+
}));
|
|
2696
2967
|
});
|
|
2697
2968
|
const unmountCanceled = requestManager.events.onAbort(({ requestId }) => {
|
|
2698
2969
|
setRequests((prev) => prev.map((el) => el.requestId === requestId ? {
|
|
@@ -2700,6 +2971,12 @@ var useQueue = (request, options) => {
|
|
|
2700
2971
|
canceled: true
|
|
2701
2972
|
} : el));
|
|
2702
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
|
+
});
|
|
2703
2980
|
const unmountRemoved = requestManager.events.onRemove(({ requestId }) => {
|
|
2704
2981
|
setRequests((prev) => prev.map((el) => el.requestId === requestId && canUpdate(el) ? {
|
|
2705
2982
|
...el,
|
|
@@ -2731,8 +3008,9 @@ var useQueue = (request, options) => {
|
|
|
2731
3008
|
unmountChange();
|
|
2732
3009
|
unmountDownload();
|
|
2733
3010
|
unmountUpload();
|
|
2734
|
-
|
|
3011
|
+
unmountResponse();
|
|
2735
3012
|
unmountCanceled();
|
|
3013
|
+
unmountStarted();
|
|
2736
3014
|
unmountRemoved();
|
|
2737
3015
|
};
|
|
2738
3016
|
return unmount;
|
|
@@ -2743,10 +3021,6 @@ var useQueue = (request, options) => {
|
|
|
2743
3021
|
queryKey
|
|
2744
3022
|
]);
|
|
2745
3023
|
useEffect(mountEvents, [
|
|
2746
|
-
stopped,
|
|
2747
|
-
requests,
|
|
2748
|
-
setRequests,
|
|
2749
|
-
setStopped,
|
|
2750
3024
|
queryKey,
|
|
2751
3025
|
dispatcher.events,
|
|
2752
3026
|
requestManager.events,
|
|
@@ -2770,6 +3044,18 @@ var useQueueDefaultOptions = {
|
|
|
2770
3044
|
};
|
|
2771
3045
|
//#endregion
|
|
2772
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
|
+
};
|
|
2773
3059
|
/** Subscribe to cache updates for a given request without triggering network activity. */
|
|
2774
3060
|
var useCache = (request, options) => {
|
|
2775
3061
|
const { cacheKey, client } = request;
|
|
@@ -2778,11 +3064,7 @@ var useCache = (request, options) => {
|
|
|
2778
3064
|
const [dispatcher] = getRequestDispatcher(request);
|
|
2779
3065
|
const updateKey = JSON.stringify(request.toJSON());
|
|
2780
3066
|
const { config: globalConfig } = useProvider();
|
|
2781
|
-
const { dependencyTracking, initialResponse, deepCompare, structuralSharing } =
|
|
2782
|
-
...useCacheDefaultOptions,
|
|
2783
|
-
...globalConfig.useCacheConfig,
|
|
2784
|
-
...options
|
|
2785
|
-
};
|
|
3067
|
+
const { dependencyTracking, initialResponse, deepCompare, structuralSharing } = mergeOptions(useCacheDefaultOptions, globalConfig.useCacheConfig, options);
|
|
2786
3068
|
/**
|
|
2787
3069
|
* State handler with optimization for rerendering, that hooks into the cache state and dispatchers queues
|
|
2788
3070
|
*/
|
|
@@ -2793,6 +3075,7 @@ var useCache = (request, options) => {
|
|
|
2793
3075
|
initialResponse,
|
|
2794
3076
|
deepCompare,
|
|
2795
3077
|
dependencyTracking,
|
|
3078
|
+
keepPreviousData: "clean",
|
|
2796
3079
|
select: options?.select,
|
|
2797
3080
|
structuralSharing
|
|
2798
3081
|
});
|
|
@@ -3046,6 +3329,7 @@ var useListener = (listener, options) => {
|
|
|
3046
3329
|
i(() => {
|
|
3047
3330
|
listen();
|
|
3048
3331
|
}, [
|
|
3332
|
+
listener.socket,
|
|
3049
3333
|
listener.topic,
|
|
3050
3334
|
JSON.stringify(listener.options),
|
|
3051
3335
|
getDeliveryKey(listener.delivery)
|
|
@@ -3201,6 +3485,6 @@ var useIsSubmitting = (client, options) => {
|
|
|
3201
3485
|
return useRunningRequestsCount(client.submitDispatcher, options);
|
|
3202
3486
|
};
|
|
3203
3487
|
//#endregion
|
|
3204
|
-
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 };
|
|
3205
3489
|
|
|
3206
3490
|
//# sourceMappingURL=index.mjs.map
|