@tanstack/query-core 4.36.1 → 4.39.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/lib/queryCache.d.ts +14 -4
- package/build/lib/queryClient.d.ts +83 -20
- package/build/lib/tests/queryClient.types.test.d.ts +2 -0
- package/build/lib/tests/utils.d.ts +1 -0
- package/build/lib/types.d.ts +8 -0
- package/package.json +2 -9
- package/src/mutationObserver.ts +3 -1
- package/src/queryCache.ts +37 -6
- package/src/queryClient.ts +202 -37
- package/src/tests/mutations.test.tsx +8 -0
- package/src/tests/queryClient.types.test.tsx +13 -0
- package/src/tests/utils.ts +2 -0
- package/src/types.ts +20 -0
- package/build/lib/focusManager.d.ts.map +0 -1
- package/build/lib/focusManager.esm.js +0 -91
- package/build/lib/focusManager.esm.js.map +0 -1
- package/build/lib/focusManager.js +0 -96
- package/build/lib/focusManager.js.map +0 -1
- package/build/lib/focusManager.mjs +0 -91
- package/build/lib/focusManager.mjs.map +0 -1
- package/build/lib/hydration.d.ts.map +0 -1
- package/build/lib/hydration.esm.js +0 -109
- package/build/lib/hydration.esm.js.map +0 -1
- package/build/lib/hydration.js +0 -116
- package/build/lib/hydration.js.map +0 -1
- package/build/lib/hydration.mjs +0 -109
- package/build/lib/hydration.mjs.map +0 -1
- package/build/lib/index.d.ts.map +0 -1
- package/build/lib/index.esm.js +0 -15
- package/build/lib/index.esm.js.map +0 -1
- package/build/lib/index.js +0 -48
- package/build/lib/index.js.map +0 -1
- package/build/lib/index.mjs +0 -15
- package/build/lib/index.mjs.map +0 -1
- package/build/lib/infiniteQueryBehavior.d.ts.map +0 -1
- package/build/lib/infiniteQueryBehavior.esm.js +0 -146
- package/build/lib/infiniteQueryBehavior.esm.js.map +0 -1
- package/build/lib/infiniteQueryBehavior.js +0 -154
- package/build/lib/infiniteQueryBehavior.js.map +0 -1
- package/build/lib/infiniteQueryBehavior.mjs +0 -146
- package/build/lib/infiniteQueryBehavior.mjs.map +0 -1
- package/build/lib/infiniteQueryObserver.d.ts.map +0 -1
- package/build/lib/infiniteQueryObserver.esm.js +0 -85
- package/build/lib/infiniteQueryObserver.esm.js.map +0 -1
- package/build/lib/infiniteQueryObserver.js +0 -89
- package/build/lib/infiniteQueryObserver.js.map +0 -1
- package/build/lib/infiniteQueryObserver.mjs +0 -85
- package/build/lib/infiniteQueryObserver.mjs.map +0 -1
- package/build/lib/logger.d.ts.map +0 -1
- package/build/lib/logger.esm.js +0 -4
- package/build/lib/logger.esm.js.map +0 -1
- package/build/lib/logger.js +0 -8
- package/build/lib/logger.js.map +0 -1
- package/build/lib/logger.mjs +0 -4
- package/build/lib/logger.mjs.map +0 -1
- package/build/lib/logger.native.d.ts.map +0 -1
- package/build/lib/logger.native.esm.js +0 -12
- package/build/lib/logger.native.esm.js.map +0 -1
- package/build/lib/logger.native.js +0 -16
- package/build/lib/logger.native.js.map +0 -1
- package/build/lib/logger.native.mjs +0 -12
- package/build/lib/logger.native.mjs.map +0 -1
- package/build/lib/mutation.d.ts.map +0 -1
- package/build/lib/mutation.esm.js +0 -261
- package/build/lib/mutation.esm.js.map +0 -1
- package/build/lib/mutation.js +0 -266
- package/build/lib/mutation.js.map +0 -1
- package/build/lib/mutation.mjs +0 -261
- package/build/lib/mutation.mjs.map +0 -1
- package/build/lib/mutationCache.d.ts.map +0 -1
- package/build/lib/mutationCache.esm.js +0 -93
- package/build/lib/mutationCache.esm.js.map +0 -1
- package/build/lib/mutationCache.js +0 -97
- package/build/lib/mutationCache.js.map +0 -1
- package/build/lib/mutationCache.mjs +0 -93
- package/build/lib/mutationCache.mjs.map +0 -1
- package/build/lib/mutationObserver.d.ts.map +0 -1
- package/build/lib/mutationObserver.esm.js +0 -132
- package/build/lib/mutationObserver.esm.js.map +0 -1
- package/build/lib/mutationObserver.js +0 -136
- package/build/lib/mutationObserver.js.map +0 -1
- package/build/lib/mutationObserver.mjs +0 -132
- package/build/lib/mutationObserver.mjs.map +0 -1
- package/build/lib/notifyManager.d.ts.map +0 -1
- package/build/lib/notifyManager.esm.js +0 -99
- package/build/lib/notifyManager.esm.js.map +0 -1
- package/build/lib/notifyManager.js +0 -104
- package/build/lib/notifyManager.js.map +0 -1
- package/build/lib/notifyManager.mjs +0 -99
- package/build/lib/notifyManager.mjs.map +0 -1
- package/build/lib/onlineManager.d.ts.map +0 -1
- package/build/lib/onlineManager.esm.js +0 -93
- package/build/lib/onlineManager.esm.js.map +0 -1
- package/build/lib/onlineManager.js +0 -98
- package/build/lib/onlineManager.js.map +0 -1
- package/build/lib/onlineManager.mjs +0 -93
- package/build/lib/onlineManager.mjs.map +0 -1
- package/build/lib/queriesObserver.d.ts.map +0 -1
- package/build/lib/queriesObserver.esm.js +0 -168
- package/build/lib/queriesObserver.esm.js.map +0 -1
- package/build/lib/queriesObserver.js +0 -172
- package/build/lib/queriesObserver.js.map +0 -1
- package/build/lib/queriesObserver.mjs +0 -168
- package/build/lib/queriesObserver.mjs.map +0 -1
- package/build/lib/query.d.ts.map +0 -1
- package/build/lib/query.esm.js +0 -475
- package/build/lib/query.esm.js.map +0 -1
- package/build/lib/query.js +0 -479
- package/build/lib/query.js.map +0 -1
- package/build/lib/query.mjs +0 -475
- package/build/lib/query.mjs.map +0 -1
- package/build/lib/queryCache.d.ts.map +0 -1
- package/build/lib/queryCache.esm.js +0 -127
- package/build/lib/queryCache.esm.js.map +0 -1
- package/build/lib/queryCache.js +0 -131
- package/build/lib/queryCache.js.map +0 -1
- package/build/lib/queryCache.mjs +0 -127
- package/build/lib/queryCache.mjs.map +0 -1
- package/build/lib/queryClient.d.ts.map +0 -1
- package/build/lib/queryClient.esm.js +0 -356
- package/build/lib/queryClient.esm.js.map +0 -1
- package/build/lib/queryClient.js +0 -360
- package/build/lib/queryClient.js.map +0 -1
- package/build/lib/queryClient.mjs +0 -356
- package/build/lib/queryClient.mjs.map +0 -1
- package/build/lib/queryObserver.d.ts.map +0 -1
- package/build/lib/queryObserver.esm.js +0 -580
- package/build/lib/queryObserver.esm.js.map +0 -1
- package/build/lib/queryObserver.js +0 -584
- package/build/lib/queryObserver.js.map +0 -1
- package/build/lib/queryObserver.mjs +0 -580
- package/build/lib/queryObserver.mjs.map +0 -1
- package/build/lib/removable.d.ts.map +0 -1
- package/build/lib/removable.esm.js +0 -33
- package/build/lib/removable.esm.js.map +0 -1
- package/build/lib/removable.js +0 -37
- package/build/lib/removable.js.map +0 -1
- package/build/lib/removable.mjs +0 -33
- package/build/lib/removable.mjs.map +0 -1
- package/build/lib/retryer.d.ts.map +0 -1
- package/build/lib/retryer.esm.js +0 -167
- package/build/lib/retryer.esm.js.map +0 -1
- package/build/lib/retryer.js +0 -174
- package/build/lib/retryer.js.map +0 -1
- package/build/lib/retryer.mjs +0 -167
- package/build/lib/retryer.mjs.map +0 -1
- package/build/lib/subscribable.d.ts.map +0 -1
- package/build/lib/subscribable.esm.js +0 -32
- package/build/lib/subscribable.esm.js.map +0 -1
- package/build/lib/subscribable.js +0 -36
- package/build/lib/subscribable.js.map +0 -1
- package/build/lib/subscribable.mjs +0 -32
- package/build/lib/subscribable.mjs.map +0 -1
- package/build/lib/tests/focusManager.test.d.ts.map +0 -1
- package/build/lib/tests/hydration.test.d.ts.map +0 -1
- package/build/lib/tests/infiniteQueryBehavior.test.d.ts.map +0 -1
- package/build/lib/tests/infiniteQueryObserver.test.d.ts.map +0 -1
- package/build/lib/tests/mutationCache.test.d.ts.map +0 -1
- package/build/lib/tests/mutationObserver.test.d.ts.map +0 -1
- package/build/lib/tests/mutations.test.d.ts.map +0 -1
- package/build/lib/tests/notifyManager.test.d.ts.map +0 -1
- package/build/lib/tests/onlineManager.test.d.ts.map +0 -1
- package/build/lib/tests/queriesObserver.test.d.ts.map +0 -1
- package/build/lib/tests/query.test.d.ts.map +0 -1
- package/build/lib/tests/queryCache.test.d.ts.map +0 -1
- package/build/lib/tests/queryClient.test.d.ts.map +0 -1
- package/build/lib/tests/queryObserver.test.d.ts.map +0 -1
- package/build/lib/tests/utils.d.ts.map +0 -1
- package/build/lib/tests/utils.test.d.ts.map +0 -1
- package/build/lib/types.d.ts.map +0 -1
- package/build/lib/utils.d.ts.map +0 -1
- package/build/lib/utils.esm.js +0 -320
- package/build/lib/utils.esm.js.map +0 -1
- package/build/lib/utils.js +0 -350
- package/build/lib/utils.js.map +0 -1
- package/build/lib/utils.mjs +0 -320
- package/build/lib/utils.mjs.map +0 -1
- package/build/umd/index.development.js +0 -3307
- package/build/umd/index.development.js.map +0 -1
- package/build/umd/index.production.js +0 -2
- package/build/umd/index.production.js.map +0 -1
|
@@ -1,580 +0,0 @@
|
|
|
1
|
-
import { shallowEqualObjects, noop, isServer, isValidTimeout, timeUntilStale, replaceData } from './utils.mjs';
|
|
2
|
-
import { notifyManager } from './notifyManager.mjs';
|
|
3
|
-
import { focusManager } from './focusManager.mjs';
|
|
4
|
-
import { Subscribable } from './subscribable.mjs';
|
|
5
|
-
import { canFetch, isCancelledError } from './retryer.mjs';
|
|
6
|
-
|
|
7
|
-
class QueryObserver extends Subscribable {
|
|
8
|
-
constructor(client, options) {
|
|
9
|
-
super();
|
|
10
|
-
this.client = client;
|
|
11
|
-
this.options = options;
|
|
12
|
-
this.trackedProps = new Set();
|
|
13
|
-
this.selectError = null;
|
|
14
|
-
this.bindMethods();
|
|
15
|
-
this.setOptions(options);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
bindMethods() {
|
|
19
|
-
this.remove = this.remove.bind(this);
|
|
20
|
-
this.refetch = this.refetch.bind(this);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
onSubscribe() {
|
|
24
|
-
if (this.listeners.size === 1) {
|
|
25
|
-
this.currentQuery.addObserver(this);
|
|
26
|
-
|
|
27
|
-
if (shouldFetchOnMount(this.currentQuery, this.options)) {
|
|
28
|
-
this.executeFetch();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
this.updateTimers();
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
onUnsubscribe() {
|
|
36
|
-
if (!this.hasListeners()) {
|
|
37
|
-
this.destroy();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
shouldFetchOnReconnect() {
|
|
42
|
-
return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnReconnect);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
shouldFetchOnWindowFocus() {
|
|
46
|
-
return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnWindowFocus);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
destroy() {
|
|
50
|
-
this.listeners = new Set();
|
|
51
|
-
this.clearStaleTimeout();
|
|
52
|
-
this.clearRefetchInterval();
|
|
53
|
-
this.currentQuery.removeObserver(this);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
setOptions(options, notifyOptions) {
|
|
57
|
-
const prevOptions = this.options;
|
|
58
|
-
const prevQuery = this.currentQuery;
|
|
59
|
-
this.options = this.client.defaultQueryOptions(options);
|
|
60
|
-
|
|
61
|
-
if (process.env.NODE_ENV !== 'production' && typeof (options == null ? void 0 : options.isDataEqual) !== 'undefined') {
|
|
62
|
-
this.client.getLogger().error("The isDataEqual option has been deprecated and will be removed in the next major version. You can achieve the same functionality by passing a function as the structuralSharing option");
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (!shallowEqualObjects(prevOptions, this.options)) {
|
|
66
|
-
this.client.getQueryCache().notify({
|
|
67
|
-
type: 'observerOptionsUpdated',
|
|
68
|
-
query: this.currentQuery,
|
|
69
|
-
observer: this
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (typeof this.options.enabled !== 'undefined' && typeof this.options.enabled !== 'boolean') {
|
|
74
|
-
throw new Error('Expected enabled to be a boolean');
|
|
75
|
-
} // Keep previous query key if the user does not supply one
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (!this.options.queryKey) {
|
|
79
|
-
this.options.queryKey = prevOptions.queryKey;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
this.updateQuery();
|
|
83
|
-
const mounted = this.hasListeners(); // Fetch if there are subscribers
|
|
84
|
-
|
|
85
|
-
if (mounted && shouldFetchOptionally(this.currentQuery, prevQuery, this.options, prevOptions)) {
|
|
86
|
-
this.executeFetch();
|
|
87
|
-
} // Update result
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
this.updateResult(notifyOptions); // Update stale interval if needed
|
|
91
|
-
|
|
92
|
-
if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || this.options.staleTime !== prevOptions.staleTime)) {
|
|
93
|
-
this.updateStaleTimeout();
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const nextRefetchInterval = this.computeRefetchInterval(); // Update refetch interval if needed
|
|
97
|
-
|
|
98
|
-
if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || nextRefetchInterval !== this.currentRefetchInterval)) {
|
|
99
|
-
this.updateRefetchInterval(nextRefetchInterval);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
getOptimisticResult(options) {
|
|
104
|
-
const query = this.client.getQueryCache().build(this.client, options);
|
|
105
|
-
const result = this.createResult(query, options);
|
|
106
|
-
|
|
107
|
-
if (shouldAssignObserverCurrentProperties(this, result, options)) {
|
|
108
|
-
// this assigns the optimistic result to the current Observer
|
|
109
|
-
// because if the query function changes, useQuery will be performing
|
|
110
|
-
// an effect where it would fetch again.
|
|
111
|
-
// When the fetch finishes, we perform a deep data cloning in order
|
|
112
|
-
// to reuse objects references. This deep data clone is performed against
|
|
113
|
-
// the `observer.currentResult.data` property
|
|
114
|
-
// When QueryKey changes, we refresh the query and get new `optimistic`
|
|
115
|
-
// result, while we leave the `observer.currentResult`, so when new data
|
|
116
|
-
// arrives, it finds the old `observer.currentResult` which is related
|
|
117
|
-
// to the old QueryKey. Which means that currentResult and selectData are
|
|
118
|
-
// out of sync already.
|
|
119
|
-
// To solve this, we move the cursor of the currentResult everytime
|
|
120
|
-
// an observer reads an optimistic value.
|
|
121
|
-
// When keeping the previous data, the result doesn't change until new
|
|
122
|
-
// data arrives.
|
|
123
|
-
this.currentResult = result;
|
|
124
|
-
this.currentResultOptions = this.options;
|
|
125
|
-
this.currentResultState = this.currentQuery.state;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return result;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
getCurrentResult() {
|
|
132
|
-
return this.currentResult;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
trackResult(result) {
|
|
136
|
-
const trackedResult = {};
|
|
137
|
-
Object.keys(result).forEach(key => {
|
|
138
|
-
Object.defineProperty(trackedResult, key, {
|
|
139
|
-
configurable: false,
|
|
140
|
-
enumerable: true,
|
|
141
|
-
get: () => {
|
|
142
|
-
this.trackedProps.add(key);
|
|
143
|
-
return result[key];
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
return trackedResult;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
getCurrentQuery() {
|
|
151
|
-
return this.currentQuery;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
remove() {
|
|
155
|
-
this.client.getQueryCache().remove(this.currentQuery);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
refetch({
|
|
159
|
-
refetchPage,
|
|
160
|
-
...options
|
|
161
|
-
} = {}) {
|
|
162
|
-
return this.fetch({ ...options,
|
|
163
|
-
meta: {
|
|
164
|
-
refetchPage
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
fetchOptimistic(options) {
|
|
170
|
-
const defaultedOptions = this.client.defaultQueryOptions(options);
|
|
171
|
-
const query = this.client.getQueryCache().build(this.client, defaultedOptions);
|
|
172
|
-
query.isFetchingOptimistic = true;
|
|
173
|
-
return query.fetch().then(() => this.createResult(query, defaultedOptions));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
fetch(fetchOptions) {
|
|
177
|
-
var _fetchOptions$cancelR;
|
|
178
|
-
|
|
179
|
-
return this.executeFetch({ ...fetchOptions,
|
|
180
|
-
cancelRefetch: (_fetchOptions$cancelR = fetchOptions.cancelRefetch) != null ? _fetchOptions$cancelR : true
|
|
181
|
-
}).then(() => {
|
|
182
|
-
this.updateResult();
|
|
183
|
-
return this.currentResult;
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
executeFetch(fetchOptions) {
|
|
188
|
-
// Make sure we reference the latest query as the current one might have been removed
|
|
189
|
-
this.updateQuery(); // Fetch
|
|
190
|
-
|
|
191
|
-
let promise = this.currentQuery.fetch(this.options, fetchOptions);
|
|
192
|
-
|
|
193
|
-
if (!(fetchOptions != null && fetchOptions.throwOnError)) {
|
|
194
|
-
promise = promise.catch(noop);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return promise;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
updateStaleTimeout() {
|
|
201
|
-
this.clearStaleTimeout();
|
|
202
|
-
|
|
203
|
-
if (isServer || this.currentResult.isStale || !isValidTimeout(this.options.staleTime)) {
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const time = timeUntilStale(this.currentResult.dataUpdatedAt, this.options.staleTime); // The timeout is sometimes triggered 1 ms before the stale time expiration.
|
|
208
|
-
// To mitigate this issue we always add 1 ms to the timeout.
|
|
209
|
-
|
|
210
|
-
const timeout = time + 1;
|
|
211
|
-
this.staleTimeoutId = setTimeout(() => {
|
|
212
|
-
if (!this.currentResult.isStale) {
|
|
213
|
-
this.updateResult();
|
|
214
|
-
}
|
|
215
|
-
}, timeout);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
computeRefetchInterval() {
|
|
219
|
-
var _this$options$refetch;
|
|
220
|
-
|
|
221
|
-
return typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.currentResult.data, this.currentQuery) : (_this$options$refetch = this.options.refetchInterval) != null ? _this$options$refetch : false;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
updateRefetchInterval(nextInterval) {
|
|
225
|
-
this.clearRefetchInterval();
|
|
226
|
-
this.currentRefetchInterval = nextInterval;
|
|
227
|
-
|
|
228
|
-
if (isServer || this.options.enabled === false || !isValidTimeout(this.currentRefetchInterval) || this.currentRefetchInterval === 0) {
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
this.refetchIntervalId = setInterval(() => {
|
|
233
|
-
if (this.options.refetchIntervalInBackground || focusManager.isFocused()) {
|
|
234
|
-
this.executeFetch();
|
|
235
|
-
}
|
|
236
|
-
}, this.currentRefetchInterval);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
updateTimers() {
|
|
240
|
-
this.updateStaleTimeout();
|
|
241
|
-
this.updateRefetchInterval(this.computeRefetchInterval());
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
clearStaleTimeout() {
|
|
245
|
-
if (this.staleTimeoutId) {
|
|
246
|
-
clearTimeout(this.staleTimeoutId);
|
|
247
|
-
this.staleTimeoutId = undefined;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
clearRefetchInterval() {
|
|
252
|
-
if (this.refetchIntervalId) {
|
|
253
|
-
clearInterval(this.refetchIntervalId);
|
|
254
|
-
this.refetchIntervalId = undefined;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
createResult(query, options) {
|
|
259
|
-
const prevQuery = this.currentQuery;
|
|
260
|
-
const prevOptions = this.options;
|
|
261
|
-
const prevResult = this.currentResult;
|
|
262
|
-
const prevResultState = this.currentResultState;
|
|
263
|
-
const prevResultOptions = this.currentResultOptions;
|
|
264
|
-
const queryChange = query !== prevQuery;
|
|
265
|
-
const queryInitialState = queryChange ? query.state : this.currentQueryInitialState;
|
|
266
|
-
const prevQueryResult = queryChange ? this.currentResult : this.previousQueryResult;
|
|
267
|
-
const {
|
|
268
|
-
state
|
|
269
|
-
} = query;
|
|
270
|
-
let {
|
|
271
|
-
dataUpdatedAt,
|
|
272
|
-
error,
|
|
273
|
-
errorUpdatedAt,
|
|
274
|
-
fetchStatus,
|
|
275
|
-
status
|
|
276
|
-
} = state;
|
|
277
|
-
let isPreviousData = false;
|
|
278
|
-
let isPlaceholderData = false;
|
|
279
|
-
let data; // Optimistically set result in fetching state if needed
|
|
280
|
-
|
|
281
|
-
if (options._optimisticResults) {
|
|
282
|
-
const mounted = this.hasListeners();
|
|
283
|
-
const fetchOnMount = !mounted && shouldFetchOnMount(query, options);
|
|
284
|
-
const fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions);
|
|
285
|
-
|
|
286
|
-
if (fetchOnMount || fetchOptionally) {
|
|
287
|
-
fetchStatus = canFetch(query.options.networkMode) ? 'fetching' : 'paused';
|
|
288
|
-
|
|
289
|
-
if (!dataUpdatedAt) {
|
|
290
|
-
status = 'loading';
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
if (options._optimisticResults === 'isRestoring') {
|
|
295
|
-
fetchStatus = 'idle';
|
|
296
|
-
}
|
|
297
|
-
} // Keep previous data if needed
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
if (options.keepPreviousData && !state.dataUpdatedAt && prevQueryResult != null && prevQueryResult.isSuccess && status !== 'error') {
|
|
301
|
-
data = prevQueryResult.data;
|
|
302
|
-
dataUpdatedAt = prevQueryResult.dataUpdatedAt;
|
|
303
|
-
status = prevQueryResult.status;
|
|
304
|
-
isPreviousData = true;
|
|
305
|
-
} // Select data if needed
|
|
306
|
-
else if (options.select && typeof state.data !== 'undefined') {
|
|
307
|
-
// Memoize select result
|
|
308
|
-
if (prevResult && state.data === (prevResultState == null ? void 0 : prevResultState.data) && options.select === this.selectFn) {
|
|
309
|
-
data = this.selectResult;
|
|
310
|
-
} else {
|
|
311
|
-
try {
|
|
312
|
-
this.selectFn = options.select;
|
|
313
|
-
data = options.select(state.data);
|
|
314
|
-
data = replaceData(prevResult == null ? void 0 : prevResult.data, data, options);
|
|
315
|
-
this.selectResult = data;
|
|
316
|
-
this.selectError = null;
|
|
317
|
-
} catch (selectError) {
|
|
318
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
319
|
-
this.client.getLogger().error(selectError);
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
this.selectError = selectError;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
} // Use query data
|
|
326
|
-
else {
|
|
327
|
-
data = state.data;
|
|
328
|
-
} // Show placeholder data if needed
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
if (typeof options.placeholderData !== 'undefined' && typeof data === 'undefined' && status === 'loading') {
|
|
332
|
-
let placeholderData; // Memoize placeholder data
|
|
333
|
-
|
|
334
|
-
if (prevResult != null && prevResult.isPlaceholderData && options.placeholderData === (prevResultOptions == null ? void 0 : prevResultOptions.placeholderData)) {
|
|
335
|
-
placeholderData = prevResult.data;
|
|
336
|
-
} else {
|
|
337
|
-
placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData() : options.placeholderData;
|
|
338
|
-
|
|
339
|
-
if (options.select && typeof placeholderData !== 'undefined') {
|
|
340
|
-
try {
|
|
341
|
-
placeholderData = options.select(placeholderData);
|
|
342
|
-
this.selectError = null;
|
|
343
|
-
} catch (selectError) {
|
|
344
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
345
|
-
this.client.getLogger().error(selectError);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
this.selectError = selectError;
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
if (typeof placeholderData !== 'undefined') {
|
|
354
|
-
status = 'success';
|
|
355
|
-
data = replaceData(prevResult == null ? void 0 : prevResult.data, placeholderData, options);
|
|
356
|
-
isPlaceholderData = true;
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
if (this.selectError) {
|
|
361
|
-
error = this.selectError;
|
|
362
|
-
data = this.selectResult;
|
|
363
|
-
errorUpdatedAt = Date.now();
|
|
364
|
-
status = 'error';
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
const isFetching = fetchStatus === 'fetching';
|
|
368
|
-
const isLoading = status === 'loading';
|
|
369
|
-
const isError = status === 'error';
|
|
370
|
-
const result = {
|
|
371
|
-
status,
|
|
372
|
-
fetchStatus,
|
|
373
|
-
isLoading,
|
|
374
|
-
isSuccess: status === 'success',
|
|
375
|
-
isError,
|
|
376
|
-
isInitialLoading: isLoading && isFetching,
|
|
377
|
-
data,
|
|
378
|
-
dataUpdatedAt,
|
|
379
|
-
error,
|
|
380
|
-
errorUpdatedAt,
|
|
381
|
-
failureCount: state.fetchFailureCount,
|
|
382
|
-
failureReason: state.fetchFailureReason,
|
|
383
|
-
errorUpdateCount: state.errorUpdateCount,
|
|
384
|
-
isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,
|
|
385
|
-
isFetchedAfterMount: state.dataUpdateCount > queryInitialState.dataUpdateCount || state.errorUpdateCount > queryInitialState.errorUpdateCount,
|
|
386
|
-
isFetching,
|
|
387
|
-
isRefetching: isFetching && !isLoading,
|
|
388
|
-
isLoadingError: isError && state.dataUpdatedAt === 0,
|
|
389
|
-
isPaused: fetchStatus === 'paused',
|
|
390
|
-
isPlaceholderData,
|
|
391
|
-
isPreviousData,
|
|
392
|
-
isRefetchError: isError && state.dataUpdatedAt !== 0,
|
|
393
|
-
isStale: isStale(query, options),
|
|
394
|
-
refetch: this.refetch,
|
|
395
|
-
remove: this.remove
|
|
396
|
-
};
|
|
397
|
-
return result;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
updateResult(notifyOptions) {
|
|
401
|
-
const prevResult = this.currentResult;
|
|
402
|
-
const nextResult = this.createResult(this.currentQuery, this.options);
|
|
403
|
-
this.currentResultState = this.currentQuery.state;
|
|
404
|
-
this.currentResultOptions = this.options; // Only notify and update result if something has changed
|
|
405
|
-
|
|
406
|
-
if (shallowEqualObjects(nextResult, prevResult)) {
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
this.currentResult = nextResult; // Determine which callbacks to trigger
|
|
411
|
-
|
|
412
|
-
const defaultNotifyOptions = {
|
|
413
|
-
cache: true
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
const shouldNotifyListeners = () => {
|
|
417
|
-
if (!prevResult) {
|
|
418
|
-
return true;
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
const {
|
|
422
|
-
notifyOnChangeProps
|
|
423
|
-
} = this.options;
|
|
424
|
-
const notifyOnChangePropsValue = typeof notifyOnChangeProps === 'function' ? notifyOnChangeProps() : notifyOnChangeProps;
|
|
425
|
-
|
|
426
|
-
if (notifyOnChangePropsValue === 'all' || !notifyOnChangePropsValue && !this.trackedProps.size) {
|
|
427
|
-
return true;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
const includedProps = new Set(notifyOnChangePropsValue != null ? notifyOnChangePropsValue : this.trackedProps);
|
|
431
|
-
|
|
432
|
-
if (this.options.useErrorBoundary) {
|
|
433
|
-
includedProps.add('error');
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
return Object.keys(this.currentResult).some(key => {
|
|
437
|
-
const typedKey = key;
|
|
438
|
-
const changed = this.currentResult[typedKey] !== prevResult[typedKey];
|
|
439
|
-
return changed && includedProps.has(typedKey);
|
|
440
|
-
});
|
|
441
|
-
};
|
|
442
|
-
|
|
443
|
-
if ((notifyOptions == null ? void 0 : notifyOptions.listeners) !== false && shouldNotifyListeners()) {
|
|
444
|
-
defaultNotifyOptions.listeners = true;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
this.notify({ ...defaultNotifyOptions,
|
|
448
|
-
...notifyOptions
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
updateQuery() {
|
|
453
|
-
const query = this.client.getQueryCache().build(this.client, this.options);
|
|
454
|
-
|
|
455
|
-
if (query === this.currentQuery) {
|
|
456
|
-
return;
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
const prevQuery = this.currentQuery;
|
|
460
|
-
this.currentQuery = query;
|
|
461
|
-
this.currentQueryInitialState = query.state;
|
|
462
|
-
this.previousQueryResult = this.currentResult;
|
|
463
|
-
|
|
464
|
-
if (this.hasListeners()) {
|
|
465
|
-
prevQuery == null ? void 0 : prevQuery.removeObserver(this);
|
|
466
|
-
query.addObserver(this);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
onQueryUpdate(action) {
|
|
471
|
-
const notifyOptions = {};
|
|
472
|
-
|
|
473
|
-
if (action.type === 'success') {
|
|
474
|
-
notifyOptions.onSuccess = !action.manual;
|
|
475
|
-
} else if (action.type === 'error' && !isCancelledError(action.error)) {
|
|
476
|
-
notifyOptions.onError = true;
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
this.updateResult(notifyOptions);
|
|
480
|
-
|
|
481
|
-
if (this.hasListeners()) {
|
|
482
|
-
this.updateTimers();
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
notify(notifyOptions) {
|
|
487
|
-
notifyManager.batch(() => {
|
|
488
|
-
// First trigger the configuration callbacks
|
|
489
|
-
if (notifyOptions.onSuccess) {
|
|
490
|
-
var _this$options$onSucce, _this$options, _this$options$onSettl, _this$options2;
|
|
491
|
-
|
|
492
|
-
(_this$options$onSucce = (_this$options = this.options).onSuccess) == null ? void 0 : _this$options$onSucce.call(_this$options, this.currentResult.data);
|
|
493
|
-
(_this$options$onSettl = (_this$options2 = this.options).onSettled) == null ? void 0 : _this$options$onSettl.call(_this$options2, this.currentResult.data, null);
|
|
494
|
-
} else if (notifyOptions.onError) {
|
|
495
|
-
var _this$options$onError, _this$options3, _this$options$onSettl2, _this$options4;
|
|
496
|
-
|
|
497
|
-
(_this$options$onError = (_this$options3 = this.options).onError) == null ? void 0 : _this$options$onError.call(_this$options3, this.currentResult.error);
|
|
498
|
-
(_this$options$onSettl2 = (_this$options4 = this.options).onSettled) == null ? void 0 : _this$options$onSettl2.call(_this$options4, undefined, this.currentResult.error);
|
|
499
|
-
} // Then trigger the listeners
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
if (notifyOptions.listeners) {
|
|
503
|
-
this.listeners.forEach(({
|
|
504
|
-
listener
|
|
505
|
-
}) => {
|
|
506
|
-
listener(this.currentResult);
|
|
507
|
-
});
|
|
508
|
-
} // Then the cache listeners
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
if (notifyOptions.cache) {
|
|
512
|
-
this.client.getQueryCache().notify({
|
|
513
|
-
query: this.currentQuery,
|
|
514
|
-
type: 'observerResultsUpdated'
|
|
515
|
-
});
|
|
516
|
-
}
|
|
517
|
-
});
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
function shouldLoadOnMount(query, options) {
|
|
523
|
-
return options.enabled !== false && !query.state.dataUpdatedAt && !(query.state.status === 'error' && options.retryOnMount === false);
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
function shouldFetchOnMount(query, options) {
|
|
527
|
-
return shouldLoadOnMount(query, options) || query.state.dataUpdatedAt > 0 && shouldFetchOn(query, options, options.refetchOnMount);
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
function shouldFetchOn(query, options, field) {
|
|
531
|
-
if (options.enabled !== false) {
|
|
532
|
-
const value = typeof field === 'function' ? field(query) : field;
|
|
533
|
-
return value === 'always' || value !== false && isStale(query, options);
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
return false;
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
function shouldFetchOptionally(query, prevQuery, options, prevOptions) {
|
|
540
|
-
return options.enabled !== false && (query !== prevQuery || prevOptions.enabled === false) && (!options.suspense || query.state.status !== 'error') && isStale(query, options);
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
function isStale(query, options) {
|
|
544
|
-
return query.isStaleByTime(options.staleTime);
|
|
545
|
-
} // this function would decide if we will update the observer's 'current'
|
|
546
|
-
// properties after an optimistic reading via getOptimisticResult
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
function shouldAssignObserverCurrentProperties(observer, optimisticResult, options) {
|
|
550
|
-
// it is important to keep this condition like this for three reasons:
|
|
551
|
-
// 1. It will get removed in the v5
|
|
552
|
-
// 2. it reads: don't update the properties if we want to keep the previous
|
|
553
|
-
// data.
|
|
554
|
-
// 3. The opposite condition (!options.keepPreviousData) would fallthrough
|
|
555
|
-
// and will result in a bad decision
|
|
556
|
-
if (options.keepPreviousData) {
|
|
557
|
-
return false;
|
|
558
|
-
} // this means we want to put some placeholder data when pending and queryKey
|
|
559
|
-
// changed.
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
if (options.placeholderData !== undefined) {
|
|
563
|
-
// re-assign properties only if current data is placeholder data
|
|
564
|
-
// which means that data did not arrive yet, so, if there is some cached data
|
|
565
|
-
// we need to "prepare" to receive it
|
|
566
|
-
return optimisticResult.isPlaceholderData;
|
|
567
|
-
} // if the newly created result isn't what the observer is holding as current,
|
|
568
|
-
// then we'll need to update the properties as well
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {
|
|
572
|
-
return true;
|
|
573
|
-
} // basically, just keep previous properties if nothing changed
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
return false;
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
export { QueryObserver };
|
|
580
|
-
//# sourceMappingURL=queryObserver.mjs.map
|