@hyper-fetch/react 9.4.1 → 9.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/features/hydrate/hydrate.spec.tsx +511 -0
- package/__tests__/features/hydrate/hydrate.ssr.spec.tsx +189 -0
- package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +2 -1
- package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +4 -2
- package/__tests__/features/use-fetch/use-fetch.scope.spec.tsx +211 -0
- package/__tests__/features/use-fetch/use-fetch.suspense.spec.tsx +223 -0
- package/__tests__/tsconfig.json +2 -0
- package/dist/helpers/use-request-events/use-request-events.hooks.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 +4 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -1
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -1
- package/dist/hydrate/hydrate.d.ts +16 -0
- package/dist/hydrate/hydrate.d.ts.map +1 -0
- package/dist/hydrate/hydrate.types.d.ts +52 -0
- package/dist/hydrate/hydrate.types.d.ts.map +1 -0
- package/dist/hydrate/hydrate.utils.d.ts +19 -0
- package/dist/hydrate/hydrate.utils.d.ts.map +1 -0
- package/dist/hydrate/index.d.ts +3 -0
- package/dist/hydrate/index.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +225 -28
- package/dist/index.mjs.map +1 -1
- package/dist/utils/scope.utils.d.ts +7 -0
- package/dist/utils/scope.utils.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/helpers/use-request-events/use-request-events.hooks.ts +15 -6
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +8 -3
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +9 -1
- package/src/hooks/use-fetch/use-fetch.hooks.ts +78 -16
- package/src/hydrate/hydrate.tsx +74 -0
- package/src/hydrate/hydrate.types.ts +55 -0
- package/src/hydrate/hydrate.utils.ts +124 -0
- package/src/hydrate/index.ts +2 -0
- package/src/index.ts +1 -0
- package/src/utils/scope.utils.ts +8 -0
- package/tsconfig.json +2 -0
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
-
import { Request, Time, getRequestDispatcher, replaceEqualDeep, scopeKey, sendRequest } from "@hyper-fetch/core";
|
|
1
|
+
import React, { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import { Request, Time, getRequestDispatcher, parseResponse, replaceEqualDeep, scopeKey, sendRequest } from "@hyper-fetch/core";
|
|
3
3
|
import { jsx } from "react/jsx-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
|
|
@@ -118,6 +118,16 @@ var y = function(n) {
|
|
|
118
118
|
};
|
|
119
119
|
"undefined" == typeof window || void 0 === window.document || window.document.createElement;
|
|
120
120
|
//#endregion
|
|
121
|
+
//#region src/utils/scope.utils.ts
|
|
122
|
+
/**
|
|
123
|
+
* Core emits request and cache events by the logical (unscoped) keys, while dispatcher queues and cache storage
|
|
124
|
+
* use the scoped keys. Listeners subscribe by the logical key and use this check to skip events of other scopes.
|
|
125
|
+
* @internal
|
|
126
|
+
*/
|
|
127
|
+
var isSameScope = (scope, otherScope) => {
|
|
128
|
+
return (scope ?? null) === (otherScope ?? null);
|
|
129
|
+
};
|
|
130
|
+
//#endregion
|
|
121
131
|
//#region src/helpers/use-request-events/use-request-events.hooks.ts
|
|
122
132
|
/**
|
|
123
133
|
* This is helper hook that handles main Hyper-Fetch event/data flow
|
|
@@ -199,7 +209,8 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
199
209
|
if (!willRetry) optimisticResultsRef.current.delete(values.requestId);
|
|
200
210
|
};
|
|
201
211
|
const handleGetLoadingEvent = (req) => {
|
|
202
|
-
return ({ loading }) => {
|
|
212
|
+
return ({ loading, request: eventRequest }) => {
|
|
213
|
+
if (!isSameScope(eventRequest.scope, req.scope)) return;
|
|
203
214
|
if (getIsDataProcessing(req.cacheKey)) return;
|
|
204
215
|
const canDisableLoading = !loading && !dispatcher.hasRunningRequests(scopeKey(req.queryKey, req.scope));
|
|
205
216
|
if (loading || canDisableLoading) actions.setLoading(loading);
|
|
@@ -248,8 +259,11 @@ var useRequestEvents = ({ request, dispatcher, logger, actions, setCacheData, ge
|
|
|
248
259
|
dataEvents.current = null;
|
|
249
260
|
};
|
|
250
261
|
const addCacheDataListener = (req) => {
|
|
251
|
-
const loadingUnmount = requestManager.events.onLoadingByQueue(
|
|
252
|
-
const getResponseUnmount = cache.events.onDataByKey(req.cacheKey,
|
|
262
|
+
const loadingUnmount = requestManager.events.onLoadingByQueue(req.queryKey, handleGetLoadingEvent(req));
|
|
263
|
+
const getResponseUnmount = cache.events.onDataByKey(req.cacheKey, (cacheData) => {
|
|
264
|
+
if (!isSameScope(cacheData.scope, req.scope)) return;
|
|
265
|
+
setCacheData(cacheData);
|
|
266
|
+
});
|
|
253
267
|
const unmount = () => {
|
|
254
268
|
loadingUnmount();
|
|
255
269
|
getResponseUnmount();
|
|
@@ -370,6 +384,154 @@ var initialState = {
|
|
|
370
384
|
requestTimestamp: null
|
|
371
385
|
};
|
|
372
386
|
//#endregion
|
|
387
|
+
//#region src/hydrate/hydrate.utils.ts
|
|
388
|
+
/**
|
|
389
|
+
* Items already written to the cache (or skipped because the cache held newer data), per client.
|
|
390
|
+
* Once an item is consumed the cache is the source of truth, so a hook mounted after `client.clear()`
|
|
391
|
+
* does not get the old server snapshot back.
|
|
392
|
+
*/
|
|
393
|
+
var consumedItems = /* @__PURE__ */ new WeakMap();
|
|
394
|
+
var getConsumed = (client) => {
|
|
395
|
+
let consumed = consumedItems.get(client);
|
|
396
|
+
if (!consumed) {
|
|
397
|
+
consumed = /* @__PURE__ */ new WeakSet();
|
|
398
|
+
consumedItems.set(client, consumed);
|
|
399
|
+
}
|
|
400
|
+
return consumed;
|
|
401
|
+
};
|
|
402
|
+
var isConsumed = (client, item) => {
|
|
403
|
+
return getConsumed(client).has(item);
|
|
404
|
+
};
|
|
405
|
+
var markConsumed = (client, item) => {
|
|
406
|
+
getConsumed(client).add(item);
|
|
407
|
+
};
|
|
408
|
+
/**
|
|
409
|
+
* Resolves the config of a single item exactly like `client.hydrate()` does
|
|
410
|
+
*/
|
|
411
|
+
var resolveConfig = (item, options) => {
|
|
412
|
+
const { cacheKey, scope, response: _response, ...fallbackOptions } = item;
|
|
413
|
+
const defaults = {
|
|
414
|
+
cache: true,
|
|
415
|
+
override: true
|
|
416
|
+
};
|
|
417
|
+
return {
|
|
418
|
+
...typeof options === "function" ? {
|
|
419
|
+
...defaults,
|
|
420
|
+
...fallbackOptions,
|
|
421
|
+
...options(item)
|
|
422
|
+
} : {
|
|
423
|
+
...defaults,
|
|
424
|
+
...fallbackOptions,
|
|
425
|
+
...options
|
|
426
|
+
},
|
|
427
|
+
cacheKey,
|
|
428
|
+
scope
|
|
429
|
+
};
|
|
430
|
+
};
|
|
431
|
+
var getResponseTimestamp = (value, fallback = 0) => {
|
|
432
|
+
const timestamp = Number(value?.responseTimestamp);
|
|
433
|
+
return Number.isFinite(timestamp) ? timestamp : fallback;
|
|
434
|
+
};
|
|
435
|
+
/**
|
|
436
|
+
* Turns dehydrated items into the values they will have in the cache, keyed by storage key.
|
|
437
|
+
* The last item wins when the same key is passed twice.
|
|
438
|
+
*/
|
|
439
|
+
var getHydratedEntries = (client, data, options) => {
|
|
440
|
+
const entries = /* @__PURE__ */ new Map();
|
|
441
|
+
data?.forEach((item) => {
|
|
442
|
+
if (!item) return;
|
|
443
|
+
const config = resolveConfig(item, options);
|
|
444
|
+
const response = parseResponse(item.response);
|
|
445
|
+
const storageKey = scopeKey(config.cacheKey, config.scope ?? null);
|
|
446
|
+
const value = {
|
|
447
|
+
...response,
|
|
448
|
+
cacheKey: config.cacheKey,
|
|
449
|
+
scope: config.scope ?? null,
|
|
450
|
+
staleTime: config.staleTime,
|
|
451
|
+
cacheTime: config.cacheTime,
|
|
452
|
+
version: client.cache.version,
|
|
453
|
+
cached: !!config.cache
|
|
454
|
+
};
|
|
455
|
+
entries.set(storageKey, {
|
|
456
|
+
item,
|
|
457
|
+
storageKey,
|
|
458
|
+
value,
|
|
459
|
+
override: config.override !== false,
|
|
460
|
+
timestamp: getResponseTimestamp(response, item.timestamp ?? 0)
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
return entries;
|
|
464
|
+
};
|
|
465
|
+
/**
|
|
466
|
+
* Decides whether the hydrated value should be used instead of the cached one.
|
|
467
|
+
* Newer data always wins; with `override: false` existing cache data is kept.
|
|
468
|
+
*/
|
|
469
|
+
var shouldUseHydratedValue = (cacheData, entry) => {
|
|
470
|
+
if (!cacheData) return true;
|
|
471
|
+
if (!entry.override) return false;
|
|
472
|
+
return entry.timestamp > getResponseTimestamp(cacheData);
|
|
473
|
+
};
|
|
474
|
+
/**
|
|
475
|
+
* Picks the data a hook should start with: cached data, unless the hydrated data is newer
|
|
476
|
+
*/
|
|
477
|
+
var pickInitialCacheData = (cacheData, entry) => {
|
|
478
|
+
if (!entry || !shouldUseHydratedValue(cacheData, entry)) return cacheData;
|
|
479
|
+
return entry.value;
|
|
480
|
+
};
|
|
481
|
+
//#endregion
|
|
482
|
+
//#region src/hydrate/hydrate.tsx
|
|
483
|
+
var HydrationContext = React.createContext(null);
|
|
484
|
+
var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
|
|
485
|
+
/**
|
|
486
|
+
* Hydrates the client cache with data fetched on the server.
|
|
487
|
+
*
|
|
488
|
+
* - Hooks below it render with the hydrated data on the first render, also during server rendering.
|
|
489
|
+
* - On the client the data is written to the cache before the hooks mount, so fresh data is not refetched.
|
|
490
|
+
* - Newer cache data is never replaced by an older snapshot.
|
|
491
|
+
* - It can be nested and rendered conditionally; every `Hydrate` serves only its own client.
|
|
492
|
+
*/
|
|
493
|
+
var Hydrate = ({ client, data, options, children }) => {
|
|
494
|
+
const parent = useContext(HydrationContext);
|
|
495
|
+
const entries = useMemo(() => getHydratedEntries(client, data, options), [
|
|
496
|
+
client,
|
|
497
|
+
data,
|
|
498
|
+
options
|
|
499
|
+
]);
|
|
500
|
+
const entriesRef = useRef(entries);
|
|
501
|
+
entriesRef.current = entries;
|
|
502
|
+
useIsomorphicLayoutEffect(() => {
|
|
503
|
+
if (client.mode === "server") return;
|
|
504
|
+
entries.forEach((entry) => {
|
|
505
|
+
if (isConsumed(client, entry.item)) return;
|
|
506
|
+
markConsumed(client, entry.item);
|
|
507
|
+
if (!shouldUseHydratedValue(client.cache.get(entry.storageKey), entry)) return;
|
|
508
|
+
client.hydrate([entry.item], options);
|
|
509
|
+
});
|
|
510
|
+
}, [
|
|
511
|
+
client,
|
|
512
|
+
entries,
|
|
513
|
+
options
|
|
514
|
+
]);
|
|
515
|
+
const value = useMemo(() => ({ get: (targetClient, storageKey) => {
|
|
516
|
+
if (targetClient === client) {
|
|
517
|
+
const entry = entriesRef.current.get(storageKey);
|
|
518
|
+
if (entry) return isConsumed(client, entry.item) ? void 0 : entry;
|
|
519
|
+
}
|
|
520
|
+
return parent?.get(targetClient, storageKey);
|
|
521
|
+
} }), [client, parent]);
|
|
522
|
+
return /* @__PURE__ */ jsx(HydrationContext.Provider, {
|
|
523
|
+
value,
|
|
524
|
+
children
|
|
525
|
+
});
|
|
526
|
+
};
|
|
527
|
+
/**
|
|
528
|
+
* Access to the closest hydration data
|
|
529
|
+
* @internal
|
|
530
|
+
*/
|
|
531
|
+
var useHydration = () => {
|
|
532
|
+
return useContext(HydrationContext);
|
|
533
|
+
};
|
|
534
|
+
//#endregion
|
|
373
535
|
//#region src/utils/deep-equal.utils.ts
|
|
374
536
|
/**
|
|
375
537
|
* Check if value is empty
|
|
@@ -507,10 +669,10 @@ var getIsInitiallyLoading = ({ queryKey, dispatcher, hasState, revalidate, disab
|
|
|
507
669
|
const queue = dispatcher.getQueue(queryKey);
|
|
508
670
|
return dispatcher.hasRunningRequests(queryKey) || !queue.stopped && disabled === false;
|
|
509
671
|
};
|
|
510
|
-
var getInitialState = ({ initialResponse, dispatcher, request, disabled, revalidate }) => {
|
|
672
|
+
var getInitialState = ({ initialResponse, dispatcher, request, disabled, revalidate, hydratedEntry }) => {
|
|
511
673
|
const { client, cacheKey, unstable_responseMapper } = request;
|
|
512
674
|
const { cache } = client;
|
|
513
|
-
const cacheState = getValidCacheData(request, initialResponse, cache.get(scopeKey(cacheKey, request.scope)));
|
|
675
|
+
const cacheState = getValidCacheData(request, initialResponse, pickInitialCacheData(cache.get(scopeKey(cacheKey, request.scope)), hydratedEntry));
|
|
514
676
|
const initialLoading = getIsInitiallyLoading({
|
|
515
677
|
queryKey: scopeKey(request.queryKey, request.scope),
|
|
516
678
|
dispatcher,
|
|
@@ -558,6 +720,7 @@ var getInitialState = ({ initialResponse, dispatcher, request, disabled, revalid
|
|
|
558
720
|
var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, dependencyTracking, keepPreviousData = "auto", disabled, revalidate, select, structuralSharing = true }) => {
|
|
559
721
|
const { client, cacheKey, queryKey, staleTime, unstable_responseMapper } = request;
|
|
560
722
|
const { cache } = client;
|
|
723
|
+
const hydration = useHydration();
|
|
561
724
|
const state = useRef(null);
|
|
562
725
|
const renderKeys = useRef([]);
|
|
563
726
|
const isProcessingData = useRef("");
|
|
@@ -584,12 +747,16 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
584
747
|
data: selectData(newState.data, state.current?.data)
|
|
585
748
|
};
|
|
586
749
|
};
|
|
587
|
-
if (!state.current)
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
750
|
+
if (!state.current) {
|
|
751
|
+
const hydratedEntry = hydration?.get(client, scopeKey(cacheKey, request.scope));
|
|
752
|
+
state.current = getSelectedState(getInitialState({
|
|
753
|
+
initialResponse,
|
|
754
|
+
dispatcher,
|
|
755
|
+
request,
|
|
756
|
+
disabled,
|
|
757
|
+
hydratedEntry
|
|
758
|
+
}));
|
|
759
|
+
}
|
|
593
760
|
if (selectError.current) {
|
|
594
761
|
const { error } = selectError.current;
|
|
595
762
|
selectError.current = null;
|
|
@@ -626,7 +793,7 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
626
793
|
i(() => {
|
|
627
794
|
const oldKey = previousCacheKey.current;
|
|
628
795
|
previousCacheKey.current = cacheKey;
|
|
629
|
-
const cacheState = getValidCacheData(request, initialResponse, cache.get(cacheKey));
|
|
796
|
+
const cacheState = getValidCacheData(request, initialResponse, cache.get(scopeKey(cacheKey, request.scope)));
|
|
630
797
|
if (getShouldClearState(keepPreviousData, oldKey, cacheKey) && !cacheState) {
|
|
631
798
|
state.current = getSelectedState(getInitialState({
|
|
632
799
|
initialResponse,
|
|
@@ -685,7 +852,7 @@ var useTrackedState = ({ request, dispatcher, initialResponse, deepCompare, depe
|
|
|
685
852
|
retries: cacheData.retries,
|
|
686
853
|
responseTimestamp: new Date(cacheData.responseTimestamp),
|
|
687
854
|
requestTimestamp: new Date(cacheData.requestTimestamp),
|
|
688
|
-
loading: dispatcher.hasRunningRequests(queryKey)
|
|
855
|
+
loading: dispatcher.hasRunningRequests(scopeKey(queryKey, request.scope))
|
|
689
856
|
};
|
|
690
857
|
const nextState = { ...state.current };
|
|
691
858
|
const changedKeys = Object.keys(newStateValues).filter((key) => {
|
|
@@ -993,8 +1160,19 @@ var useProvider = () => {
|
|
|
993
1160
|
};
|
|
994
1161
|
//#endregion
|
|
995
1162
|
//#region src/hooks/use-fetch/use-fetch.hooks.ts
|
|
996
|
-
var
|
|
997
|
-
var
|
|
1163
|
+
var suspenseStores = /* @__PURE__ */ new WeakMap();
|
|
1164
|
+
var suspenseFreshTime = 1e3;
|
|
1165
|
+
var getSuspenseStore = (client) => {
|
|
1166
|
+
let store = suspenseStores.get(client);
|
|
1167
|
+
if (!store) {
|
|
1168
|
+
store = {
|
|
1169
|
+
promises: /* @__PURE__ */ new Map(),
|
|
1170
|
+
results: /* @__PURE__ */ new Map()
|
|
1171
|
+
};
|
|
1172
|
+
suspenseStores.set(client, store);
|
|
1173
|
+
}
|
|
1174
|
+
return store;
|
|
1175
|
+
};
|
|
998
1176
|
/**
|
|
999
1177
|
* This hook aims to retrieve data from the server. It automatically fetches on mount and
|
|
1000
1178
|
* refetches based on dependencies, with support for caching, polling, and suspense.
|
|
@@ -1019,6 +1197,7 @@ var useFetch = (request, options) => {
|
|
|
1019
1197
|
const { cacheKey, queryKey, client } = request;
|
|
1020
1198
|
const { cache, fetchDispatcher: dispatcher, appManager, loggerManager } = client;
|
|
1021
1199
|
const ignoreReact18DoubleRender = useRef(true);
|
|
1200
|
+
const resolvedBySuspense = useRef(false);
|
|
1022
1201
|
const logger = useRef(loggerManager.initialize(client, "useFetch")).current;
|
|
1023
1202
|
const bounceData = bounceType === "throttle" ? requestThrottle : requestDebounce;
|
|
1024
1203
|
const bounceFunction = bounceType === "throttle" ? requestThrottle.throttle : requestDebounce.debounce;
|
|
@@ -1092,9 +1271,21 @@ var useFetch = (request, options) => {
|
|
|
1092
1271
|
handleRefresh();
|
|
1093
1272
|
};
|
|
1094
1273
|
const getIsFetchingIdentity = () => {
|
|
1095
|
-
return dispatcher.getRunningRequests(queryKey).some((running) => running.request.cacheKey === cacheKey);
|
|
1274
|
+
return dispatcher.getRunningRequests(scopeKey(queryKey, request.scope)).some((running) => running.request.cacheKey === cacheKey);
|
|
1275
|
+
};
|
|
1276
|
+
/**
|
|
1277
|
+
* A suspended component mounts right after its own request resolved - fetching again on mount would
|
|
1278
|
+
* duplicate that request. Data received within the last moment counts as fresh, like the minimal stale time
|
|
1279
|
+
* applied by other suspense-enabled libraries.
|
|
1280
|
+
*/
|
|
1281
|
+
const getHasJustResolvedSuspense = () => {
|
|
1282
|
+
if (!suspense) return false;
|
|
1283
|
+
if (resolvedBySuspense.current) return true;
|
|
1284
|
+
const cacheData = cache.get(scopeKey(cacheKey, request.scope));
|
|
1285
|
+
return !!cacheData && Date.now() - cacheData.responseTimestamp < suspenseFreshTime;
|
|
1096
1286
|
};
|
|
1097
1287
|
const initialFetchData = () => {
|
|
1288
|
+
if (getHasJustResolvedSuspense()) return;
|
|
1098
1289
|
const hasStaleData = getStaleStatus();
|
|
1099
1290
|
const isFetching = getIsFetchingIdentity();
|
|
1100
1291
|
if ((revalidate || hasStaleData) && !isFetching) handleFetch();
|
|
@@ -1191,24 +1382,29 @@ var useFetch = (request, options) => {
|
|
|
1191
1382
|
ignoreReact18DoubleRender.current = true;
|
|
1192
1383
|
});
|
|
1193
1384
|
if (suspense && !disabled) {
|
|
1194
|
-
const
|
|
1385
|
+
const { promises: suspensePromiseMap, results: suspenseResultMap } = getSuspenseStore(client);
|
|
1386
|
+
const suspenseKey = scopeKey(cacheKey, request.scope);
|
|
1387
|
+
const suspenseResult = suspenseResultMap.get(suspenseKey);
|
|
1195
1388
|
if (suspenseResult) {
|
|
1196
|
-
suspenseResultMap.delete(
|
|
1389
|
+
suspenseResultMap.delete(suspenseKey);
|
|
1390
|
+
resolvedBySuspense.current = true;
|
|
1197
1391
|
if (suspenseResult.data !== null) state.data = selectData(suspenseResult.data);
|
|
1198
1392
|
if (suspenseResult.error !== null) state.error = suspenseResult.error;
|
|
1199
1393
|
if (suspenseResult.status !== null) state.status = suspenseResult.status;
|
|
1200
1394
|
if (suspenseResult.extra !== null) state.extra = suspenseResult.extra;
|
|
1201
1395
|
state.success = suspenseResult.success;
|
|
1396
|
+
state.loading = dispatcher.hasRunningRequests(scopeKey(queryKey, request.scope));
|
|
1202
1397
|
}
|
|
1203
1398
|
if (!(state.data !== null || state.error !== null)) {
|
|
1204
|
-
let entry = suspensePromiseMap.get(
|
|
1399
|
+
let entry = suspensePromiseMap.get(suspenseKey);
|
|
1205
1400
|
if (!entry) {
|
|
1206
1401
|
let resolvePromise;
|
|
1207
1402
|
const promise = new Promise((r) => {
|
|
1208
1403
|
resolvePromise = r;
|
|
1209
1404
|
});
|
|
1210
1405
|
const unsubscribe = cache.events.onDataByKey(cacheKey, (cacheData) => {
|
|
1211
|
-
|
|
1406
|
+
if (!isSameScope(cacheData.scope, request.scope)) return;
|
|
1407
|
+
suspenseResultMap.set(suspenseKey, {
|
|
1212
1408
|
data: cacheData.data ?? null,
|
|
1213
1409
|
error: cacheData.error ?? null,
|
|
1214
1410
|
status: cacheData.status ?? null,
|
|
@@ -1217,22 +1413,23 @@ var useFetch = (request, options) => {
|
|
|
1217
1413
|
});
|
|
1218
1414
|
resolvePromise();
|
|
1219
1415
|
unsubscribe();
|
|
1220
|
-
suspensePromiseMap.delete(
|
|
1416
|
+
suspensePromiseMap.delete(suspenseKey);
|
|
1221
1417
|
});
|
|
1222
1418
|
entry = {
|
|
1223
1419
|
promise,
|
|
1224
1420
|
resolve: resolvePromise,
|
|
1225
1421
|
cleanup: unsubscribe
|
|
1226
1422
|
};
|
|
1227
|
-
suspensePromiseMap.set(
|
|
1423
|
+
suspensePromiseMap.set(suspenseKey, entry);
|
|
1228
1424
|
}
|
|
1229
|
-
|
|
1425
|
+
const scopedQueryKey = scopeKey(queryKey, request.scope);
|
|
1426
|
+
if (!dispatcher.hasRunningRequests(scopedQueryKey) && !dispatcher.getIsActiveQueue(scopedQueryKey)) dispatcher.add(request);
|
|
1230
1427
|
throw entry.promise;
|
|
1231
1428
|
}
|
|
1232
|
-
const entry = suspensePromiseMap.get(
|
|
1429
|
+
const entry = suspensePromiseMap.get(suspenseKey);
|
|
1233
1430
|
if (entry) {
|
|
1234
1431
|
entry.cleanup();
|
|
1235
|
-
suspensePromiseMap.delete(
|
|
1432
|
+
suspensePromiseMap.delete(suspenseKey);
|
|
1236
1433
|
}
|
|
1237
1434
|
}
|
|
1238
1435
|
if (throwOnError) {
|
|
@@ -2093,6 +2290,6 @@ var useIsSubmitting = (client, options) => {
|
|
|
2093
2290
|
return useRunningRequestsCount(client.submitDispatcher, options);
|
|
2094
2291
|
};
|
|
2095
2292
|
//#endregion
|
|
2096
|
-
export { Provider, createTrackedProxy, getBounceData, getDetailsState, getInitialState, getIsInitiallyLoading, getRefreshTime, getShouldClearState, getTimestamp, getValidCacheData, initialSocketState, initialState, isEmpty, isEqual, isStaleCacheData, useAppManager, useCache, useCacheDefaultOptions, useEmitter, useFetch, useFetchDefaultOptions, useIsFetching, useIsSubmitting, useListener, useProvider, useQueue, useQueueDefaultOptions, useRequestEvents, useSocketState, useStream, useSubmit, useSubmitDefaultOptions, useTrackedState };
|
|
2293
|
+
export { Hydrate, Provider, createTrackedProxy, getBounceData, getDetailsState, getInitialState, getIsInitiallyLoading, getRefreshTime, getShouldClearState, getTimestamp, getValidCacheData, initialSocketState, initialState, isEmpty, isEqual, isStaleCacheData, useAppManager, useCache, useCacheDefaultOptions, useEmitter, useFetch, useFetchDefaultOptions, useIsFetching, useIsSubmitting, useListener, useProvider, useQueue, useQueueDefaultOptions, useRequestEvents, useSocketState, useStream, useSubmit, useSubmitDefaultOptions, useTrackedState };
|
|
2097
2294
|
|
|
2098
2295
|
//# sourceMappingURL=index.mjs.map
|