@tanstack/query-core 5.104.0 → 5.104.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/build/legacy/environmentManager.cjs +3 -1
- package/build/legacy/environmentManager.cjs.map +1 -1
- package/build/legacy/environmentManager.d.cts +6 -1
- package/build/legacy/environmentManager.d.cts.map +1 -1
- package/build/legacy/environmentManager.d.ts +6 -1
- package/build/legacy/environmentManager.d.ts.map +1 -1
- package/build/legacy/environmentManager.js +3 -1
- package/build/legacy/environmentManager.js.map +1 -1
- package/build/legacy/focusManager.cjs +4 -2
- package/build/legacy/focusManager.cjs.map +1 -1
- package/build/legacy/focusManager.d.cts +5 -3
- package/build/legacy/focusManager.d.cts.map +1 -1
- package/build/legacy/focusManager.d.ts +5 -3
- package/build/legacy/focusManager.d.ts.map +1 -1
- package/build/legacy/focusManager.js +4 -2
- package/build/legacy/focusManager.js.map +1 -1
- package/build/legacy/{hydration-B9t5w6NC.d.ts → hydration-Dfuxd5FW.d.cts} +1410 -128
- package/build/legacy/hydration-Dfuxd5FW.d.cts.map +1 -0
- package/build/legacy/{hydration-Br9VIGXa.d.cts → hydration-UAqdtMIf.d.ts} +1410 -128
- package/build/legacy/hydration-UAqdtMIf.d.ts.map +1 -0
- package/build/legacy/hydration.cjs +35 -0
- package/build/legacy/hydration.cjs.map +1 -1
- package/build/legacy/hydration.d.cts +1 -1
- package/build/legacy/hydration.d.ts +1 -1
- package/build/legacy/hydration.js +35 -0
- package/build/legacy/hydration.js.map +1 -1
- package/build/legacy/index.d.cts +1 -1
- package/build/legacy/index.d.ts +1 -1
- package/build/legacy/infiniteQueryBehavior.cjs +29 -0
- package/build/legacy/infiniteQueryBehavior.cjs.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.cts +16 -1
- package/build/legacy/infiniteQueryBehavior.d.cts.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.ts +16 -1
- package/build/legacy/infiniteQueryBehavior.d.ts.map +1 -1
- package/build/legacy/infiniteQueryBehavior.js +29 -0
- package/build/legacy/infiniteQueryBehavior.js.map +1 -1
- package/build/legacy/infiniteQueryObserver.cjs +13 -7
- package/build/legacy/infiniteQueryObserver.cjs.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.cts +15 -9
- package/build/legacy/infiniteQueryObserver.d.cts.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.ts +15 -9
- package/build/legacy/infiniteQueryObserver.d.ts.map +1 -1
- package/build/legacy/infiniteQueryObserver.js +13 -7
- package/build/legacy/infiniteQueryObserver.js.map +1 -1
- package/build/legacy/mutation.cjs +27 -10
- package/build/legacy/mutation.cjs.map +1 -1
- package/build/legacy/mutation.d.cts +1 -1
- package/build/legacy/mutation.d.ts +1 -1
- package/build/legacy/mutation.js +27 -10
- package/build/legacy/mutation.js.map +1 -1
- package/build/legacy/mutationCache.cjs +53 -12
- package/build/legacy/mutationCache.cjs.map +1 -1
- package/build/legacy/mutationCache.d.cts +1 -1
- package/build/legacy/mutationCache.d.ts +1 -1
- package/build/legacy/mutationCache.js +53 -12
- package/build/legacy/mutationCache.js.map +1 -1
- package/build/legacy/mutationObserver.cjs +27 -16
- package/build/legacy/mutationObserver.cjs.map +1 -1
- package/build/legacy/mutationObserver.d.cts +1 -1
- package/build/legacy/mutationObserver.d.ts +1 -1
- package/build/legacy/mutationObserver.js +27 -16
- package/build/legacy/mutationObserver.js.map +1 -1
- package/build/legacy/notifyManager.cjs +12 -2
- package/build/legacy/notifyManager.cjs.map +1 -1
- package/build/legacy/notifyManager.d.cts +19 -4
- package/build/legacy/notifyManager.d.cts.map +1 -1
- package/build/legacy/notifyManager.d.ts +19 -4
- package/build/legacy/notifyManager.d.ts.map +1 -1
- package/build/legacy/notifyManager.js +12 -2
- package/build/legacy/notifyManager.js.map +1 -1
- package/build/legacy/onlineManager.cjs +4 -2
- package/build/legacy/onlineManager.cjs.map +1 -1
- package/build/legacy/onlineManager.d.cts +5 -3
- package/build/legacy/onlineManager.d.cts.map +1 -1
- package/build/legacy/onlineManager.d.ts +5 -3
- package/build/legacy/onlineManager.d.ts.map +1 -1
- package/build/legacy/onlineManager.js +4 -2
- package/build/legacy/onlineManager.js.map +1 -1
- package/build/legacy/queriesObserver.cjs +15 -3
- package/build/legacy/queriesObserver.cjs.map +1 -1
- package/build/legacy/queriesObserver.d.cts +14 -5
- package/build/legacy/queriesObserver.d.cts.map +1 -1
- package/build/legacy/queriesObserver.d.ts +14 -5
- package/build/legacy/queriesObserver.d.ts.map +1 -1
- package/build/legacy/queriesObserver.js +15 -3
- package/build/legacy/queriesObserver.js.map +1 -1
- package/build/legacy/query.cjs +89 -15
- package/build/legacy/query.cjs.map +1 -1
- package/build/legacy/query.d.cts +1 -1
- package/build/legacy/query.d.ts +1 -1
- package/build/legacy/query.js +89 -15
- package/build/legacy/query.js.map +1 -1
- package/build/legacy/queryCache.cjs +34 -12
- package/build/legacy/queryCache.cjs.map +1 -1
- package/build/legacy/queryCache.d.cts +1 -1
- package/build/legacy/queryCache.d.ts +1 -1
- package/build/legacy/queryCache.js +34 -12
- package/build/legacy/queryCache.js.map +1 -1
- package/build/legacy/queryClient.cjs +104 -32
- package/build/legacy/queryClient.cjs.map +1 -1
- package/build/legacy/queryClient.d.cts +1 -1
- package/build/legacy/queryClient.d.ts +1 -1
- package/build/legacy/queryClient.js +104 -32
- package/build/legacy/queryClient.js.map +1 -1
- package/build/legacy/queryObserver.cjs +78 -6
- package/build/legacy/queryObserver.cjs.map +1 -1
- package/build/legacy/queryObserver.d.cts +1 -1
- package/build/legacy/queryObserver.d.ts +1 -1
- package/build/legacy/queryObserver.js +78 -6
- package/build/legacy/queryObserver.js.map +1 -1
- package/build/{modern/removable-0GzhW1PK.d.cts → legacy/removable-Cw9GNGyK.d.cts} +17 -1
- package/build/legacy/removable-Cw9GNGyK.d.cts.map +1 -0
- package/build/legacy/{removable-0GzhW1PK.d.cts → removable-Cw9GNGyK.d.ts} +17 -1
- package/build/legacy/removable-Cw9GNGyK.d.ts.map +1 -0
- package/build/legacy/removable.cjs +12 -0
- package/build/legacy/removable.cjs.map +1 -1
- package/build/legacy/removable.d.cts +1 -1
- package/build/legacy/removable.d.ts +1 -1
- package/build/legacy/removable.js +12 -0
- package/build/legacy/removable.js.map +1 -1
- package/build/legacy/retryer.cjs +26 -4
- package/build/legacy/retryer.cjs.map +1 -1
- package/build/legacy/retryer.d.cts +1 -1
- package/build/legacy/retryer.d.ts +1 -1
- package/build/legacy/retryer.js +26 -4
- package/build/legacy/retryer.js.map +1 -1
- package/build/legacy/streamedQuery.cjs +3 -1
- package/build/legacy/streamedQuery.cjs.map +1 -1
- package/build/legacy/streamedQuery.d.cts +4 -2
- package/build/legacy/streamedQuery.d.cts.map +1 -1
- package/build/legacy/streamedQuery.d.ts +4 -2
- package/build/legacy/streamedQuery.d.ts.map +1 -1
- package/build/legacy/streamedQuery.js +3 -1
- package/build/legacy/streamedQuery.js.map +1 -1
- package/build/{modern/subscribable-CysQ0tne.d.cts → legacy/subscribable-czlHOxMP.d.cts} +11 -1
- package/build/legacy/subscribable-czlHOxMP.d.cts.map +1 -0
- package/build/legacy/{subscribable-CysQ0tne.d.cts → subscribable-czlHOxMP.d.ts} +11 -1
- package/build/legacy/subscribable-czlHOxMP.d.ts.map +1 -0
- package/build/legacy/subscribable.cjs +10 -0
- package/build/legacy/subscribable.cjs.map +1 -1
- package/build/legacy/subscribable.d.cts +1 -1
- package/build/legacy/subscribable.d.ts +1 -1
- package/build/legacy/subscribable.js +10 -0
- package/build/legacy/subscribable.js.map +1 -1
- package/build/legacy/timeoutManager.cjs +11 -6
- package/build/legacy/timeoutManager.cjs.map +1 -1
- package/build/legacy/timeoutManager.d.cts +23 -6
- package/build/legacy/timeoutManager.d.cts.map +1 -1
- package/build/legacy/timeoutManager.d.ts +23 -6
- package/build/legacy/timeoutManager.d.ts.map +1 -1
- package/build/legacy/timeoutManager.js +11 -6
- package/build/legacy/timeoutManager.js.map +1 -1
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +1 -1
- package/build/legacy/types.d.ts +1 -1
- package/build/legacy/types.js.map +1 -1
- package/build/legacy/utils.cjs +117 -8
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts +1 -1
- package/build/legacy/utils.d.ts +1 -1
- package/build/legacy/utils.js +117 -8
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/environmentManager.cjs +3 -1
- package/build/modern/environmentManager.cjs.map +1 -1
- package/build/modern/environmentManager.d.cts +6 -1
- package/build/modern/environmentManager.d.cts.map +1 -1
- package/build/modern/environmentManager.d.ts +6 -1
- package/build/modern/environmentManager.d.ts.map +1 -1
- package/build/modern/environmentManager.js +3 -1
- package/build/modern/environmentManager.js.map +1 -1
- package/build/modern/focusManager.cjs +4 -2
- package/build/modern/focusManager.cjs.map +1 -1
- package/build/modern/focusManager.d.cts +5 -3
- package/build/modern/focusManager.d.cts.map +1 -1
- package/build/modern/focusManager.d.ts +5 -3
- package/build/modern/focusManager.d.ts.map +1 -1
- package/build/modern/focusManager.js +4 -2
- package/build/modern/focusManager.js.map +1 -1
- package/build/modern/{hydration-B9t5w6NC.d.ts → hydration-Dfuxd5FW.d.cts} +1410 -128
- package/build/modern/hydration-Dfuxd5FW.d.cts.map +1 -0
- package/build/modern/{hydration-Br9VIGXa.d.cts → hydration-UAqdtMIf.d.ts} +1410 -128
- package/build/modern/hydration-UAqdtMIf.d.ts.map +1 -0
- package/build/modern/hydration.cjs +35 -0
- package/build/modern/hydration.cjs.map +1 -1
- package/build/modern/hydration.d.cts +1 -1
- package/build/modern/hydration.d.ts +1 -1
- package/build/modern/hydration.js +35 -0
- package/build/modern/hydration.js.map +1 -1
- package/build/modern/index.d.cts +1 -1
- package/build/modern/index.d.ts +1 -1
- package/build/modern/infiniteQueryBehavior.cjs +29 -0
- package/build/modern/infiniteQueryBehavior.cjs.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.cts +16 -1
- package/build/modern/infiniteQueryBehavior.d.cts.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.ts +16 -1
- package/build/modern/infiniteQueryBehavior.d.ts.map +1 -1
- package/build/modern/infiniteQueryBehavior.js +29 -0
- package/build/modern/infiniteQueryBehavior.js.map +1 -1
- package/build/modern/infiniteQueryObserver.cjs +13 -7
- package/build/modern/infiniteQueryObserver.cjs.map +1 -1
- package/build/modern/infiniteQueryObserver.d.cts +15 -9
- package/build/modern/infiniteQueryObserver.d.cts.map +1 -1
- package/build/modern/infiniteQueryObserver.d.ts +15 -9
- package/build/modern/infiniteQueryObserver.d.ts.map +1 -1
- package/build/modern/infiniteQueryObserver.js +13 -7
- package/build/modern/infiniteQueryObserver.js.map +1 -1
- package/build/modern/mutation.cjs +27 -10
- package/build/modern/mutation.cjs.map +1 -1
- package/build/modern/mutation.d.cts +1 -1
- package/build/modern/mutation.d.ts +1 -1
- package/build/modern/mutation.js +27 -10
- package/build/modern/mutation.js.map +1 -1
- package/build/modern/mutationCache.cjs +53 -12
- package/build/modern/mutationCache.cjs.map +1 -1
- package/build/modern/mutationCache.d.cts +1 -1
- package/build/modern/mutationCache.d.ts +1 -1
- package/build/modern/mutationCache.js +53 -12
- package/build/modern/mutationCache.js.map +1 -1
- package/build/modern/mutationObserver.cjs +23 -12
- package/build/modern/mutationObserver.cjs.map +1 -1
- package/build/modern/mutationObserver.d.cts +1 -1
- package/build/modern/mutationObserver.d.ts +1 -1
- package/build/modern/mutationObserver.js +23 -12
- package/build/modern/mutationObserver.js.map +1 -1
- package/build/modern/notifyManager.cjs +12 -2
- package/build/modern/notifyManager.cjs.map +1 -1
- package/build/modern/notifyManager.d.cts +19 -4
- package/build/modern/notifyManager.d.cts.map +1 -1
- package/build/modern/notifyManager.d.ts +19 -4
- package/build/modern/notifyManager.d.ts.map +1 -1
- package/build/modern/notifyManager.js +12 -2
- package/build/modern/notifyManager.js.map +1 -1
- package/build/modern/onlineManager.cjs +4 -2
- package/build/modern/onlineManager.cjs.map +1 -1
- package/build/modern/onlineManager.d.cts +5 -3
- package/build/modern/onlineManager.d.cts.map +1 -1
- package/build/modern/onlineManager.d.ts +5 -3
- package/build/modern/onlineManager.d.ts.map +1 -1
- package/build/modern/onlineManager.js +4 -2
- package/build/modern/onlineManager.js.map +1 -1
- package/build/modern/queriesObserver.cjs +15 -3
- package/build/modern/queriesObserver.cjs.map +1 -1
- package/build/modern/queriesObserver.d.cts +14 -5
- package/build/modern/queriesObserver.d.cts.map +1 -1
- package/build/modern/queriesObserver.d.ts +14 -5
- package/build/modern/queriesObserver.d.ts.map +1 -1
- package/build/modern/queriesObserver.js +15 -3
- package/build/modern/queriesObserver.js.map +1 -1
- package/build/modern/query.cjs +89 -15
- package/build/modern/query.cjs.map +1 -1
- package/build/modern/query.d.cts +1 -1
- package/build/modern/query.d.ts +1 -1
- package/build/modern/query.js +89 -15
- package/build/modern/query.js.map +1 -1
- package/build/modern/queryCache.cjs +34 -12
- package/build/modern/queryCache.cjs.map +1 -1
- package/build/modern/queryCache.d.cts +1 -1
- package/build/modern/queryCache.d.ts +1 -1
- package/build/modern/queryCache.js +34 -12
- package/build/modern/queryCache.js.map +1 -1
- package/build/modern/queryClient.cjs +104 -32
- package/build/modern/queryClient.cjs.map +1 -1
- package/build/modern/queryClient.d.cts +1 -1
- package/build/modern/queryClient.d.ts +1 -1
- package/build/modern/queryClient.js +104 -32
- package/build/modern/queryClient.js.map +1 -1
- package/build/modern/queryObserver.cjs +78 -6
- package/build/modern/queryObserver.cjs.map +1 -1
- package/build/modern/queryObserver.d.cts +1 -1
- package/build/modern/queryObserver.d.ts +1 -1
- package/build/modern/queryObserver.js +78 -6
- package/build/modern/queryObserver.js.map +1 -1
- package/build/modern/{removable-0GzhW1PK.d.ts → removable-Cw9GNGyK.d.cts} +17 -1
- package/build/modern/removable-Cw9GNGyK.d.cts.map +1 -0
- package/build/{legacy/removable-0GzhW1PK.d.ts → modern/removable-Cw9GNGyK.d.ts} +17 -1
- package/build/modern/removable-Cw9GNGyK.d.ts.map +1 -0
- package/build/modern/removable.cjs +12 -0
- package/build/modern/removable.cjs.map +1 -1
- package/build/modern/removable.d.cts +1 -1
- package/build/modern/removable.d.ts +1 -1
- package/build/modern/removable.js +12 -0
- package/build/modern/removable.js.map +1 -1
- package/build/modern/retryer.cjs +26 -4
- package/build/modern/retryer.cjs.map +1 -1
- package/build/modern/retryer.d.cts +1 -1
- package/build/modern/retryer.d.ts +1 -1
- package/build/modern/retryer.js +26 -4
- package/build/modern/retryer.js.map +1 -1
- package/build/modern/streamedQuery.cjs +3 -1
- package/build/modern/streamedQuery.cjs.map +1 -1
- package/build/modern/streamedQuery.d.cts +4 -2
- package/build/modern/streamedQuery.d.cts.map +1 -1
- package/build/modern/streamedQuery.d.ts +4 -2
- package/build/modern/streamedQuery.d.ts.map +1 -1
- package/build/modern/streamedQuery.js +3 -1
- package/build/modern/streamedQuery.js.map +1 -1
- package/build/modern/{subscribable-CysQ0tne.d.ts → subscribable-czlHOxMP.d.cts} +11 -1
- package/build/modern/subscribable-czlHOxMP.d.cts.map +1 -0
- package/build/{legacy/subscribable-CysQ0tne.d.ts → modern/subscribable-czlHOxMP.d.ts} +11 -1
- package/build/modern/subscribable-czlHOxMP.d.ts.map +1 -0
- package/build/modern/subscribable.cjs +10 -0
- package/build/modern/subscribable.cjs.map +1 -1
- package/build/modern/subscribable.d.cts +1 -1
- package/build/modern/subscribable.d.ts +1 -1
- package/build/modern/subscribable.js +10 -0
- package/build/modern/subscribable.js.map +1 -1
- package/build/modern/timeoutManager.cjs +11 -6
- package/build/modern/timeoutManager.cjs.map +1 -1
- package/build/modern/timeoutManager.d.cts +23 -6
- package/build/modern/timeoutManager.d.cts.map +1 -1
- package/build/modern/timeoutManager.d.ts +23 -6
- package/build/modern/timeoutManager.d.ts.map +1 -1
- package/build/modern/timeoutManager.js +11 -6
- package/build/modern/timeoutManager.js.map +1 -1
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +1 -1
- package/build/modern/types.d.ts +1 -1
- package/build/modern/types.js.map +1 -1
- package/build/modern/utils.cjs +117 -8
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts +1 -1
- package/build/modern/utils.d.ts +1 -1
- package/build/modern/utils.js +117 -8
- package/build/modern/utils.js.map +1 -1
- package/package.json +4 -4
- package/src/environmentManager.ts +6 -1
- package/src/focusManager.ts +8 -6
- package/src/hydration.ts +42 -1
- package/src/infiniteQueryBehavior.ts +29 -0
- package/src/infiniteQueryObserver.ts +20 -14
- package/src/mutation.ts +32 -11
- package/src/mutationCache.ts +53 -12
- package/src/mutationObserver.ts +29 -14
- package/src/notifyManager.ts +12 -2
- package/src/onlineManager.ts +7 -5
- package/src/queriesObserver.ts +20 -5
- package/src/query.ts +153 -20
- package/src/queryCache.ts +54 -13
- package/src/queryClient.ts +110 -38
- package/src/queryObserver.ts +88 -12
- package/src/removable.ts +16 -0
- package/src/retryer.ts +57 -4
- package/src/streamedQuery.ts +4 -3
- package/src/subscribable.ts +10 -0
- package/src/timeoutManager.ts +23 -6
- package/src/types.ts +801 -27
- package/src/utils.ts +130 -10
- package/build/legacy/hydration-B9t5w6NC.d.ts.map +0 -1
- package/build/legacy/hydration-Br9VIGXa.d.cts.map +0 -1
- package/build/legacy/removable-0GzhW1PK.d.cts.map +0 -1
- package/build/legacy/removable-0GzhW1PK.d.ts.map +0 -1
- package/build/legacy/subscribable-CysQ0tne.d.cts.map +0 -1
- package/build/legacy/subscribable-CysQ0tne.d.ts.map +0 -1
- package/build/modern/hydration-B9t5w6NC.d.ts.map +0 -1
- package/build/modern/hydration-Br9VIGXa.d.cts.map +0 -1
- package/build/modern/removable-0GzhW1PK.d.cts.map +0 -1
- package/build/modern/removable-0GzhW1PK.d.ts.map +0 -1
- package/build/modern/subscribable-CysQ0tne.d.cts.map +0 -1
- package/build/modern/subscribable-CysQ0tne.d.ts.map +0 -1
|
@@ -12,11 +12,27 @@ declare abstract class Removable {
|
|
|
12
12
|
* in-flight fetch.
|
|
13
13
|
*/
|
|
14
14
|
destroy(): void;
|
|
15
|
+
/**
|
|
16
|
+
* Schedules the entry to be removed from its cache after `gcTime`, replacing any timeout scheduled
|
|
17
|
+
* before. Nothing is scheduled if `gcTime` is `Infinity`.
|
|
18
|
+
*/
|
|
15
19
|
protected scheduleGc(): void;
|
|
20
|
+
/**
|
|
21
|
+
* Updates `gcTime`, keeping the longest one seen so far. Without a value, it defaults to 5 minutes
|
|
22
|
+
* on the client and `Infinity` on the server.
|
|
23
|
+
* @param newGcTime - The `gcTime` from the latest options, if any.
|
|
24
|
+
*/
|
|
16
25
|
protected updateGcTime(newGcTime: number | undefined): void;
|
|
26
|
+
/**
|
|
27
|
+
* Cancels the scheduled removal, if there is one.
|
|
28
|
+
*/
|
|
17
29
|
protected clearGcTimeout(): void;
|
|
30
|
+
/**
|
|
31
|
+
* Called when the garbage collection timeout fires. Subclasses remove the entry from its cache
|
|
32
|
+
* unless it is still in use, e.g. because it has observers.
|
|
33
|
+
*/
|
|
18
34
|
protected abstract optionalRemove(): void;
|
|
19
35
|
}
|
|
20
36
|
//#endregion
|
|
21
37
|
export { Removable as t };
|
|
22
|
-
//# sourceMappingURL=removable-
|
|
38
|
+
//# sourceMappingURL=removable-Cw9GNGyK.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"removable-Cw9GNGyK.d.cts","names":[],"sources":["../../src/removable.ts"],"mappings":";;;;;uBASsB;;EACpB;;;;;;EAQA;;;;;YAQU;;;;;;YAeA,aAAa;;;;YAWb;;;;;qBAWS"}
|
|
@@ -12,11 +12,27 @@ declare abstract class Removable {
|
|
|
12
12
|
* in-flight fetch.
|
|
13
13
|
*/
|
|
14
14
|
destroy(): void;
|
|
15
|
+
/**
|
|
16
|
+
* Schedules the entry to be removed from its cache after `gcTime`, replacing any timeout scheduled
|
|
17
|
+
* before. Nothing is scheduled if `gcTime` is `Infinity`.
|
|
18
|
+
*/
|
|
15
19
|
protected scheduleGc(): void;
|
|
20
|
+
/**
|
|
21
|
+
* Updates `gcTime`, keeping the longest one seen so far. Without a value, it defaults to 5 minutes
|
|
22
|
+
* on the client and `Infinity` on the server.
|
|
23
|
+
* @param newGcTime - The `gcTime` from the latest options, if any.
|
|
24
|
+
*/
|
|
16
25
|
protected updateGcTime(newGcTime: number | undefined): void;
|
|
26
|
+
/**
|
|
27
|
+
* Cancels the scheduled removal, if there is one.
|
|
28
|
+
*/
|
|
17
29
|
protected clearGcTimeout(): void;
|
|
30
|
+
/**
|
|
31
|
+
* Called when the garbage collection timeout fires. Subclasses remove the entry from its cache
|
|
32
|
+
* unless it is still in use, e.g. because it has observers.
|
|
33
|
+
*/
|
|
18
34
|
protected abstract optionalRemove(): void;
|
|
19
35
|
}
|
|
20
36
|
//#endregion
|
|
21
37
|
export { Removable as t };
|
|
22
|
-
//# sourceMappingURL=removable-
|
|
38
|
+
//# sourceMappingURL=removable-Cw9GNGyK.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"removable-Cw9GNGyK.d.ts","names":[],"sources":["../../src/removable.ts"],"mappings":";;;;;uBASsB;;EACpB;;;;;;EAQA;;;;;YAQU;;;;;;YAeA,aAAa;;;;YAWb;;;;;qBAWS"}
|
|
@@ -21,15 +21,27 @@ var Removable = class {
|
|
|
21
21
|
destroy() {
|
|
22
22
|
this.clearGcTimeout();
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Schedules the entry to be removed from its cache after `gcTime`, replacing any timeout scheduled
|
|
26
|
+
* before. Nothing is scheduled if `gcTime` is `Infinity`.
|
|
27
|
+
*/
|
|
24
28
|
scheduleGc() {
|
|
25
29
|
this.clearGcTimeout();
|
|
26
30
|
if (require_utils.isValidTimeout(this.gcTime)) require_classPrivateFieldSet2._classPrivateFieldSet2(_gcTimeout, this, require_timeoutManager.timeoutManager.setTimeout(() => {
|
|
27
31
|
this.optionalRemove();
|
|
28
32
|
}, this.gcTime));
|
|
29
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Updates `gcTime`, keeping the longest one seen so far. Without a value, it defaults to 5 minutes
|
|
36
|
+
* on the client and `Infinity` on the server.
|
|
37
|
+
* @param newGcTime - The `gcTime` from the latest options, if any.
|
|
38
|
+
*/
|
|
30
39
|
updateGcTime(newGcTime) {
|
|
31
40
|
this.gcTime = Math.max(this.gcTime || 0, newGcTime ?? (require_environmentManager.isServer() ? Infinity : 3e5));
|
|
32
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Cancels the scheduled removal, if there is one.
|
|
44
|
+
*/
|
|
33
45
|
clearGcTimeout() {
|
|
34
46
|
if (require_classPrivateFieldSet2._classPrivateFieldGet2(_gcTimeout, this) !== void 0) {
|
|
35
47
|
require_timeoutManager.timeoutManager.clearTimeout(require_classPrivateFieldSet2._classPrivateFieldGet2(_gcTimeout, this));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"removable.cjs","names":["isValidTimeout","timeoutManager","isServerEnvironment"],"sources":["../../src/removable.ts"],"sourcesContent":["import { timeoutManager } from './timeoutManager'\nimport { isServer as isServerEnvironment } from './environmentManager'\nimport { isValidTimeout } from './utils'\nimport type { ManagedTimerId } from './timeoutManager'\n\n/**\n * The base class for cache entries that are garbage collected once nothing is using them —\n * `Query` and `Mutation` both extend it. `gcTime` controls how long an unused entry is kept.\n */\nexport abstract class Removable {\n gcTime!: number\n #gcTimeout?: ManagedTimerId\n\n /**\n * Clears the pending garbage collection timeout, so the entry is no longer scheduled for removal.\n * A subclass may override this to release what it holds on to as well — `Query` also cancels any\n * in-flight fetch.\n */\n destroy(): void {\n this.clearGcTimeout()\n }\n\n protected scheduleGc(): void {\n this.clearGcTimeout()\n\n if (isValidTimeout(this.gcTime)) {\n this.#gcTimeout = timeoutManager.setTimeout(() => {\n this.optionalRemove()\n }, this.gcTime)\n }\n }\n\n protected updateGcTime(newGcTime: number | undefined): void {\n // Default to 5 minutes (Infinity for server-side) if no gcTime is set\n this.gcTime = Math.max(\n this.gcTime || 0,\n newGcTime ?? (isServerEnvironment() ? Infinity : 5 * 60 * 1000),\n )\n }\n\n protected clearGcTimeout() {\n if (this.#gcTimeout !== undefined) {\n timeoutManager.clearTimeout(this.#gcTimeout)\n this.#gcTimeout = undefined\n }\n }\n\n protected abstract optionalRemove(): void\n}\n"],"mappings":";;;;;;;;;;;AASA,IAAsB,YAAtB,MAAgC;;;;;;;;;CAS9B,UAAgB;EACd,KAAK,eAAe;CACtB
|
|
1
|
+
{"version":3,"file":"removable.cjs","names":["isValidTimeout","timeoutManager","isServerEnvironment"],"sources":["../../src/removable.ts"],"sourcesContent":["import { timeoutManager } from './timeoutManager'\nimport { isServer as isServerEnvironment } from './environmentManager'\nimport { isValidTimeout } from './utils'\nimport type { ManagedTimerId } from './timeoutManager'\n\n/**\n * The base class for cache entries that are garbage collected once nothing is using them —\n * `Query` and `Mutation` both extend it. `gcTime` controls how long an unused entry is kept.\n */\nexport abstract class Removable {\n gcTime!: number\n #gcTimeout?: ManagedTimerId\n\n /**\n * Clears the pending garbage collection timeout, so the entry is no longer scheduled for removal.\n * A subclass may override this to release what it holds on to as well — `Query` also cancels any\n * in-flight fetch.\n */\n destroy(): void {\n this.clearGcTimeout()\n }\n\n /**\n * Schedules the entry to be removed from its cache after `gcTime`, replacing any timeout scheduled\n * before. Nothing is scheduled if `gcTime` is `Infinity`.\n */\n protected scheduleGc(): void {\n this.clearGcTimeout()\n\n if (isValidTimeout(this.gcTime)) {\n this.#gcTimeout = timeoutManager.setTimeout(() => {\n this.optionalRemove()\n }, this.gcTime)\n }\n }\n\n /**\n * Updates `gcTime`, keeping the longest one seen so far. Without a value, it defaults to 5 minutes\n * on the client and `Infinity` on the server.\n * @param newGcTime - The `gcTime` from the latest options, if any.\n */\n protected updateGcTime(newGcTime: number | undefined): void {\n // Default to 5 minutes (Infinity for server-side) if no gcTime is set\n this.gcTime = Math.max(\n this.gcTime || 0,\n newGcTime ?? (isServerEnvironment() ? Infinity : 5 * 60 * 1000),\n )\n }\n\n /**\n * Cancels the scheduled removal, if there is one.\n */\n protected clearGcTimeout() {\n if (this.#gcTimeout !== undefined) {\n timeoutManager.clearTimeout(this.#gcTimeout)\n this.#gcTimeout = undefined\n }\n }\n\n /**\n * Called when the garbage collection timeout fires. Subclasses remove the entry from its cache\n * unless it is still in use, e.g. because it has observers.\n */\n protected abstract optionalRemove(): void\n}\n"],"mappings":";;;;;;;;;;;AASA,IAAsB,YAAtB,MAAgC;;;;;;;;;CAS9B,UAAgB;EACd,KAAK,eAAe;CACtB;;;;;CAMA,aAA6B;EAC3B,KAAK,eAAe;EAEpB,IAAIA,cAAAA,eAAe,KAAK,MAAM,GAC5B,8BAAA,uBAAA,YAAA,MAAkBC,uBAAAA,eAAe,iBAAiB;GAChD,KAAK,eAAe;EACtB,GAAG,KAAK,MAAM,CAAA;CAElB;;;;;;CAOA,aAAuB,WAAqC;EAE1D,KAAK,SAAS,KAAK,IACjB,KAAK,UAAU,GACf,cAAcC,2BAAAA,SAAoB,IAAI,WAAW,IACnD;CACF;;;;CAKA,iBAA2B;EACzB,IAAA,8BAAA,uBAAA,YAAI,IAAA,MAAoB,KAAA,GAAW;GACjC,uBAAA,eAAe,aAAA,8BAAA,uBAAA,YAAa,IAAA,CAAe;GAC3C,8BAAA,uBAAA,YAAA,MAAkB,KAAA,CAAA;EACpB;CACF;AAOF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as Removable } from "./removable-
|
|
1
|
+
import { t as Removable } from "./removable-Cw9GNGyK.cjs";
|
|
2
2
|
export { Removable };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as Removable } from "./removable-
|
|
1
|
+
import { t as Removable } from "./removable-Cw9GNGyK.js";
|
|
2
2
|
export { Removable };
|
|
@@ -20,15 +20,27 @@ var Removable = class {
|
|
|
20
20
|
destroy() {
|
|
21
21
|
this.clearGcTimeout();
|
|
22
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Schedules the entry to be removed from its cache after `gcTime`, replacing any timeout scheduled
|
|
25
|
+
* before. Nothing is scheduled if `gcTime` is `Infinity`.
|
|
26
|
+
*/
|
|
23
27
|
scheduleGc() {
|
|
24
28
|
this.clearGcTimeout();
|
|
25
29
|
if (isValidTimeout(this.gcTime)) _classPrivateFieldSet2(_gcTimeout, this, timeoutManager.setTimeout(() => {
|
|
26
30
|
this.optionalRemove();
|
|
27
31
|
}, this.gcTime));
|
|
28
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Updates `gcTime`, keeping the longest one seen so far. Without a value, it defaults to 5 minutes
|
|
35
|
+
* on the client and `Infinity` on the server.
|
|
36
|
+
* @param newGcTime - The `gcTime` from the latest options, if any.
|
|
37
|
+
*/
|
|
29
38
|
updateGcTime(newGcTime) {
|
|
30
39
|
this.gcTime = Math.max(this.gcTime || 0, newGcTime ?? (isServer() ? Infinity : 3e5));
|
|
31
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Cancels the scheduled removal, if there is one.
|
|
43
|
+
*/
|
|
32
44
|
clearGcTimeout() {
|
|
33
45
|
if (_classPrivateFieldGet2(_gcTimeout, this) !== void 0) {
|
|
34
46
|
timeoutManager.clearTimeout(_classPrivateFieldGet2(_gcTimeout, this));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"removable.js","names":["isServerEnvironment"],"sources":["../../src/removable.ts"],"sourcesContent":["import { timeoutManager } from './timeoutManager'\nimport { isServer as isServerEnvironment } from './environmentManager'\nimport { isValidTimeout } from './utils'\nimport type { ManagedTimerId } from './timeoutManager'\n\n/**\n * The base class for cache entries that are garbage collected once nothing is using them —\n * `Query` and `Mutation` both extend it. `gcTime` controls how long an unused entry is kept.\n */\nexport abstract class Removable {\n gcTime!: number\n #gcTimeout?: ManagedTimerId\n\n /**\n * Clears the pending garbage collection timeout, so the entry is no longer scheduled for removal.\n * A subclass may override this to release what it holds on to as well — `Query` also cancels any\n * in-flight fetch.\n */\n destroy(): void {\n this.clearGcTimeout()\n }\n\n protected scheduleGc(): void {\n this.clearGcTimeout()\n\n if (isValidTimeout(this.gcTime)) {\n this.#gcTimeout = timeoutManager.setTimeout(() => {\n this.optionalRemove()\n }, this.gcTime)\n }\n }\n\n protected updateGcTime(newGcTime: number | undefined): void {\n // Default to 5 minutes (Infinity for server-side) if no gcTime is set\n this.gcTime = Math.max(\n this.gcTime || 0,\n newGcTime ?? (isServerEnvironment() ? Infinity : 5 * 60 * 1000),\n )\n }\n\n protected clearGcTimeout() {\n if (this.#gcTimeout !== undefined) {\n timeoutManager.clearTimeout(this.#gcTimeout)\n this.#gcTimeout = undefined\n }\n }\n\n protected abstract optionalRemove(): void\n}\n"],"mappings":";;;;;;;;;;AASA,IAAsB,YAAtB,MAAgC;;;;;;;;;CAS9B,UAAgB;EACd,KAAK,eAAe;CACtB
|
|
1
|
+
{"version":3,"file":"removable.js","names":["isServerEnvironment"],"sources":["../../src/removable.ts"],"sourcesContent":["import { timeoutManager } from './timeoutManager'\nimport { isServer as isServerEnvironment } from './environmentManager'\nimport { isValidTimeout } from './utils'\nimport type { ManagedTimerId } from './timeoutManager'\n\n/**\n * The base class for cache entries that are garbage collected once nothing is using them —\n * `Query` and `Mutation` both extend it. `gcTime` controls how long an unused entry is kept.\n */\nexport abstract class Removable {\n gcTime!: number\n #gcTimeout?: ManagedTimerId\n\n /**\n * Clears the pending garbage collection timeout, so the entry is no longer scheduled for removal.\n * A subclass may override this to release what it holds on to as well — `Query` also cancels any\n * in-flight fetch.\n */\n destroy(): void {\n this.clearGcTimeout()\n }\n\n /**\n * Schedules the entry to be removed from its cache after `gcTime`, replacing any timeout scheduled\n * before. Nothing is scheduled if `gcTime` is `Infinity`.\n */\n protected scheduleGc(): void {\n this.clearGcTimeout()\n\n if (isValidTimeout(this.gcTime)) {\n this.#gcTimeout = timeoutManager.setTimeout(() => {\n this.optionalRemove()\n }, this.gcTime)\n }\n }\n\n /**\n * Updates `gcTime`, keeping the longest one seen so far. Without a value, it defaults to 5 minutes\n * on the client and `Infinity` on the server.\n * @param newGcTime - The `gcTime` from the latest options, if any.\n */\n protected updateGcTime(newGcTime: number | undefined): void {\n // Default to 5 minutes (Infinity for server-side) if no gcTime is set\n this.gcTime = Math.max(\n this.gcTime || 0,\n newGcTime ?? (isServerEnvironment() ? Infinity : 5 * 60 * 1000),\n )\n }\n\n /**\n * Cancels the scheduled removal, if there is one.\n */\n protected clearGcTimeout() {\n if (this.#gcTimeout !== undefined) {\n timeoutManager.clearTimeout(this.#gcTimeout)\n this.#gcTimeout = undefined\n }\n }\n\n /**\n * Called when the garbage collection timeout fires. Subclasses remove the entry from its cache\n * unless it is still in use, e.g. because it has observers.\n */\n protected abstract optionalRemove(): void\n}\n"],"mappings":";;;;;;;;;;AASA,IAAsB,YAAtB,MAAgC;;;;;;;;;CAS9B,UAAgB;EACd,KAAK,eAAe;CACtB;;;;;CAMA,aAA6B;EAC3B,KAAK,eAAe;EAEpB,IAAI,eAAe,KAAK,MAAM,GAC5B,uBAAA,YAAA,MAAkB,eAAe,iBAAiB;GAChD,KAAK,eAAe;EACtB,GAAG,KAAK,MAAM,CAAA;CAElB;;;;;;CAOA,aAAuB,WAAqC;EAE1D,KAAK,SAAS,KAAK,IACjB,KAAK,UAAU,GACf,cAAcA,SAAoB,IAAI,WAAW,IACnD;CACF;;;;CAKA,iBAA2B;EACzB,IAAA,uBAAA,YAAI,IAAA,MAAoB,KAAA,GAAW;GACjC,eAAe,aAAA,uBAAA,YAAa,IAAA,CAAe;GAC3C,uBAAA,YAAA,MAAkB,KAAA,CAAA;EACpB;CACF;AAOF"}
|
package/build/legacy/retryer.cjs
CHANGED
|
@@ -4,9 +4,19 @@ const require_environmentManager = require("./environmentManager.cjs");
|
|
|
4
4
|
const require_focusManager = require("./focusManager.cjs");
|
|
5
5
|
const require_onlineManager = require("./onlineManager.cjs");
|
|
6
6
|
//#region src/retryer.ts
|
|
7
|
+
/**
|
|
8
|
+
* The default `retryDelay`: doubles with every failure, starting at 1 second, up to 30 seconds.
|
|
9
|
+
* @param failureCount - The number of failures so far.
|
|
10
|
+
* @returns The delay before the next retry, in milliseconds.
|
|
11
|
+
*/
|
|
7
12
|
function defaultRetryDelay(failureCount) {
|
|
8
13
|
return Math.min(1e3 * 2 ** failureCount, 3e4);
|
|
9
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Checks whether a fetch can run for the given network mode.
|
|
17
|
+
* @param networkMode - The `networkMode` option. Defaults to `'online'`.
|
|
18
|
+
* @returns `true` if the network mode is not `'online'`, or if it is and the client is online.
|
|
19
|
+
*/
|
|
10
20
|
function canFetch(networkMode) {
|
|
11
21
|
return (networkMode ?? "online") === "online" ? require_onlineManager.onlineManager.isOnline() : true;
|
|
12
22
|
}
|
|
@@ -36,11 +46,23 @@ var CancelledError = class extends Error {
|
|
|
36
46
|
}
|
|
37
47
|
};
|
|
38
48
|
/**
|
|
49
|
+
* Checks whether a value is a `CancelledError`.
|
|
50
|
+
* @param value - The value to check.
|
|
51
|
+
* @returns `true` if `value` is a `CancelledError`.
|
|
39
52
|
* @deprecated Use instanceof `CancelledError` instead.
|
|
40
53
|
*/
|
|
41
54
|
function isCancelledError(value) {
|
|
42
55
|
return value instanceof CancelledError;
|
|
43
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Creates a retryer, which runs a function and retries it on failure according to the `retry` and
|
|
59
|
+
* `retryDelay` options. It pauses while the network mode doesn't allow fetching or `canRun` returns
|
|
60
|
+
* `false`, and can be cancelled. Queries and mutations use it to run their `queryFn` and
|
|
61
|
+
* `mutationFn`.
|
|
62
|
+
* @param config - The function to run, the retry options, and the callbacks for failures, pausing,
|
|
63
|
+
* continuing, and cancellation.
|
|
64
|
+
* @returns The retryer, whose `promise` settles with the result of the function.
|
|
65
|
+
*/
|
|
44
66
|
function createRetryer(config) {
|
|
45
67
|
let isRetryCancelled = false;
|
|
46
68
|
let failureCount = 0;
|
|
@@ -84,11 +106,11 @@ function createRetryer(config) {
|
|
|
84
106
|
promiseReject(value);
|
|
85
107
|
}
|
|
86
108
|
};
|
|
87
|
-
const pause = () => {
|
|
109
|
+
const pause = (canResume) => {
|
|
88
110
|
return new Promise((continueResolve) => {
|
|
89
111
|
var _config$onPause;
|
|
90
112
|
continueFn = (value) => {
|
|
91
|
-
if (isResolved() ||
|
|
113
|
+
if (isResolved() || canResume()) continueResolve(value);
|
|
92
114
|
};
|
|
93
115
|
(_config$onPause = config.onPause) === null || _config$onPause === void 0 || _config$onPause.call(config);
|
|
94
116
|
}).then(() => {
|
|
@@ -122,7 +144,7 @@ function createRetryer(config) {
|
|
|
122
144
|
failureCount++;
|
|
123
145
|
(_config$onFail = config.onFail) === null || _config$onFail === void 0 || _config$onFail.call(config, failureCount, error);
|
|
124
146
|
require_utils.sleep(delay).then(() => {
|
|
125
|
-
return canContinue() ? void 0 : pause();
|
|
147
|
+
return canContinue() ? void 0 : pause(canContinue);
|
|
126
148
|
}).then(() => {
|
|
127
149
|
if (isRetryCancelled) reject(error);
|
|
128
150
|
else run();
|
|
@@ -142,7 +164,7 @@ function createRetryer(config) {
|
|
|
142
164
|
canStart,
|
|
143
165
|
start: () => {
|
|
144
166
|
if (canStart()) run();
|
|
145
|
-
else pause().then(run);
|
|
167
|
+
else pause(canStart).then(run);
|
|
146
168
|
return promise;
|
|
147
169
|
}
|
|
148
170
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retryer.cjs","names":["onlineManager","noop","focusManager","isServerEnvironment"],"sources":["../../src/retryer.ts"],"sourcesContent":["import { focusManager } from './focusManager'\nimport { onlineManager } from './onlineManager'\nimport { isServer as isServerEnvironment } from './environmentManager'\nimport { noop, sleep } from './utils'\nimport type { CancelOptions, DefaultError, NetworkMode } from './types'\n\n// TYPES\n\ninterface RetryerConfig<TData = unknown, TError = DefaultError> {\n fn: () => TData | Promise<TData>\n initialPromise?: Promise<TData>\n onCancel?: (error: TError) => void\n onFail?: (failureCount: number, error: TError) => void\n onPause?: () => void\n onContinue?: () => void\n retry?: RetryValue<TError>\n retryDelay?: RetryDelayValue<TError>\n networkMode: NetworkMode | undefined\n canRun: () => boolean\n}\n\nexport interface Retryer<TData = unknown> {\n promise: Promise<TData>\n cancel: (cancelOptions?: CancelOptions) => void\n continue: () => Promise<unknown>\n cancelRetry: () => void\n continueRetry: () => void\n canStart: () => boolean\n start: () => Promise<TData>\n status: () => 'pending' | 'resolved' | 'rejected'\n}\n\ntype RetryerStatus = 'pending' | 'resolved' | 'rejected'\n\n/** @inline */\nexport type RetryValue<TError> = boolean | number | ShouldRetryFunction<TError>\n\n/** @inline */\ntype ShouldRetryFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => boolean\n\n/** @inline */\nexport type RetryDelayValue<TError> = number | RetryDelayFunction<TError>\n\n/** @inline */\ntype RetryDelayFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => number\n\nfunction defaultRetryDelay(failureCount: number) {\n return Math.min(1000 * 2 ** failureCount, 30000)\n}\n\nexport function canFetch(networkMode: NetworkMode | undefined): boolean {\n return (networkMode ?? 'online') === 'online'\n ? onlineManager.isOnline()\n : true\n}\n\n/**\n * The error thrown by a `Retryer` (and surfaced to `query.promise`/`mutation`) when a fetch is cancelled, e.g. via\n * `query.cancel()`. `revert`, if `true`, tells the caller to restore the state the query was in before the fetch\n * started instead of surfacing the error. `silent`, if `true`, tells the caller to suppress this error and instead\n * resolve with the promise of the fetch that triggered the cancellation.\n * @example\n * ```ts\n * query.cancel()\n *\n * try {\n * await query.promise\n * } catch (error) {\n * if (error instanceof CancelledError) {\n * // the fetch was cancelled, e.g. via `query.cancel()`\n * }\n * }\n * ```\n */\nexport class CancelledError extends Error {\n revert?: boolean\n silent?: boolean\n constructor(options?: CancelOptions) {\n super('CancelledError')\n this.revert = options?.revert\n this.silent = options?.silent\n }\n}\n\n/**\n * @deprecated Use instanceof `CancelledError` instead.\n */\nexport function isCancelledError(value: any): value is CancelledError {\n return value instanceof CancelledError\n}\n\nexport function createRetryer<TData = unknown, TError = DefaultError>(\n config: RetryerConfig<TData, TError>,\n): Retryer<TData> {\n let isRetryCancelled = false\n let failureCount = 0\n let continueFn: ((value?: unknown) => void) | undefined\n let status: RetryerStatus = 'pending'\n let promiseResolve: (data: TData) => void\n let promiseReject: (error: TError) => void\n\n const promise = new Promise<TData>((resolve, reject) => {\n promiseResolve = resolve\n promiseReject = reject\n })\n promise.catch(noop)\n\n const isResolved = () => status !== 'pending'\n\n const cancel = (cancelOptions?: CancelOptions): void => {\n if (!isResolved()) {\n const error = new CancelledError(cancelOptions) as TError\n reject(error)\n\n config.onCancel?.(error)\n }\n }\n const cancelRetry = () => {\n isRetryCancelled = true\n }\n\n const continueRetry = () => {\n isRetryCancelled = false\n }\n\n const canContinue = () =>\n focusManager.isFocused() &&\n (config.networkMode === 'always' || onlineManager.isOnline()) &&\n config.canRun()\n\n const canStart = () => canFetch(config.networkMode) && config.canRun()\n\n const resolve = (value: any) => {\n if (!isResolved()) {\n continueFn?.()\n status = 'resolved'\n promiseResolve(value)\n }\n }\n\n const reject = (value: any) => {\n if (!isResolved()) {\n continueFn?.()\n status = 'rejected'\n promiseReject(value)\n }\n }\n\n const pause = () => {\n return new Promise((continueResolve) => {\n continueFn = (value) => {\n if (isResolved() || canContinue()) {\n continueResolve(value)\n }\n }\n config.onPause?.()\n }).then(() => {\n continueFn = undefined\n if (!isResolved()) {\n config.onContinue?.()\n }\n })\n }\n\n // Create loop function\n const run = () => {\n // Do nothing if already resolved\n if (isResolved()) {\n return\n }\n\n let promiseOrValue: any\n\n // we can re-use config.initialPromise on the first call of run()\n const initialPromise =\n failureCount === 0 ? config.initialPromise : undefined\n\n // Execute query\n try {\n promiseOrValue = initialPromise ?? config.fn()\n } catch (error) {\n promiseOrValue = Promise.reject(error)\n }\n\n Promise.resolve(promiseOrValue)\n .then(resolve)\n .catch((error) => {\n // Stop if the fetch is already resolved\n if (isResolved()) {\n return\n }\n\n // Do we need to retry the request?\n const retry = config.retry ?? (isServerEnvironment() ? 0 : 3)\n const retryDelay = config.retryDelay ?? defaultRetryDelay\n const delay =\n typeof retryDelay === 'function'\n ? retryDelay(failureCount, error)\n : retryDelay\n const shouldRetry =\n retry === true ||\n (typeof retry === 'number' && failureCount < retry) ||\n (typeof retry === 'function' && retry(failureCount, error))\n\n if (isRetryCancelled || !shouldRetry) {\n // We are done if the query does not need to be retried\n reject(error)\n return\n }\n\n failureCount++\n\n // Notify on fail\n config.onFail?.(failureCount, error)\n\n // Delay\n sleep(delay)\n // Pause if the document is not visible or when the device is offline\n .then(() => {\n return canContinue() ? undefined : pause()\n })\n .then(() => {\n if (isRetryCancelled) {\n reject(error)\n } else {\n run()\n }\n })\n })\n }\n\n return {\n promise,\n status: () => status,\n cancel,\n continue: () => {\n continueFn?.()\n return promise\n },\n cancelRetry,\n continueRetry,\n canStart,\n start: () => {\n // Start loop\n if (canStart()) {\n run()\n } else {\n pause().then(run)\n }\n return promise\n },\n }\n}\n"],"mappings":";;;;;;AAoDA,SAAS,kBAAkB,cAAsB;CAC/C,OAAO,KAAK,IAAI,MAAO,KAAK,cAAc,GAAK;AACjD;AAEA,SAAgB,SAAS,aAA+C;CACtE,QAAQ,eAAe,cAAc,WACjCA,sBAAAA,cAAc,SAAS,IACvB;AACN;;;;;;;;;;;;;;;;;;;AAoBA,IAAa,iBAAb,cAAoC,MAAM;CAGxC,YAAY,SAAyB;EACnC,MAAM,gBAAgB;EACtB,KAAK,SAAA,YAAA,QAAA,YAAA,KAAA,IAAA,KAAA,IAAS,QAAS;EACvB,KAAK,SAAA,YAAA,QAAA,YAAA,KAAA,IAAA,KAAA,IAAS,QAAS;CACzB;AACF;;;;AAKA,SAAgB,iBAAiB,OAAqC;CACpE,OAAO,iBAAiB;AAC1B;AAEA,SAAgB,cACd,QACgB;CAChB,IAAI,mBAAmB;CACvB,IAAI,eAAe;CACnB,IAAI;CACJ,IAAI,SAAwB;CAC5B,IAAI;CACJ,IAAI;CAEJ,MAAM,UAAU,IAAI,SAAgB,SAAS,WAAW;EACtD,iBAAiB;EACjB,gBAAgB;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;EAGJ,MAAM,iBACJ,iBAAiB,IAAI,OAAO,iBAAiB,KAAA;EAG/C,IAAI;GACF,iBAAiB,kBAAkB,OAAO,GAAG;EAC/C,SAAS,OAAO;GACd,iBAAiB,QAAQ,OAAO,KAAK;EACvC;EAEA,QAAQ,QAAQ,cAAc,CAAC,CAC5B,KAAK,OAAO,CAAC,CACb,OAAO,UAAU;;GAEhB,IAAI,WAAW,GACb;GAIF,MAAM,QAAQ,OAAO,UAAUG,2BAAAA,SAAoB,IAAI,IAAI;GAC3D,MAAM,aAAa,OAAO,cAAc;GACxC,MAAM,QACJ,OAAO,eAAe,aAClB,WAAW,cAAc,KAAK,IAC9B;GACN,MAAM,cACJ,UAAU,QACT,OAAO,UAAU,YAAY,eAAe,SAC5C,OAAO,UAAU,cAAc,MAAM,cAAc,KAAK;GAE3D,IAAI,oBAAoB,CAAC,aAAa;IAEpC,OAAO,KAAK;IACZ;GACF;GAEA;GAGA,CAAA,iBAAA,OAAO,YAAA,QAAA,mBAAA,KAAA,KAAA,eAAA,KAAA,QAAS,cAAc,KAAK;GAGnC,cAAA,MAAM,KAAK,CAAC,CAET,WAAW;IACV,OAAO,YAAY,IAAI,KAAA,IAAY,MAAM;GAC3C,CAAC,CAAC,CACD,WAAW;IACV,IAAI,kBACF,OAAO,KAAK;SAEZ,IAAI;GAER,CAAC;EACL,CAAC;CACL;CAEA,OAAO;EACL;EACA,cAAc;EACd;EACA,gBAAgB;GACd,eAAA,QAAA,eAAA,KAAA,KAAA,WAAa;GACb,OAAO;EACT;EACA;EACA;EACA;EACA,aAAa;GAEX,IAAI,SAAS,GACX,IAAI;QAEJ,MAAM,CAAC,CAAC,KAAK,GAAG;GAElB,OAAO;EACT;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"retryer.cjs","names":["onlineManager","noop","focusManager","isServerEnvironment"],"sources":["../../src/retryer.ts"],"sourcesContent":["import { focusManager } from './focusManager'\nimport { onlineManager } from './onlineManager'\nimport { isServer as isServerEnvironment } from './environmentManager'\nimport { noop, sleep } from './utils'\nimport type { CancelOptions, DefaultError, NetworkMode } from './types'\n\n// TYPES\n\ninterface RetryerConfig<TData = unknown, TError = DefaultError> {\n fn: () => TData | Promise<TData>\n initialPromise?: Promise<TData>\n onCancel?: (error: TError) => void\n onFail?: (failureCount: number, error: TError) => void\n onPause?: () => void\n onContinue?: () => void\n retry?: RetryValue<TError>\n retryDelay?: RetryDelayValue<TError>\n networkMode: NetworkMode | undefined\n canRun: () => boolean\n}\n\n/**\n * Runs a function with retries, and can pause, continue, or cancel it. Queries and mutations use\n * one for each fetch or execution.\n */\nexport interface Retryer<TData = unknown> {\n /**\n * The promise that resolves with the data, or rejects once retries are exhausted or it is\n * cancelled.\n */\n promise: Promise<TData>\n /**\n * Cancels the run, rejecting `promise` with a `CancelledError`.\n */\n cancel: (cancelOptions?: CancelOptions) => void\n /**\n * Resumes a paused run, and returns `promise`.\n */\n continue: () => Promise<unknown>\n /**\n * Stops further retries. The current attempt still finishes.\n */\n cancelRetry: () => void\n /**\n * Allows retries again after `cancelRetry`.\n */\n continueRetry: () => void\n /**\n * Returns whether the run can start now, based on the network mode and `canRun`.\n */\n canStart: () => boolean\n /**\n * Starts the run, or waits until it can start, and returns `promise`.\n */\n start: () => Promise<TData>\n /**\n * Returns whether `promise` is still pending, resolved, or rejected.\n */\n status: () => 'pending' | 'resolved' | 'rejected'\n}\n\ntype RetryerStatus = 'pending' | 'resolved' | 'rejected'\n\n/** @inline */\nexport type RetryValue<TError> = boolean | number | ShouldRetryFunction<TError>\n\n/** @inline */\ntype ShouldRetryFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => boolean\n\n/** @inline */\nexport type RetryDelayValue<TError> = number | RetryDelayFunction<TError>\n\n/** @inline */\ntype RetryDelayFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => number\n\n/**\n * The default `retryDelay`: doubles with every failure, starting at 1 second, up to 30 seconds.\n * @param failureCount - The number of failures so far.\n * @returns The delay before the next retry, in milliseconds.\n */\nfunction defaultRetryDelay(failureCount: number) {\n return Math.min(1000 * 2 ** failureCount, 30000)\n}\n\n/**\n * Checks whether a fetch can run for the given network mode.\n * @param networkMode - The `networkMode` option. Defaults to `'online'`.\n * @returns `true` if the network mode is not `'online'`, or if it is and the client is online.\n */\nexport function canFetch(networkMode: NetworkMode | undefined): boolean {\n return (networkMode ?? 'online') === 'online'\n ? onlineManager.isOnline()\n : true\n}\n\n/**\n * The error thrown by a `Retryer` (and surfaced to `query.promise`/`mutation`) when a fetch is cancelled, e.g. via\n * `query.cancel()`. `revert`, if `true`, tells the caller to restore the state the query was in before the fetch\n * started instead of surfacing the error. `silent`, if `true`, tells the caller to suppress this error and instead\n * resolve with the promise of the fetch that triggered the cancellation.\n * @example\n * ```ts\n * query.cancel()\n *\n * try {\n * await query.promise\n * } catch (error) {\n * if (error instanceof CancelledError) {\n * // the fetch was cancelled, e.g. via `query.cancel()`\n * }\n * }\n * ```\n */\nexport class CancelledError extends Error {\n revert?: boolean\n silent?: boolean\n constructor(options?: CancelOptions) {\n super('CancelledError')\n this.revert = options?.revert\n this.silent = options?.silent\n }\n}\n\n/**\n * Checks whether a value is a `CancelledError`.\n * @param value - The value to check.\n * @returns `true` if `value` is a `CancelledError`.\n * @deprecated Use instanceof `CancelledError` instead.\n */\nexport function isCancelledError(value: any): value is CancelledError {\n return value instanceof CancelledError\n}\n\n/**\n * Creates a retryer, which runs a function and retries it on failure according to the `retry` and\n * `retryDelay` options. It pauses while the network mode doesn't allow fetching or `canRun` returns\n * `false`, and can be cancelled. Queries and mutations use it to run their `queryFn` and\n * `mutationFn`.\n * @param config - The function to run, the retry options, and the callbacks for failures, pausing,\n * continuing, and cancellation.\n * @returns The retryer, whose `promise` settles with the result of the function.\n */\nexport function createRetryer<TData = unknown, TError = DefaultError>(\n config: RetryerConfig<TData, TError>,\n): Retryer<TData> {\n let isRetryCancelled = false\n let failureCount = 0\n let continueFn: ((value?: unknown) => void) | undefined\n let status: RetryerStatus = 'pending'\n let promiseResolve: (data: TData) => void\n let promiseReject: (error: TError) => void\n\n const promise = new Promise<TData>((resolve, reject) => {\n promiseResolve = resolve\n promiseReject = reject\n })\n promise.catch(noop)\n\n const isResolved = () => status !== 'pending'\n\n const cancel = (cancelOptions?: CancelOptions): void => {\n if (!isResolved()) {\n const error = new CancelledError(cancelOptions) as TError\n reject(error)\n\n config.onCancel?.(error)\n }\n }\n const cancelRetry = () => {\n isRetryCancelled = true\n }\n\n const continueRetry = () => {\n isRetryCancelled = false\n }\n\n const canContinue = () =>\n focusManager.isFocused() &&\n (config.networkMode === 'always' || onlineManager.isOnline()) &&\n config.canRun()\n\n const canStart = () => canFetch(config.networkMode) && config.canRun()\n\n const resolve = (value: any) => {\n if (!isResolved()) {\n continueFn?.()\n status = 'resolved'\n promiseResolve(value)\n }\n }\n\n const reject = (value: any) => {\n if (!isResolved()) {\n continueFn?.()\n status = 'rejected'\n promiseReject(value)\n }\n }\n\n const pause = (canResume: () => boolean) => {\n return new Promise((continueResolve) => {\n continueFn = (value) => {\n if (isResolved() || canResume()) {\n continueResolve(value)\n }\n }\n config.onPause?.()\n }).then(() => {\n continueFn = undefined\n if (!isResolved()) {\n config.onContinue?.()\n }\n })\n }\n\n // Create loop function\n const run = () => {\n // Do nothing if already resolved\n if (isResolved()) {\n return\n }\n\n let promiseOrValue: any\n\n // we can re-use config.initialPromise on the first call of run()\n const initialPromise =\n failureCount === 0 ? config.initialPromise : undefined\n\n // Execute query\n try {\n promiseOrValue = initialPromise ?? config.fn()\n } catch (error) {\n promiseOrValue = Promise.reject(error)\n }\n\n Promise.resolve(promiseOrValue)\n .then(resolve)\n .catch((error) => {\n // Stop if the fetch is already resolved\n if (isResolved()) {\n return\n }\n\n // Do we need to retry the request?\n const retry = config.retry ?? (isServerEnvironment() ? 0 : 3)\n const retryDelay = config.retryDelay ?? defaultRetryDelay\n const delay =\n typeof retryDelay === 'function'\n ? retryDelay(failureCount, error)\n : retryDelay\n const shouldRetry =\n retry === true ||\n (typeof retry === 'number' && failureCount < retry) ||\n (typeof retry === 'function' && retry(failureCount, error))\n\n if (isRetryCancelled || !shouldRetry) {\n // We are done if the query does not need to be retried\n reject(error)\n return\n }\n\n failureCount++\n\n // Notify on fail\n config.onFail?.(failureCount, error)\n\n // Delay\n sleep(delay)\n // Pause if the document is not visible or when the device is offline\n .then(() => {\n return canContinue() ? undefined : pause(canContinue)\n })\n .then(() => {\n if (isRetryCancelled) {\n reject(error)\n } else {\n run()\n }\n })\n })\n }\n\n return {\n promise,\n status: () => status,\n cancel,\n continue: () => {\n continueFn?.()\n return promise\n },\n cancelRetry,\n continueRetry,\n canStart,\n start: () => {\n // Start loop\n if (canStart()) {\n run()\n } else {\n // Waiting to start (e.g. queued behind a scope) does not depend on focus,\n // only on the same conditions that allow starting in the first place\n pause(canStart).then(run)\n }\n return promise\n },\n }\n}\n"],"mappings":";;;;;;;;;;;AAsFA,SAAS,kBAAkB,cAAsB;CAC/C,OAAO,KAAK,IAAI,MAAO,KAAK,cAAc,GAAK;AACjD;;;;;;AAOA,SAAgB,SAAS,aAA+C;CACtE,QAAQ,eAAe,cAAc,WACjCA,sBAAAA,cAAc,SAAS,IACvB;AACN;;;;;;;;;;;;;;;;;;;AAoBA,IAAa,iBAAb,cAAoC,MAAM;CAGxC,YAAY,SAAyB;EACnC,MAAM,gBAAgB;EACtB,KAAK,SAAA,YAAA,QAAA,YAAA,KAAA,IAAA,KAAA,IAAS,QAAS;EACvB,KAAK,SAAA,YAAA,QAAA,YAAA,KAAA,IAAA,KAAA,IAAS,QAAS;CACzB;AACF;;;;;;;AAQA,SAAgB,iBAAiB,OAAqC;CACpE,OAAO,iBAAiB;AAC1B;;;;;;;;;;AAWA,SAAgB,cACd,QACgB;CAChB,IAAI,mBAAmB;CACvB,IAAI,eAAe;CACnB,IAAI;CACJ,IAAI,SAAwB;CAC5B,IAAI;CACJ,IAAI;CAEJ,MAAM,UAAU,IAAI,SAAgB,SAAS,WAAW;EACtD,iBAAi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AI,WAAW,GACb;EAGF,IAAI;EAGJ,MAAM,iBACJ,iBAAiB,IAAI,OAAO,iBAAiB,KAAA;EAG/C,IAAI;GACF,iBAAiB,kBAAkB,OAAO,GAAG;EAC/C,SAAS,OAAO;GACd,iBAAiB,QAAQ,OAAO,KAAK;EACvC;EAEA,QAAQ,QAAQ,cAAc,CAAC,CAC5B,KAAK,OAAO,CAAC,CACb,OAAO,UAAU;;GAEhB,IAAI,WAAW,GACb;GAIF,MAAM,QAAQ,OAAO,UAAUG,2BAAAA,SAAoB,IAAI,IAAI;GAC3D,MAAM,aAAa,OAAO,cAAc;GACxC,MAAM,QACJ,OAAO,eAAe,aAClB,WAAW,cAAc,KAAK,IAC9B;GACN,MAAM,cACJ,UAAU,QACT,OAAO,UAAU,YAAY,eAAe,SAC5C,OAAO,UAAU,cAAc,MAAM,cAAc,KAAK;GAE3D,IAAI,oBAAoB,CAAC,aAAa;IAEpC,OAAO,KAAK;IACZ;GACF;GAEA;GAGA,CAAA,iBAAA,OAAO,YAAA,QAAA,mBAAA,KAAA,KAAA,eAAA,KAAA,QAAS,cAAc,KAAK;GAGnC,cAAA,MAAM,KAAK,CAAC,CAET,WAAW;IACV,OAAO,YAAY,IAAI,KAAA,IAAY,MAAM,WAAW;GACtD,CAAC,CAAC,CACD,WAAW;IACV,IAAI,kBACF,OAAO,KAAK;SAEZ,IAAI;GAER,CAAC;EACL,CAAC;CACL;CAEA,OAAO;EACL;EACA,cAAc;EACd;EACA,gBAAgB;GACd,eAAA,QAAA,eAAA,KAAA,KAAA,WAAa;GACb,OAAO;EACT;EACA;EACA;EACA;EACA,aAAa;GAEX,IAAI,SAAS,GACX,IAAI;QAIJ,MAAM,QAAQ,CAAC,CAAC,KAAK,GAAG;GAE1B,OAAO;EACT;CACF;AACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { $t as CancelledError, an as isCancelledError, en as RetryDelayValue, in as createRetryer, nn as Retryer, rn as canFetch, tn as RetryValue } from "./hydration-
|
|
1
|
+
import { $t as CancelledError, an as isCancelledError, en as RetryDelayValue, in as createRetryer, nn as Retryer, rn as canFetch, tn as RetryValue } from "./hydration-Dfuxd5FW.cjs";
|
|
2
2
|
export { CancelledError, RetryDelayValue, RetryValue, Retryer, canFetch, createRetryer, isCancelledError };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { $t as CancelledError, an as isCancelledError, en as RetryDelayValue, in as createRetryer, nn as Retryer, rn as canFetch, tn as RetryValue } from "./hydration-
|
|
1
|
+
import { $t as CancelledError, an as isCancelledError, en as RetryDelayValue, in as createRetryer, nn as Retryer, rn as canFetch, tn as RetryValue } from "./hydration-UAqdtMIf.js";
|
|
2
2
|
export { CancelledError, RetryDelayValue, RetryValue, Retryer, canFetch, createRetryer, isCancelledError };
|
package/build/legacy/retryer.js
CHANGED
|
@@ -3,9 +3,19 @@ import { isServer } from "./environmentManager.js";
|
|
|
3
3
|
import { focusManager } from "./focusManager.js";
|
|
4
4
|
import { onlineManager } from "./onlineManager.js";
|
|
5
5
|
//#region src/retryer.ts
|
|
6
|
+
/**
|
|
7
|
+
* The default `retryDelay`: doubles with every failure, starting at 1 second, up to 30 seconds.
|
|
8
|
+
* @param failureCount - The number of failures so far.
|
|
9
|
+
* @returns The delay before the next retry, in milliseconds.
|
|
10
|
+
*/
|
|
6
11
|
function defaultRetryDelay(failureCount) {
|
|
7
12
|
return Math.min(1e3 * 2 ** failureCount, 3e4);
|
|
8
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Checks whether a fetch can run for the given network mode.
|
|
16
|
+
* @param networkMode - The `networkMode` option. Defaults to `'online'`.
|
|
17
|
+
* @returns `true` if the network mode is not `'online'`, or if it is and the client is online.
|
|
18
|
+
*/
|
|
9
19
|
function canFetch(networkMode) {
|
|
10
20
|
return (networkMode ?? "online") === "online" ? onlineManager.isOnline() : true;
|
|
11
21
|
}
|
|
@@ -35,11 +45,23 @@ var CancelledError = class extends Error {
|
|
|
35
45
|
}
|
|
36
46
|
};
|
|
37
47
|
/**
|
|
48
|
+
* Checks whether a value is a `CancelledError`.
|
|
49
|
+
* @param value - The value to check.
|
|
50
|
+
* @returns `true` if `value` is a `CancelledError`.
|
|
38
51
|
* @deprecated Use instanceof `CancelledError` instead.
|
|
39
52
|
*/
|
|
40
53
|
function isCancelledError(value) {
|
|
41
54
|
return value instanceof CancelledError;
|
|
42
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Creates a retryer, which runs a function and retries it on failure according to the `retry` and
|
|
58
|
+
* `retryDelay` options. It pauses while the network mode doesn't allow fetching or `canRun` returns
|
|
59
|
+
* `false`, and can be cancelled. Queries and mutations use it to run their `queryFn` and
|
|
60
|
+
* `mutationFn`.
|
|
61
|
+
* @param config - The function to run, the retry options, and the callbacks for failures, pausing,
|
|
62
|
+
* continuing, and cancellation.
|
|
63
|
+
* @returns The retryer, whose `promise` settles with the result of the function.
|
|
64
|
+
*/
|
|
43
65
|
function createRetryer(config) {
|
|
44
66
|
let isRetryCancelled = false;
|
|
45
67
|
let failureCount = 0;
|
|
@@ -83,11 +105,11 @@ function createRetryer(config) {
|
|
|
83
105
|
promiseReject(value);
|
|
84
106
|
}
|
|
85
107
|
};
|
|
86
|
-
const pause = () => {
|
|
108
|
+
const pause = (canResume) => {
|
|
87
109
|
return new Promise((continueResolve) => {
|
|
88
110
|
var _config$onPause;
|
|
89
111
|
continueFn = (value) => {
|
|
90
|
-
if (isResolved() ||
|
|
112
|
+
if (isResolved() || canResume()) continueResolve(value);
|
|
91
113
|
};
|
|
92
114
|
(_config$onPause = config.onPause) === null || _config$onPause === void 0 || _config$onPause.call(config);
|
|
93
115
|
}).then(() => {
|
|
@@ -121,7 +143,7 @@ function createRetryer(config) {
|
|
|
121
143
|
failureCount++;
|
|
122
144
|
(_config$onFail = config.onFail) === null || _config$onFail === void 0 || _config$onFail.call(config, failureCount, error);
|
|
123
145
|
sleep(delay).then(() => {
|
|
124
|
-
return canContinue() ? void 0 : pause();
|
|
146
|
+
return canContinue() ? void 0 : pause(canContinue);
|
|
125
147
|
}).then(() => {
|
|
126
148
|
if (isRetryCancelled) reject(error);
|
|
127
149
|
else run();
|
|
@@ -141,7 +163,7 @@ function createRetryer(config) {
|
|
|
141
163
|
canStart,
|
|
142
164
|
start: () => {
|
|
143
165
|
if (canStart()) run();
|
|
144
|
-
else pause().then(run);
|
|
166
|
+
else pause(canStart).then(run);
|
|
145
167
|
return promise;
|
|
146
168
|
}
|
|
147
169
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retryer.js","names":["isServerEnvironment"],"sources":["../../src/retryer.ts"],"sourcesContent":["import { focusManager } from './focusManager'\nimport { onlineManager } from './onlineManager'\nimport { isServer as isServerEnvironment } from './environmentManager'\nimport { noop, sleep } from './utils'\nimport type { CancelOptions, DefaultError, NetworkMode } from './types'\n\n// TYPES\n\ninterface RetryerConfig<TData = unknown, TError = DefaultError> {\n fn: () => TData | Promise<TData>\n initialPromise?: Promise<TData>\n onCancel?: (error: TError) => void\n onFail?: (failureCount: number, error: TError) => void\n onPause?: () => void\n onContinue?: () => void\n retry?: RetryValue<TError>\n retryDelay?: RetryDelayValue<TError>\n networkMode: NetworkMode | undefined\n canRun: () => boolean\n}\n\nexport interface Retryer<TData = unknown> {\n promise: Promise<TData>\n cancel: (cancelOptions?: CancelOptions) => void\n continue: () => Promise<unknown>\n cancelRetry: () => void\n continueRetry: () => void\n canStart: () => boolean\n start: () => Promise<TData>\n status: () => 'pending' | 'resolved' | 'rejected'\n}\n\ntype RetryerStatus = 'pending' | 'resolved' | 'rejected'\n\n/** @inline */\nexport type RetryValue<TError> = boolean | number | ShouldRetryFunction<TError>\n\n/** @inline */\ntype ShouldRetryFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => boolean\n\n/** @inline */\nexport type RetryDelayValue<TError> = number | RetryDelayFunction<TError>\n\n/** @inline */\ntype RetryDelayFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => number\n\nfunction defaultRetryDelay(failureCount: number) {\n return Math.min(1000 * 2 ** failureCount, 30000)\n}\n\nexport function canFetch(networkMode: NetworkMode | undefined): boolean {\n return (networkMode ?? 'online') === 'online'\n ? onlineManager.isOnline()\n : true\n}\n\n/**\n * The error thrown by a `Retryer` (and surfaced to `query.promise`/`mutation`) when a fetch is cancelled, e.g. via\n * `query.cancel()`. `revert`, if `true`, tells the caller to restore the state the query was in before the fetch\n * started instead of surfacing the error. `silent`, if `true`, tells the caller to suppress this error and instead\n * resolve with the promise of the fetch that triggered the cancellation.\n * @example\n * ```ts\n * query.cancel()\n *\n * try {\n * await query.promise\n * } catch (error) {\n * if (error instanceof CancelledError) {\n * // the fetch was cancelled, e.g. via `query.cancel()`\n * }\n * }\n * ```\n */\nexport class CancelledError extends Error {\n revert?: boolean\n silent?: boolean\n constructor(options?: CancelOptions) {\n super('CancelledError')\n this.revert = options?.revert\n this.silent = options?.silent\n }\n}\n\n/**\n * @deprecated Use instanceof `CancelledError` instead.\n */\nexport function isCancelledError(value: any): value is CancelledError {\n return value instanceof CancelledError\n}\n\nexport function createRetryer<TData = unknown, TError = DefaultError>(\n config: RetryerConfig<TData, TError>,\n): Retryer<TData> {\n let isRetryCancelled = false\n let failureCount = 0\n let continueFn: ((value?: unknown) => void) | undefined\n let status: RetryerStatus = 'pending'\n let promiseResolve: (data: TData) => void\n let promiseReject: (error: TError) => void\n\n const promise = new Promise<TData>((resolve, reject) => {\n promiseResolve = resolve\n promiseReject = reject\n })\n promise.catch(noop)\n\n const isResolved = () => status !== 'pending'\n\n const cancel = (cancelOptions?: CancelOptions): void => {\n if (!isResolved()) {\n const error = new CancelledError(cancelOptions) as TError\n reject(error)\n\n config.onCancel?.(error)\n }\n }\n const cancelRetry = () => {\n isRetryCancelled = true\n }\n\n const continueRetry = () => {\n isRetryCancelled = false\n }\n\n const canContinue = () =>\n focusManager.isFocused() &&\n (config.networkMode === 'always' || onlineManager.isOnline()) &&\n config.canRun()\n\n const canStart = () => canFetch(config.networkMode) && config.canRun()\n\n const resolve = (value: any) => {\n if (!isResolved()) {\n continueFn?.()\n status = 'resolved'\n promiseResolve(value)\n }\n }\n\n const reject = (value: any) => {\n if (!isResolved()) {\n continueFn?.()\n status = 'rejected'\n promiseReject(value)\n }\n }\n\n const pause = () => {\n return new Promise((continueResolve) => {\n continueFn = (value) => {\n if (isResolved() || canContinue()) {\n continueResolve(value)\n }\n }\n config.onPause?.()\n }).then(() => {\n continueFn = undefined\n if (!isResolved()) {\n config.onContinue?.()\n }\n })\n }\n\n // Create loop function\n const run = () => {\n // Do nothing if already resolved\n if (isResolved()) {\n return\n }\n\n let promiseOrValue: any\n\n // we can re-use config.initialPromise on the first call of run()\n const initialPromise =\n failureCount === 0 ? config.initialPromise : undefined\n\n // Execute query\n try {\n promiseOrValue = initialPromise ?? config.fn()\n } catch (error) {\n promiseOrValue = Promise.reject(error)\n }\n\n Promise.resolve(promiseOrValue)\n .then(resolve)\n .catch((error) => {\n // Stop if the fetch is already resolved\n if (isResolved()) {\n return\n }\n\n // Do we need to retry the request?\n const retry = config.retry ?? (isServerEnvironment() ? 0 : 3)\n const retryDelay = config.retryDelay ?? defaultRetryDelay\n const delay =\n typeof retryDelay === 'function'\n ? retryDelay(failureCount, error)\n : retryDelay\n const shouldRetry =\n retry === true ||\n (typeof retry === 'number' && failureCount < retry) ||\n (typeof retry === 'function' && retry(failureCount, error))\n\n if (isRetryCancelled || !shouldRetry) {\n // We are done if the query does not need to be retried\n reject(error)\n return\n }\n\n failureCount++\n\n // Notify on fail\n config.onFail?.(failureCount, error)\n\n // Delay\n sleep(delay)\n // Pause if the document is not visible or when the device is offline\n .then(() => {\n return canContinue() ? undefined : pause()\n })\n .then(() => {\n if (isRetryCancelled) {\n reject(error)\n } else {\n run()\n }\n })\n })\n }\n\n return {\n promise,\n status: () => status,\n cancel,\n continue: () => {\n continueFn?.()\n return promise\n },\n cancelRetry,\n continueRetry,\n canStart,\n start: () => {\n // Start loop\n if (canStart()) {\n run()\n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AK;IACZ;GACF;GAEA;GAGA,CAAA,iBAAA,OAAO,YAAA,QAAA,mBAAA,KAAA,KAAA,eAAA,KAAA,QAAS,cAAc,KAAK;GAGnC,MAAM,KAAK,CAAC,CAET,WAAW;IACV,OAAO,YAAY,IAAI,KAAA,IAAY,MAAM;GAC3C,CAAC,CAAC,CACD,WAAW;IACV,IAAI,kBACF,OAAO,KAAK;SAEZ,IAAI;GAER,CAAC;EACL,CAAC;CACL;CAEA,OAAO;EACL;EACA,cAAc;EACd;EACA,gBAAgB;GACd,eAAA,QAAA,eAAA,KAAA,KAAA,WAAa;GACb,OAAO;EACT;EACA;EACA;EACA;EACA,aAAa;GAEX,IAAI,SAAS,GACX,IAAI;QAEJ,MAAM,CAAC,CAAC,KAAK,GAAG;GAElB,OAAO;EACT;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"retryer.js","names":["isServerEnvironment"],"sources":["../../src/retryer.ts"],"sourcesContent":["import { focusManager } from './focusManager'\nimport { onlineManager } from './onlineManager'\nimport { isServer as isServerEnvironment } from './environmentManager'\nimport { noop, sleep } from './utils'\nimport type { CancelOptions, DefaultError, NetworkMode } from './types'\n\n// TYPES\n\ninterface RetryerConfig<TData = unknown, TError = DefaultError> {\n fn: () => TData | Promise<TData>\n initialPromise?: Promise<TData>\n onCancel?: (error: TError) => void\n onFail?: (failureCount: number, error: TError) => void\n onPause?: () => void\n onContinue?: () => void\n retry?: RetryValue<TError>\n retryDelay?: RetryDelayValue<TError>\n networkMode: NetworkMode | undefined\n canRun: () => boolean\n}\n\n/**\n * Runs a function with retries, and can pause, continue, or cancel it. Queries and mutations use\n * one for each fetch or execution.\n */\nexport interface Retryer<TData = unknown> {\n /**\n * The promise that resolves with the data, or rejects once retries are exhausted or it is\n * cancelled.\n */\n promise: Promise<TData>\n /**\n * Cancels the run, rejecting `promise` with a `CancelledError`.\n */\n cancel: (cancelOptions?: CancelOptions) => void\n /**\n * Resumes a paused run, and returns `promise`.\n */\n continue: () => Promise<unknown>\n /**\n * Stops further retries. The current attempt still finishes.\n */\n cancelRetry: () => void\n /**\n * Allows retries again after `cancelRetry`.\n */\n continueRetry: () => void\n /**\n * Returns whether the run can start now, based on the network mode and `canRun`.\n */\n canStart: () => boolean\n /**\n * Starts the run, or waits until it can start, and returns `promise`.\n */\n start: () => Promise<TData>\n /**\n * Returns whether `promise` is still pending, resolved, or rejected.\n */\n status: () => 'pending' | 'resolved' | 'rejected'\n}\n\ntype RetryerStatus = 'pending' | 'resolved' | 'rejected'\n\n/** @inline */\nexport type RetryValue<TError> = boolean | number | ShouldRetryFunction<TError>\n\n/** @inline */\ntype ShouldRetryFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => boolean\n\n/** @inline */\nexport type RetryDelayValue<TError> = number | RetryDelayFunction<TError>\n\n/** @inline */\ntype RetryDelayFunction<TError = DefaultError> = (\n failureCount: number,\n error: TError,\n) => number\n\n/**\n * The default `retryDelay`: doubles with every failure, starting at 1 second, up to 30 seconds.\n * @param failureCount - The number of failures so far.\n * @returns The delay before the next retry, in milliseconds.\n */\nfunction defaultRetryDelay(failureCount: number) {\n return Math.min(1000 * 2 ** failureCount, 30000)\n}\n\n/**\n * Checks whether a fetch can run for the given network mode.\n * @param networkMode - The `networkMode` option. Defaults to `'online'`.\n * @returns `true` if the network mode is not `'online'`, or if it is and the client is online.\n */\nexport function canFetch(networkMode: NetworkMode | undefined): boolean {\n return (networkMode ?? 'online') === 'online'\n ? onlineManager.isOnline()\n : true\n}\n\n/**\n * The error thrown by a `Retryer` (and surfaced to `query.promise`/`mutation`) when a fetch is cancelled, e.g. via\n * `query.cancel()`. `revert`, if `true`, tells the caller to restore the state the query was in before the fetch\n * started instead of surfacing the error. `silent`, if `true`, tells the caller to suppress this error and instead\n * resolve with the promise of the fetch that triggered the cancellation.\n * @example\n * ```ts\n * query.cancel()\n *\n * try {\n * await query.promise\n * } catch (error) {\n * if (error instanceof CancelledError) {\n * // the fetch was cancelled, e.g. via `query.cancel()`\n * }\n * }\n * ```\n */\nexport class CancelledError extends Error {\n revert?: boolean\n silent?: boolean\n constructor(options?: CancelOptions) {\n super('CancelledError')\n this.revert = options?.revert\n this.silent = options?.silent\n }\n}\n\n/**\n * Checks whether a value is a `CancelledError`.\n * @param value - The value to check.\n * @returns `true` if `value` is a `CancelledError`.\n * @deprecated Use instanceof `CancelledError` instead.\n */\nexport function isCancelledError(value: any): value is CancelledError {\n return value instanceof CancelledError\n}\n\n/**\n * Creates a retryer, which runs a function and retries it on failure according to the `retry` and\n * `retryDelay` options. It pauses while the network mode doesn't allow fetching or `canRun` returns\n * `false`, and can be cancelled. Queries and mutations use it to run their `queryFn` and\n * `mutationFn`.\n * @param config - The function to run, the retry options, and the callbacks for failures, pausing,\n * continuing, and cancellation.\n * @returns The retryer, whose `promise` settles with the result of the function.\n */\nexport function createRetryer<TData = unknown, TError = DefaultError>(\n config: RetryerConfig<TData, TError>,\n): Retryer<TData> {\n let isRetryCancelled = false\n let failureCount = 0\n let continueFn: ((value?: unknown) => void) | undefined\n let status: RetryerStatus = 'pending'\n let promiseResolve: (data: TData) => void\n let promiseReject: (error: TError) => void\n\n const promise = new Promise<TData>((resolve, reject) => {\n promiseResolve = resolve\n promiseReject = reject\n })\n promise.catch(noop)\n\n const isResolved = () => status !== 'pending'\n\n const cancel = (cancelOptions?: CancelOptions): void => {\n if (!isResolved()) {\n const error = new CancelledError(cancelOptions) as TError\n reject(error)\n\n config.onCancel?.(error)\n }\n }\n const cancelRetry = () => {\n isRetryCancelled = true\n }\n\n const continueRetry = () => {\n isRetryCancelled = false\n }\n\n const canContinue = () =>\n focusManager.isFocused() &&\n (config.networkMode === 'always' || onlineManager.isOnline()) &&\n config.canRun()\n\n const canStart = () => canFetch(config.networkMode) && config.canRun()\n\n const resolve = (value: any) => {\n if (!isResolved()) {\n continueFn?.()\n status = 'resolved'\n promiseResolve(value)\n }\n }\n\n const reject = (value: any) => {\n if (!isResolved()) {\n continueFn?.()\n status = 'rejected'\n promiseReject(value)\n }\n }\n\n const pause = (canResume: () => boolean) => {\n return new Promise((continueResolve) => {\n continueFn = (value) => {\n if (isResolved() || canResume()) {\n continueResolve(value)\n }\n }\n config.onPause?.()\n }).then(() => {\n continueFn = undefined\n if (!isResolved()) {\n config.onContinue?.()\n }\n })\n }\n\n // Create loop function\n const run = () => {\n // Do nothing if already resolved\n if (isResolved()) {\n return\n }\n\n let promiseOrValue: any\n\n // we can re-use config.initialPromise on the first call of run()\n const initialPromise =\n failureCount === 0 ? config.initialPromise : undefined\n\n // Execute query\n try {\n promiseOrValue = initialPromise ?? config.fn()\n } catch (error) {\n promiseOrValue = Promise.reject(error)\n }\n\n Promise.resolve(promiseOrValue)\n .then(resolve)\n .catch((error) => {\n // Stop if the fetch is already resolved\n if (isResolved()) {\n return\n }\n\n // Do we need to retry the request?\n const retry = config.retry ?? (isServerEnvironment() ? 0 : 3)\n const retryDelay = config.retryDelay ?? defaultRetryDelay\n const delay =\n typeof retryDelay === 'function'\n ? retryDelay(failureCount, error)\n : retryDelay\n const shouldRetry =\n retry === true ||\n (typeof retry === 'number' && failureCount < retry) ||\n (typeof retry === 'function' && retry(failureCount, error))\n\n if (isRetryCancelled || !shouldRetry) {\n // We are done if the query does not need to be retried\n reject(error)\n return\n }\n\n failureCount++\n\n // Notify on fail\n config.onFail?.(failureCount, error)\n\n // Delay\n sleep(delay)\n // Pause if the document is not visible or when the device is offline\n .then(() => {\n return canContinue() ? undefined : pause(canContinue)\n })\n .then(() => {\n if (isRetryCancelled) {\n reject(error)\n } else {\n run()\n }\n })\n })\n }\n\n return {\n promise,\n status: () => status,\n cancel,\n continue: () => {\n continueFn?.()\n return promise\n },\n cancelRetry,\n continueRetry,\n canStart,\n start: () => {\n // Start loop\n if (canStart()) {\n run()\n } else {\n // Waiting to start (e.g. queued behind a scope) does not depend on focus,\n // only on the same conditions that allow starting in the first place\n pause(canStart).then(run)\n }\n return promise\n },\n }\n}\n"],"mappings":";;;;;;;;;;AAsFA,SAAS,kBAAkB,c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YAAA,QAAA,mBAAA,KAAA,KAAA,eAAA,KAAA,QAAS,cAAc,KAAK;GAGnC,MAAM,KAAK,CAAC,CAET,WAAW;IACV,OAAO,YAAY,IAAI,KAAA,IAAY,MAAM,WAAW;GACtD,CAAC,CAAC,CACD,WAAW;IACV,IAAI,kBACF,OAAO,KAAK;SAEZ,IAAI;GAER,CAAC;EACL,CAAC;CACL;CAEA,OAAO;EACL;EACA,cAAc;EACd;EACA,gBAAgB;GACd,eAAA,QAAA,eAAA,KAAA,KAAA,WAAa;GACb,OAAO;EACT;EACA;EACA;EACA;EACA,aAAa;GAEX,IAAI,SAAS,GACX,IAAI;QAIJ,MAAM,QAAQ,CAAC,CAAC,KAAK,GAAG;GAE1B,OAAO;EACT;CACF;AACF"}
|
|
@@ -6,7 +6,9 @@ const require_utils = require("./utils.cjs");
|
|
|
6
6
|
* Data will be an Array of all the chunks received.
|
|
7
7
|
* The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.
|
|
8
8
|
* The query will stay in fetchStatus 'fetching' until the stream ends.
|
|
9
|
-
* @param
|
|
9
|
+
* @param options - The `streamFn` that returns an AsyncIterable to stream data from, and the optional
|
|
10
|
+
* `refetchMode`, `reducer`, and `initialValue` options.
|
|
11
|
+
* @returns A query function to pass as `queryFn`.
|
|
10
12
|
* @example
|
|
11
13
|
* ```ts
|
|
12
14
|
* await queryClient.query({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streamedQuery.cjs","names":["addToEnd","addConsumeAwareSignal"],"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addConsumeAwareSignal, addToEnd } from './utils'\nimport type {\n OmitKeyof,\n QueryFunction,\n QueryFunctionContext,\n QueryKey,\n} from './types'\n\ntype BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {\n /** The function that returns an `AsyncIterable` to stream data from. */\n streamFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n /**\n * Defines how refetches are handled.\n * - `'reset'` (default): erases all data and puts the query back into `pending` state.\n * - `'append'`: appends new data to the existing data.\n * - `'replace'`: writes all data to the cache once the stream ends.\n */\n refetchMode?: 'append' | 'reset' | 'replace'\n}\n\ntype SimpleStreamedQueryParams<\n TQueryFnData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n reducer?: never\n initialValue?: never\n}\n\ntype ReducibleStreamedQueryParams<\n TQueryFnData,\n TData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n /**\n * Reduces streamed chunks into the final data shape. Required whenever `TData` is not an\n * array, since there is no default way to accumulate non-array chunks.\n */\n reducer: (acc: TData, chunk: TQueryFnData) => TData\n /**\n * The value used while the first chunk is being fetched, and returned if the stream yields no\n * values. Required together with a custom `reducer`.\n */\n initialValue: TData\n}\n\ntype StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> =\n | SimpleStreamedQueryParams<TQueryFnData, TQueryKey>\n | ReducibleStreamedQueryParams<TQueryFnData, TData, TQueryKey>\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param
|
|
1
|
+
{"version":3,"file":"streamedQuery.cjs","names":["addToEnd","addConsumeAwareSignal"],"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addConsumeAwareSignal, addToEnd } from './utils'\nimport type {\n OmitKeyof,\n QueryFunction,\n QueryFunctionContext,\n QueryKey,\n} from './types'\n\ntype BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {\n /** The function that returns an `AsyncIterable` to stream data from. */\n streamFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n /**\n * Defines how refetches are handled.\n * - `'reset'` (default): erases all data and puts the query back into `pending` state.\n * - `'append'`: appends new data to the existing data.\n * - `'replace'`: writes all data to the cache once the stream ends.\n */\n refetchMode?: 'append' | 'reset' | 'replace'\n}\n\ntype SimpleStreamedQueryParams<\n TQueryFnData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n reducer?: never\n initialValue?: never\n}\n\ntype ReducibleStreamedQueryParams<\n TQueryFnData,\n TData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n /**\n * Reduces streamed chunks into the final data shape. Required whenever `TData` is not an\n * array, since there is no default way to accumulate non-array chunks.\n */\n reducer: (acc: TData, chunk: TQueryFnData) => TData\n /**\n * The value used while the first chunk is being fetched, and returned if the stream yields no\n * values. Required together with a custom `reducer`.\n */\n initialValue: TData\n}\n\ntype StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> =\n | SimpleStreamedQueryParams<TQueryFnData, TQueryKey>\n | ReducibleStreamedQueryParams<TQueryFnData, TData, TQueryKey>\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param options - The `streamFn` that returns an AsyncIterable to stream data from, and the optional\n * `refetchMode`, `reducer`, and `initialValue` options.\n * @returns A query function to pass as `queryFn`.\n * @example\n * ```ts\n * await queryClient.query({\n * queryKey: ['data'],\n * queryFn: streamedQuery({\n * streamFn: fetchDataInChunks,\n * }),\n * })\n * ```\n */\nexport function streamedQuery<\n TQueryFnData = unknown,\n TData = Array<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n>({\n streamFn,\n refetchMode = 'reset',\n reducer = (items, chunk) =>\n addToEnd(items as Array<TQueryFnData>, chunk) as TData,\n initialValue = [] as TData,\n}: StreamedQueryParams<TQueryFnData, TData, TQueryKey>): QueryFunction<\n TData,\n TQueryKey\n> {\n return async (context) => {\n const query = context.client\n .getQueryCache()\n .find({ queryKey: context.queryKey, exact: true })\n const isRefetch = !!query && query.isFetched()\n if (isRefetch && refetchMode === 'reset') {\n query.setState({\n ...query.resetState,\n fetchStatus: 'fetching',\n })\n }\n\n let result = initialValue\n\n let cancelled = false as boolean\n const streamFnContext = addConsumeAwareSignal<\n OmitKeyof<typeof context, 'signal'>\n >(\n {\n client: context.client,\n meta: context.meta,\n queryKey: context.queryKey,\n pageParam: context.pageParam,\n direction: context.direction,\n },\n () => context.signal,\n () => (cancelled = true),\n )\n\n const stream = await streamFn(streamFnContext)\n\n const isReplaceRefetch = isRefetch && refetchMode === 'replace'\n\n for await (const chunk of stream) {\n if (cancelled) {\n break\n }\n\n if (isReplaceRefetch) {\n // don't append to the cache directly when replace-refetching\n result = reducer(result, chunk)\n } else {\n context.client.setQueryData<TData>(context.queryKey, (prev) =>\n reducer(prev === undefined ? initialValue : prev, chunk),\n )\n }\n }\n\n // finalize result: replace-refetching needs to write to the cache\n if (isReplaceRefetch && !cancelled) {\n context.client.setQueryData<TData>(context.queryKey, result)\n }\n\n const data = context.client.getQueryData<TData>(context.queryKey)\n return data === undefined ? initialValue : data\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAqEA,SAAgB,cAId,EACA,UACA,cAAc,SACd,WAAW,OAAO,UAChBA,cAAAA,SAAS,OAA8B,KAAK,GAC9C,eAAe,CAAC,KAIhB;CACA,OAAO,OAAO,YAAY;EACxB,MAAM,QAAQ,QAAQ,OACnB,cAAc,CAAC,CACf,KAAK;GAAE,UAAU,QAAQ;GAAU,OAAO;EAAK,CAAC;EACnD,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,UAAU;EAC7C,IAAI,aAAa,gBAAgB,SAC/B,MAAM,SAAS;GACb,GAAG,MAAM;GACT,aAAa;EACf,CAAC;EAGH,IAAI,SAAS;EAEb,IAAI,YAAY;EAehB,MAAM,SAAS,MAAM,SAdGC,cAAAA,sBAGtB;GACE,QAAQ,QAAQ;GAChB,MAAM,QAAQ;GACd,UAAU,QAAQ;GAClB,WAAW,QAAQ;GACnB,WAAW,QAAQ;EACrB,SACM,QAAQ,cACP,YAAY,IAGuB,CAAC;EAE7C,MAAM,mBAAmB,aAAa,gBAAgB;EAEtD,WAAW,MAAM,SAAS,QAAQ;GAChC,IAAI,WACF;GAGF,IAAI,kBAEF,SAAS,QAAQ,QAAQ,KAAK;QAE9B,QAAQ,OAAO,aAAoB,QAAQ,WAAW,SACpD,QAAQ,SAAS,KAAA,IAAY,eAAe,MAAM,KAAK,CACzD;EAEJ;EAGA,IAAI,oBAAoB,CAAC,WACvB,QAAQ,OAAO,aAAoB,QAAQ,UAAU,MAAM;EAG7D,MAAM,OAAO,QAAQ,OAAO,aAAoB,QAAQ,QAAQ;EAChE,OAAO,SAAS,KAAA,IAAY,eAAe;CAC7C;AACF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Ct as QueryKey, St as QueryFunctionContext, xt as QueryFunction } from "./hydration-
|
|
1
|
+
import { Ct as QueryKey, St as QueryFunctionContext, xt as QueryFunction } from "./hydration-Dfuxd5FW.cjs";
|
|
2
2
|
//#region src/streamedQuery.d.ts
|
|
3
3
|
type BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {
|
|
4
4
|
/** The function that returns an `AsyncIterable` to stream data from. */
|
|
@@ -33,7 +33,9 @@ type StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> = Simp
|
|
|
33
33
|
* Data will be an Array of all the chunks received.
|
|
34
34
|
* The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.
|
|
35
35
|
* The query will stay in fetchStatus 'fetching' until the stream ends.
|
|
36
|
-
* @param
|
|
36
|
+
* @param options - The `streamFn` that returns an AsyncIterable to stream data from, and the optional
|
|
37
|
+
* `refetchMode`, `reducer`, and `initialValue` options.
|
|
38
|
+
* @returns A query function to pass as `queryFn`.
|
|
37
39
|
* @example
|
|
38
40
|
* ```ts
|
|
39
41
|
* await queryClient.query({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streamedQuery.d.cts","names":[],"sources":["../../src/streamedQuery.ts"],"mappings":";;KAQK,wBAAwB,cAAc,kBAAkB;;EAE3D,WACE,SAAS,qBAAqB,eAC3B,cAAc,gBAAgB,QAAQ,cAAc;;;;;;;EAOzD;;KAGG,0BACH,cACA,kBAAkB,YAChB,wBAAwB,cAAc;EACxC;EACA;;KAGG,6BACH,cACA,OACA,kBAAkB,YAChB,wBAAwB,cAAc;;;;;EAKxC,UAAU,KAAK,OAAO,OAAO,iBAAiB;;;;;EAK9C,cAAc;;KAGX,oBAAoB,cAAc,OAAO,kBAAkB,YAC5D,0BAA0B,cAAc,aACxC,6BAA6B,cAAc,OAAO
|
|
1
|
+
{"version":3,"file":"streamedQuery.d.cts","names":[],"sources":["../../src/streamedQuery.ts"],"mappings":";;KAQK,wBAAwB,cAAc,kBAAkB;;EAE3D,WACE,SAAS,qBAAqB,eAC3B,cAAc,gBAAgB,QAAQ,cAAc;;;;;;;EAOzD;;KAGG,0BACH,cACA,kBAAkB,YAChB,wBAAwB,cAAc;EACxC;EACA;;KAGG,6BACH,cACA,OACA,kBAAkB,YAChB,wBAAwB,cAAc;;;;;EAKxC,UAAU,KAAK,OAAO,OAAO,iBAAiB;;;;;EAK9C,cAAc;;KAGX,oBAAoB,cAAc,OAAO,kBAAkB,YAC5D,0BAA0B,cAAc,aACxC,6BAA6B,cAAc,OAAO;;;;;;;;;;;;;;;;;;;wBAoBtC,cACd,wBACA,QAAQ,MAAM,eACd,kBAAkB,WAAW,YAE7B,UACA,aACA,SAEA,gBACC,oBAAoB,cAAc,OAAO,aAAa,cACvD,OACA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Ct as QueryKey, St as QueryFunctionContext, xt as QueryFunction } from "./hydration-
|
|
1
|
+
import { Ct as QueryKey, St as QueryFunctionContext, xt as QueryFunction } from "./hydration-UAqdtMIf.js";
|
|
2
2
|
//#region src/streamedQuery.d.ts
|
|
3
3
|
type BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {
|
|
4
4
|
/** The function that returns an `AsyncIterable` to stream data from. */
|
|
@@ -33,7 +33,9 @@ type StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> = Simp
|
|
|
33
33
|
* Data will be an Array of all the chunks received.
|
|
34
34
|
* The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.
|
|
35
35
|
* The query will stay in fetchStatus 'fetching' until the stream ends.
|
|
36
|
-
* @param
|
|
36
|
+
* @param options - The `streamFn` that returns an AsyncIterable to stream data from, and the optional
|
|
37
|
+
* `refetchMode`, `reducer`, and `initialValue` options.
|
|
38
|
+
* @returns A query function to pass as `queryFn`.
|
|
37
39
|
* @example
|
|
38
40
|
* ```ts
|
|
39
41
|
* await queryClient.query({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streamedQuery.d.ts","names":[],"sources":["../../src/streamedQuery.ts"],"mappings":";;KAQK,wBAAwB,cAAc,kBAAkB;;EAE3D,WACE,SAAS,qBAAqB,eAC3B,cAAc,gBAAgB,QAAQ,cAAc;;;;;;;EAOzD;;KAGG,0BACH,cACA,kBAAkB,YAChB,wBAAwB,cAAc;EACxC;EACA;;KAGG,6BACH,cACA,OACA,kBAAkB,YAChB,wBAAwB,cAAc;;;;;EAKxC,UAAU,KAAK,OAAO,OAAO,iBAAiB;;;;;EAK9C,cAAc;;KAGX,oBAAoB,cAAc,OAAO,kBAAkB,YAC5D,0BAA0B,cAAc,aACxC,6BAA6B,cAAc,OAAO
|
|
1
|
+
{"version":3,"file":"streamedQuery.d.ts","names":[],"sources":["../../src/streamedQuery.ts"],"mappings":";;KAQK,wBAAwB,cAAc,kBAAkB;;EAE3D,WACE,SAAS,qBAAqB,eAC3B,cAAc,gBAAgB,QAAQ,cAAc;;;;;;;EAOzD;;KAGG,0BACH,cACA,kBAAkB,YAChB,wBAAwB,cAAc;EACxC;EACA;;KAGG,6BACH,cACA,OACA,kBAAkB,YAChB,wBAAwB,cAAc;;;;;EAKxC,UAAU,KAAK,OAAO,OAAO,iBAAiB;;;;;EAK9C,cAAc;;KAGX,oBAAoB,cAAc,OAAO,kBAAkB,YAC5D,0BAA0B,cAAc,aACxC,6BAA6B,cAAc,OAAO;;;;;;;;;;;;;;;;;;;wBAoBtC,cACd,wBACA,QAAQ,MAAM,eACd,kBAAkB,WAAW,YAE7B,UACA,aACA,SAEA,gBACC,oBAAoB,cAAc,OAAO,aAAa,cACvD,OACA"}
|
|
@@ -5,7 +5,9 @@ import { addConsumeAwareSignal, addToEnd } from "./utils.js";
|
|
|
5
5
|
* Data will be an Array of all the chunks received.
|
|
6
6
|
* The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.
|
|
7
7
|
* The query will stay in fetchStatus 'fetching' until the stream ends.
|
|
8
|
-
* @param
|
|
8
|
+
* @param options - The `streamFn` that returns an AsyncIterable to stream data from, and the optional
|
|
9
|
+
* `refetchMode`, `reducer`, and `initialValue` options.
|
|
10
|
+
* @returns A query function to pass as `queryFn`.
|
|
9
11
|
* @example
|
|
10
12
|
* ```ts
|
|
11
13
|
* await queryClient.query({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streamedQuery.js","names":[],"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addConsumeAwareSignal, addToEnd } from './utils'\nimport type {\n OmitKeyof,\n QueryFunction,\n QueryFunctionContext,\n QueryKey,\n} from './types'\n\ntype BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {\n /** The function that returns an `AsyncIterable` to stream data from. */\n streamFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n /**\n * Defines how refetches are handled.\n * - `'reset'` (default): erases all data and puts the query back into `pending` state.\n * - `'append'`: appends new data to the existing data.\n * - `'replace'`: writes all data to the cache once the stream ends.\n */\n refetchMode?: 'append' | 'reset' | 'replace'\n}\n\ntype SimpleStreamedQueryParams<\n TQueryFnData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n reducer?: never\n initialValue?: never\n}\n\ntype ReducibleStreamedQueryParams<\n TQueryFnData,\n TData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n /**\n * Reduces streamed chunks into the final data shape. Required whenever `TData` is not an\n * array, since there is no default way to accumulate non-array chunks.\n */\n reducer: (acc: TData, chunk: TQueryFnData) => TData\n /**\n * The value used while the first chunk is being fetched, and returned if the stream yields no\n * values. Required together with a custom `reducer`.\n */\n initialValue: TData\n}\n\ntype StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> =\n | SimpleStreamedQueryParams<TQueryFnData, TQueryKey>\n | ReducibleStreamedQueryParams<TQueryFnData, TData, TQueryKey>\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param
|
|
1
|
+
{"version":3,"file":"streamedQuery.js","names":[],"sources":["../../src/streamedQuery.ts"],"sourcesContent":["import { addConsumeAwareSignal, addToEnd } from './utils'\nimport type {\n OmitKeyof,\n QueryFunction,\n QueryFunctionContext,\n QueryKey,\n} from './types'\n\ntype BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {\n /** The function that returns an `AsyncIterable` to stream data from. */\n streamFn: (\n context: QueryFunctionContext<TQueryKey>,\n ) => AsyncIterable<TQueryFnData> | Promise<AsyncIterable<TQueryFnData>>\n /**\n * Defines how refetches are handled.\n * - `'reset'` (default): erases all data and puts the query back into `pending` state.\n * - `'append'`: appends new data to the existing data.\n * - `'replace'`: writes all data to the cache once the stream ends.\n */\n refetchMode?: 'append' | 'reset' | 'replace'\n}\n\ntype SimpleStreamedQueryParams<\n TQueryFnData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n reducer?: never\n initialValue?: never\n}\n\ntype ReducibleStreamedQueryParams<\n TQueryFnData,\n TData,\n TQueryKey extends QueryKey,\n> = BaseStreamedQueryParams<TQueryFnData, TQueryKey> & {\n /**\n * Reduces streamed chunks into the final data shape. Required whenever `TData` is not an\n * array, since there is no default way to accumulate non-array chunks.\n */\n reducer: (acc: TData, chunk: TQueryFnData) => TData\n /**\n * The value used while the first chunk is being fetched, and returned if the stream yields no\n * values. Required together with a custom `reducer`.\n */\n initialValue: TData\n}\n\ntype StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> =\n | SimpleStreamedQueryParams<TQueryFnData, TQueryKey>\n | ReducibleStreamedQueryParams<TQueryFnData, TData, TQueryKey>\n\n/**\n * This is a helper function to create a query function that streams data from an AsyncIterable.\n * Data will be an Array of all the chunks received.\n * The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.\n * The query will stay in fetchStatus 'fetching' until the stream ends.\n * @param options - The `streamFn` that returns an AsyncIterable to stream data from, and the optional\n * `refetchMode`, `reducer`, and `initialValue` options.\n * @returns A query function to pass as `queryFn`.\n * @example\n * ```ts\n * await queryClient.query({\n * queryKey: ['data'],\n * queryFn: streamedQuery({\n * streamFn: fetchDataInChunks,\n * }),\n * })\n * ```\n */\nexport function streamedQuery<\n TQueryFnData = unknown,\n TData = Array<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n>({\n streamFn,\n refetchMode = 'reset',\n reducer = (items, chunk) =>\n addToEnd(items as Array<TQueryFnData>, chunk) as TData,\n initialValue = [] as TData,\n}: StreamedQueryParams<TQueryFnData, TData, TQueryKey>): QueryFunction<\n TData,\n TQueryKey\n> {\n return async (context) => {\n const query = context.client\n .getQueryCache()\n .find({ queryKey: context.queryKey, exact: true })\n const isRefetch = !!query && query.isFetched()\n if (isRefetch && refetchMode === 'reset') {\n query.setState({\n ...query.resetState,\n fetchStatus: 'fetching',\n })\n }\n\n let result = initialValue\n\n let cancelled = false as boolean\n const streamFnContext = addConsumeAwareSignal<\n OmitKeyof<typeof context, 'signal'>\n >(\n {\n client: context.client,\n meta: context.meta,\n queryKey: context.queryKey,\n pageParam: context.pageParam,\n direction: context.direction,\n },\n () => context.signal,\n () => (cancelled = true),\n )\n\n const stream = await streamFn(streamFnContext)\n\n const isReplaceRefetch = isRefetch && refetchMode === 'replace'\n\n for await (const chunk of stream) {\n if (cancelled) {\n break\n }\n\n if (isReplaceRefetch) {\n // don't append to the cache directly when replace-refetching\n result = reducer(result, chunk)\n } else {\n context.client.setQueryData<TData>(context.queryKey, (prev) =>\n reducer(prev === undefined ? initialValue : prev, chunk),\n )\n }\n }\n\n // finalize result: replace-refetching needs to write to the cache\n if (isReplaceRefetch && !cancelled) {\n context.client.setQueryData<TData>(context.queryKey, result)\n }\n\n const data = context.client.getQueryData<TData>(context.queryKey)\n return data === undefined ? initialValue : data\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAqEA,SAAgB,cAId,EACA,UACA,cAAc,SACd,WAAW,OAAO,UAChB,SAAS,OAA8B,KAAK,GAC9C,eAAe,CAAC,KAIhB;CACA,OAAO,OAAO,YAAY;EACxB,MAAM,QAAQ,QAAQ,OACnB,cAAc,CAAC,CACf,KAAK;GAAE,UAAU,QAAQ;GAAU,OAAO;EAAK,CAAC;EACnD,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,UAAU;EAC7C,IAAI,aAAa,gBAAgB,SAC/B,MAAM,SAAS;GACb,GAAG,MAAM;GACT,aAAa;EACf,CAAC;EAGH,IAAI,SAAS;EAEb,IAAI,YAAY;EAehB,MAAM,SAAS,MAAM,SAdG,sBAGtB;GACE,QAAQ,QAAQ;GAChB,MAAM,QAAQ;GACd,UAAU,QAAQ;GAClB,WAAW,QAAQ;GACnB,WAAW,QAAQ;EACrB,SACM,QAAQ,cACP,YAAY,IAGuB,CAAC;EAE7C,MAAM,mBAAmB,aAAa,gBAAgB;EAEtD,WAAW,MAAM,SAAS,QAAQ;GAChC,IAAI,WACF;GAGF,IAAI,kBAEF,SAAS,QAAQ,QAAQ,KAAK;QAE9B,QAAQ,OAAO,aAAoB,QAAQ,WAAW,SACpD,QAAQ,SAAS,KAAA,IAAY,eAAe,MAAM,KAAK,CACzD;EAEJ;EAGA,IAAI,oBAAoB,CAAC,WACvB,QAAQ,OAAO,aAAoB,QAAQ,UAAU,MAAM;EAG7D,MAAM,OAAO,QAAQ,OAAO,aAAoB,QAAQ,QAAQ;EAChE,OAAO,SAAS,KAAA,IAAY,eAAe;CAC7C;AACF"}
|
|
@@ -12,6 +12,7 @@ declare class Subscribable<TListener extends Function> {
|
|
|
12
12
|
* that removes the listener again — call it to stop listening. The base class never drops a listener
|
|
13
13
|
* on its own, though some subclasses clear all of theirs in `destroy()`.
|
|
14
14
|
* @param listener - Called on each update, with whatever the subclass passes to its subscribers.
|
|
15
|
+
* @returns A function that removes the listener.
|
|
15
16
|
* @example
|
|
16
17
|
* ```ts
|
|
17
18
|
* const unsubscribe = subscribable.subscribe(() => {
|
|
@@ -24,11 +25,20 @@ declare class Subscribable<TListener extends Function> {
|
|
|
24
25
|
subscribe(listener: TListener): () => void;
|
|
25
26
|
/**
|
|
26
27
|
* Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
|
|
28
|
+
* @returns `true` if at least one listener is registered.
|
|
27
29
|
*/
|
|
28
30
|
hasListeners(): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Called after a listener is added. Does nothing here; subclasses override it, e.g. to start
|
|
33
|
+
* tracking what they observe once the first listener subscribes.
|
|
34
|
+
*/
|
|
29
35
|
protected onSubscribe(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Called after a listener is removed. Does nothing here; subclasses override it, e.g. to clean up
|
|
38
|
+
* once the last listener unsubscribes.
|
|
39
|
+
*/
|
|
30
40
|
protected onUnsubscribe(): void;
|
|
31
41
|
}
|
|
32
42
|
//#endregion
|
|
33
43
|
export { Subscribable as t };
|
|
34
|
-
//# sourceMappingURL=subscribable-
|
|
44
|
+
//# sourceMappingURL=subscribable-czlHOxMP.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscribable-czlHOxMP.d.cts","names":[],"sources":["../../src/subscribable.ts"],"mappings":";;;;;;cAKa,aAAa,kBAAkB;YAChC,WAAS,IAAA;;;;;;;;;;;;;;;;;EAqBnB,UAAU,UAAU;;;;;EAepB;;;;;YAQU;;;;;YAQA"}
|