@tanstack/query-core 5.104.1 → 5.104.3
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-Bf0oMcwO.d.ts} +1411 -129
- package/build/legacy/hydration-Bf0oMcwO.d.ts.map +1 -0
- package/build/legacy/{hydration-Br9VIGXa.d.cts → hydration-C6zghADG.d.cts} +1411 -129
- package/build/legacy/hydration-C6zghADG.d.cts.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-Bf0oMcwO.d.ts} +1411 -129
- package/build/modern/hydration-Bf0oMcwO.d.ts.map +1 -0
- package/build/modern/{hydration-Br9VIGXa.d.cts → hydration-C6zghADG.d.cts} +1411 -129
- package/build/modern/hydration-C6zghADG.d.cts.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 +3 -3
- 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 +114 -42
- 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
package/src/queryObserver.ts
CHANGED
|
@@ -41,7 +41,6 @@ interface ObserverFetchOptions extends FetchOptions {
|
|
|
41
41
|
* the primitive that framework adapters (e.g. `useQuery`) build their hooks
|
|
42
42
|
* on top of, but it can also be used directly to observe and switch between
|
|
43
43
|
* queries outside of any framework.
|
|
44
|
-
*
|
|
45
44
|
* @example
|
|
46
45
|
* ```ts
|
|
47
46
|
* const observer = new QueryObserver(queryClient, {
|
|
@@ -103,11 +102,15 @@ export class QueryObserver<
|
|
|
103
102
|
this.setOptions(options)
|
|
104
103
|
}
|
|
105
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Binds the methods of the result (`refetch`) to the observer, so they keep working when destructured
|
|
107
|
+
* from it. Subclasses override it to bind their own result methods as well.
|
|
108
|
+
*/
|
|
106
109
|
protected bindMethods(): void {
|
|
107
110
|
this.refetch = this.refetch.bind(this)
|
|
108
111
|
}
|
|
109
112
|
|
|
110
|
-
protected onSubscribe(): void {
|
|
113
|
+
protected override onSubscribe(): void {
|
|
111
114
|
if (this.listeners.size === 1) {
|
|
112
115
|
this.#currentQuery.addObserver(this)
|
|
113
116
|
|
|
@@ -121,7 +124,7 @@ export class QueryObserver<
|
|
|
121
124
|
}
|
|
122
125
|
}
|
|
123
126
|
|
|
124
|
-
protected onUnsubscribe(): void {
|
|
127
|
+
protected override onUnsubscribe(): void {
|
|
125
128
|
if (!this.hasListeners()) {
|
|
126
129
|
this.destroy()
|
|
127
130
|
}
|
|
@@ -131,6 +134,7 @@ export class QueryObserver<
|
|
|
131
134
|
* Returns whether the observed query is currently stale and configured
|
|
132
135
|
* (via the `refetchOnReconnect` option) to refetch when the network
|
|
133
136
|
* reconnects.
|
|
137
|
+
* @returns `true` if the observer should refetch the query on reconnect.
|
|
134
138
|
*/
|
|
135
139
|
shouldFetchOnReconnect(): boolean {
|
|
136
140
|
return shouldFetchOn(
|
|
@@ -144,6 +148,7 @@ export class QueryObserver<
|
|
|
144
148
|
* Returns whether the observed query is currently stale and configured
|
|
145
149
|
* (via the `refetchOnWindowFocus` option) to refetch when the window
|
|
146
150
|
* regains focus.
|
|
151
|
+
* @returns `true` if the observer should refetch the query on window focus.
|
|
147
152
|
*/
|
|
148
153
|
shouldFetchOnWindowFocus(): boolean {
|
|
149
154
|
return shouldFetchOn(
|
|
@@ -171,7 +176,9 @@ export class QueryObserver<
|
|
|
171
176
|
* trigger a fetch if the new options require one and the observer has
|
|
172
177
|
* subscribers, recompute the current result, and reschedule the stale and
|
|
173
178
|
* refetch-interval timers as needed.
|
|
174
|
-
*
|
|
179
|
+
* @param options - The new observer options. They are defaulted with {@link
|
|
180
|
+
* QueryClient#defaultQueryOptions} before being applied.
|
|
181
|
+
* @throws {Error} If `enabled` is neither a boolean nor a function.
|
|
175
182
|
* @example
|
|
176
183
|
* ```ts
|
|
177
184
|
* observer.setOptions({ queryKey: ['posts', 1], queryFn: () => fetchPost(1) })
|
|
@@ -268,6 +275,8 @@ export class QueryObserver<
|
|
|
268
275
|
* right now, building the underlying `Query` if it doesn't exist yet, without waiting for a
|
|
269
276
|
* subscription callback. Called by framework adapters on every render (e.g. `useQuery`) so the
|
|
270
277
|
* returned value is available synchronously, ahead of `setOptions` triggering an actual fetch.
|
|
278
|
+
* @param options - The defaulted observer options to compute the result for.
|
|
279
|
+
* @returns The result for the given options.
|
|
271
280
|
*/
|
|
272
281
|
getOptimisticResult(
|
|
273
282
|
options: DefaultedQueryObserverOptions<
|
|
@@ -311,7 +320,7 @@ export class QueryObserver<
|
|
|
311
320
|
* observed query. This is a point-in-time read; to be notified of updates
|
|
312
321
|
* as they happen, subscribe to the observer instead (its inherited
|
|
313
322
|
* `subscribe` method).
|
|
314
|
-
*
|
|
323
|
+
* @returns The observer's latest result.
|
|
315
324
|
* @example
|
|
316
325
|
* ```ts
|
|
317
326
|
* const result = observer.getCurrentResult()
|
|
@@ -327,6 +336,9 @@ export class QueryObserver<
|
|
|
327
336
|
* {@link QueryObserver#trackProp} (and an optional `onPropTracked` callback). Used by framework
|
|
328
337
|
* adapters when `notifyOnChangeProps` is not set, to implement its default "only re-render on
|
|
329
338
|
* properties you actually read" behavior.
|
|
339
|
+
* @param result - The result to wrap.
|
|
340
|
+
* @param onPropTracked - Called with the name of each property that is read.
|
|
341
|
+
* @returns A proxy of `result` that tracks property reads.
|
|
330
342
|
*/
|
|
331
343
|
trackResult(
|
|
332
344
|
result: QueryObserverResult<TData, TError>,
|
|
@@ -346,6 +358,7 @@ export class QueryObserver<
|
|
|
346
358
|
* notifies this observer if a tracked property actually changed. Normally called indirectly via
|
|
347
359
|
* {@link QueryObserver#trackResult}'s proxy; exposed directly for adapters that track property
|
|
348
360
|
* access themselves (e.g. through their own reactivity system) instead of via the proxy.
|
|
361
|
+
* @param key - The name of the property that was read.
|
|
349
362
|
*/
|
|
350
363
|
trackProp(key: keyof QueryObserverResult) {
|
|
351
364
|
this.#trackedProps.add(key)
|
|
@@ -353,6 +366,7 @@ export class QueryObserver<
|
|
|
353
366
|
|
|
354
367
|
/**
|
|
355
368
|
* Returns the `Query` instance this observer is currently observing.
|
|
369
|
+
* @returns The observed query.
|
|
356
370
|
*/
|
|
357
371
|
getCurrentQuery(): Query<TQueryFnData, TError, TQueryData, TQueryKey> {
|
|
358
372
|
return this.#currentQuery
|
|
@@ -361,7 +375,9 @@ export class QueryObserver<
|
|
|
361
375
|
/**
|
|
362
376
|
* Refetches the observed query and returns a promise that resolves with
|
|
363
377
|
* the resulting `QueryObserverResult`.
|
|
364
|
-
*
|
|
378
|
+
* @param options - Set `cancelRefetch` to `false` to keep a running fetch instead of cancelling
|
|
379
|
+
* it, and `throwOnError` to `true` to reject when the refetch fails.
|
|
380
|
+
* @returns A promise that resolves with the result after the refetch.
|
|
365
381
|
* @example
|
|
366
382
|
* ```ts
|
|
367
383
|
* const result = await observer.refetch({ cancelRefetch: false })
|
|
@@ -382,7 +398,8 @@ export class QueryObserver<
|
|
|
382
398
|
* resolves with the `QueryObserverResult` for that fetch. This is useful
|
|
383
399
|
* for prefetching data that another observer (e.g. a query about to be
|
|
384
400
|
* navigated to) will need, ahead of time.
|
|
385
|
-
*
|
|
401
|
+
* @param options - The observer options of the query to fetch.
|
|
402
|
+
* @returns A promise that resolves with the result for the fetched query.
|
|
386
403
|
* @example
|
|
387
404
|
* ```ts
|
|
388
405
|
* const result = await observer.fetchOptimistic({
|
|
@@ -409,7 +426,8 @@ export class QueryObserver<
|
|
|
409
426
|
|
|
410
427
|
let unsubscribe = () => {}
|
|
411
428
|
let resolveEarly:
|
|
412
|
-
((result: QueryObserverResult<TData, TError>) => void)
|
|
429
|
+
| ((result: QueryObserverResult<TData, TError>) => void)
|
|
430
|
+
| undefined
|
|
413
431
|
|
|
414
432
|
const cachePromise = new Promise<QueryObserverResult<TData, TError>>(
|
|
415
433
|
(resolve) => {
|
|
@@ -444,6 +462,12 @@ export class QueryObserver<
|
|
|
444
462
|
])
|
|
445
463
|
}
|
|
446
464
|
|
|
465
|
+
/**
|
|
466
|
+
* Fetches the observed query and updates the result once the fetch settles. Used by `refetch` and,
|
|
467
|
+
* in `InfiniteQueryObserver`, to fetch more pages.
|
|
468
|
+
* @param fetchOptions - Options for this fetch. `cancelRefetch` defaults to `true`.
|
|
469
|
+
* @returns A promise that resolves with the updated result.
|
|
470
|
+
*/
|
|
447
471
|
protected fetch(
|
|
448
472
|
fetchOptions: ObserverFetchOptions,
|
|
449
473
|
): Promise<QueryObserverResult<TData, TError>> {
|
|
@@ -555,6 +579,13 @@ export class QueryObserver<
|
|
|
555
579
|
}
|
|
556
580
|
}
|
|
557
581
|
|
|
582
|
+
/**
|
|
583
|
+
* Computes the result for a query and options from the query's state, applying `select`,
|
|
584
|
+
* `placeholderData`, and the derived flags. Subclasses override it to add their own fields.
|
|
585
|
+
* @param query - The query to compute the result for.
|
|
586
|
+
* @param options - The observer options to compute the result with.
|
|
587
|
+
* @returns The computed result.
|
|
588
|
+
*/
|
|
558
589
|
protected createResult(
|
|
559
590
|
query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,
|
|
560
591
|
options: QueryObserverOptions<
|
|
@@ -568,7 +599,8 @@ export class QueryObserver<
|
|
|
568
599
|
const prevQuery = this.#currentQuery
|
|
569
600
|
const prevOptions = this.options
|
|
570
601
|
const prevResult = this.#currentResult as
|
|
571
|
-
QueryObserverResult<TData, TError>
|
|
602
|
+
| QueryObserverResult<TData, TError>
|
|
603
|
+
| undefined
|
|
572
604
|
const prevResultState = this.#currentResultState
|
|
573
605
|
const prevResultOptions = this.#currentResultOptions
|
|
574
606
|
const queryChange = query !== prevQuery
|
|
@@ -732,7 +764,8 @@ export class QueryObserver<
|
|
|
732
764
|
*/
|
|
733
765
|
updateResult(): void {
|
|
734
766
|
const prevResult = this.#currentResult as
|
|
735
|
-
QueryObserverResult<TData, TError>
|
|
767
|
+
| QueryObserverResult<TData, TError>
|
|
768
|
+
| undefined
|
|
736
769
|
|
|
737
770
|
const nextResult = this.createResult(this.#currentQuery, this.options)
|
|
738
771
|
|
|
@@ -810,7 +843,8 @@ export class QueryObserver<
|
|
|
810
843
|
}
|
|
811
844
|
|
|
812
845
|
const prevQuery = this.#currentQuery as
|
|
813
|
-
Query<TQueryFnData, TError, TQueryData, TQueryKey>
|
|
846
|
+
| Query<TQueryFnData, TError, TQueryData, TQueryKey>
|
|
847
|
+
| undefined
|
|
814
848
|
this.#currentQuery = query
|
|
815
849
|
this.#currentQueryInitialState = query.state
|
|
816
850
|
|
|
@@ -820,7 +854,11 @@ export class QueryObserver<
|
|
|
820
854
|
}
|
|
821
855
|
}
|
|
822
856
|
|
|
823
|
-
/**
|
|
857
|
+
/**
|
|
858
|
+
* Updates the observer's result when the observed query changes, and reschedules its timers if it
|
|
859
|
+
* has listeners.
|
|
860
|
+
* @internal
|
|
861
|
+
*/
|
|
824
862
|
onQueryUpdate(): void {
|
|
825
863
|
this.updateResult()
|
|
826
864
|
|
|
@@ -830,6 +868,13 @@ export class QueryObserver<
|
|
|
830
868
|
}
|
|
831
869
|
}
|
|
832
870
|
|
|
871
|
+
/**
|
|
872
|
+
* Checks whether a query has no data yet and should do its initial load when an observer mounts.
|
|
873
|
+
* @param query - The query to check.
|
|
874
|
+
* @param options - The observer options.
|
|
875
|
+
* @returns `true` if the query is enabled, has no data, and isn't in an error state with
|
|
876
|
+
* `retryOnMount: false`.
|
|
877
|
+
*/
|
|
833
878
|
function shouldLoadOnMount(
|
|
834
879
|
query: Query<any, any, any, any>,
|
|
835
880
|
options: QueryObserverOptions<any, any, any, any>,
|
|
@@ -844,6 +889,13 @@ function shouldLoadOnMount(
|
|
|
844
889
|
)
|
|
845
890
|
}
|
|
846
891
|
|
|
892
|
+
/**
|
|
893
|
+
* Checks whether a query should fetch when an observer mounts: either for its initial load, or
|
|
894
|
+
* because it has data and `refetchOnMount` asks for a refetch.
|
|
895
|
+
* @param query - The query to check.
|
|
896
|
+
* @param options - The observer options.
|
|
897
|
+
* @returns `true` if the query should fetch on mount.
|
|
898
|
+
*/
|
|
847
899
|
function shouldFetchOnMount(
|
|
848
900
|
query: Query<any, any, any, any>,
|
|
849
901
|
options: QueryObserverOptions<any, any, any, any, any>,
|
|
@@ -855,6 +907,15 @@ function shouldFetchOnMount(
|
|
|
855
907
|
)
|
|
856
908
|
}
|
|
857
909
|
|
|
910
|
+
/**
|
|
911
|
+
* Checks whether a query should fetch for a `refetchOnMount`, `refetchOnWindowFocus`, or
|
|
912
|
+
* `refetchOnReconnect` option.
|
|
913
|
+
* @param query - The query to check.
|
|
914
|
+
* @param options - The observer options.
|
|
915
|
+
* @param field - The value of the option to check.
|
|
916
|
+
* @returns `true` if the query is enabled, not static, and the option is `'always'`, or is not
|
|
917
|
+
* `false` and the query is stale.
|
|
918
|
+
*/
|
|
858
919
|
function shouldFetchOn(
|
|
859
920
|
query: Query<any, any, any, any>,
|
|
860
921
|
options: QueryObserverOptions<any, any, any, any, any>,
|
|
@@ -873,6 +934,15 @@ function shouldFetchOn(
|
|
|
873
934
|
return false
|
|
874
935
|
}
|
|
875
936
|
|
|
937
|
+
/**
|
|
938
|
+
* Checks whether a query should fetch after the observer's options change.
|
|
939
|
+
* @param query - The query observed with the new options.
|
|
940
|
+
* @param prevQuery - The query observed with the previous options.
|
|
941
|
+
* @param options - The new observer options.
|
|
942
|
+
* @param prevOptions - The previous observer options.
|
|
943
|
+
* @returns `true` if the query changed or was disabled before, it isn't a suspense query in an
|
|
944
|
+
* error state, and it is stale.
|
|
945
|
+
*/
|
|
876
946
|
function shouldFetchOptionally(
|
|
877
947
|
query: Query<any, any, any, any>,
|
|
878
948
|
prevQuery: Query<any, any, any, any>,
|
|
@@ -887,6 +957,12 @@ function shouldFetchOptionally(
|
|
|
887
957
|
)
|
|
888
958
|
}
|
|
889
959
|
|
|
960
|
+
/**
|
|
961
|
+
* Checks whether a query is stale for the given observer options.
|
|
962
|
+
* @param query - The query to check.
|
|
963
|
+
* @param options - The observer options, whose `enabled` and `staleTime` are used.
|
|
964
|
+
* @returns `true` if the query is enabled and its data is stale for `staleTime`.
|
|
965
|
+
*/
|
|
890
966
|
function isStale(
|
|
891
967
|
query: Query<any, any, any, any>,
|
|
892
968
|
options: QueryObserverOptions<any, any, any, any, any>,
|
package/src/removable.ts
CHANGED
|
@@ -20,6 +20,10 @@ export abstract class Removable {
|
|
|
20
20
|
this.clearGcTimeout()
|
|
21
21
|
}
|
|
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
|
protected scheduleGc(): void {
|
|
24
28
|
this.clearGcTimeout()
|
|
25
29
|
|
|
@@ -30,6 +34,11 @@ export abstract class Removable {
|
|
|
30
34
|
}
|
|
31
35
|
}
|
|
32
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Updates `gcTime`, keeping the longest one seen so far. Without a value, it defaults to 5 minutes
|
|
39
|
+
* on the client and `Infinity` on the server.
|
|
40
|
+
* @param newGcTime - The `gcTime` from the latest options, if any.
|
|
41
|
+
*/
|
|
33
42
|
protected updateGcTime(newGcTime: number | undefined): void {
|
|
34
43
|
// Default to 5 minutes (Infinity for server-side) if no gcTime is set
|
|
35
44
|
this.gcTime = Math.max(
|
|
@@ -38,6 +47,9 @@ export abstract class Removable {
|
|
|
38
47
|
)
|
|
39
48
|
}
|
|
40
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Cancels the scheduled removal, if there is one.
|
|
52
|
+
*/
|
|
41
53
|
protected clearGcTimeout() {
|
|
42
54
|
if (this.#gcTimeout !== undefined) {
|
|
43
55
|
timeoutManager.clearTimeout(this.#gcTimeout)
|
|
@@ -45,5 +57,9 @@ export abstract class Removable {
|
|
|
45
57
|
}
|
|
46
58
|
}
|
|
47
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Called when the garbage collection timeout fires. Subclasses remove the entry from its cache
|
|
62
|
+
* unless it is still in use, e.g. because it has observers.
|
|
63
|
+
*/
|
|
48
64
|
protected abstract optionalRemove(): void
|
|
49
65
|
}
|
package/src/retryer.ts
CHANGED
|
@@ -19,14 +19,43 @@ interface RetryerConfig<TData = unknown, TError = DefaultError> {
|
|
|
19
19
|
canRun: () => boolean
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Runs a function with retries, and can pause, continue, or cancel it. Queries and mutations use
|
|
24
|
+
* one for each fetch or execution.
|
|
25
|
+
*/
|
|
22
26
|
export interface Retryer<TData = unknown> {
|
|
27
|
+
/**
|
|
28
|
+
* The promise that resolves with the data, or rejects once retries are exhausted or it is
|
|
29
|
+
* cancelled.
|
|
30
|
+
*/
|
|
23
31
|
promise: Promise<TData>
|
|
32
|
+
/**
|
|
33
|
+
* Cancels the run, rejecting `promise` with a `CancelledError`.
|
|
34
|
+
*/
|
|
24
35
|
cancel: (cancelOptions?: CancelOptions) => void
|
|
36
|
+
/**
|
|
37
|
+
* Resumes a paused run, and returns `promise`.
|
|
38
|
+
*/
|
|
25
39
|
continue: () => Promise<unknown>
|
|
40
|
+
/**
|
|
41
|
+
* Stops further retries. The current attempt still finishes.
|
|
42
|
+
*/
|
|
26
43
|
cancelRetry: () => void
|
|
44
|
+
/**
|
|
45
|
+
* Allows retries again after `cancelRetry`.
|
|
46
|
+
*/
|
|
27
47
|
continueRetry: () => void
|
|
48
|
+
/**
|
|
49
|
+
* Returns whether the run can start now, based on the network mode and `canRun`.
|
|
50
|
+
*/
|
|
28
51
|
canStart: () => boolean
|
|
52
|
+
/**
|
|
53
|
+
* Starts the run, or waits until it can start, and returns `promise`.
|
|
54
|
+
*/
|
|
29
55
|
start: () => Promise<TData>
|
|
56
|
+
/**
|
|
57
|
+
* Returns whether `promise` is still pending, resolved, or rejected.
|
|
58
|
+
*/
|
|
30
59
|
status: () => 'pending' | 'resolved' | 'rejected'
|
|
31
60
|
}
|
|
32
61
|
|
|
@@ -50,10 +79,20 @@ type RetryDelayFunction<TError = DefaultError> = (
|
|
|
50
79
|
error: TError,
|
|
51
80
|
) => number
|
|
52
81
|
|
|
82
|
+
/**
|
|
83
|
+
* The default `retryDelay`: doubles with every failure, starting at 1 second, up to 30 seconds.
|
|
84
|
+
* @param failureCount - The number of failures so far.
|
|
85
|
+
* @returns The delay before the next retry, in milliseconds.
|
|
86
|
+
*/
|
|
53
87
|
function defaultRetryDelay(failureCount: number) {
|
|
54
88
|
return Math.min(1000 * 2 ** failureCount, 30000)
|
|
55
89
|
}
|
|
56
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Checks whether a fetch can run for the given network mode.
|
|
93
|
+
* @param networkMode - The `networkMode` option. Defaults to `'online'`.
|
|
94
|
+
* @returns `true` if the network mode is not `'online'`, or if it is and the client is online.
|
|
95
|
+
*/
|
|
57
96
|
export function canFetch(networkMode: NetworkMode | undefined): boolean {
|
|
58
97
|
return (networkMode ?? 'online') === 'online'
|
|
59
98
|
? onlineManager.isOnline()
|
|
@@ -89,12 +128,24 @@ export class CancelledError extends Error {
|
|
|
89
128
|
}
|
|
90
129
|
|
|
91
130
|
/**
|
|
131
|
+
* Checks whether a value is a `CancelledError`.
|
|
132
|
+
* @param value - The value to check.
|
|
133
|
+
* @returns `true` if `value` is a `CancelledError`.
|
|
92
134
|
* @deprecated Use instanceof `CancelledError` instead.
|
|
93
135
|
*/
|
|
94
136
|
export function isCancelledError(value: any): value is CancelledError {
|
|
95
137
|
return value instanceof CancelledError
|
|
96
138
|
}
|
|
97
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Creates a retryer, which runs a function and retries it on failure according to the `retry` and
|
|
142
|
+
* `retryDelay` options. It pauses while the network mode doesn't allow fetching or `canRun` returns
|
|
143
|
+
* `false`, and can be cancelled. Queries and mutations use it to run their `queryFn` and
|
|
144
|
+
* `mutationFn`.
|
|
145
|
+
* @param config - The function to run, the retry options, and the callbacks for failures, pausing,
|
|
146
|
+
* continuing, and cancellation.
|
|
147
|
+
* @returns The retryer, whose `promise` settles with the result of the function.
|
|
148
|
+
*/
|
|
98
149
|
export function createRetryer<TData = unknown, TError = DefaultError>(
|
|
99
150
|
config: RetryerConfig<TData, TError>,
|
|
100
151
|
): Retryer<TData> {
|
|
@@ -152,10 +203,10 @@ export function createRetryer<TData = unknown, TError = DefaultError>(
|
|
|
152
203
|
}
|
|
153
204
|
}
|
|
154
205
|
|
|
155
|
-
const pause = () => {
|
|
206
|
+
const pause = (canResume: () => boolean) => {
|
|
156
207
|
return new Promise((continueResolve) => {
|
|
157
208
|
continueFn = (value) => {
|
|
158
|
-
if (isResolved() ||
|
|
209
|
+
if (isResolved() || canResume()) {
|
|
159
210
|
continueResolve(value)
|
|
160
211
|
}
|
|
161
212
|
}
|
|
@@ -223,7 +274,7 @@ export function createRetryer<TData = unknown, TError = DefaultError>(
|
|
|
223
274
|
sleep(delay)
|
|
224
275
|
// Pause if the document is not visible or when the device is offline
|
|
225
276
|
.then(() => {
|
|
226
|
-
return canContinue() ? undefined : pause()
|
|
277
|
+
return canContinue() ? undefined : pause(canContinue)
|
|
227
278
|
})
|
|
228
279
|
.then(() => {
|
|
229
280
|
if (isRetryCancelled) {
|
|
@@ -251,7 +302,9 @@ export function createRetryer<TData = unknown, TError = DefaultError>(
|
|
|
251
302
|
if (canStart()) {
|
|
252
303
|
run()
|
|
253
304
|
} else {
|
|
254
|
-
|
|
305
|
+
// Waiting to start (e.g. queued behind a scope) does not depend on focus,
|
|
306
|
+
// only on the same conditions that allow starting in the first place
|
|
307
|
+
pause(canStart).then(run)
|
|
255
308
|
}
|
|
256
309
|
return promise
|
|
257
310
|
},
|
package/src/streamedQuery.ts
CHANGED
|
@@ -54,7 +54,9 @@ type StreamedQueryParams<TQueryFnData, TData, TQueryKey extends QueryKey> =
|
|
|
54
54
|
* Data will be an Array of all the chunks received.
|
|
55
55
|
* The query will be in a 'pending' state until the first chunk of data is received, but will go to 'success' after that.
|
|
56
56
|
* The query will stay in fetchStatus 'fetching' until the stream ends.
|
|
57
|
-
* @param
|
|
57
|
+
* @param options - The `streamFn` that returns an AsyncIterable to stream data from, and the optional
|
|
58
|
+
* `refetchMode`, `reducer`, and `initialValue` options.
|
|
59
|
+
* @returns A query function to pass as `queryFn`.
|
|
58
60
|
* @example
|
|
59
61
|
* ```ts
|
|
60
62
|
* await queryClient.query({
|
|
@@ -93,8 +95,7 @@ export function streamedQuery<
|
|
|
93
95
|
|
|
94
96
|
let result = initialValue
|
|
95
97
|
|
|
96
|
-
|
|
97
|
-
let cancelled: boolean = false as boolean
|
|
98
|
+
let cancelled = false as boolean
|
|
98
99
|
const streamFnContext = addConsumeAwareSignal<
|
|
99
100
|
OmitKeyof<typeof context, 'signal'>
|
|
100
101
|
>(
|
package/src/subscribable.ts
CHANGED
|
@@ -15,6 +15,7 @@ export class Subscribable<TListener extends Function> {
|
|
|
15
15
|
* that removes the listener again — call it to stop listening. The base class never drops a listener
|
|
16
16
|
* on its own, though some subclasses clear all of theirs in `destroy()`.
|
|
17
17
|
* @param listener - Called on each update, with whatever the subclass passes to its subscribers.
|
|
18
|
+
* @returns A function that removes the listener.
|
|
18
19
|
* @example
|
|
19
20
|
* ```ts
|
|
20
21
|
* const unsubscribe = subscribable.subscribe(() => {
|
|
@@ -37,15 +38,24 @@ export class Subscribable<TListener extends Function> {
|
|
|
37
38
|
|
|
38
39
|
/**
|
|
39
40
|
* Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
|
|
41
|
+
* @returns `true` if at least one listener is registered.
|
|
40
42
|
*/
|
|
41
43
|
hasListeners(): boolean {
|
|
42
44
|
return this.listeners.size > 0
|
|
43
45
|
}
|
|
44
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Called after a listener is added. Does nothing here; subclasses override it, e.g. to start
|
|
49
|
+
* tracking what they observe once the first listener subscribes.
|
|
50
|
+
*/
|
|
45
51
|
protected onSubscribe(): void {
|
|
46
52
|
// Do nothing
|
|
47
53
|
}
|
|
48
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Called after a listener is removed. Does nothing here; subclasses override it, e.g. to clean up
|
|
57
|
+
* once the last listener unsubscribes.
|
|
58
|
+
*/
|
|
49
59
|
protected onUnsubscribe(): void {
|
|
50
60
|
// Do nothing
|
|
51
61
|
}
|
package/src/timeoutManager.ts
CHANGED
|
@@ -27,10 +27,22 @@ export type ManagedTimerId = number | { [Symbol.toPrimitive]: () => number }
|
|
|
27
27
|
*/
|
|
28
28
|
export type TimeoutProvider<TTimerId extends ManagedTimerId = ManagedTimerId> =
|
|
29
29
|
{
|
|
30
|
+
/**
|
|
31
|
+
* Schedules `callback` to run once after `delay` milliseconds, like the global `setTimeout`.
|
|
32
|
+
*/
|
|
30
33
|
readonly setTimeout: (callback: TimeoutCallback, delay: number) => TTimerId
|
|
34
|
+
/**
|
|
35
|
+
* Cancels a timeout scheduled with `setTimeout`.
|
|
36
|
+
*/
|
|
31
37
|
readonly clearTimeout: (timeoutId: TTimerId | undefined) => void
|
|
32
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Schedules `callback` to run every `delay` milliseconds, like the global `setInterval`.
|
|
41
|
+
*/
|
|
33
42
|
readonly setInterval: (callback: TimeoutCallback, delay: number) => TTimerId
|
|
43
|
+
/**
|
|
44
|
+
* Cancels an interval scheduled with `setInterval`.
|
|
45
|
+
*/
|
|
34
46
|
readonly clearInterval: (intervalId: TTimerId | undefined) => void
|
|
35
47
|
}
|
|
36
48
|
|
|
@@ -59,7 +71,7 @@ export const defaultTimeoutProvider: TimeoutProvider = {
|
|
|
59
71
|
/**
|
|
60
72
|
* Allows customization of how timeouts are created.
|
|
61
73
|
*
|
|
62
|
-
*
|
|
74
|
+
* `@tanstack/query-core` makes liberal use of timeouts to implement `staleTime`
|
|
63
75
|
* and `gcTime`. The default TimeoutManager provider uses the platform's global
|
|
64
76
|
* `setTimeout` implementation, which is known to have scalability issues with
|
|
65
77
|
* thousands of timeouts on the event loop.
|
|
@@ -92,7 +104,7 @@ export class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
|
|
|
92
104
|
* QueryClient or queries, so that the same provider is used consistently
|
|
93
105
|
* for all timers in the application, since different TimeoutProviders
|
|
94
106
|
* cannot cancel each others' timers.
|
|
95
|
-
*
|
|
107
|
+
* @param provider - The `TimeoutProvider` to use for all timers from now on.
|
|
96
108
|
* @example
|
|
97
109
|
* ```ts
|
|
98
110
|
* import { timeoutManager, QueryClient } from '@tanstack/query-core'
|
|
@@ -139,7 +151,9 @@ export class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
|
|
|
139
151
|
*
|
|
140
152
|
* It returns a timer ID, which may be a number or an object that can be
|
|
141
153
|
* coerced to a number via `Symbol.toPrimitive`.
|
|
142
|
-
*
|
|
154
|
+
* @param callback - The function to call when the timeout elapses.
|
|
155
|
+
* @param delay - The time to wait before calling `callback`, in milliseconds.
|
|
156
|
+
* @returns The timer ID, to pass to {@link TimeoutManager#clearTimeout}.
|
|
143
157
|
* @example
|
|
144
158
|
* ```ts
|
|
145
159
|
* import { timeoutManager } from '@tanstack/query-core'
|
|
@@ -163,7 +177,7 @@ export class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
|
|
|
163
177
|
* `clearTimeout` cancels a timeout callback scheduled with `setTimeout`,
|
|
164
178
|
* like the global `clearTimeout` function. It should be called with a
|
|
165
179
|
* timer ID returned by `setTimeout`.
|
|
166
|
-
*
|
|
180
|
+
* @param timeoutId - The timer ID returned by `setTimeout`, or `undefined`.
|
|
167
181
|
* @example
|
|
168
182
|
* ```ts
|
|
169
183
|
* import { timeoutManager } from '@tanstack/query-core'
|
|
@@ -186,7 +200,9 @@ export class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
|
|
|
186
200
|
*
|
|
187
201
|
* Like `setTimeout`, it returns a timer ID, which may be a number or an
|
|
188
202
|
* object that can be coerced to a number via `Symbol.toPrimitive`.
|
|
189
|
-
*
|
|
203
|
+
* @param callback - The function to call on every interval.
|
|
204
|
+
* @param delay - The time between calls, in milliseconds.
|
|
205
|
+
* @returns The timer ID, to pass to {@link TimeoutManager#clearInterval}.
|
|
190
206
|
* @example
|
|
191
207
|
* ```ts
|
|
192
208
|
* import { timeoutManager } from '@tanstack/query-core'
|
|
@@ -208,7 +224,7 @@ export class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
|
|
|
208
224
|
* `clearInterval` can be used to cancel an interval, like the global
|
|
209
225
|
* `clearInterval` function. It should be called with an interval ID
|
|
210
226
|
* returned by `setInterval`.
|
|
211
|
-
*
|
|
227
|
+
* @param intervalId - The timer ID returned by `setInterval`, or `undefined`.
|
|
212
228
|
* @example
|
|
213
229
|
* ```ts
|
|
214
230
|
* import { timeoutManager } from '@tanstack/query-core'
|
|
@@ -237,6 +253,7 @@ export const timeoutManager = new TimeoutManager()
|
|
|
237
253
|
*
|
|
238
254
|
* This function is provided to make auditing the `tanstack/query-core` for
|
|
239
255
|
* incorrect use of system `setTimeout` easier.
|
|
256
|
+
* @param callback - The function to call on the next event loop tick.
|
|
240
257
|
*/
|
|
241
258
|
export function systemSetTimeoutZero(callback: TimeoutCallback): void {
|
|
242
259
|
setTimeout(callback, 0)
|