@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
|
@@ -5,6 +5,24 @@ import { onlineManager } from "./onlineManager.js";
|
|
|
5
5
|
import { MutationCache } from "./mutationCache.js";
|
|
6
6
|
import { QueryCache } from "./queryCache.js";
|
|
7
7
|
//#region src/queryClient.ts
|
|
8
|
+
/**
|
|
9
|
+
* `QueryClient` is used to interact with a cache of queries and mutations. It owns a
|
|
10
|
+
* `QueryCache` and a `MutationCache` (creating default ones if none are passed in) and holds
|
|
11
|
+
* the default options that are applied to queries and mutations created through it.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const queryClient = new QueryClient({
|
|
16
|
+
* defaultOptions: {
|
|
17
|
+
* queries: {
|
|
18
|
+
* staleTime: Infinity,
|
|
19
|
+
* },
|
|
20
|
+
* },
|
|
21
|
+
* })
|
|
22
|
+
*
|
|
23
|
+
* await queryClient.query({ queryKey: ['posts'], queryFn: fetchPosts })
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
8
26
|
var QueryClient = class {
|
|
9
27
|
#queryCache;
|
|
10
28
|
#mutationCache;
|
|
@@ -22,6 +40,12 @@ var QueryClient = class {
|
|
|
22
40
|
this.#mutationDefaults = /* @__PURE__ */ new Map();
|
|
23
41
|
this.#mountCount = 0;
|
|
24
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Called by a framework adapter's `QueryClientProvider`-equivalent when it mounts, to start
|
|
45
|
+
* listening for focus/online events and resume paused mutations. Ref-counted via an internal
|
|
46
|
+
* mount count, so nested or multiple providers sharing the same `QueryClient` don't tear down
|
|
47
|
+
* the shared listeners until the last one unmounts.
|
|
48
|
+
*/
|
|
25
49
|
mount() {
|
|
26
50
|
this.#mountCount++;
|
|
27
51
|
if (this.#mountCount !== 1) return;
|
|
@@ -38,6 +62,11 @@ var QueryClient = class {
|
|
|
38
62
|
}
|
|
39
63
|
});
|
|
40
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* The inverse of {@link QueryClient#mount} — called by a framework adapter's
|
|
67
|
+
* `QueryClientProvider`-equivalent when it unmounts. Only tears down the focus/online
|
|
68
|
+
* listeners once the mount count returns to `0`.
|
|
69
|
+
*/
|
|
41
70
|
unmount() {
|
|
42
71
|
this.#mountCount--;
|
|
43
72
|
if (this.#mountCount !== 0) return;
|
|
@@ -46,12 +75,35 @@ var QueryClient = class {
|
|
|
46
75
|
this.#unsubscribeOnline?.();
|
|
47
76
|
this.#unsubscribeOnline = void 0;
|
|
48
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* Returns the number of queries in the cache that are currently fetching, optionally
|
|
80
|
+
* matching a set of filters. This includes background-fetching, loading new pages, and
|
|
81
|
+
* loading more infinite query results.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* if (queryClient.isFetching()) {
|
|
86
|
+
* console.log('At least one query is fetching!')
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
49
90
|
isFetching(filters) {
|
|
50
91
|
return this.#queryCache.findAll({
|
|
51
92
|
...filters,
|
|
52
93
|
fetchStatus: "fetching"
|
|
53
94
|
}).length;
|
|
54
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* Returns the number of mutations in the cache that are currently pending, optionally
|
|
98
|
+
* matching a set of filters.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* if (queryClient.isMutating()) {
|
|
103
|
+
* console.log('At least one mutation is pending!')
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
55
107
|
isMutating(filters) {
|
|
56
108
|
return this.#mutationCache.findAll({
|
|
57
109
|
...filters,
|
|
@@ -64,6 +116,8 @@ var QueryClient = class {
|
|
|
64
116
|
*
|
|
65
117
|
* Hint: Do not use this function inside a component, because it won't receive updates.
|
|
66
118
|
* Use `useQuery` to create a `QueryObserver` that subscribes to changes.
|
|
119
|
+
*
|
|
120
|
+
* @see {@link QueryClient#getQueriesData}
|
|
67
121
|
*/
|
|
68
122
|
getQueryData(queryKey) {
|
|
69
123
|
const options = this.defaultQueryOptions({ queryKey });
|
|
@@ -80,11 +134,49 @@ var QueryClient = class {
|
|
|
80
134
|
if (options.revalidateIfStale && query.isStaleByTime(resolveQueryValue(defaultedOptions.staleTime, query))) this.prefetchQuery(defaultedOptions);
|
|
81
135
|
return Promise.resolve(cachedData);
|
|
82
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Imperative (non-reactive) way to retrieve the cached data of multiple queries at once.
|
|
139
|
+
* Only queries matching the given filters are returned; if none match, an empty array is
|
|
140
|
+
* returned.
|
|
141
|
+
*
|
|
142
|
+
* Because the matched queries can hold data of different shapes (e.g. a broad filter can match
|
|
143
|
+
* queries with unrelated data types), the `TQueryFnData` generic defaults to `unknown` rather
|
|
144
|
+
* than being inferred. Passing a more specific type is a convenience for call sites that know
|
|
145
|
+
* every matched query holds the same shape — it is not checked against the actual cache
|
|
146
|
+
* contents.
|
|
147
|
+
*
|
|
148
|
+
* @see {@link QueryClient#getQueryData}
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* const data = queryClient.getQueriesData({ queryKey: ['posts'] })
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
83
154
|
getQueriesData(filters) {
|
|
84
155
|
return this.#queryCache.findAll(filters).map(({ queryKey, state }) => {
|
|
85
156
|
return [queryKey, state.data];
|
|
86
157
|
});
|
|
87
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Synchronous way to immediately update a query's cached data. If the updater (or the value
|
|
161
|
+
* passed) resolves to `undefined`, the cache is left untouched and no query is created;
|
|
162
|
+
* otherwise, if the query does not exist yet, it will be created. To update multiple queries
|
|
163
|
+
* at once by partially matching query keys, use {@link QueryClient#setQueriesData} instead.
|
|
164
|
+
*
|
|
165
|
+
* Updates must be performed immutably: do not mutate `oldData`, or data previously retrieved
|
|
166
|
+
* via {@link QueryClient#getQueryData}, in place.
|
|
167
|
+
*
|
|
168
|
+
* @param queryKey - The query key to set data for.
|
|
169
|
+
* @param updater - Either the new data, or a function that receives the current data (which
|
|
170
|
+
* may be `undefined`) and returns the new data.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```ts
|
|
174
|
+
* queryClient.setQueryData(['posts'], newPosts)
|
|
175
|
+
*
|
|
176
|
+
* // Or, using an updater function that receives the current data:
|
|
177
|
+
* queryClient.setQueryData(['posts'], (oldPosts) => [...oldPosts, newPost])
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
88
180
|
setQueryData(queryKey, updater, options) {
|
|
89
181
|
const defaultedOptions = this.defaultQueryOptions({ queryKey });
|
|
90
182
|
const prevData = this.#queryCache.get(defaultedOptions.queryHash)?.state.data;
|
|
@@ -95,13 +187,47 @@ var QueryClient = class {
|
|
|
95
187
|
manual: true
|
|
96
188
|
});
|
|
97
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* Synchronous way to immediately update the cached data of multiple queries at once, using
|
|
192
|
+
* filters or partial query key matching. Only queries that already exist and match the given
|
|
193
|
+
* filters are updated; no new cache entries are created. Internally this calls
|
|
194
|
+
* {@link QueryClient#setQueryData} for each matching query.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* queryClient.setQueriesData({ queryKey: ['posts'] }, (oldPosts) =>
|
|
199
|
+
* oldPosts ? oldPosts.filter((post) => post.id !== deletedId) : oldPosts,
|
|
200
|
+
* )
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
98
203
|
setQueriesData(filters, updater, options) {
|
|
99
204
|
return notifyManager.batch(() => this.#queryCache.findAll(filters).map(({ queryKey }) => [queryKey, this.setQueryData(queryKey, updater, options)]));
|
|
100
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* Imperative (non-reactive) way to retrieve an existing query's state. If the query does not
|
|
208
|
+
* exist, `undefined` is returned.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```ts
|
|
212
|
+
* const state = queryClient.getQueryState(['posts'])
|
|
213
|
+
* console.log(state?.dataUpdatedAt)
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
101
216
|
getQueryState(queryKey) {
|
|
102
217
|
const options = this.defaultQueryOptions({ queryKey });
|
|
103
218
|
return this.#queryCache.get(options.queryHash)?.state;
|
|
104
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* Removes queries from the cache that match the given filters. Unlike
|
|
222
|
+
* {@link QueryClient#invalidateQueries} or {@link QueryClient#refetchQueries}, this removes
|
|
223
|
+
* matching queries from the cache instead of refetching them. Without filters, every query in
|
|
224
|
+
* the cache is removed.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* queryClient.removeQueries({ queryKey: ['posts'], exact: true })
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
105
231
|
removeQueries(filters) {
|
|
106
232
|
const queryCache = this.#queryCache;
|
|
107
233
|
notifyManager.batch(() => {
|
|
@@ -110,6 +236,16 @@ var QueryClient = class {
|
|
|
110
236
|
});
|
|
111
237
|
});
|
|
112
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Resets queries matching the given filters back to their initial state (e.g. any
|
|
241
|
+
* `initialData`), notifying subscribers rather than removing them. Active queries among the
|
|
242
|
+
* matched set are then refetched, and the returned promise resolves once that refetch settles.
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* await queryClient.resetQueries({ queryKey: ['posts'], exact: true })
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
113
249
|
resetQueries(filters, options) {
|
|
114
250
|
const queryCache = this.#queryCache;
|
|
115
251
|
return notifyManager.batch(() => {
|
|
@@ -124,6 +260,19 @@ var QueryClient = class {
|
|
|
124
260
|
}, options);
|
|
125
261
|
});
|
|
126
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Cancels outgoing fetches for queries matching the given filters. Most useful when performing
|
|
265
|
+
* optimistic updates, since any outgoing refetch that resolves afterwards would otherwise
|
|
266
|
+
* overwrite the optimistic update. By default (`revert: true`), a cancelled query's data is
|
|
267
|
+
* reverted to its state before the outgoing fetch started.
|
|
268
|
+
*
|
|
269
|
+
* The returned promise never rejects, even if individual cancellations fail.
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```ts
|
|
273
|
+
* await queryClient.cancelQueries({ queryKey: ['posts'], exact: true })
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
127
276
|
cancelQueries(filters, cancelOptions = {}) {
|
|
128
277
|
const defaultedCancelOptions = {
|
|
129
278
|
revert: true,
|
|
@@ -132,6 +281,19 @@ var QueryClient = class {
|
|
|
132
281
|
const promises = notifyManager.batch(() => this.#queryCache.findAll(filters).map((query) => query.cancel(defaultedCancelOptions)));
|
|
133
282
|
return Promise.all(promises).then(noop).catch(noop);
|
|
134
283
|
}
|
|
284
|
+
/**
|
|
285
|
+
* Marks queries matching the given filters as invalidated. Unlike
|
|
286
|
+
* {@link QueryClient#removeQueries}, invalidated queries stay in the cache.
|
|
287
|
+
*
|
|
288
|
+
* Unless `filters.refetchType` is `'none'`, matching queries are then refetched via
|
|
289
|
+
* {@link QueryClient#refetchQueries}, using `filters.refetchType` if set, otherwise
|
|
290
|
+
* `filters.type`, otherwise `'active'`.
|
|
291
|
+
*
|
|
292
|
+
* @example
|
|
293
|
+
* ```ts
|
|
294
|
+
* await queryClient.invalidateQueries({ queryKey: ['posts'], refetchType: 'active' })
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
135
297
|
invalidateQueries(filters, options = {}) {
|
|
136
298
|
return notifyManager.batch(() => {
|
|
137
299
|
this.#queryCache.findAll(filters).forEach((query) => {
|
|
@@ -144,6 +306,21 @@ var QueryClient = class {
|
|
|
144
306
|
}, options);
|
|
145
307
|
});
|
|
146
308
|
}
|
|
309
|
+
/**
|
|
310
|
+
* Refetches queries matching the given filters, regardless of whether they are stale. Without
|
|
311
|
+
* filters, every query in the cache is refetched. Queries that are disabled, or static (only
|
|
312
|
+
* have observers with a static `staleTime`), are never refetched.
|
|
313
|
+
*
|
|
314
|
+
* By default (`cancelRefetch: true`), a currently running fetch is cancelled before the new
|
|
315
|
+
* one starts. The returned promise resolves once all matching queries have settled; it does
|
|
316
|
+
* not reject on individual query failures unless `throwOnError` is set.
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* ```ts
|
|
320
|
+
* // refetch all active queries partially matching a query key:
|
|
321
|
+
* await queryClient.refetchQueries({ queryKey: ['posts'], type: 'active' })
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
147
324
|
refetchQueries(filters, options = {}) {
|
|
148
325
|
const fetchOptions = {
|
|
149
326
|
...options,
|
|
@@ -156,6 +333,37 @@ var QueryClient = class {
|
|
|
156
333
|
}));
|
|
157
334
|
return Promise.all(promises).then(noop);
|
|
158
335
|
}
|
|
336
|
+
/**
|
|
337
|
+
* Asynchronous method to fetch and cache a query, resolving with the data or throwing with
|
|
338
|
+
* the error.
|
|
339
|
+
*
|
|
340
|
+
* If the query already exists in the cache and its data is not stale (per the given
|
|
341
|
+
* `staleTime`), the cached data is returned without fetching. Otherwise, the query is fetched
|
|
342
|
+
* and the promise resolves once the fetch settles. If a `select` function is provided, it is
|
|
343
|
+
* applied to the data in both cases (cached or freshly fetched) before it is returned.
|
|
344
|
+
*
|
|
345
|
+
* Unlike a reactive observer, retries are disabled by default here (`retry: false`) unless
|
|
346
|
+
* explicitly configured, since there is no component to catch a thrown error and retry through
|
|
347
|
+
* re-render.
|
|
348
|
+
*
|
|
349
|
+
* The accepted options are `QueryObserverOptions` minus the fields that only make sense for a
|
|
350
|
+
* reactive observer — `enabled`, `refetchInterval`, `refetchIntervalInBackground`,
|
|
351
|
+
* `refetchOnWindowFocus`, `refetchOnReconnect`, `refetchOnMount`, `retryOnMount`,
|
|
352
|
+
* `notifyOnChangeProps`, `throwOnError`, `suspense`, and `placeholderData` are not part of this
|
|
353
|
+
* method's options.
|
|
354
|
+
*
|
|
355
|
+
* This method replaces the deprecated `fetchQuery`, and — combined with
|
|
356
|
+
* `{ staleTime: 'static' }` — the deprecated `ensureQueryData`.
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```ts
|
|
360
|
+
* try {
|
|
361
|
+
* const data = await queryClient.query({ queryKey, queryFn, staleTime: 10000 })
|
|
362
|
+
* } catch (error) {
|
|
363
|
+
* console.log(error)
|
|
364
|
+
* }
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
159
367
|
async query(options) {
|
|
160
368
|
const defaultedOptions = this.defaultQueryOptions(options);
|
|
161
369
|
if (defaultedOptions.retry === void 0) defaultedOptions.retry = false;
|
|
@@ -180,6 +388,27 @@ var QueryClient = class {
|
|
|
180
388
|
prefetchQuery(options) {
|
|
181
389
|
return this.fetchQuery(options).then(noop).catch(noop);
|
|
182
390
|
}
|
|
391
|
+
/**
|
|
392
|
+
* Asynchronous method to fetch and cache an infinite query, resolving with an
|
|
393
|
+
* {@link InfiniteData} object or throwing with the error.
|
|
394
|
+
*
|
|
395
|
+
* Behaves like {@link QueryClient#query}, accepting the same options (minus
|
|
396
|
+
* `initialPageParam`), plus the required `initialPageParam`, and an optional `pages` /
|
|
397
|
+
* `getNextPageParam` pair used to refetch a fixed number of pages from the start.
|
|
398
|
+
*
|
|
399
|
+
* This method replaces the deprecated `fetchInfiniteQuery`, and — combined with
|
|
400
|
+
* `{ staleTime: 'static' }` — the deprecated `ensureInfiniteQueryData`.
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* ```ts
|
|
404
|
+
* try {
|
|
405
|
+
* const data = await queryClient.infiniteQuery({ queryKey, queryFn, initialPageParam: 0 })
|
|
406
|
+
* console.log(data.pages)
|
|
407
|
+
* } catch (error) {
|
|
408
|
+
* console.log(error)
|
|
409
|
+
* }
|
|
410
|
+
* ```
|
|
411
|
+
*/
|
|
183
412
|
infiniteQuery(options) {
|
|
184
413
|
options._type = "infinite";
|
|
185
414
|
return this.query(options);
|
|
@@ -204,28 +433,118 @@ var QueryClient = class {
|
|
|
204
433
|
options._type = "infinite";
|
|
205
434
|
return this.ensureQueryData(options);
|
|
206
435
|
}
|
|
436
|
+
/**
|
|
437
|
+
* Resumes mutations that were paused because there was no network connection. Does nothing
|
|
438
|
+
* (resolving immediately) if the client is currently offline.
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```ts
|
|
442
|
+
* import { QueryClient } from '@tanstack/query-core'
|
|
443
|
+
*
|
|
444
|
+
* const queryClient = new QueryClient()
|
|
445
|
+
* await queryClient.resumePausedMutations()
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
207
448
|
resumePausedMutations() {
|
|
208
449
|
if (onlineManager.isOnline()) return this.#mutationCache.resumePausedMutations();
|
|
209
450
|
return Promise.resolve();
|
|
210
451
|
}
|
|
452
|
+
/**
|
|
453
|
+
* Returns the query cache this client is connected to.
|
|
454
|
+
*
|
|
455
|
+
* @example
|
|
456
|
+
* ```ts
|
|
457
|
+
* import { QueryClient } from '@tanstack/query-core'
|
|
458
|
+
*
|
|
459
|
+
* const queryClient = new QueryClient()
|
|
460
|
+
* const queryCache = queryClient.getQueryCache()
|
|
461
|
+
* const queries = queryCache.findAll({ queryKey: ['posts'] })
|
|
462
|
+
* ```
|
|
463
|
+
*/
|
|
211
464
|
getQueryCache() {
|
|
212
465
|
return this.#queryCache;
|
|
213
466
|
}
|
|
467
|
+
/**
|
|
468
|
+
* Returns the mutation cache this client is connected to.
|
|
469
|
+
*
|
|
470
|
+
* @example
|
|
471
|
+
* ```ts
|
|
472
|
+
* import { QueryClient } from '@tanstack/query-core'
|
|
473
|
+
*
|
|
474
|
+
* const queryClient = new QueryClient()
|
|
475
|
+
* const mutationCache = queryClient.getMutationCache()
|
|
476
|
+
* const mutations = mutationCache.findAll({ status: 'pending' })
|
|
477
|
+
* ```
|
|
478
|
+
*/
|
|
214
479
|
getMutationCache() {
|
|
215
480
|
return this.#mutationCache;
|
|
216
481
|
}
|
|
482
|
+
/**
|
|
483
|
+
* Returns the default options that were set when creating the client, or via
|
|
484
|
+
* {@link QueryClient#setDefaultOptions}.
|
|
485
|
+
*
|
|
486
|
+
* @example
|
|
487
|
+
* ```ts
|
|
488
|
+
* import { QueryClient } from '@tanstack/query-core'
|
|
489
|
+
*
|
|
490
|
+
* const queryClient = new QueryClient()
|
|
491
|
+
* const defaultOptions = queryClient.getDefaultOptions()
|
|
492
|
+
* ```
|
|
493
|
+
*/
|
|
217
494
|
getDefaultOptions() {
|
|
218
495
|
return this.#defaultOptions;
|
|
219
496
|
}
|
|
497
|
+
/**
|
|
498
|
+
* Dynamically sets the default options for this client, overwriting any previously defined
|
|
499
|
+
* default options.
|
|
500
|
+
*
|
|
501
|
+
* @see {@link QueryClient#getDefaultOptions}
|
|
502
|
+
* @example
|
|
503
|
+
* ```ts
|
|
504
|
+
* import { QueryClient } from '@tanstack/query-core'
|
|
505
|
+
*
|
|
506
|
+
* const queryClient = new QueryClient()
|
|
507
|
+
* queryClient.setDefaultOptions({
|
|
508
|
+
* queries: {
|
|
509
|
+
* staleTime: Infinity,
|
|
510
|
+
* },
|
|
511
|
+
* })
|
|
512
|
+
* ```
|
|
513
|
+
*/
|
|
220
514
|
setDefaultOptions(options) {
|
|
221
515
|
this.#defaultOptions = options;
|
|
222
516
|
}
|
|
517
|
+
/**
|
|
518
|
+
* Sets default options for queries whose query key partially matches the given `queryKey`.
|
|
519
|
+
*
|
|
520
|
+
* If several registered query defaults match a given query key, they are merged together in
|
|
521
|
+
* registration order by {@link QueryClient#getQueryDefaults}, so register defaults from the
|
|
522
|
+
* most generic key to the least generic one — more specific defaults should be registered
|
|
523
|
+
* after more generic ones so they take precedence.
|
|
524
|
+
*
|
|
525
|
+
* @example
|
|
526
|
+
* ```ts
|
|
527
|
+
* queryClient.setQueryDefaults(['posts'], { queryFn: fetchPosts })
|
|
528
|
+
*
|
|
529
|
+
* await queryClient.query({ queryKey: ['posts'] })
|
|
530
|
+
* ```
|
|
531
|
+
*/
|
|
223
532
|
setQueryDefaults(queryKey, options) {
|
|
224
533
|
this.#queryDefaults.set(hashKey(queryKey), {
|
|
225
534
|
queryKey,
|
|
226
535
|
defaultOptions: options
|
|
227
536
|
});
|
|
228
537
|
}
|
|
538
|
+
/**
|
|
539
|
+
* Returns the default options registered for queries whose query key partially matches the
|
|
540
|
+
* given `queryKey`, via {@link QueryClient#setQueryDefaults}. If multiple registered defaults
|
|
541
|
+
* match, they are merged together in registration order.
|
|
542
|
+
*
|
|
543
|
+
* @example
|
|
544
|
+
* ```ts
|
|
545
|
+
* const defaultOptions = queryClient.getQueryDefaults(['posts'])
|
|
546
|
+
* ```
|
|
547
|
+
*/
|
|
229
548
|
getQueryDefaults(queryKey) {
|
|
230
549
|
const defaults = [...this.#queryDefaults.values()];
|
|
231
550
|
const result = {};
|
|
@@ -234,12 +553,33 @@ var QueryClient = class {
|
|
|
234
553
|
});
|
|
235
554
|
return result;
|
|
236
555
|
}
|
|
556
|
+
/**
|
|
557
|
+
* Sets default options for mutations whose mutation key partially matches the given
|
|
558
|
+
* `mutationKey`. As with {@link QueryClient#setQueryDefaults}, the order of registration
|
|
559
|
+
* matters when several registered defaults match the same mutation key.
|
|
560
|
+
*
|
|
561
|
+
* @see {@link QueryClient#getMutationDefaults}
|
|
562
|
+
* @example
|
|
563
|
+
* ```ts
|
|
564
|
+
* queryClient.setMutationDefaults(['addPost'], { mutationFn: addPost })
|
|
565
|
+
* ```
|
|
566
|
+
*/
|
|
237
567
|
setMutationDefaults(mutationKey, options) {
|
|
238
568
|
this.#mutationDefaults.set(hashKey(mutationKey), {
|
|
239
569
|
mutationKey,
|
|
240
570
|
defaultOptions: options
|
|
241
571
|
});
|
|
242
572
|
}
|
|
573
|
+
/**
|
|
574
|
+
* Returns the default options registered for mutations whose mutation key partially matches
|
|
575
|
+
* the given `mutationKey`, via {@link QueryClient#setMutationDefaults}. If multiple registered
|
|
576
|
+
* defaults match, they are merged together in registration order.
|
|
577
|
+
*
|
|
578
|
+
* @example
|
|
579
|
+
* ```ts
|
|
580
|
+
* const defaultOptions = queryClient.getMutationDefaults(['addPost'])
|
|
581
|
+
* ```
|
|
582
|
+
*/
|
|
243
583
|
getMutationDefaults(mutationKey) {
|
|
244
584
|
const defaults = [...this.#mutationDefaults.values()];
|
|
245
585
|
const result = {};
|
|
@@ -248,6 +588,12 @@ var QueryClient = class {
|
|
|
248
588
|
});
|
|
249
589
|
return result;
|
|
250
590
|
}
|
|
591
|
+
/**
|
|
592
|
+
* Called by framework adapters (e.g. inside `useQuery`) to resolve the options passed by the
|
|
593
|
+
* caller into their final, defaulted form: merging `queryClient.setQueryDefaults` for the
|
|
594
|
+
* given `queryKey`, then the client's own `defaultOptions.queries`, then the caller's options
|
|
595
|
+
* on top. A no-op if the options are already defaulted (`_defaulted: true`).
|
|
596
|
+
*/
|
|
251
597
|
defaultQueryOptions(options) {
|
|
252
598
|
if (options._defaulted) return options;
|
|
253
599
|
const defaultedOptions = {
|
|
@@ -263,6 +609,12 @@ var QueryClient = class {
|
|
|
263
609
|
if (defaultedOptions.queryFn === skipToken) defaultedOptions.enabled = false;
|
|
264
610
|
return defaultedOptions;
|
|
265
611
|
}
|
|
612
|
+
/**
|
|
613
|
+
* The mutation counterpart of {@link QueryClient#defaultQueryOptions}. Called by framework
|
|
614
|
+
* adapters (e.g. inside `useMutation`) to merge `queryClient.setMutationDefaults` for the
|
|
615
|
+
* given `mutationKey`, then the client's `defaultOptions.mutations`, then the caller's options
|
|
616
|
+
* on top. A no-op if the options are already defaulted (`_defaulted: true`).
|
|
617
|
+
*/
|
|
266
618
|
defaultMutationOptions(options) {
|
|
267
619
|
if (options?._defaulted) return options;
|
|
268
620
|
return {
|
|
@@ -272,6 +624,17 @@ var QueryClient = class {
|
|
|
272
624
|
_defaulted: true
|
|
273
625
|
};
|
|
274
626
|
}
|
|
627
|
+
/**
|
|
628
|
+
* Clears both the query cache and the mutation cache this client is connected to.
|
|
629
|
+
*
|
|
630
|
+
* @example
|
|
631
|
+
* ```ts
|
|
632
|
+
* import { QueryClient } from '@tanstack/query-core'
|
|
633
|
+
*
|
|
634
|
+
* const queryClient = new QueryClient()
|
|
635
|
+
* queryClient.clear()
|
|
636
|
+
* ```
|
|
637
|
+
*/
|
|
275
638
|
clear() {
|
|
276
639
|
this.#queryCache.clear();
|
|
277
640
|
this.#mutationCache.clear();
|