@tanstack/query-core 5.102.7 → 5.103.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/environmentManager.cjs +15 -1
- package/build/legacy/environmentManager.cjs.map +1 -1
- package/build/legacy/environmentManager.d.cts +15 -1
- package/build/legacy/environmentManager.d.cts.map +1 -1
- package/build/legacy/environmentManager.d.ts +15 -1
- package/build/legacy/environmentManager.d.ts.map +1 -1
- package/build/legacy/environmentManager.js +15 -1
- package/build/legacy/environmentManager.js.map +1 -1
- package/build/legacy/focusManager.cjs +57 -0
- package/build/legacy/focusManager.cjs.map +1 -1
- package/build/legacy/focusManager.d.cts +57 -0
- package/build/legacy/focusManager.d.cts.map +1 -1
- package/build/legacy/focusManager.d.ts +57 -0
- package/build/legacy/focusManager.d.ts.map +1 -1
- package/build/legacy/focusManager.js +57 -0
- package/build/legacy/focusManager.js.map +1 -1
- package/build/legacy/{hydration-Bjs0MSgg.d.ts → hydration-Cq7QYAzB.d.ts} +1424 -7
- package/build/legacy/hydration-Cq7QYAzB.d.ts.map +1 -0
- package/build/legacy/{hydration-DWGr3cFC.d.cts → hydration-DwR10Hi-.d.cts} +1424 -7
- package/build/legacy/hydration-DwR10Hi-.d.cts.map +1 -0
- package/build/legacy/hydration.cjs +57 -4
- 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 +57 -4
- package/build/legacy/hydration.js.map +1 -1
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +5 -5
- package/build/legacy/index.d.ts +5 -5
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
- package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
- package/build/legacy/infiniteQueryObserver.cjs +67 -0
- package/build/legacy/infiniteQueryObserver.cjs.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.cts +68 -1
- package/build/legacy/infiniteQueryObserver.d.cts.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.ts +68 -1
- package/build/legacy/infiniteQueryObserver.d.ts.map +1 -1
- package/build/legacy/infiniteQueryObserver.js +67 -0
- package/build/legacy/infiniteQueryObserver.js.map +1 -1
- package/build/legacy/mutation.cjs +74 -0
- 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 +74 -0
- package/build/legacy/mutation.js.map +1 -1
- package/build/legacy/mutationCache.cjs +74 -0
- 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 +74 -0
- package/build/legacy/mutationCache.js.map +1 -1
- package/build/legacy/mutationObserver.cjs +71 -0
- 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 +71 -0
- package/build/legacy/mutationObserver.js.map +1 -1
- package/build/legacy/notifyManager.cjs +45 -1
- package/build/legacy/notifyManager.cjs.map +1 -1
- package/build/legacy/notifyManager.d.cts +83 -2
- package/build/legacy/notifyManager.d.cts.map +1 -1
- package/build/legacy/notifyManager.d.ts +83 -2
- package/build/legacy/notifyManager.d.ts.map +1 -1
- package/build/legacy/notifyManager.js +45 -1
- package/build/legacy/notifyManager.js.map +1 -1
- package/build/legacy/onlineManager.cjs +47 -0
- package/build/legacy/onlineManager.cjs.map +1 -1
- package/build/legacy/onlineManager.d.cts +47 -0
- package/build/legacy/onlineManager.d.cts.map +1 -1
- package/build/legacy/onlineManager.d.ts +47 -0
- package/build/legacy/onlineManager.d.ts.map +1 -1
- package/build/legacy/onlineManager.js +47 -0
- package/build/legacy/onlineManager.js.map +1 -1
- package/build/legacy/queriesObserver.cjs +64 -0
- package/build/legacy/queriesObserver.cjs.map +1 -1
- package/build/legacy/queriesObserver.d.cts +73 -1
- package/build/legacy/queriesObserver.d.cts.map +1 -1
- package/build/legacy/queriesObserver.d.ts +73 -1
- package/build/legacy/queriesObserver.d.ts.map +1 -1
- package/build/legacy/queriesObserver.js +64 -0
- package/build/legacy/queriesObserver.js.map +1 -1
- package/build/legacy/query.cjs +141 -0
- 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 +141 -0
- package/build/legacy/query.js.map +1 -1
- package/build/legacy/queryCache.cjs +120 -0
- 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 +120 -0
- package/build/legacy/queryCache.js.map +1 -1
- package/build/legacy/queryClient.cjs +363 -0
- 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 +363 -0
- package/build/legacy/queryClient.js.map +1 -1
- package/build/legacy/queryObserver.cjs +116 -2
- 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 +116 -2
- package/build/legacy/queryObserver.js.map +1 -1
- package/build/legacy/retryer.cjs +18 -0
- 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 +18 -0
- package/build/legacy/retryer.js.map +1 -1
- package/build/legacy/streamedQuery.cjs +10 -1
- package/build/legacy/streamedQuery.cjs.map +1 -1
- package/build/legacy/streamedQuery.d.cts +26 -2
- package/build/legacy/streamedQuery.d.cts.map +1 -1
- package/build/legacy/streamedQuery.d.ts +26 -2
- package/build/legacy/streamedQuery.d.ts.map +1 -1
- package/build/legacy/streamedQuery.js +10 -1
- package/build/legacy/streamedQuery.js.map +1 -1
- package/build/legacy/timeoutManager.cjs +99 -0
- package/build/legacy/timeoutManager.cjs.map +1 -1
- package/build/legacy/timeoutManager.d.cts +105 -0
- package/build/legacy/timeoutManager.d.cts.map +1 -1
- package/build/legacy/timeoutManager.d.ts +105 -0
- package/build/legacy/timeoutManager.d.ts.map +1 -1
- package/build/legacy/timeoutManager.js +99 -0
- 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 +76 -0
- 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 +76 -0
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/environmentManager.cjs +15 -1
- package/build/modern/environmentManager.cjs.map +1 -1
- package/build/modern/environmentManager.d.cts +15 -1
- package/build/modern/environmentManager.d.cts.map +1 -1
- package/build/modern/environmentManager.d.ts +15 -1
- package/build/modern/environmentManager.d.ts.map +1 -1
- package/build/modern/environmentManager.js +15 -1
- package/build/modern/environmentManager.js.map +1 -1
- package/build/modern/focusManager.cjs +57 -0
- package/build/modern/focusManager.cjs.map +1 -1
- package/build/modern/focusManager.d.cts +57 -0
- package/build/modern/focusManager.d.cts.map +1 -1
- package/build/modern/focusManager.d.ts +57 -0
- package/build/modern/focusManager.d.ts.map +1 -1
- package/build/modern/focusManager.js +57 -0
- package/build/modern/focusManager.js.map +1 -1
- package/build/modern/{hydration-Bjs0MSgg.d.ts → hydration-Cq7QYAzB.d.ts} +1424 -7
- package/build/modern/hydration-Cq7QYAzB.d.ts.map +1 -0
- package/build/modern/{hydration-DWGr3cFC.d.cts → hydration-DwR10Hi-.d.cts} +1424 -7
- package/build/modern/hydration-DwR10Hi-.d.cts.map +1 -0
- package/build/modern/hydration.cjs +57 -4
- 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 +57 -4
- package/build/modern/hydration.js.map +1 -1
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +5 -5
- package/build/modern/index.d.ts +5 -5
- package/build/modern/index.js.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.cts +1 -1
- package/build/modern/infiniteQueryBehavior.d.ts +1 -1
- package/build/modern/infiniteQueryObserver.cjs +67 -0
- package/build/modern/infiniteQueryObserver.cjs.map +1 -1
- package/build/modern/infiniteQueryObserver.d.cts +68 -1
- package/build/modern/infiniteQueryObserver.d.cts.map +1 -1
- package/build/modern/infiniteQueryObserver.d.ts +68 -1
- package/build/modern/infiniteQueryObserver.d.ts.map +1 -1
- package/build/modern/infiniteQueryObserver.js +67 -0
- package/build/modern/infiniteQueryObserver.js.map +1 -1
- package/build/modern/mutation.cjs +74 -0
- 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 +74 -0
- package/build/modern/mutation.js.map +1 -1
- package/build/modern/mutationCache.cjs +74 -0
- 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 +74 -0
- package/build/modern/mutationCache.js.map +1 -1
- package/build/modern/mutationObserver.cjs +71 -0
- 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 +71 -0
- package/build/modern/mutationObserver.js.map +1 -1
- package/build/modern/notifyManager.cjs +45 -1
- package/build/modern/notifyManager.cjs.map +1 -1
- package/build/modern/notifyManager.d.cts +83 -2
- package/build/modern/notifyManager.d.cts.map +1 -1
- package/build/modern/notifyManager.d.ts +83 -2
- package/build/modern/notifyManager.d.ts.map +1 -1
- package/build/modern/notifyManager.js +45 -1
- package/build/modern/notifyManager.js.map +1 -1
- package/build/modern/onlineManager.cjs +47 -0
- package/build/modern/onlineManager.cjs.map +1 -1
- package/build/modern/onlineManager.d.cts +47 -0
- package/build/modern/onlineManager.d.cts.map +1 -1
- package/build/modern/onlineManager.d.ts +47 -0
- package/build/modern/onlineManager.d.ts.map +1 -1
- package/build/modern/onlineManager.js +47 -0
- package/build/modern/onlineManager.js.map +1 -1
- package/build/modern/queriesObserver.cjs +64 -0
- package/build/modern/queriesObserver.cjs.map +1 -1
- package/build/modern/queriesObserver.d.cts +73 -1
- package/build/modern/queriesObserver.d.cts.map +1 -1
- package/build/modern/queriesObserver.d.ts +73 -1
- package/build/modern/queriesObserver.d.ts.map +1 -1
- package/build/modern/queriesObserver.js +64 -0
- package/build/modern/queriesObserver.js.map +1 -1
- package/build/modern/query.cjs +141 -0
- 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 +141 -0
- package/build/modern/query.js.map +1 -1
- package/build/modern/queryCache.cjs +120 -0
- 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 +120 -0
- package/build/modern/queryCache.js.map +1 -1
- package/build/modern/queryClient.cjs +363 -0
- 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 +363 -0
- package/build/modern/queryClient.js.map +1 -1
- package/build/modern/queryObserver.cjs +116 -2
- 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 +116 -2
- package/build/modern/queryObserver.js.map +1 -1
- package/build/modern/retryer.cjs +18 -0
- 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 +18 -0
- package/build/modern/retryer.js.map +1 -1
- package/build/modern/streamedQuery.cjs +10 -1
- package/build/modern/streamedQuery.cjs.map +1 -1
- package/build/modern/streamedQuery.d.cts +26 -2
- package/build/modern/streamedQuery.d.cts.map +1 -1
- package/build/modern/streamedQuery.d.ts +26 -2
- package/build/modern/streamedQuery.d.ts.map +1 -1
- package/build/modern/streamedQuery.js +10 -1
- package/build/modern/streamedQuery.js.map +1 -1
- package/build/modern/timeoutManager.cjs +99 -0
- package/build/modern/timeoutManager.cjs.map +1 -1
- package/build/modern/timeoutManager.d.cts +105 -0
- package/build/modern/timeoutManager.d.cts.map +1 -1
- package/build/modern/timeoutManager.d.ts +105 -0
- package/build/modern/timeoutManager.d.ts.map +1 -1
- package/build/modern/timeoutManager.js +99 -0
- 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 +76 -0
- 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 +76 -0
- package/build/modern/utils.js.map +1 -1
- package/package.json +9 -9
- package/src/environmentManager.ts +15 -1
- package/src/focusManager.ts +57 -0
- package/src/hydration.ts +82 -16
- package/src/index.ts +3 -0
- package/src/infiniteQueryObserver.ts +67 -0
- package/src/mutation.ts +108 -0
- package/src/mutationCache.ts +91 -0
- package/src/mutationObserver.ts +71 -0
- package/src/notifyManager.ts +46 -1
- package/src/onlineManager.ts +47 -0
- package/src/queriesObserver.ts +72 -0
- package/src/query.ts +193 -0
- package/src/queryCache.ts +134 -0
- package/src/queryClient.ts +369 -0
- package/src/queryObserver.ts +117 -4
- package/src/retryer.ts +18 -0
- package/src/streamedQuery.ts +25 -1
- package/src/timeoutManager.ts +105 -0
- package/src/types.ts +147 -5
- package/src/utils.ts +107 -0
- package/build/legacy/hydration-Bjs0MSgg.d.ts.map +0 -1
- package/build/legacy/hydration-DWGr3cFC.d.cts.map +0 -1
- package/build/modern/hydration-Bjs0MSgg.d.ts.map +0 -1
- package/build/modern/hydration-DWGr3cFC.d.cts.map +0 -1
package/src/queriesObserver.ts
CHANGED
|
@@ -23,9 +23,36 @@ type CombineFn<TCombinedResult> = (
|
|
|
23
23
|
export interface QueriesObserverOptions<
|
|
24
24
|
TCombinedResult = Array<QueryObserverResult>,
|
|
25
25
|
> {
|
|
26
|
+
/**
|
|
27
|
+
* A function that combines the array of `QueryObserverResult`s (one per
|
|
28
|
+
* observed query) into a single value. The combined value is memoized and
|
|
29
|
+
* only recomputed when one of the underlying results, the query hashes, or
|
|
30
|
+
* the `combine` function itself changes.
|
|
31
|
+
*
|
|
32
|
+
* Defaults to returning the array of `QueryObserverResult`s unchanged.
|
|
33
|
+
*/
|
|
26
34
|
combine?: CombineFn<TCombinedResult>
|
|
27
35
|
}
|
|
28
36
|
|
|
37
|
+
/**
|
|
38
|
+
* A `QueriesObserver` watches an array of queries at once, exposing them as
|
|
39
|
+
* a single array of `QueryObserverResult`s (or, when a `combine` option is
|
|
40
|
+
* given, as a combined value derived from that array). It manages one
|
|
41
|
+
* internal `QueryObserver` per query, and is the primitive that framework
|
|
42
|
+
* adapters (e.g. `useQueries`) build their hooks on top of.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const observer = new QueriesObserver(queryClient, [
|
|
47
|
+
* { queryKey: ['post', 1], queryFn: fetchPost },
|
|
48
|
+
* { queryKey: ['post', 2], queryFn: fetchPost },
|
|
49
|
+
* ])
|
|
50
|
+
*
|
|
51
|
+
* const unsubscribe = observer.subscribe((result) => {
|
|
52
|
+
* console.log(result)
|
|
53
|
+
* })
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
29
56
|
export class QueriesObserver<
|
|
30
57
|
TCombinedResult = Array<QueryObserverResult>,
|
|
31
58
|
> extends Subscribable<QueriesObserverListener> {
|
|
@@ -72,6 +99,10 @@ export class QueriesObserver<
|
|
|
72
99
|
}
|
|
73
100
|
}
|
|
74
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Stops observing all queries: clears all listeners and destroys every
|
|
104
|
+
* underlying `QueryObserver` this observer manages.
|
|
105
|
+
*/
|
|
75
106
|
destroy(): void {
|
|
76
107
|
this.listeners = new Set()
|
|
77
108
|
this.#observers.forEach((observer) => {
|
|
@@ -79,6 +110,20 @@ export class QueriesObserver<
|
|
|
79
110
|
})
|
|
80
111
|
}
|
|
81
112
|
|
|
113
|
+
/**
|
|
114
|
+
* Replaces the set of queries being observed. Existing `QueryObserver`s
|
|
115
|
+
* are reused for queries that match an already-observed query hash;
|
|
116
|
+
* observers for queries that are no longer present are destroyed, and new
|
|
117
|
+
* observers are created and subscribed to for newly added queries.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* observer.setQueries([
|
|
122
|
+
* { queryKey: ['post', 1], queryFn: fetchPost },
|
|
123
|
+
* { queryKey: ['post', 3], queryFn: fetchPost },
|
|
124
|
+
* ])
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
82
127
|
setQueries(
|
|
83
128
|
queries: Array<QueryObserverOptions>,
|
|
84
129
|
options?: QueriesObserverOptions<TCombinedResult>,
|
|
@@ -151,18 +196,45 @@ export class QueriesObserver<
|
|
|
151
196
|
})
|
|
152
197
|
}
|
|
153
198
|
|
|
199
|
+
/**
|
|
200
|
+
* Returns the most recently computed array of `QueryObserverResult`s, one
|
|
201
|
+
* per observed query, in the same order as the queries passed to the
|
|
202
|
+
* constructor or `setQueries`.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* const results = observer.getCurrentResult()
|
|
207
|
+
* const data = results.map((result) => result.data)
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
154
210
|
getCurrentResult(): Array<QueryObserverResult> {
|
|
155
211
|
return this.#result
|
|
156
212
|
}
|
|
157
213
|
|
|
214
|
+
/**
|
|
215
|
+
* Returns the underlying `Query` instances currently being observed, in
|
|
216
|
+
* the same order as the queries passed to the constructor or `setQueries`.
|
|
217
|
+
*/
|
|
158
218
|
getQueries() {
|
|
159
219
|
return this.#observers.map((observer) => observer.getCurrentQuery())
|
|
160
220
|
}
|
|
161
221
|
|
|
222
|
+
/**
|
|
223
|
+
* Returns the underlying `QueryObserver` instances this observer manages,
|
|
224
|
+
* in the same order as the queries passed to the constructor or
|
|
225
|
+
* `setQueries`.
|
|
226
|
+
*/
|
|
162
227
|
getObservers() {
|
|
163
228
|
return this.#observers
|
|
164
229
|
}
|
|
165
230
|
|
|
231
|
+
/**
|
|
232
|
+
* The `QueriesObserver` counterpart of {@link QueryObserver#getOptimisticResult} — computes
|
|
233
|
+
* the result for the given (already-defaulted) queries right now, synchronously. Called by
|
|
234
|
+
* framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw
|
|
235
|
+
* per-query results, a function to compute the combined result from them, and a function to
|
|
236
|
+
* wrap the results for property-access tracking.
|
|
237
|
+
*/
|
|
166
238
|
getOptimisticResult(
|
|
167
239
|
queries: Array<QueryObserverOptions>,
|
|
168
240
|
combine: CombineFn<TCombinedResult> | undefined,
|
package/src/query.ts
CHANGED
|
@@ -45,18 +45,70 @@ interface QueryConfig<
|
|
|
45
45
|
state?: QueryState<TData, TError>
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* The raw state stored on a `Query` instance. This is the underlying state
|
|
50
|
+
* that observer results (e.g. `QueryObserverResult`) are derived from.
|
|
51
|
+
*/
|
|
48
52
|
export interface QueryState<TData = unknown, TError = DefaultError> {
|
|
53
|
+
/**
|
|
54
|
+
* The last successfully resolved data for the query.
|
|
55
|
+
*/
|
|
49
56
|
data: TData | undefined
|
|
57
|
+
/**
|
|
58
|
+
* The number of times the query has successfully resolved.
|
|
59
|
+
*/
|
|
50
60
|
dataUpdateCount: number
|
|
61
|
+
/**
|
|
62
|
+
* The timestamp for when the query most recently returned the `status` as `"success"`.
|
|
63
|
+
*/
|
|
51
64
|
dataUpdatedAt: number
|
|
65
|
+
/**
|
|
66
|
+
* The error object for the query, if the last attempt resulted in an error.
|
|
67
|
+
* - Defaults to `null`.
|
|
68
|
+
*/
|
|
52
69
|
error: TError | null
|
|
70
|
+
/**
|
|
71
|
+
* The sum of all errors, incremented every time the query resolves with an error.
|
|
72
|
+
*/
|
|
53
73
|
errorUpdateCount: number
|
|
74
|
+
/**
|
|
75
|
+
* The timestamp for when the query most recently returned the `status` as `"error"`.
|
|
76
|
+
*/
|
|
54
77
|
errorUpdatedAt: number
|
|
78
|
+
/**
|
|
79
|
+
* The failure count for the current fetch.
|
|
80
|
+
* - Incremented every time the fetch fails.
|
|
81
|
+
* - Reset to `0` when the fetch succeeds.
|
|
82
|
+
*/
|
|
55
83
|
fetchFailureCount: number
|
|
84
|
+
/**
|
|
85
|
+
* The reason the current fetch failed, as reported by the retryer.
|
|
86
|
+
* - Reset to `null` when the fetch succeeds.
|
|
87
|
+
*/
|
|
56
88
|
fetchFailureReason: TError | null
|
|
89
|
+
/**
|
|
90
|
+
* Metadata passed to the currently in-flight (or most recent) fetch, e.g. the
|
|
91
|
+
* `fetchMore` direction for infinite queries.
|
|
92
|
+
*/
|
|
57
93
|
fetchMeta: FetchMeta | null
|
|
94
|
+
/**
|
|
95
|
+
* Whether the query has been marked as invalidated via `invalidate()`.
|
|
96
|
+
* - Reset to `false` whenever the query resolves successfully.
|
|
97
|
+
*/
|
|
58
98
|
isInvalidated: boolean
|
|
99
|
+
/**
|
|
100
|
+
* The status of the query.
|
|
101
|
+
* - `pending` if there's no cached data and no attempt was finished yet.
|
|
102
|
+
* - `error` if the last attempt resulted in an error.
|
|
103
|
+
* - `success` if the query has data.
|
|
104
|
+
*/
|
|
59
105
|
status: QueryStatus
|
|
106
|
+
/**
|
|
107
|
+
* The fetch status of the query.
|
|
108
|
+
* - `fetching`: the `queryFn` is currently executing.
|
|
109
|
+
* - `paused`: a fetch wanted to run but has been paused (see network mode).
|
|
110
|
+
* - `idle`: the query is not fetching.
|
|
111
|
+
*/
|
|
60
112
|
fetchStatus: FetchStatus
|
|
61
113
|
}
|
|
62
114
|
|
|
@@ -151,6 +203,25 @@ export type Action<TData, TError> =
|
|
|
151
203
|
|
|
152
204
|
// CLASS
|
|
153
205
|
|
|
206
|
+
/**
|
|
207
|
+
* Represents a single cached query. A `Query` holds the query's key, options,
|
|
208
|
+
* state (data/error/status), and the observers currently subscribed to it.
|
|
209
|
+
*
|
|
210
|
+
* Instances are created and managed internally by `QueryCache`; application
|
|
211
|
+
* code typically interacts with queries indirectly through `QueryClient` or
|
|
212
|
+
* a framework hook like `useQuery`. Direct access to a `Query` instance is
|
|
213
|
+
* possible via `queryCache.find()`/`findAll()` for inspecting cache state.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```ts
|
|
217
|
+
* const queryCache = queryClient.getQueryCache()
|
|
218
|
+
* const query = queryCache.find({ queryKey: ['posts'] })
|
|
219
|
+
*
|
|
220
|
+
* if (query) {
|
|
221
|
+
* console.log(query.state.dataUpdatedAt)
|
|
222
|
+
* }
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
154
225
|
export class Query<
|
|
155
226
|
TQueryFnData = unknown,
|
|
156
227
|
TError = DefaultError,
|
|
@@ -187,18 +258,27 @@ export class Query<
|
|
|
187
258
|
this.state = config.state ?? this.#initialState
|
|
188
259
|
this.scheduleGc()
|
|
189
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* The `meta` object passed in the query's options, if any.
|
|
263
|
+
*/
|
|
190
264
|
get meta(): QueryMeta | undefined {
|
|
191
265
|
return this.options.meta
|
|
192
266
|
}
|
|
193
267
|
|
|
268
|
+
/** @internal */
|
|
194
269
|
get queryType() {
|
|
195
270
|
return this.#queryType
|
|
196
271
|
}
|
|
197
272
|
|
|
273
|
+
/**
|
|
274
|
+
* The promise for the currently in-flight fetch, if the query is fetching.
|
|
275
|
+
* `undefined` when the query is not fetching.
|
|
276
|
+
*/
|
|
198
277
|
get promise(): Promise<TData> | undefined {
|
|
199
278
|
return this.#retryer?.promise
|
|
200
279
|
}
|
|
201
280
|
|
|
281
|
+
/** @internal */
|
|
202
282
|
setOptions(
|
|
203
283
|
options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
204
284
|
): void {
|
|
@@ -228,6 +308,7 @@ export class Query<
|
|
|
228
308
|
}
|
|
229
309
|
}
|
|
230
310
|
|
|
311
|
+
/** @internal */
|
|
231
312
|
setData(
|
|
232
313
|
newData: TData,
|
|
233
314
|
options?: SetDataOptions & { manual: boolean },
|
|
@@ -245,37 +326,77 @@ export class Query<
|
|
|
245
326
|
return data
|
|
246
327
|
}
|
|
247
328
|
|
|
329
|
+
/**
|
|
330
|
+
* Merges the given partial state directly into this query's state, notifying observers. Used
|
|
331
|
+
* by persistence and broadcast plugins to restore a state snapshot, and by devtools to let a
|
|
332
|
+
* user manually trigger a loading/error state or edit the cached data.
|
|
333
|
+
*/
|
|
248
334
|
setState(state: Partial<QueryState<TData, TError>>): void {
|
|
249
335
|
this.#dispatch({ type: 'setState', state })
|
|
250
336
|
}
|
|
251
337
|
|
|
338
|
+
/**
|
|
339
|
+
* Cancels the query's currently in-flight fetch, if any.
|
|
340
|
+
* - Returns a promise that resolves once the cancellation has settled.
|
|
341
|
+
* - If no fetch is in progress, resolves immediately.
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* ```ts
|
|
345
|
+
* await query.cancel()
|
|
346
|
+
* ```
|
|
347
|
+
*/
|
|
252
348
|
cancel(options?: CancelOptions): Promise<void> {
|
|
253
349
|
const promise = this.#retryer?.promise
|
|
254
350
|
this.#retryer?.cancel(options)
|
|
255
351
|
return promise ? promise.then(noop).catch(noop) : Promise.resolve()
|
|
256
352
|
}
|
|
257
353
|
|
|
354
|
+
/**
|
|
355
|
+
* Clears the query's garbage collection timeout and silently cancels any
|
|
356
|
+
* in-flight fetch. Called by `QueryCache` when the query is removed from
|
|
357
|
+
* the cache.
|
|
358
|
+
*
|
|
359
|
+
* @see {@link Query#cancel}
|
|
360
|
+
*/
|
|
258
361
|
destroy(): void {
|
|
259
362
|
super.destroy()
|
|
260
363
|
|
|
261
364
|
this.cancel({ silent: true })
|
|
262
365
|
}
|
|
263
366
|
|
|
367
|
+
/** @internal */
|
|
264
368
|
get resetState(): QueryState<TData, TError> {
|
|
265
369
|
return this.#initialState
|
|
266
370
|
}
|
|
267
371
|
|
|
372
|
+
/**
|
|
373
|
+
* Resets the query back to its initial state (the state it had when it was
|
|
374
|
+
* first created, e.g. any `initialData`), destroying it first to cancel any
|
|
375
|
+
* in-flight fetch.
|
|
376
|
+
*/
|
|
268
377
|
reset(): void {
|
|
269
378
|
this.destroy()
|
|
270
379
|
this.setState(this.resetState)
|
|
271
380
|
}
|
|
272
381
|
|
|
382
|
+
/**
|
|
383
|
+
* Returns `true` if the query has at least one observer for which `enabled`
|
|
384
|
+
* does not resolve to `false`.
|
|
385
|
+
*/
|
|
273
386
|
isActive(): boolean {
|
|
274
387
|
return this.observers.some(
|
|
275
388
|
(observer) => resolveQueryValue(observer.options.enabled, this) !== false,
|
|
276
389
|
)
|
|
277
390
|
}
|
|
278
391
|
|
|
392
|
+
/**
|
|
393
|
+
* Returns `true` if the query is disabled, meaning it will not fetch
|
|
394
|
+
* automatically.
|
|
395
|
+
* - If the query has observers, it is disabled when none of them are active
|
|
396
|
+
* (see `isActive`).
|
|
397
|
+
* - If the query has no observers, it is disabled when its `queryFn` is
|
|
398
|
+
* `skipToken` or it has never been fetched.
|
|
399
|
+
*/
|
|
279
400
|
isDisabled(): boolean {
|
|
280
401
|
if (this.getObserversCount() > 0) {
|
|
281
402
|
return !this.isActive()
|
|
@@ -284,10 +405,18 @@ export class Query<
|
|
|
284
405
|
return this.options.queryFn === skipToken || !this.isFetched()
|
|
285
406
|
}
|
|
286
407
|
|
|
408
|
+
/**
|
|
409
|
+
* Returns `true` if the query has been fetched, i.e. it has resolved with
|
|
410
|
+
* either data or an error at least once.
|
|
411
|
+
*/
|
|
287
412
|
isFetched() {
|
|
288
413
|
return this.state.dataUpdateCount + this.state.errorUpdateCount > 0
|
|
289
414
|
}
|
|
290
415
|
|
|
416
|
+
/**
|
|
417
|
+
* Returns `true` if the query has at least one observer configured with
|
|
418
|
+
* `staleTime: 'static'`, meaning it is treated as never stale.
|
|
419
|
+
*/
|
|
291
420
|
isStatic(): boolean {
|
|
292
421
|
if (this.getObserversCount() > 0) {
|
|
293
422
|
return this.observers.some(
|
|
@@ -299,6 +428,22 @@ export class Query<
|
|
|
299
428
|
return false
|
|
300
429
|
}
|
|
301
430
|
|
|
431
|
+
/**
|
|
432
|
+
* Returns `true` if the query is stale.
|
|
433
|
+
* - If the query has observers, defers to whether any observer's current
|
|
434
|
+
* result reports `isStale` (which accounts for each observer's own
|
|
435
|
+
* `staleTime` and `enabled` state).
|
|
436
|
+
* - If the query has no observers, it is considered stale when it has no
|
|
437
|
+
* data or has been invalidated.
|
|
438
|
+
*
|
|
439
|
+
* @see {@link Query#isStaleByTime}
|
|
440
|
+
* @example
|
|
441
|
+
* ```ts
|
|
442
|
+
* if (query.isStale()) {
|
|
443
|
+
* // refetch or otherwise treat the cached data as outdated
|
|
444
|
+
* }
|
|
445
|
+
* ```
|
|
446
|
+
*/
|
|
302
447
|
isStale(): boolean {
|
|
303
448
|
// check observers first, their `isStale` has the source of truth
|
|
304
449
|
// calculated with `isStaleByTime` and it takes `enabled` into account
|
|
@@ -311,6 +456,20 @@ export class Query<
|
|
|
311
456
|
return this.state.data === undefined || this.state.isInvalidated
|
|
312
457
|
}
|
|
313
458
|
|
|
459
|
+
/**
|
|
460
|
+
* Returns `true` if the query's data is stale relative to the given
|
|
461
|
+
* `staleTime` (defaults to `0`).
|
|
462
|
+
* - A query with no data is always stale.
|
|
463
|
+
* - `staleTime: 'static'` is never stale.
|
|
464
|
+
* - An invalidated query is always stale.
|
|
465
|
+
* - Otherwise, staleness is based on elapsed time since `dataUpdatedAt`.
|
|
466
|
+
*
|
|
467
|
+
* @see {@link Query#isStale}
|
|
468
|
+
* @example
|
|
469
|
+
* ```ts
|
|
470
|
+
* const isStale = query.isStaleByTime(1000 * 60)
|
|
471
|
+
* ```
|
|
472
|
+
*/
|
|
314
473
|
isStaleByTime(staleTime: StaleTime = 0): boolean {
|
|
315
474
|
// no data is always stale
|
|
316
475
|
if (this.state.data === undefined) {
|
|
@@ -328,6 +487,7 @@ export class Query<
|
|
|
328
487
|
return !timeUntilStale(this.state.dataUpdatedAt, staleTime)
|
|
329
488
|
}
|
|
330
489
|
|
|
490
|
+
/** @internal */
|
|
331
491
|
onFocus(): void {
|
|
332
492
|
const observer = this.observers.find((x) => x.shouldFetchOnWindowFocus())
|
|
333
493
|
|
|
@@ -337,6 +497,7 @@ export class Query<
|
|
|
337
497
|
this.#retryer?.continue()
|
|
338
498
|
}
|
|
339
499
|
|
|
500
|
+
/** @internal */
|
|
340
501
|
onOnline(): void {
|
|
341
502
|
const observer = this.observers.find((x) => x.shouldFetchOnReconnect())
|
|
342
503
|
|
|
@@ -346,6 +507,7 @@ export class Query<
|
|
|
346
507
|
this.#retryer?.continue()
|
|
347
508
|
}
|
|
348
509
|
|
|
510
|
+
/** @internal */
|
|
349
511
|
addObserver(observer: QueryObserver<any, any, any, any, any>): void {
|
|
350
512
|
if (!this.observers.includes(observer)) {
|
|
351
513
|
this.observers.push(observer)
|
|
@@ -357,6 +519,7 @@ export class Query<
|
|
|
357
519
|
}
|
|
358
520
|
}
|
|
359
521
|
|
|
522
|
+
/** @internal */
|
|
360
523
|
removeObserver(observer: QueryObserver<any, any, any, any, any>): void {
|
|
361
524
|
const index = this.observers.indexOf(observer)
|
|
362
525
|
if (index !== -1) {
|
|
@@ -384,16 +547,46 @@ export class Query<
|
|
|
384
547
|
}
|
|
385
548
|
}
|
|
386
549
|
|
|
550
|
+
/**
|
|
551
|
+
* Returns the number of observers currently subscribed to this query.
|
|
552
|
+
*
|
|
553
|
+
* @example
|
|
554
|
+
* ```ts
|
|
555
|
+
* if (query.getObserversCount() === 0) {
|
|
556
|
+
* // no component is currently watching this query
|
|
557
|
+
* }
|
|
558
|
+
* ```
|
|
559
|
+
*/
|
|
387
560
|
getObserversCount(): number {
|
|
388
561
|
return this.observers.length
|
|
389
562
|
}
|
|
390
563
|
|
|
564
|
+
/**
|
|
565
|
+
* Marks the query as invalidated, unless it is already invalidated. This
|
|
566
|
+
* updates `state.isInvalidated` and notifies observers, but does not by
|
|
567
|
+
* itself trigger a refetch.
|
|
568
|
+
*
|
|
569
|
+
* @example
|
|
570
|
+
* ```ts
|
|
571
|
+
* query.invalidate()
|
|
572
|
+
* ```
|
|
573
|
+
*/
|
|
391
574
|
invalidate(): void {
|
|
392
575
|
if (!this.state.isInvalidated) {
|
|
393
576
|
this.#dispatch({ type: 'invalidate' })
|
|
394
577
|
}
|
|
395
578
|
}
|
|
396
579
|
|
|
580
|
+
/**
|
|
581
|
+
* Fetches the query, i.e. runs its `queryFn` (through any configured
|
|
582
|
+
* retryer/behavior) and updates the query's state with the result.
|
|
583
|
+
* - If a fetch is already in flight, returns its promise instead of
|
|
584
|
+
* starting a new one, unless `fetchOptions.cancelRefetch` is set and the
|
|
585
|
+
* query already has data, in which case the current fetch is silently
|
|
586
|
+
* cancelled first.
|
|
587
|
+
* - If `options` is passed, it replaces the query's current options
|
|
588
|
+
* before fetching.
|
|
589
|
+
*/
|
|
397
590
|
async fetch(
|
|
398
591
|
options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
399
592
|
fetchOptions?: FetchOptions<TQueryFnData>,
|
package/src/queryCache.ts
CHANGED
|
@@ -16,12 +16,21 @@ import type { QueryObserver } from './queryObserver'
|
|
|
16
16
|
|
|
17
17
|
// TYPES
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Global callbacks that fire for every query handled by a `QueryCache`, regardless of which
|
|
21
|
+
* component or observer triggered it. Unlike `QueryClient`'s `defaultOptions`, which a query can
|
|
22
|
+
* override, these callbacks are always called. Unlike `MutationCacheConfig`'s callbacks, these
|
|
23
|
+
* are fire-and-forget: their return value is not awaited before the query settles.
|
|
24
|
+
*/
|
|
19
25
|
export interface QueryCacheConfig {
|
|
26
|
+
/** Called when any query in the cache encounters an error. */
|
|
20
27
|
onError?: (
|
|
21
28
|
error: DefaultError,
|
|
22
29
|
query: Query<unknown, unknown, unknown>,
|
|
23
30
|
) => void
|
|
31
|
+
/** Called when any query in the cache is successful. */
|
|
24
32
|
onSuccess?: (data: unknown, query: Query<unknown, unknown, unknown>) => void
|
|
33
|
+
/** Called when any query in the cache is settled, either successfully or with an error. */
|
|
25
34
|
onSettled?: (
|
|
26
35
|
data: unknown | undefined,
|
|
27
36
|
error: DefaultError | null,
|
|
@@ -68,6 +77,11 @@ interface NotifyEventQueryObserverOptionsUpdated extends NotifyEvent {
|
|
|
68
77
|
observer: QueryObserver<any, any, any, any, any>
|
|
69
78
|
}
|
|
70
79
|
|
|
80
|
+
/**
|
|
81
|
+
* The event passed to a `QueryCache` subscriber. Fired whenever a query is added or removed from
|
|
82
|
+
* the cache, its state is updated (e.g. via `query.setState` or `queryClient.removeQueries`), or
|
|
83
|
+
* one of its observers is added, removed, or has its results or options updated.
|
|
84
|
+
*/
|
|
71
85
|
export type QueryCacheNotifyEvent =
|
|
72
86
|
| NotifyEventQueryAdded
|
|
73
87
|
| NotifyEventQueryRemoved
|
|
@@ -89,6 +103,23 @@ export interface QueryStore {
|
|
|
89
103
|
|
|
90
104
|
// CLASS
|
|
91
105
|
|
|
106
|
+
/**
|
|
107
|
+
* The `QueryCache` is the storage mechanism for TanStack Query. It stores all the data, meta
|
|
108
|
+
* information, and state of the queries it contains.
|
|
109
|
+
*
|
|
110
|
+
* Normally, you will not interact with the `QueryCache` directly and instead use a `QueryClient`
|
|
111
|
+
* for a specific cache. You can subscribe to it (inherited from `Subscribable`) to be informed of
|
|
112
|
+
* safe/known updates to the cache, such as queries being added, removed, or updated — updates made
|
|
113
|
+
* outside of the cache's own tracked mechanisms (e.g. mutating a query's state object directly) do
|
|
114
|
+
* not notify subscribers.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* const unsubscribe = queryCache.subscribe((event) => {
|
|
119
|
+
* console.log(event.type, event.query)
|
|
120
|
+
* })
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
92
123
|
export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
93
124
|
#queries: QueryStore
|
|
94
125
|
|
|
@@ -97,6 +128,22 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
97
128
|
this.#queries = new Map<string, Query>()
|
|
98
129
|
}
|
|
99
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Returns the existing `Query` instance for the given options' `queryKey`/`queryHash`, or
|
|
133
|
+
* builds and adds a new one to the cache if none exists yet. Used by framework adapters and
|
|
134
|
+
* plugins (e.g. broadcast/persistence) that need to get-or-create a `Query` directly, bypassing
|
|
135
|
+
* the reactive `QueryObserver` machinery.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* const queryCache = queryClient.getQueryCache()
|
|
140
|
+
*
|
|
141
|
+
* const query = queryCache.build(queryClient, {
|
|
142
|
+
* queryKey: ['posts'],
|
|
143
|
+
* queryFn: fetchPosts,
|
|
144
|
+
* })
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
100
147
|
build<
|
|
101
148
|
TQueryFnData = unknown,
|
|
102
149
|
TError = DefaultError,
|
|
@@ -130,6 +177,7 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
130
177
|
return query
|
|
131
178
|
}
|
|
132
179
|
|
|
180
|
+
/** @internal */
|
|
133
181
|
add(query: Query<any, any, any, any>): void {
|
|
134
182
|
if (!this.#queries.has(query.queryHash)) {
|
|
135
183
|
this.#queries.set(query.queryHash, query)
|
|
@@ -141,6 +189,22 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
141
189
|
}
|
|
142
190
|
}
|
|
143
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Destroys the given `Query` and removes it from the cache, notifying subscribers with a
|
|
194
|
+
* `'removed'` event. A no-op if the query is no longer the one currently stored under its hash
|
|
195
|
+
* (e.g. it was already replaced). Used by plugins (e.g. the broadcast client) that mirror
|
|
196
|
+
* removals across `QueryCache` instances.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```ts
|
|
200
|
+
* const queryCache = queryClient.getQueryCache()
|
|
201
|
+
* const query = queryCache.find({ queryKey: ['posts'] })
|
|
202
|
+
*
|
|
203
|
+
* if (query) {
|
|
204
|
+
* queryCache.remove(query)
|
|
205
|
+
* }
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
144
208
|
remove(query: Query<any, any, any, any>): void {
|
|
145
209
|
const queryInMap = this.#queries.get(query.queryHash)
|
|
146
210
|
|
|
@@ -155,6 +219,16 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
155
219
|
}
|
|
156
220
|
}
|
|
157
221
|
|
|
222
|
+
/**
|
|
223
|
+
* Removes all queries from the cache.
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* const queryCache = queryClient.getQueryCache()
|
|
228
|
+
*
|
|
229
|
+
* queryCache.clear()
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
158
232
|
clear(): void {
|
|
159
233
|
notifyManager.batch(() => {
|
|
160
234
|
this.getAll().forEach((query) => {
|
|
@@ -163,6 +237,20 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
163
237
|
})
|
|
164
238
|
}
|
|
165
239
|
|
|
240
|
+
/**
|
|
241
|
+
* Returns the `Query` instance stored under the given `queryHash`, or `undefined` if none
|
|
242
|
+
* exists. Unlike {@link QueryCache#find}, this looks up by the already-computed hash rather
|
|
243
|
+
* than by `QueryFilters`. Used by plugins (e.g. broadcast/hydration) that already have a hash
|
|
244
|
+
* to look up directly.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```ts
|
|
248
|
+
* const queryCache = queryClient.getQueryCache()
|
|
249
|
+
* const queryHash = hashKey(['posts'])
|
|
250
|
+
*
|
|
251
|
+
* const query = queryCache.get(queryHash)
|
|
252
|
+
* ```
|
|
253
|
+
*/
|
|
166
254
|
get<
|
|
167
255
|
TQueryFnData = unknown,
|
|
168
256
|
TError = DefaultError,
|
|
@@ -176,10 +264,38 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
176
264
|
| undefined
|
|
177
265
|
}
|
|
178
266
|
|
|
267
|
+
/**
|
|
268
|
+
* Returns all queries within the cache.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```ts
|
|
272
|
+
* const queryCache = queryClient.getQueryCache()
|
|
273
|
+
*
|
|
274
|
+
* const queries = queryCache.getAll()
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
179
277
|
getAll(): Array<Query> {
|
|
180
278
|
return [...this.#queries.values()]
|
|
181
279
|
}
|
|
182
280
|
|
|
281
|
+
/**
|
|
282
|
+
* A slightly more advanced method that can be used to get an existing query instance from the
|
|
283
|
+
* cache. This instance not only contains all the state for the query, but all of the instances,
|
|
284
|
+
* and underlying guts of the query as well. If the query does not exist, `undefined` is
|
|
285
|
+
* returned.
|
|
286
|
+
*
|
|
287
|
+
* This is not typically needed for most applications, but can come in handy when needing more
|
|
288
|
+
* information about a query in rare scenarios (e.g. looking at `query.state.dataUpdatedAt` to
|
|
289
|
+
* decide whether a query is fresh enough to be used as an initial value).
|
|
290
|
+
*
|
|
291
|
+
* @see {@link QueryCache#findAll}
|
|
292
|
+
* @example
|
|
293
|
+
* ```ts
|
|
294
|
+
* const queryCache = queryClient.getQueryCache()
|
|
295
|
+
*
|
|
296
|
+
* const query = queryCache.find({ queryKey: ['posts'] })
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
183
299
|
find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(
|
|
184
300
|
filters: WithRequired<QueryFilters, 'queryKey'>,
|
|
185
301
|
): Query<TQueryFnData, TError, TData> | undefined {
|
|
@@ -190,6 +306,21 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
190
306
|
) as Query<TQueryFnData, TError, TData> | undefined
|
|
191
307
|
}
|
|
192
308
|
|
|
309
|
+
/**
|
|
310
|
+
* An even more advanced method that can be used to get existing query instances from the cache
|
|
311
|
+
* that partially match a query key. If no queries match, an empty array is returned.
|
|
312
|
+
*
|
|
313
|
+
* This is not typically needed for most applications, but can come in handy when needing more
|
|
314
|
+
* information about queries in rare scenarios.
|
|
315
|
+
*
|
|
316
|
+
* @see {@link QueryCache#find}
|
|
317
|
+
* @example
|
|
318
|
+
* ```ts
|
|
319
|
+
* const queryCache = queryClient.getQueryCache()
|
|
320
|
+
*
|
|
321
|
+
* const queries = queryCache.findAll({ queryKey: ['posts'] })
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
193
324
|
findAll(filters: QueryFilters<any> = {}): Array<Query> {
|
|
194
325
|
const queries = this.getAll()
|
|
195
326
|
return Object.keys(filters).length > 0
|
|
@@ -197,6 +328,7 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
197
328
|
: queries
|
|
198
329
|
}
|
|
199
330
|
|
|
331
|
+
/** @internal */
|
|
200
332
|
notify(event: QueryCacheNotifyEvent): void {
|
|
201
333
|
notifyManager.batch(() => {
|
|
202
334
|
this.listeners.forEach((listener) => {
|
|
@@ -205,6 +337,7 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
205
337
|
})
|
|
206
338
|
}
|
|
207
339
|
|
|
340
|
+
/** @internal */
|
|
208
341
|
onFocus(): void {
|
|
209
342
|
notifyManager.batch(() => {
|
|
210
343
|
this.getAll().forEach((query) => {
|
|
@@ -213,6 +346,7 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
|
|
|
213
346
|
})
|
|
214
347
|
}
|
|
215
348
|
|
|
349
|
+
/** @internal */
|
|
216
350
|
onOnline(): void {
|
|
217
351
|
notifyManager.batch(() => {
|
|
218
352
|
this.getAll().forEach((query) => {
|