@tanstack/query-core 4.39.0 → 4.39.1
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/focusManager.esm.js +91 -0
- package/build/lib/focusManager.esm.js.map +1 -0
- package/build/lib/focusManager.js +96 -0
- package/build/lib/focusManager.js.map +1 -0
- package/build/lib/focusManager.mjs +91 -0
- package/build/lib/focusManager.mjs.map +1 -0
- package/build/lib/hydration.esm.js +109 -0
- package/build/lib/hydration.esm.js.map +1 -0
- package/build/lib/hydration.js +116 -0
- package/build/lib/hydration.js.map +1 -0
- package/build/lib/hydration.mjs +109 -0
- package/build/lib/hydration.mjs.map +1 -0
- package/build/lib/index.esm.js +15 -0
- package/build/lib/index.esm.js.map +1 -0
- package/build/lib/index.js +48 -0
- package/build/lib/index.js.map +1 -0
- package/build/lib/index.mjs +15 -0
- package/build/lib/index.mjs.map +1 -0
- package/build/lib/infiniteQueryBehavior.esm.js +146 -0
- package/build/lib/infiniteQueryBehavior.esm.js.map +1 -0
- package/build/lib/infiniteQueryBehavior.js +154 -0
- package/build/lib/infiniteQueryBehavior.js.map +1 -0
- package/build/lib/infiniteQueryBehavior.mjs +146 -0
- package/build/lib/infiniteQueryBehavior.mjs.map +1 -0
- package/build/lib/infiniteQueryObserver.esm.js +85 -0
- package/build/lib/infiniteQueryObserver.esm.js.map +1 -0
- package/build/lib/infiniteQueryObserver.js +89 -0
- package/build/lib/infiniteQueryObserver.js.map +1 -0
- package/build/lib/infiniteQueryObserver.mjs +85 -0
- package/build/lib/infiniteQueryObserver.mjs.map +1 -0
- package/build/lib/logger.esm.js +4 -0
- package/build/lib/logger.esm.js.map +1 -0
- package/build/lib/logger.js +8 -0
- package/build/lib/logger.js.map +1 -0
- package/build/lib/logger.mjs +4 -0
- package/build/lib/logger.mjs.map +1 -0
- package/build/lib/logger.native.esm.js +12 -0
- package/build/lib/logger.native.esm.js.map +1 -0
- package/build/lib/logger.native.js +16 -0
- package/build/lib/logger.native.js.map +1 -0
- package/build/lib/logger.native.mjs +12 -0
- package/build/lib/logger.native.mjs.map +1 -0
- package/build/lib/mutation.esm.js +261 -0
- package/build/lib/mutation.esm.js.map +1 -0
- package/build/lib/mutation.js +266 -0
- package/build/lib/mutation.js.map +1 -0
- package/build/lib/mutation.mjs +261 -0
- package/build/lib/mutation.mjs.map +1 -0
- package/build/lib/mutationCache.esm.js +93 -0
- package/build/lib/mutationCache.esm.js.map +1 -0
- package/build/lib/mutationCache.js +97 -0
- package/build/lib/mutationCache.js.map +1 -0
- package/build/lib/mutationCache.mjs +93 -0
- package/build/lib/mutationCache.mjs.map +1 -0
- package/build/lib/mutationObserver.esm.js +134 -0
- package/build/lib/mutationObserver.esm.js.map +1 -0
- package/build/lib/mutationObserver.js +138 -0
- package/build/lib/mutationObserver.js.map +1 -0
- package/build/lib/mutationObserver.mjs +134 -0
- package/build/lib/mutationObserver.mjs.map +1 -0
- package/build/lib/notifyManager.esm.js +99 -0
- package/build/lib/notifyManager.esm.js.map +1 -0
- package/build/lib/notifyManager.js +104 -0
- package/build/lib/notifyManager.js.map +1 -0
- package/build/lib/notifyManager.mjs +99 -0
- package/build/lib/notifyManager.mjs.map +1 -0
- package/build/lib/onlineManager.esm.js +93 -0
- package/build/lib/onlineManager.esm.js.map +1 -0
- package/build/lib/onlineManager.js +98 -0
- package/build/lib/onlineManager.js.map +1 -0
- package/build/lib/onlineManager.mjs +93 -0
- package/build/lib/onlineManager.mjs.map +1 -0
- package/build/lib/queriesObserver.esm.js +168 -0
- package/build/lib/queriesObserver.esm.js.map +1 -0
- package/build/lib/queriesObserver.js +172 -0
- package/build/lib/queriesObserver.js.map +1 -0
- package/build/lib/queriesObserver.mjs +168 -0
- package/build/lib/queriesObserver.mjs.map +1 -0
- package/build/lib/query.esm.js +475 -0
- package/build/lib/query.esm.js.map +1 -0
- package/build/lib/query.js +479 -0
- package/build/lib/query.js.map +1 -0
- package/build/lib/query.mjs +475 -0
- package/build/lib/query.mjs.map +1 -0
- package/build/lib/queryCache.esm.js +133 -0
- package/build/lib/queryCache.esm.js.map +1 -0
- package/build/lib/queryCache.js +137 -0
- package/build/lib/queryCache.js.map +1 -0
- package/build/lib/queryCache.mjs +133 -0
- package/build/lib/queryCache.mjs.map +1 -0
- package/build/lib/queryClient.esm.js +402 -0
- package/build/lib/queryClient.esm.js.map +1 -0
- package/build/lib/queryClient.js +406 -0
- package/build/lib/queryClient.js.map +1 -0
- package/build/lib/queryClient.mjs +402 -0
- package/build/lib/queryClient.mjs.map +1 -0
- package/build/lib/queryObserver.esm.js +580 -0
- package/build/lib/queryObserver.esm.js.map +1 -0
- package/build/lib/queryObserver.js +584 -0
- package/build/lib/queryObserver.js.map +1 -0
- package/build/lib/queryObserver.mjs +580 -0
- package/build/lib/queryObserver.mjs.map +1 -0
- package/build/lib/removable.esm.js +33 -0
- package/build/lib/removable.esm.js.map +1 -0
- package/build/lib/removable.js +37 -0
- package/build/lib/removable.js.map +1 -0
- package/build/lib/removable.mjs +33 -0
- package/build/lib/removable.mjs.map +1 -0
- package/build/lib/retryer.esm.js +167 -0
- package/build/lib/retryer.esm.js.map +1 -0
- package/build/lib/retryer.js +174 -0
- package/build/lib/retryer.js.map +1 -0
- package/build/lib/retryer.mjs +167 -0
- package/build/lib/retryer.mjs.map +1 -0
- package/build/lib/subscribable.esm.js +32 -0
- package/build/lib/subscribable.esm.js.map +1 -0
- package/build/lib/subscribable.js +36 -0
- package/build/lib/subscribable.js.map +1 -0
- package/build/lib/subscribable.mjs +32 -0
- package/build/lib/subscribable.mjs.map +1 -0
- package/build/lib/utils.esm.js +320 -0
- package/build/lib/utils.esm.js.map +1 -0
- package/build/lib/utils.js +350 -0
- package/build/lib/utils.js.map +1 -0
- package/build/lib/utils.mjs +320 -0
- package/build/lib/utils.mjs.map +1 -0
- package/build/umd/index.development.js +3361 -0
- package/build/umd/index.development.js.map +1 -0
- package/build/umd/index.production.js +2 -0
- package/build/umd/index.production.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,3361 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.QueryCore = {}));
|
|
5
|
+
})(this, (function (exports) { 'use strict';
|
|
6
|
+
|
|
7
|
+
class Subscribable {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.listeners = new Set();
|
|
10
|
+
this.subscribe = this.subscribe.bind(this);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
subscribe(listener) {
|
|
14
|
+
const identity = {
|
|
15
|
+
listener
|
|
16
|
+
};
|
|
17
|
+
this.listeners.add(identity);
|
|
18
|
+
this.onSubscribe();
|
|
19
|
+
return () => {
|
|
20
|
+
this.listeners.delete(identity);
|
|
21
|
+
this.onUnsubscribe();
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
hasListeners() {
|
|
26
|
+
return this.listeners.size > 0;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
onSubscribe() {// Do nothing
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
onUnsubscribe() {// Do nothing
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// TYPES
|
|
38
|
+
// UTILS
|
|
39
|
+
const isServer = typeof window === 'undefined' || 'Deno' in window;
|
|
40
|
+
function noop() {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
function functionalUpdate(updater, input) {
|
|
44
|
+
return typeof updater === 'function' ? updater(input) : updater;
|
|
45
|
+
}
|
|
46
|
+
function isValidTimeout(value) {
|
|
47
|
+
return typeof value === 'number' && value >= 0 && value !== Infinity;
|
|
48
|
+
}
|
|
49
|
+
function difference(array1, array2) {
|
|
50
|
+
return array1.filter(x => !array2.includes(x));
|
|
51
|
+
}
|
|
52
|
+
function replaceAt(array, index, value) {
|
|
53
|
+
const copy = array.slice(0);
|
|
54
|
+
copy[index] = value;
|
|
55
|
+
return copy;
|
|
56
|
+
}
|
|
57
|
+
function timeUntilStale(updatedAt, staleTime) {
|
|
58
|
+
return Math.max(updatedAt + (staleTime || 0) - Date.now(), 0);
|
|
59
|
+
}
|
|
60
|
+
function parseQueryArgs(arg1, arg2, arg3) {
|
|
61
|
+
if (!isQueryKey(arg1)) {
|
|
62
|
+
return arg1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (typeof arg2 === 'function') {
|
|
66
|
+
return { ...arg3,
|
|
67
|
+
queryKey: arg1,
|
|
68
|
+
queryFn: arg2
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return { ...arg2,
|
|
73
|
+
queryKey: arg1
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function parseMutationArgs(arg1, arg2, arg3) {
|
|
77
|
+
if (isQueryKey(arg1)) {
|
|
78
|
+
if (typeof arg2 === 'function') {
|
|
79
|
+
return { ...arg3,
|
|
80
|
+
mutationKey: arg1,
|
|
81
|
+
mutationFn: arg2
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return { ...arg2,
|
|
86
|
+
mutationKey: arg1
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (typeof arg1 === 'function') {
|
|
91
|
+
return { ...arg2,
|
|
92
|
+
mutationFn: arg1
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return { ...arg1
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function parseFilterArgs(arg1, arg2, arg3) {
|
|
100
|
+
return isQueryKey(arg1) ? [{ ...arg2,
|
|
101
|
+
queryKey: arg1
|
|
102
|
+
}, arg3] : [arg1 || {}, arg2];
|
|
103
|
+
}
|
|
104
|
+
function parseMutationFilterArgs(arg1, arg2, arg3) {
|
|
105
|
+
return isQueryKey(arg1) ? [{ ...arg2,
|
|
106
|
+
mutationKey: arg1
|
|
107
|
+
}, arg3] : [arg1 || {}, arg2];
|
|
108
|
+
}
|
|
109
|
+
function matchQuery(filters, query) {
|
|
110
|
+
const {
|
|
111
|
+
type = 'all',
|
|
112
|
+
exact,
|
|
113
|
+
fetchStatus,
|
|
114
|
+
predicate,
|
|
115
|
+
queryKey,
|
|
116
|
+
stale
|
|
117
|
+
} = filters;
|
|
118
|
+
|
|
119
|
+
if (isQueryKey(queryKey)) {
|
|
120
|
+
if (exact) {
|
|
121
|
+
if (query.queryHash !== hashQueryKeyByOptions(queryKey, query.options)) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
} else if (!partialMatchKey(query.queryKey, queryKey)) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (type !== 'all') {
|
|
130
|
+
const isActive = query.isActive();
|
|
131
|
+
|
|
132
|
+
if (type === 'active' && !isActive) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (type === 'inactive' && isActive) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (typeof stale === 'boolean' && query.isStale() !== stale) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (typeof fetchStatus !== 'undefined' && fetchStatus !== query.state.fetchStatus) {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (predicate && !predicate(query)) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
function matchMutation(filters, mutation) {
|
|
156
|
+
const {
|
|
157
|
+
exact,
|
|
158
|
+
fetching,
|
|
159
|
+
predicate,
|
|
160
|
+
mutationKey
|
|
161
|
+
} = filters;
|
|
162
|
+
|
|
163
|
+
if (isQueryKey(mutationKey)) {
|
|
164
|
+
if (!mutation.options.mutationKey) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (exact) {
|
|
169
|
+
if (hashQueryKey(mutation.options.mutationKey) !== hashQueryKey(mutationKey)) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
} else if (!partialMatchKey(mutation.options.mutationKey, mutationKey)) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (typeof fetching === 'boolean' && mutation.state.status === 'loading' !== fetching) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (predicate && !predicate(mutation)) {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
function hashQueryKeyByOptions(queryKey, options) {
|
|
188
|
+
const hashFn = (options == null ? void 0 : options.queryKeyHashFn) || hashQueryKey;
|
|
189
|
+
return hashFn(queryKey);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Default query keys hash function.
|
|
193
|
+
* Hashes the value into a stable hash.
|
|
194
|
+
*/
|
|
195
|
+
|
|
196
|
+
function hashQueryKey(queryKey) {
|
|
197
|
+
return JSON.stringify(queryKey, (_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
|
|
198
|
+
result[key] = val[key];
|
|
199
|
+
return result;
|
|
200
|
+
}, {}) : val);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Checks if key `b` partially matches with key `a`.
|
|
204
|
+
*/
|
|
205
|
+
|
|
206
|
+
function partialMatchKey(a, b) {
|
|
207
|
+
return partialDeepEqual(a, b);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Checks if `b` partially matches with `a`.
|
|
211
|
+
*/
|
|
212
|
+
|
|
213
|
+
function partialDeepEqual(a, b) {
|
|
214
|
+
if (a === b) {
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (typeof a !== typeof b) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
223
|
+
return !Object.keys(b).some(key => !partialDeepEqual(a[key], b[key]));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* This function returns `a` if `b` is deeply equal.
|
|
230
|
+
* If not, it will replace any deeply equal children of `b` with those of `a`.
|
|
231
|
+
* This can be used for structural sharing between JSON values for example.
|
|
232
|
+
*/
|
|
233
|
+
|
|
234
|
+
function replaceEqualDeep(a, b) {
|
|
235
|
+
if (a === b) {
|
|
236
|
+
return a;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const array = isPlainArray(a) && isPlainArray(b);
|
|
240
|
+
|
|
241
|
+
if (array || isPlainObject(a) && isPlainObject(b)) {
|
|
242
|
+
const aSize = array ? a.length : Object.keys(a).length;
|
|
243
|
+
const bItems = array ? b : Object.keys(b);
|
|
244
|
+
const bSize = bItems.length;
|
|
245
|
+
const copy = array ? [] : {};
|
|
246
|
+
let equalItems = 0;
|
|
247
|
+
|
|
248
|
+
for (let i = 0; i < bSize; i++) {
|
|
249
|
+
const key = array ? i : bItems[i];
|
|
250
|
+
copy[key] = replaceEqualDeep(a[key], b[key]);
|
|
251
|
+
|
|
252
|
+
if (copy[key] === a[key]) {
|
|
253
|
+
equalItems++;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return aSize === bSize && equalItems === aSize ? a : copy;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return b;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Shallow compare objects. Only works with objects that always have the same properties.
|
|
264
|
+
*/
|
|
265
|
+
|
|
266
|
+
function shallowEqualObjects(a, b) {
|
|
267
|
+
if (a && !b || b && !a) {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
for (const key in a) {
|
|
272
|
+
if (a[key] !== b[key]) {
|
|
273
|
+
return false;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
function isPlainArray(value) {
|
|
280
|
+
return Array.isArray(value) && value.length === Object.keys(value).length;
|
|
281
|
+
} // Copied from: https://github.com/jonschlinkert/is-plain-object
|
|
282
|
+
|
|
283
|
+
function isPlainObject(o) {
|
|
284
|
+
if (!hasObjectPrototype(o)) {
|
|
285
|
+
return false;
|
|
286
|
+
} // If has modified constructor
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
const ctor = o.constructor;
|
|
290
|
+
|
|
291
|
+
if (typeof ctor === 'undefined') {
|
|
292
|
+
return true;
|
|
293
|
+
} // If has modified prototype
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
const prot = ctor.prototype;
|
|
297
|
+
|
|
298
|
+
if (!hasObjectPrototype(prot)) {
|
|
299
|
+
return false;
|
|
300
|
+
} // If constructor does not have an Object-specific method
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
if (!prot.hasOwnProperty('isPrototypeOf')) {
|
|
304
|
+
return false;
|
|
305
|
+
} // Most likely a plain Object
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
return true;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function hasObjectPrototype(o) {
|
|
312
|
+
return Object.prototype.toString.call(o) === '[object Object]';
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function isQueryKey(value) {
|
|
316
|
+
return Array.isArray(value);
|
|
317
|
+
}
|
|
318
|
+
function isError(value) {
|
|
319
|
+
return value instanceof Error;
|
|
320
|
+
}
|
|
321
|
+
function sleep(timeout) {
|
|
322
|
+
return new Promise(resolve => {
|
|
323
|
+
setTimeout(resolve, timeout);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Schedules a microtask.
|
|
328
|
+
* This can be useful to schedule state updates after rendering.
|
|
329
|
+
*/
|
|
330
|
+
|
|
331
|
+
function scheduleMicrotask(callback) {
|
|
332
|
+
sleep(0).then(callback);
|
|
333
|
+
}
|
|
334
|
+
function getAbortController() {
|
|
335
|
+
if (typeof AbortController === 'function') {
|
|
336
|
+
return new AbortController();
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
function replaceData(prevData, data, options) {
|
|
342
|
+
// Use prev data if an isDataEqual function is defined and returns `true`
|
|
343
|
+
if (options.isDataEqual != null && options.isDataEqual(prevData, data)) {
|
|
344
|
+
return prevData;
|
|
345
|
+
} else if (typeof options.structuralSharing === 'function') {
|
|
346
|
+
return options.structuralSharing(prevData, data);
|
|
347
|
+
} else if (options.structuralSharing !== false) {
|
|
348
|
+
// Structurally share data between prev and new data if needed
|
|
349
|
+
return replaceEqualDeep(prevData, data);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return data;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
class FocusManager extends Subscribable {
|
|
356
|
+
constructor() {
|
|
357
|
+
super();
|
|
358
|
+
|
|
359
|
+
this.setup = onFocus => {
|
|
360
|
+
// addEventListener does not exist in React Native, but window does
|
|
361
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
362
|
+
if (!isServer && window.addEventListener) {
|
|
363
|
+
const listener = () => onFocus(); // Listen to visibillitychange and focus
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
window.addEventListener('visibilitychange', listener, false);
|
|
367
|
+
window.addEventListener('focus', listener, false);
|
|
368
|
+
return () => {
|
|
369
|
+
// Be sure to unsubscribe if a new handler is set
|
|
370
|
+
window.removeEventListener('visibilitychange', listener);
|
|
371
|
+
window.removeEventListener('focus', listener);
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return;
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
onSubscribe() {
|
|
380
|
+
if (!this.cleanup) {
|
|
381
|
+
this.setEventListener(this.setup);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
onUnsubscribe() {
|
|
386
|
+
if (!this.hasListeners()) {
|
|
387
|
+
var _this$cleanup;
|
|
388
|
+
|
|
389
|
+
(_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
|
|
390
|
+
this.cleanup = undefined;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
setEventListener(setup) {
|
|
395
|
+
var _this$cleanup2;
|
|
396
|
+
|
|
397
|
+
this.setup = setup;
|
|
398
|
+
(_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
|
|
399
|
+
this.cleanup = setup(focused => {
|
|
400
|
+
if (typeof focused === 'boolean') {
|
|
401
|
+
this.setFocused(focused);
|
|
402
|
+
} else {
|
|
403
|
+
this.onFocus();
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
setFocused(focused) {
|
|
409
|
+
const changed = this.focused !== focused;
|
|
410
|
+
|
|
411
|
+
if (changed) {
|
|
412
|
+
this.focused = focused;
|
|
413
|
+
this.onFocus();
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
onFocus() {
|
|
418
|
+
this.listeners.forEach(({
|
|
419
|
+
listener
|
|
420
|
+
}) => {
|
|
421
|
+
listener();
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
isFocused() {
|
|
426
|
+
if (typeof this.focused === 'boolean') {
|
|
427
|
+
return this.focused;
|
|
428
|
+
} // document global can be unavailable in react native
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
if (typeof document === 'undefined') {
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
return [undefined, 'visible', 'prerender'].includes(document.visibilityState);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
}
|
|
439
|
+
const focusManager = new FocusManager();
|
|
440
|
+
|
|
441
|
+
const onlineEvents = ['online', 'offline'];
|
|
442
|
+
class OnlineManager extends Subscribable {
|
|
443
|
+
constructor() {
|
|
444
|
+
super();
|
|
445
|
+
|
|
446
|
+
this.setup = onOnline => {
|
|
447
|
+
// addEventListener does not exist in React Native, but window does
|
|
448
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
449
|
+
if (!isServer && window.addEventListener) {
|
|
450
|
+
const listener = () => onOnline(); // Listen to online
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
onlineEvents.forEach(event => {
|
|
454
|
+
window.addEventListener(event, listener, false);
|
|
455
|
+
});
|
|
456
|
+
return () => {
|
|
457
|
+
// Be sure to unsubscribe if a new handler is set
|
|
458
|
+
onlineEvents.forEach(event => {
|
|
459
|
+
window.removeEventListener(event, listener);
|
|
460
|
+
});
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
return;
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
onSubscribe() {
|
|
469
|
+
if (!this.cleanup) {
|
|
470
|
+
this.setEventListener(this.setup);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
onUnsubscribe() {
|
|
475
|
+
if (!this.hasListeners()) {
|
|
476
|
+
var _this$cleanup;
|
|
477
|
+
|
|
478
|
+
(_this$cleanup = this.cleanup) == null ? void 0 : _this$cleanup.call(this);
|
|
479
|
+
this.cleanup = undefined;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
setEventListener(setup) {
|
|
484
|
+
var _this$cleanup2;
|
|
485
|
+
|
|
486
|
+
this.setup = setup;
|
|
487
|
+
(_this$cleanup2 = this.cleanup) == null ? void 0 : _this$cleanup2.call(this);
|
|
488
|
+
this.cleanup = setup(online => {
|
|
489
|
+
if (typeof online === 'boolean') {
|
|
490
|
+
this.setOnline(online);
|
|
491
|
+
} else {
|
|
492
|
+
this.onOnline();
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
setOnline(online) {
|
|
498
|
+
const changed = this.online !== online;
|
|
499
|
+
|
|
500
|
+
if (changed) {
|
|
501
|
+
this.online = online;
|
|
502
|
+
this.onOnline();
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
onOnline() {
|
|
507
|
+
this.listeners.forEach(({
|
|
508
|
+
listener
|
|
509
|
+
}) => {
|
|
510
|
+
listener();
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
isOnline() {
|
|
515
|
+
if (typeof this.online === 'boolean') {
|
|
516
|
+
return this.online;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
if (typeof navigator === 'undefined' || typeof navigator.onLine === 'undefined') {
|
|
520
|
+
return true;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
return navigator.onLine;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
}
|
|
527
|
+
const onlineManager = new OnlineManager();
|
|
528
|
+
|
|
529
|
+
function defaultRetryDelay(failureCount) {
|
|
530
|
+
return Math.min(1000 * 2 ** failureCount, 30000);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function canFetch(networkMode) {
|
|
534
|
+
return (networkMode != null ? networkMode : 'online') === 'online' ? onlineManager.isOnline() : true;
|
|
535
|
+
}
|
|
536
|
+
class CancelledError {
|
|
537
|
+
constructor(options) {
|
|
538
|
+
this.revert = options == null ? void 0 : options.revert;
|
|
539
|
+
this.silent = options == null ? void 0 : options.silent;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
}
|
|
543
|
+
function isCancelledError(value) {
|
|
544
|
+
return value instanceof CancelledError;
|
|
545
|
+
}
|
|
546
|
+
function createRetryer(config) {
|
|
547
|
+
let isRetryCancelled = false;
|
|
548
|
+
let failureCount = 0;
|
|
549
|
+
let isResolved = false;
|
|
550
|
+
let continueFn;
|
|
551
|
+
let promiseResolve;
|
|
552
|
+
let promiseReject;
|
|
553
|
+
const promise = new Promise((outerResolve, outerReject) => {
|
|
554
|
+
promiseResolve = outerResolve;
|
|
555
|
+
promiseReject = outerReject;
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
const cancel = cancelOptions => {
|
|
559
|
+
if (!isResolved) {
|
|
560
|
+
reject(new CancelledError(cancelOptions));
|
|
561
|
+
config.abort == null ? void 0 : config.abort();
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
const cancelRetry = () => {
|
|
566
|
+
isRetryCancelled = true;
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
const continueRetry = () => {
|
|
570
|
+
isRetryCancelled = false;
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
const shouldPause = () => !focusManager.isFocused() || config.networkMode !== 'always' && !onlineManager.isOnline();
|
|
574
|
+
|
|
575
|
+
const resolve = value => {
|
|
576
|
+
if (!isResolved) {
|
|
577
|
+
isResolved = true;
|
|
578
|
+
config.onSuccess == null ? void 0 : config.onSuccess(value);
|
|
579
|
+
continueFn == null ? void 0 : continueFn();
|
|
580
|
+
promiseResolve(value);
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
const reject = value => {
|
|
585
|
+
if (!isResolved) {
|
|
586
|
+
isResolved = true;
|
|
587
|
+
config.onError == null ? void 0 : config.onError(value);
|
|
588
|
+
continueFn == null ? void 0 : continueFn();
|
|
589
|
+
promiseReject(value);
|
|
590
|
+
}
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
const pause = () => {
|
|
594
|
+
return new Promise(continueResolve => {
|
|
595
|
+
continueFn = value => {
|
|
596
|
+
const canContinue = isResolved || !shouldPause();
|
|
597
|
+
|
|
598
|
+
if (canContinue) {
|
|
599
|
+
continueResolve(value);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
return canContinue;
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
config.onPause == null ? void 0 : config.onPause();
|
|
606
|
+
}).then(() => {
|
|
607
|
+
continueFn = undefined;
|
|
608
|
+
|
|
609
|
+
if (!isResolved) {
|
|
610
|
+
config.onContinue == null ? void 0 : config.onContinue();
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
}; // Create loop function
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
const run = () => {
|
|
617
|
+
// Do nothing if already resolved
|
|
618
|
+
if (isResolved) {
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
let promiseOrValue; // Execute query
|
|
623
|
+
|
|
624
|
+
try {
|
|
625
|
+
promiseOrValue = config.fn();
|
|
626
|
+
} catch (error) {
|
|
627
|
+
promiseOrValue = Promise.reject(error);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
Promise.resolve(promiseOrValue).then(resolve).catch(error => {
|
|
631
|
+
var _config$retry, _config$retryDelay;
|
|
632
|
+
|
|
633
|
+
// Stop if the fetch is already resolved
|
|
634
|
+
if (isResolved) {
|
|
635
|
+
return;
|
|
636
|
+
} // Do we need to retry the request?
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
const retry = (_config$retry = config.retry) != null ? _config$retry : 3;
|
|
640
|
+
const retryDelay = (_config$retryDelay = config.retryDelay) != null ? _config$retryDelay : defaultRetryDelay;
|
|
641
|
+
const delay = typeof retryDelay === 'function' ? retryDelay(failureCount, error) : retryDelay;
|
|
642
|
+
const shouldRetry = retry === true || typeof retry === 'number' && failureCount < retry || typeof retry === 'function' && retry(failureCount, error);
|
|
643
|
+
|
|
644
|
+
if (isRetryCancelled || !shouldRetry) {
|
|
645
|
+
// We are done if the query does not need to be retried
|
|
646
|
+
reject(error);
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
failureCount++; // Notify on fail
|
|
651
|
+
|
|
652
|
+
config.onFail == null ? void 0 : config.onFail(failureCount, error); // Delay
|
|
653
|
+
|
|
654
|
+
sleep(delay) // Pause if the document is not visible or when the device is offline
|
|
655
|
+
.then(() => {
|
|
656
|
+
if (shouldPause()) {
|
|
657
|
+
return pause();
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
return;
|
|
661
|
+
}).then(() => {
|
|
662
|
+
if (isRetryCancelled) {
|
|
663
|
+
reject(error);
|
|
664
|
+
} else {
|
|
665
|
+
run();
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
});
|
|
669
|
+
}; // Start loop
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
if (canFetch(config.networkMode)) {
|
|
673
|
+
run();
|
|
674
|
+
} else {
|
|
675
|
+
pause().then(run);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
return {
|
|
679
|
+
promise,
|
|
680
|
+
cancel,
|
|
681
|
+
continue: () => {
|
|
682
|
+
const didContinue = continueFn == null ? void 0 : continueFn();
|
|
683
|
+
return didContinue ? promise : Promise.resolve();
|
|
684
|
+
},
|
|
685
|
+
cancelRetry,
|
|
686
|
+
continueRetry
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
const defaultLogger = console;
|
|
691
|
+
|
|
692
|
+
function createNotifyManager() {
|
|
693
|
+
let queue = [];
|
|
694
|
+
let transactions = 0;
|
|
695
|
+
|
|
696
|
+
let notifyFn = callback => {
|
|
697
|
+
callback();
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
let batchNotifyFn = callback => {
|
|
701
|
+
callback();
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
const batch = callback => {
|
|
705
|
+
let result;
|
|
706
|
+
transactions++;
|
|
707
|
+
|
|
708
|
+
try {
|
|
709
|
+
result = callback();
|
|
710
|
+
} finally {
|
|
711
|
+
transactions--;
|
|
712
|
+
|
|
713
|
+
if (!transactions) {
|
|
714
|
+
flush();
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
return result;
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
const schedule = callback => {
|
|
722
|
+
if (transactions) {
|
|
723
|
+
queue.push(callback);
|
|
724
|
+
} else {
|
|
725
|
+
scheduleMicrotask(() => {
|
|
726
|
+
notifyFn(callback);
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
/**
|
|
731
|
+
* All calls to the wrapped function will be batched.
|
|
732
|
+
*/
|
|
733
|
+
|
|
734
|
+
|
|
735
|
+
const batchCalls = callback => {
|
|
736
|
+
return (...args) => {
|
|
737
|
+
schedule(() => {
|
|
738
|
+
callback(...args);
|
|
739
|
+
});
|
|
740
|
+
};
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
const flush = () => {
|
|
744
|
+
const originalQueue = queue;
|
|
745
|
+
queue = [];
|
|
746
|
+
|
|
747
|
+
if (originalQueue.length) {
|
|
748
|
+
scheduleMicrotask(() => {
|
|
749
|
+
batchNotifyFn(() => {
|
|
750
|
+
originalQueue.forEach(callback => {
|
|
751
|
+
notifyFn(callback);
|
|
752
|
+
});
|
|
753
|
+
});
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
/**
|
|
758
|
+
* Use this method to set a custom notify function.
|
|
759
|
+
* This can be used to for example wrap notifications with `React.act` while running tests.
|
|
760
|
+
*/
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
const setNotifyFunction = fn => {
|
|
764
|
+
notifyFn = fn;
|
|
765
|
+
};
|
|
766
|
+
/**
|
|
767
|
+
* Use this method to set a custom function to batch notifications together into a single tick.
|
|
768
|
+
* By default React Query will use the batch function provided by ReactDOM or React Native.
|
|
769
|
+
*/
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
const setBatchNotifyFunction = fn => {
|
|
773
|
+
batchNotifyFn = fn;
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
return {
|
|
777
|
+
batch,
|
|
778
|
+
batchCalls,
|
|
779
|
+
schedule,
|
|
780
|
+
setNotifyFunction,
|
|
781
|
+
setBatchNotifyFunction
|
|
782
|
+
};
|
|
783
|
+
} // SINGLETON
|
|
784
|
+
|
|
785
|
+
const notifyManager = createNotifyManager();
|
|
786
|
+
|
|
787
|
+
class Removable {
|
|
788
|
+
destroy() {
|
|
789
|
+
this.clearGcTimeout();
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
scheduleGc() {
|
|
793
|
+
this.clearGcTimeout();
|
|
794
|
+
|
|
795
|
+
if (isValidTimeout(this.cacheTime)) {
|
|
796
|
+
this.gcTimeout = setTimeout(() => {
|
|
797
|
+
this.optionalRemove();
|
|
798
|
+
}, this.cacheTime);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
updateCacheTime(newCacheTime) {
|
|
803
|
+
// Default to 5 minutes (Infinity for server-side) if no cache time is set
|
|
804
|
+
this.cacheTime = Math.max(this.cacheTime || 0, newCacheTime != null ? newCacheTime : isServer ? Infinity : 5 * 60 * 1000);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
clearGcTimeout() {
|
|
808
|
+
if (this.gcTimeout) {
|
|
809
|
+
clearTimeout(this.gcTimeout);
|
|
810
|
+
this.gcTimeout = undefined;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// CLASS
|
|
817
|
+
class Query extends Removable {
|
|
818
|
+
constructor(config) {
|
|
819
|
+
super();
|
|
820
|
+
this.abortSignalConsumed = false;
|
|
821
|
+
this.defaultOptions = config.defaultOptions;
|
|
822
|
+
this.setOptions(config.options);
|
|
823
|
+
this.observers = [];
|
|
824
|
+
this.cache = config.cache;
|
|
825
|
+
this.logger = config.logger || defaultLogger;
|
|
826
|
+
this.queryKey = config.queryKey;
|
|
827
|
+
this.queryHash = config.queryHash;
|
|
828
|
+
this.initialState = config.state || getDefaultState$1(this.options);
|
|
829
|
+
this.state = this.initialState;
|
|
830
|
+
this.scheduleGc();
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
get meta() {
|
|
834
|
+
return this.options.meta;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
setOptions(options) {
|
|
838
|
+
this.options = { ...this.defaultOptions,
|
|
839
|
+
...options
|
|
840
|
+
};
|
|
841
|
+
this.updateCacheTime(this.options.cacheTime);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
optionalRemove() {
|
|
845
|
+
if (!this.observers.length && this.state.fetchStatus === 'idle') {
|
|
846
|
+
this.cache.remove(this);
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
setData(newData, options) {
|
|
851
|
+
const data = replaceData(this.state.data, newData, this.options); // Set data and mark it as cached
|
|
852
|
+
|
|
853
|
+
this.dispatch({
|
|
854
|
+
data,
|
|
855
|
+
type: 'success',
|
|
856
|
+
dataUpdatedAt: options == null ? void 0 : options.updatedAt,
|
|
857
|
+
manual: options == null ? void 0 : options.manual
|
|
858
|
+
});
|
|
859
|
+
return data;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
setState(state, setStateOptions) {
|
|
863
|
+
this.dispatch({
|
|
864
|
+
type: 'setState',
|
|
865
|
+
state,
|
|
866
|
+
setStateOptions
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
cancel(options) {
|
|
871
|
+
var _this$retryer;
|
|
872
|
+
|
|
873
|
+
const promise = this.promise;
|
|
874
|
+
(_this$retryer = this.retryer) == null ? void 0 : _this$retryer.cancel(options);
|
|
875
|
+
return promise ? promise.then(noop).catch(noop) : Promise.resolve();
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
destroy() {
|
|
879
|
+
super.destroy();
|
|
880
|
+
this.cancel({
|
|
881
|
+
silent: true
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
reset() {
|
|
886
|
+
this.destroy();
|
|
887
|
+
this.setState(this.initialState);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
isActive() {
|
|
891
|
+
return this.observers.some(observer => observer.options.enabled !== false);
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
isDisabled() {
|
|
895
|
+
return this.getObserversCount() > 0 && !this.isActive();
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
isStale() {
|
|
899
|
+
return this.state.isInvalidated || !this.state.dataUpdatedAt || this.observers.some(observer => observer.getCurrentResult().isStale);
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
isStaleByTime(staleTime = 0) {
|
|
903
|
+
return this.state.isInvalidated || !this.state.dataUpdatedAt || !timeUntilStale(this.state.dataUpdatedAt, staleTime);
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
onFocus() {
|
|
907
|
+
var _this$retryer2;
|
|
908
|
+
|
|
909
|
+
const observer = this.observers.find(x => x.shouldFetchOnWindowFocus());
|
|
910
|
+
|
|
911
|
+
if (observer) {
|
|
912
|
+
observer.refetch({
|
|
913
|
+
cancelRefetch: false
|
|
914
|
+
});
|
|
915
|
+
} // Continue fetch if currently paused
|
|
916
|
+
|
|
917
|
+
|
|
918
|
+
(_this$retryer2 = this.retryer) == null ? void 0 : _this$retryer2.continue();
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
onOnline() {
|
|
922
|
+
var _this$retryer3;
|
|
923
|
+
|
|
924
|
+
const observer = this.observers.find(x => x.shouldFetchOnReconnect());
|
|
925
|
+
|
|
926
|
+
if (observer) {
|
|
927
|
+
observer.refetch({
|
|
928
|
+
cancelRefetch: false
|
|
929
|
+
});
|
|
930
|
+
} // Continue fetch if currently paused
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
(_this$retryer3 = this.retryer) == null ? void 0 : _this$retryer3.continue();
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
addObserver(observer) {
|
|
937
|
+
if (!this.observers.includes(observer)) {
|
|
938
|
+
this.observers.push(observer); // Stop the query from being garbage collected
|
|
939
|
+
|
|
940
|
+
this.clearGcTimeout();
|
|
941
|
+
this.cache.notify({
|
|
942
|
+
type: 'observerAdded',
|
|
943
|
+
query: this,
|
|
944
|
+
observer
|
|
945
|
+
});
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
removeObserver(observer) {
|
|
950
|
+
if (this.observers.includes(observer)) {
|
|
951
|
+
this.observers = this.observers.filter(x => x !== observer);
|
|
952
|
+
|
|
953
|
+
if (!this.observers.length) {
|
|
954
|
+
// If the transport layer does not support cancellation
|
|
955
|
+
// we'll let the query continue so the result can be cached
|
|
956
|
+
if (this.retryer) {
|
|
957
|
+
if (this.abortSignalConsumed) {
|
|
958
|
+
this.retryer.cancel({
|
|
959
|
+
revert: true
|
|
960
|
+
});
|
|
961
|
+
} else {
|
|
962
|
+
this.retryer.cancelRetry();
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
this.scheduleGc();
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
this.cache.notify({
|
|
970
|
+
type: 'observerRemoved',
|
|
971
|
+
query: this,
|
|
972
|
+
observer
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
getObserversCount() {
|
|
978
|
+
return this.observers.length;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
invalidate() {
|
|
982
|
+
if (!this.state.isInvalidated) {
|
|
983
|
+
this.dispatch({
|
|
984
|
+
type: 'invalidate'
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
fetch(options, fetchOptions) {
|
|
990
|
+
var _this$options$behavio, _context$fetchOptions;
|
|
991
|
+
|
|
992
|
+
if (this.state.fetchStatus !== 'idle') {
|
|
993
|
+
if (this.state.dataUpdatedAt && fetchOptions != null && fetchOptions.cancelRefetch) {
|
|
994
|
+
// Silently cancel current fetch if the user wants to cancel refetches
|
|
995
|
+
this.cancel({
|
|
996
|
+
silent: true
|
|
997
|
+
});
|
|
998
|
+
} else if (this.promise) {
|
|
999
|
+
var _this$retryer4;
|
|
1000
|
+
|
|
1001
|
+
// make sure that retries that were potentially cancelled due to unmounts can continue
|
|
1002
|
+
(_this$retryer4 = this.retryer) == null ? void 0 : _this$retryer4.continueRetry(); // Return current promise if we are already fetching
|
|
1003
|
+
|
|
1004
|
+
return this.promise;
|
|
1005
|
+
}
|
|
1006
|
+
} // Update config if passed, otherwise the config from the last execution is used
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
if (options) {
|
|
1010
|
+
this.setOptions(options);
|
|
1011
|
+
} // Use the options from the first observer with a query function if no function is found.
|
|
1012
|
+
// This can happen when the query is hydrated or created with setQueryData.
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
if (!this.options.queryFn) {
|
|
1016
|
+
const observer = this.observers.find(x => x.options.queryFn);
|
|
1017
|
+
|
|
1018
|
+
if (observer) {
|
|
1019
|
+
this.setOptions(observer.options);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
{
|
|
1024
|
+
if (!Array.isArray(this.options.queryKey)) {
|
|
1025
|
+
this.logger.error("As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']");
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
const abortController = getAbortController(); // Create query function context
|
|
1030
|
+
|
|
1031
|
+
const queryFnContext = {
|
|
1032
|
+
queryKey: this.queryKey,
|
|
1033
|
+
pageParam: undefined,
|
|
1034
|
+
meta: this.meta
|
|
1035
|
+
}; // Adds an enumerable signal property to the object that
|
|
1036
|
+
// which sets abortSignalConsumed to true when the signal
|
|
1037
|
+
// is read.
|
|
1038
|
+
|
|
1039
|
+
const addSignalProperty = object => {
|
|
1040
|
+
Object.defineProperty(object, 'signal', {
|
|
1041
|
+
enumerable: true,
|
|
1042
|
+
get: () => {
|
|
1043
|
+
if (abortController) {
|
|
1044
|
+
this.abortSignalConsumed = true;
|
|
1045
|
+
return abortController.signal;
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
return undefined;
|
|
1049
|
+
}
|
|
1050
|
+
});
|
|
1051
|
+
};
|
|
1052
|
+
|
|
1053
|
+
addSignalProperty(queryFnContext); // Create fetch function
|
|
1054
|
+
|
|
1055
|
+
const fetchFn = () => {
|
|
1056
|
+
if (!this.options.queryFn) {
|
|
1057
|
+
return Promise.reject("Missing queryFn for queryKey '" + this.options.queryHash + "'");
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
this.abortSignalConsumed = false;
|
|
1061
|
+
return this.options.queryFn(queryFnContext);
|
|
1062
|
+
}; // Trigger behavior hook
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
const context = {
|
|
1066
|
+
fetchOptions,
|
|
1067
|
+
options: this.options,
|
|
1068
|
+
queryKey: this.queryKey,
|
|
1069
|
+
state: this.state,
|
|
1070
|
+
fetchFn
|
|
1071
|
+
};
|
|
1072
|
+
addSignalProperty(context);
|
|
1073
|
+
(_this$options$behavio = this.options.behavior) == null ? void 0 : _this$options$behavio.onFetch(context); // Store state in case the current fetch needs to be reverted
|
|
1074
|
+
|
|
1075
|
+
this.revertState = this.state; // Set to fetching state if not already in it
|
|
1076
|
+
|
|
1077
|
+
if (this.state.fetchStatus === 'idle' || this.state.fetchMeta !== ((_context$fetchOptions = context.fetchOptions) == null ? void 0 : _context$fetchOptions.meta)) {
|
|
1078
|
+
var _context$fetchOptions2;
|
|
1079
|
+
|
|
1080
|
+
this.dispatch({
|
|
1081
|
+
type: 'fetch',
|
|
1082
|
+
meta: (_context$fetchOptions2 = context.fetchOptions) == null ? void 0 : _context$fetchOptions2.meta
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
const onError = error => {
|
|
1087
|
+
// Optimistically update state if needed
|
|
1088
|
+
if (!(isCancelledError(error) && error.silent)) {
|
|
1089
|
+
this.dispatch({
|
|
1090
|
+
type: 'error',
|
|
1091
|
+
error: error
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
if (!isCancelledError(error)) {
|
|
1096
|
+
var _this$cache$config$on, _this$cache$config, _this$cache$config$on2, _this$cache$config2;
|
|
1097
|
+
|
|
1098
|
+
// Notify cache callback
|
|
1099
|
+
(_this$cache$config$on = (_this$cache$config = this.cache.config).onError) == null ? void 0 : _this$cache$config$on.call(_this$cache$config, error, this);
|
|
1100
|
+
(_this$cache$config$on2 = (_this$cache$config2 = this.cache.config).onSettled) == null ? void 0 : _this$cache$config$on2.call(_this$cache$config2, this.state.data, error, this);
|
|
1101
|
+
|
|
1102
|
+
{
|
|
1103
|
+
this.logger.error(error);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
if (!this.isFetchingOptimistic) {
|
|
1108
|
+
// Schedule query gc after fetching
|
|
1109
|
+
this.scheduleGc();
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
this.isFetchingOptimistic = false;
|
|
1113
|
+
}; // Try to fetch the data
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
this.retryer = createRetryer({
|
|
1117
|
+
fn: context.fetchFn,
|
|
1118
|
+
abort: abortController == null ? void 0 : abortController.abort.bind(abortController),
|
|
1119
|
+
onSuccess: data => {
|
|
1120
|
+
var _this$cache$config$on3, _this$cache$config3, _this$cache$config$on4, _this$cache$config4;
|
|
1121
|
+
|
|
1122
|
+
if (typeof data === 'undefined') {
|
|
1123
|
+
{
|
|
1124
|
+
this.logger.error("Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: " + this.queryHash);
|
|
1125
|
+
}
|
|
1126
|
+
|
|
1127
|
+
onError(new Error(this.queryHash + " data is undefined"));
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
this.setData(data); // Notify cache callback
|
|
1132
|
+
|
|
1133
|
+
(_this$cache$config$on3 = (_this$cache$config3 = this.cache.config).onSuccess) == null ? void 0 : _this$cache$config$on3.call(_this$cache$config3, data, this);
|
|
1134
|
+
(_this$cache$config$on4 = (_this$cache$config4 = this.cache.config).onSettled) == null ? void 0 : _this$cache$config$on4.call(_this$cache$config4, data, this.state.error, this);
|
|
1135
|
+
|
|
1136
|
+
if (!this.isFetchingOptimistic) {
|
|
1137
|
+
// Schedule query gc after fetching
|
|
1138
|
+
this.scheduleGc();
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
this.isFetchingOptimistic = false;
|
|
1142
|
+
},
|
|
1143
|
+
onError,
|
|
1144
|
+
onFail: (failureCount, error) => {
|
|
1145
|
+
this.dispatch({
|
|
1146
|
+
type: 'failed',
|
|
1147
|
+
failureCount,
|
|
1148
|
+
error
|
|
1149
|
+
});
|
|
1150
|
+
},
|
|
1151
|
+
onPause: () => {
|
|
1152
|
+
this.dispatch({
|
|
1153
|
+
type: 'pause'
|
|
1154
|
+
});
|
|
1155
|
+
},
|
|
1156
|
+
onContinue: () => {
|
|
1157
|
+
this.dispatch({
|
|
1158
|
+
type: 'continue'
|
|
1159
|
+
});
|
|
1160
|
+
},
|
|
1161
|
+
retry: context.options.retry,
|
|
1162
|
+
retryDelay: context.options.retryDelay,
|
|
1163
|
+
networkMode: context.options.networkMode
|
|
1164
|
+
});
|
|
1165
|
+
this.promise = this.retryer.promise;
|
|
1166
|
+
return this.promise;
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
dispatch(action) {
|
|
1170
|
+
const reducer = state => {
|
|
1171
|
+
var _action$meta, _action$dataUpdatedAt;
|
|
1172
|
+
|
|
1173
|
+
switch (action.type) {
|
|
1174
|
+
case 'failed':
|
|
1175
|
+
return { ...state,
|
|
1176
|
+
fetchFailureCount: action.failureCount,
|
|
1177
|
+
fetchFailureReason: action.error
|
|
1178
|
+
};
|
|
1179
|
+
|
|
1180
|
+
case 'pause':
|
|
1181
|
+
return { ...state,
|
|
1182
|
+
fetchStatus: 'paused'
|
|
1183
|
+
};
|
|
1184
|
+
|
|
1185
|
+
case 'continue':
|
|
1186
|
+
return { ...state,
|
|
1187
|
+
fetchStatus: 'fetching'
|
|
1188
|
+
};
|
|
1189
|
+
|
|
1190
|
+
case 'fetch':
|
|
1191
|
+
return { ...state,
|
|
1192
|
+
fetchFailureCount: 0,
|
|
1193
|
+
fetchFailureReason: null,
|
|
1194
|
+
fetchMeta: (_action$meta = action.meta) != null ? _action$meta : null,
|
|
1195
|
+
fetchStatus: canFetch(this.options.networkMode) ? 'fetching' : 'paused',
|
|
1196
|
+
...(!state.dataUpdatedAt && {
|
|
1197
|
+
error: null,
|
|
1198
|
+
status: 'loading'
|
|
1199
|
+
})
|
|
1200
|
+
};
|
|
1201
|
+
|
|
1202
|
+
case 'success':
|
|
1203
|
+
return { ...state,
|
|
1204
|
+
data: action.data,
|
|
1205
|
+
dataUpdateCount: state.dataUpdateCount + 1,
|
|
1206
|
+
dataUpdatedAt: (_action$dataUpdatedAt = action.dataUpdatedAt) != null ? _action$dataUpdatedAt : Date.now(),
|
|
1207
|
+
error: null,
|
|
1208
|
+
isInvalidated: false,
|
|
1209
|
+
status: 'success',
|
|
1210
|
+
...(!action.manual && {
|
|
1211
|
+
fetchStatus: 'idle',
|
|
1212
|
+
fetchFailureCount: 0,
|
|
1213
|
+
fetchFailureReason: null
|
|
1214
|
+
})
|
|
1215
|
+
};
|
|
1216
|
+
|
|
1217
|
+
case 'error':
|
|
1218
|
+
const error = action.error;
|
|
1219
|
+
|
|
1220
|
+
if (isCancelledError(error) && error.revert && this.revertState) {
|
|
1221
|
+
return { ...this.revertState,
|
|
1222
|
+
fetchStatus: 'idle'
|
|
1223
|
+
};
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
return { ...state,
|
|
1227
|
+
error: error,
|
|
1228
|
+
errorUpdateCount: state.errorUpdateCount + 1,
|
|
1229
|
+
errorUpdatedAt: Date.now(),
|
|
1230
|
+
fetchFailureCount: state.fetchFailureCount + 1,
|
|
1231
|
+
fetchFailureReason: error,
|
|
1232
|
+
fetchStatus: 'idle',
|
|
1233
|
+
status: 'error'
|
|
1234
|
+
};
|
|
1235
|
+
|
|
1236
|
+
case 'invalidate':
|
|
1237
|
+
return { ...state,
|
|
1238
|
+
isInvalidated: true
|
|
1239
|
+
};
|
|
1240
|
+
|
|
1241
|
+
case 'setState':
|
|
1242
|
+
return { ...state,
|
|
1243
|
+
...action.state
|
|
1244
|
+
};
|
|
1245
|
+
}
|
|
1246
|
+
};
|
|
1247
|
+
|
|
1248
|
+
this.state = reducer(this.state);
|
|
1249
|
+
notifyManager.batch(() => {
|
|
1250
|
+
this.observers.forEach(observer => {
|
|
1251
|
+
observer.onQueryUpdate(action);
|
|
1252
|
+
});
|
|
1253
|
+
this.cache.notify({
|
|
1254
|
+
query: this,
|
|
1255
|
+
type: 'updated',
|
|
1256
|
+
action
|
|
1257
|
+
});
|
|
1258
|
+
});
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
function getDefaultState$1(options) {
|
|
1264
|
+
const data = typeof options.initialData === 'function' ? options.initialData() : options.initialData;
|
|
1265
|
+
const hasData = typeof data !== 'undefined';
|
|
1266
|
+
const initialDataUpdatedAt = hasData ? typeof options.initialDataUpdatedAt === 'function' ? options.initialDataUpdatedAt() : options.initialDataUpdatedAt : 0;
|
|
1267
|
+
return {
|
|
1268
|
+
data,
|
|
1269
|
+
dataUpdateCount: 0,
|
|
1270
|
+
dataUpdatedAt: hasData ? initialDataUpdatedAt != null ? initialDataUpdatedAt : Date.now() : 0,
|
|
1271
|
+
error: null,
|
|
1272
|
+
errorUpdateCount: 0,
|
|
1273
|
+
errorUpdatedAt: 0,
|
|
1274
|
+
fetchFailureCount: 0,
|
|
1275
|
+
fetchFailureReason: null,
|
|
1276
|
+
fetchMeta: null,
|
|
1277
|
+
isInvalidated: false,
|
|
1278
|
+
status: hasData ? 'success' : 'loading',
|
|
1279
|
+
fetchStatus: 'idle'
|
|
1280
|
+
};
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
// CLASS
|
|
1284
|
+
class QueryCache extends Subscribable {
|
|
1285
|
+
constructor(config) {
|
|
1286
|
+
super();
|
|
1287
|
+
this.config = config || {};
|
|
1288
|
+
this.queries = [];
|
|
1289
|
+
this.queriesMap = {};
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
build(client, options, state) {
|
|
1293
|
+
var _options$queryHash;
|
|
1294
|
+
|
|
1295
|
+
const queryKey = options.queryKey;
|
|
1296
|
+
const queryHash = (_options$queryHash = options.queryHash) != null ? _options$queryHash : hashQueryKeyByOptions(queryKey, options);
|
|
1297
|
+
let query = this.get(queryHash);
|
|
1298
|
+
|
|
1299
|
+
if (!query) {
|
|
1300
|
+
query = new Query({
|
|
1301
|
+
cache: this,
|
|
1302
|
+
logger: client.getLogger(),
|
|
1303
|
+
queryKey,
|
|
1304
|
+
queryHash,
|
|
1305
|
+
options: client.defaultQueryOptions(options),
|
|
1306
|
+
state,
|
|
1307
|
+
defaultOptions: client.getQueryDefaults(queryKey)
|
|
1308
|
+
});
|
|
1309
|
+
this.add(query);
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
return query;
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
add(query) {
|
|
1316
|
+
if (!this.queriesMap[query.queryHash]) {
|
|
1317
|
+
this.queriesMap[query.queryHash] = query;
|
|
1318
|
+
this.queries.push(query);
|
|
1319
|
+
this.notify({
|
|
1320
|
+
type: 'added',
|
|
1321
|
+
query
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
remove(query) {
|
|
1327
|
+
const queryInMap = this.queriesMap[query.queryHash];
|
|
1328
|
+
|
|
1329
|
+
if (queryInMap) {
|
|
1330
|
+
query.destroy();
|
|
1331
|
+
this.queries = this.queries.filter(x => x !== query);
|
|
1332
|
+
|
|
1333
|
+
if (queryInMap === query) {
|
|
1334
|
+
delete this.queriesMap[query.queryHash];
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
this.notify({
|
|
1338
|
+
type: 'removed',
|
|
1339
|
+
query
|
|
1340
|
+
});
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
clear() {
|
|
1345
|
+
notifyManager.batch(() => {
|
|
1346
|
+
this.queries.forEach(query => {
|
|
1347
|
+
this.remove(query);
|
|
1348
|
+
});
|
|
1349
|
+
});
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
get(queryHash) {
|
|
1353
|
+
return this.queriesMap[queryHash];
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
getAll() {
|
|
1357
|
+
return this.queries;
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
/**
|
|
1361
|
+
* @deprecated This method should be used with only one object argument.
|
|
1362
|
+
*/
|
|
1363
|
+
find(arg1, arg2) {
|
|
1364
|
+
const [filters] = parseFilterArgs(arg1, arg2);
|
|
1365
|
+
|
|
1366
|
+
if (typeof filters.exact === 'undefined') {
|
|
1367
|
+
filters.exact = true;
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
return this.queries.find(query => matchQuery(filters, query));
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
/**
|
|
1374
|
+
* @deprecated This method should be used with only one object argument.
|
|
1375
|
+
*/
|
|
1376
|
+
findAll(arg1, arg2) {
|
|
1377
|
+
const [filters] = parseFilterArgs(arg1, arg2);
|
|
1378
|
+
return Object.keys(filters).length > 0 ? this.queries.filter(query => matchQuery(filters, query)) : this.queries;
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
notify(event) {
|
|
1382
|
+
notifyManager.batch(() => {
|
|
1383
|
+
this.listeners.forEach(({
|
|
1384
|
+
listener
|
|
1385
|
+
}) => {
|
|
1386
|
+
listener(event);
|
|
1387
|
+
});
|
|
1388
|
+
});
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
onFocus() {
|
|
1392
|
+
notifyManager.batch(() => {
|
|
1393
|
+
this.queries.forEach(query => {
|
|
1394
|
+
query.onFocus();
|
|
1395
|
+
});
|
|
1396
|
+
});
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
onOnline() {
|
|
1400
|
+
notifyManager.batch(() => {
|
|
1401
|
+
this.queries.forEach(query => {
|
|
1402
|
+
query.onOnline();
|
|
1403
|
+
});
|
|
1404
|
+
});
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
// CLASS
|
|
1410
|
+
class Mutation extends Removable {
|
|
1411
|
+
constructor(config) {
|
|
1412
|
+
super();
|
|
1413
|
+
this.defaultOptions = config.defaultOptions;
|
|
1414
|
+
this.mutationId = config.mutationId;
|
|
1415
|
+
this.mutationCache = config.mutationCache;
|
|
1416
|
+
this.logger = config.logger || defaultLogger;
|
|
1417
|
+
this.observers = [];
|
|
1418
|
+
this.state = config.state || getDefaultState();
|
|
1419
|
+
this.setOptions(config.options);
|
|
1420
|
+
this.scheduleGc();
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
setOptions(options) {
|
|
1424
|
+
this.options = { ...this.defaultOptions,
|
|
1425
|
+
...options
|
|
1426
|
+
};
|
|
1427
|
+
this.updateCacheTime(this.options.cacheTime);
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
get meta() {
|
|
1431
|
+
return this.options.meta;
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
setState(state) {
|
|
1435
|
+
this.dispatch({
|
|
1436
|
+
type: 'setState',
|
|
1437
|
+
state
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
addObserver(observer) {
|
|
1442
|
+
if (!this.observers.includes(observer)) {
|
|
1443
|
+
this.observers.push(observer); // Stop the mutation from being garbage collected
|
|
1444
|
+
|
|
1445
|
+
this.clearGcTimeout();
|
|
1446
|
+
this.mutationCache.notify({
|
|
1447
|
+
type: 'observerAdded',
|
|
1448
|
+
mutation: this,
|
|
1449
|
+
observer
|
|
1450
|
+
});
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
removeObserver(observer) {
|
|
1455
|
+
this.observers = this.observers.filter(x => x !== observer);
|
|
1456
|
+
this.scheduleGc();
|
|
1457
|
+
this.mutationCache.notify({
|
|
1458
|
+
type: 'observerRemoved',
|
|
1459
|
+
mutation: this,
|
|
1460
|
+
observer
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
optionalRemove() {
|
|
1465
|
+
if (!this.observers.length) {
|
|
1466
|
+
if (this.state.status === 'loading') {
|
|
1467
|
+
this.scheduleGc();
|
|
1468
|
+
} else {
|
|
1469
|
+
this.mutationCache.remove(this);
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
continue() {
|
|
1475
|
+
var _this$retryer$continu, _this$retryer;
|
|
1476
|
+
|
|
1477
|
+
return (_this$retryer$continu = (_this$retryer = this.retryer) == null ? void 0 : _this$retryer.continue()) != null ? _this$retryer$continu : this.execute();
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
async execute() {
|
|
1481
|
+
const executeMutation = () => {
|
|
1482
|
+
var _this$options$retry;
|
|
1483
|
+
|
|
1484
|
+
this.retryer = createRetryer({
|
|
1485
|
+
fn: () => {
|
|
1486
|
+
if (!this.options.mutationFn) {
|
|
1487
|
+
return Promise.reject('No mutationFn found');
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
return this.options.mutationFn(this.state.variables);
|
|
1491
|
+
},
|
|
1492
|
+
onFail: (failureCount, error) => {
|
|
1493
|
+
this.dispatch({
|
|
1494
|
+
type: 'failed',
|
|
1495
|
+
failureCount,
|
|
1496
|
+
error
|
|
1497
|
+
});
|
|
1498
|
+
},
|
|
1499
|
+
onPause: () => {
|
|
1500
|
+
this.dispatch({
|
|
1501
|
+
type: 'pause'
|
|
1502
|
+
});
|
|
1503
|
+
},
|
|
1504
|
+
onContinue: () => {
|
|
1505
|
+
this.dispatch({
|
|
1506
|
+
type: 'continue'
|
|
1507
|
+
});
|
|
1508
|
+
},
|
|
1509
|
+
retry: (_this$options$retry = this.options.retry) != null ? _this$options$retry : 0,
|
|
1510
|
+
retryDelay: this.options.retryDelay,
|
|
1511
|
+
networkMode: this.options.networkMode
|
|
1512
|
+
});
|
|
1513
|
+
return this.retryer.promise;
|
|
1514
|
+
};
|
|
1515
|
+
|
|
1516
|
+
const restored = this.state.status === 'loading';
|
|
1517
|
+
|
|
1518
|
+
try {
|
|
1519
|
+
var _this$mutationCache$c3, _this$mutationCache$c4, _this$options$onSucce, _this$options2, _this$mutationCache$c5, _this$mutationCache$c6, _this$options$onSettl, _this$options3;
|
|
1520
|
+
|
|
1521
|
+
if (!restored) {
|
|
1522
|
+
var _this$mutationCache$c, _this$mutationCache$c2, _this$options$onMutat, _this$options;
|
|
1523
|
+
|
|
1524
|
+
this.dispatch({
|
|
1525
|
+
type: 'loading',
|
|
1526
|
+
variables: this.options.variables
|
|
1527
|
+
}); // Notify cache callback
|
|
1528
|
+
|
|
1529
|
+
await ((_this$mutationCache$c = (_this$mutationCache$c2 = this.mutationCache.config).onMutate) == null ? void 0 : _this$mutationCache$c.call(_this$mutationCache$c2, this.state.variables, this));
|
|
1530
|
+
const context = await ((_this$options$onMutat = (_this$options = this.options).onMutate) == null ? void 0 : _this$options$onMutat.call(_this$options, this.state.variables));
|
|
1531
|
+
|
|
1532
|
+
if (context !== this.state.context) {
|
|
1533
|
+
this.dispatch({
|
|
1534
|
+
type: 'loading',
|
|
1535
|
+
context,
|
|
1536
|
+
variables: this.state.variables
|
|
1537
|
+
});
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
const data = await executeMutation(); // Notify cache callback
|
|
1542
|
+
|
|
1543
|
+
await ((_this$mutationCache$c3 = (_this$mutationCache$c4 = this.mutationCache.config).onSuccess) == null ? void 0 : _this$mutationCache$c3.call(_this$mutationCache$c4, data, this.state.variables, this.state.context, this));
|
|
1544
|
+
await ((_this$options$onSucce = (_this$options2 = this.options).onSuccess) == null ? void 0 : _this$options$onSucce.call(_this$options2, data, this.state.variables, this.state.context)); // Notify cache callback
|
|
1545
|
+
|
|
1546
|
+
await ((_this$mutationCache$c5 = (_this$mutationCache$c6 = this.mutationCache.config).onSettled) == null ? void 0 : _this$mutationCache$c5.call(_this$mutationCache$c6, data, null, this.state.variables, this.state.context, this));
|
|
1547
|
+
await ((_this$options$onSettl = (_this$options3 = this.options).onSettled) == null ? void 0 : _this$options$onSettl.call(_this$options3, data, null, this.state.variables, this.state.context));
|
|
1548
|
+
this.dispatch({
|
|
1549
|
+
type: 'success',
|
|
1550
|
+
data
|
|
1551
|
+
});
|
|
1552
|
+
return data;
|
|
1553
|
+
} catch (error) {
|
|
1554
|
+
try {
|
|
1555
|
+
var _this$mutationCache$c7, _this$mutationCache$c8, _this$options$onError, _this$options4, _this$mutationCache$c9, _this$mutationCache$c10, _this$options$onSettl2, _this$options5;
|
|
1556
|
+
|
|
1557
|
+
// Notify cache callback
|
|
1558
|
+
await ((_this$mutationCache$c7 = (_this$mutationCache$c8 = this.mutationCache.config).onError) == null ? void 0 : _this$mutationCache$c7.call(_this$mutationCache$c8, error, this.state.variables, this.state.context, this));
|
|
1559
|
+
|
|
1560
|
+
if ("development" !== 'production') {
|
|
1561
|
+
this.logger.error(error);
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
await ((_this$options$onError = (_this$options4 = this.options).onError) == null ? void 0 : _this$options$onError.call(_this$options4, error, this.state.variables, this.state.context)); // Notify cache callback
|
|
1565
|
+
|
|
1566
|
+
await ((_this$mutationCache$c9 = (_this$mutationCache$c10 = this.mutationCache.config).onSettled) == null ? void 0 : _this$mutationCache$c9.call(_this$mutationCache$c10, undefined, error, this.state.variables, this.state.context, this));
|
|
1567
|
+
await ((_this$options$onSettl2 = (_this$options5 = this.options).onSettled) == null ? void 0 : _this$options$onSettl2.call(_this$options5, undefined, error, this.state.variables, this.state.context));
|
|
1568
|
+
throw error;
|
|
1569
|
+
} finally {
|
|
1570
|
+
this.dispatch({
|
|
1571
|
+
type: 'error',
|
|
1572
|
+
error: error
|
|
1573
|
+
});
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
dispatch(action) {
|
|
1579
|
+
const reducer = state => {
|
|
1580
|
+
switch (action.type) {
|
|
1581
|
+
case 'failed':
|
|
1582
|
+
return { ...state,
|
|
1583
|
+
failureCount: action.failureCount,
|
|
1584
|
+
failureReason: action.error
|
|
1585
|
+
};
|
|
1586
|
+
|
|
1587
|
+
case 'pause':
|
|
1588
|
+
return { ...state,
|
|
1589
|
+
isPaused: true
|
|
1590
|
+
};
|
|
1591
|
+
|
|
1592
|
+
case 'continue':
|
|
1593
|
+
return { ...state,
|
|
1594
|
+
isPaused: false
|
|
1595
|
+
};
|
|
1596
|
+
|
|
1597
|
+
case 'loading':
|
|
1598
|
+
return { ...state,
|
|
1599
|
+
context: action.context,
|
|
1600
|
+
data: undefined,
|
|
1601
|
+
failureCount: 0,
|
|
1602
|
+
failureReason: null,
|
|
1603
|
+
error: null,
|
|
1604
|
+
isPaused: !canFetch(this.options.networkMode),
|
|
1605
|
+
status: 'loading',
|
|
1606
|
+
variables: action.variables
|
|
1607
|
+
};
|
|
1608
|
+
|
|
1609
|
+
case 'success':
|
|
1610
|
+
return { ...state,
|
|
1611
|
+
data: action.data,
|
|
1612
|
+
failureCount: 0,
|
|
1613
|
+
failureReason: null,
|
|
1614
|
+
error: null,
|
|
1615
|
+
status: 'success',
|
|
1616
|
+
isPaused: false
|
|
1617
|
+
};
|
|
1618
|
+
|
|
1619
|
+
case 'error':
|
|
1620
|
+
return { ...state,
|
|
1621
|
+
data: undefined,
|
|
1622
|
+
error: action.error,
|
|
1623
|
+
failureCount: state.failureCount + 1,
|
|
1624
|
+
failureReason: action.error,
|
|
1625
|
+
isPaused: false,
|
|
1626
|
+
status: 'error'
|
|
1627
|
+
};
|
|
1628
|
+
|
|
1629
|
+
case 'setState':
|
|
1630
|
+
return { ...state,
|
|
1631
|
+
...action.state
|
|
1632
|
+
};
|
|
1633
|
+
}
|
|
1634
|
+
};
|
|
1635
|
+
|
|
1636
|
+
this.state = reducer(this.state);
|
|
1637
|
+
notifyManager.batch(() => {
|
|
1638
|
+
this.observers.forEach(observer => {
|
|
1639
|
+
observer.onMutationUpdate(action);
|
|
1640
|
+
});
|
|
1641
|
+
this.mutationCache.notify({
|
|
1642
|
+
mutation: this,
|
|
1643
|
+
type: 'updated',
|
|
1644
|
+
action
|
|
1645
|
+
});
|
|
1646
|
+
});
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
}
|
|
1650
|
+
function getDefaultState() {
|
|
1651
|
+
return {
|
|
1652
|
+
context: undefined,
|
|
1653
|
+
data: undefined,
|
|
1654
|
+
error: null,
|
|
1655
|
+
failureCount: 0,
|
|
1656
|
+
failureReason: null,
|
|
1657
|
+
isPaused: false,
|
|
1658
|
+
status: 'idle',
|
|
1659
|
+
variables: undefined
|
|
1660
|
+
};
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
// CLASS
|
|
1664
|
+
class MutationCache extends Subscribable {
|
|
1665
|
+
constructor(config) {
|
|
1666
|
+
super();
|
|
1667
|
+
this.config = config || {};
|
|
1668
|
+
this.mutations = [];
|
|
1669
|
+
this.mutationId = 0;
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
build(client, options, state) {
|
|
1673
|
+
const mutation = new Mutation({
|
|
1674
|
+
mutationCache: this,
|
|
1675
|
+
logger: client.getLogger(),
|
|
1676
|
+
mutationId: ++this.mutationId,
|
|
1677
|
+
options: client.defaultMutationOptions(options),
|
|
1678
|
+
state,
|
|
1679
|
+
defaultOptions: options.mutationKey ? client.getMutationDefaults(options.mutationKey) : undefined
|
|
1680
|
+
});
|
|
1681
|
+
this.add(mutation);
|
|
1682
|
+
return mutation;
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
add(mutation) {
|
|
1686
|
+
this.mutations.push(mutation);
|
|
1687
|
+
this.notify({
|
|
1688
|
+
type: 'added',
|
|
1689
|
+
mutation
|
|
1690
|
+
});
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
remove(mutation) {
|
|
1694
|
+
this.mutations = this.mutations.filter(x => x !== mutation);
|
|
1695
|
+
this.notify({
|
|
1696
|
+
type: 'removed',
|
|
1697
|
+
mutation
|
|
1698
|
+
});
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
clear() {
|
|
1702
|
+
notifyManager.batch(() => {
|
|
1703
|
+
this.mutations.forEach(mutation => {
|
|
1704
|
+
this.remove(mutation);
|
|
1705
|
+
});
|
|
1706
|
+
});
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
getAll() {
|
|
1710
|
+
return this.mutations;
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
find(filters) {
|
|
1714
|
+
if (typeof filters.exact === 'undefined') {
|
|
1715
|
+
filters.exact = true;
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1718
|
+
return this.mutations.find(mutation => matchMutation(filters, mutation));
|
|
1719
|
+
}
|
|
1720
|
+
|
|
1721
|
+
findAll(filters) {
|
|
1722
|
+
return this.mutations.filter(mutation => matchMutation(filters, mutation));
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
notify(event) {
|
|
1726
|
+
notifyManager.batch(() => {
|
|
1727
|
+
this.listeners.forEach(({
|
|
1728
|
+
listener
|
|
1729
|
+
}) => {
|
|
1730
|
+
listener(event);
|
|
1731
|
+
});
|
|
1732
|
+
});
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
resumePausedMutations() {
|
|
1736
|
+
var _this$resuming;
|
|
1737
|
+
|
|
1738
|
+
this.resuming = ((_this$resuming = this.resuming) != null ? _this$resuming : Promise.resolve()).then(() => {
|
|
1739
|
+
const pausedMutations = this.mutations.filter(x => x.state.isPaused);
|
|
1740
|
+
return notifyManager.batch(() => pausedMutations.reduce((promise, mutation) => promise.then(() => mutation.continue().catch(noop)), Promise.resolve()));
|
|
1741
|
+
}).then(() => {
|
|
1742
|
+
this.resuming = undefined;
|
|
1743
|
+
});
|
|
1744
|
+
return this.resuming;
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
function infiniteQueryBehavior() {
|
|
1750
|
+
return {
|
|
1751
|
+
onFetch: context => {
|
|
1752
|
+
context.fetchFn = () => {
|
|
1753
|
+
var _context$fetchOptions, _context$fetchOptions2, _context$fetchOptions3, _context$fetchOptions4, _context$state$data, _context$state$data2;
|
|
1754
|
+
|
|
1755
|
+
const refetchPage = (_context$fetchOptions = context.fetchOptions) == null ? void 0 : (_context$fetchOptions2 = _context$fetchOptions.meta) == null ? void 0 : _context$fetchOptions2.refetchPage;
|
|
1756
|
+
const fetchMore = (_context$fetchOptions3 = context.fetchOptions) == null ? void 0 : (_context$fetchOptions4 = _context$fetchOptions3.meta) == null ? void 0 : _context$fetchOptions4.fetchMore;
|
|
1757
|
+
const pageParam = fetchMore == null ? void 0 : fetchMore.pageParam;
|
|
1758
|
+
const isFetchingNextPage = (fetchMore == null ? void 0 : fetchMore.direction) === 'forward';
|
|
1759
|
+
const isFetchingPreviousPage = (fetchMore == null ? void 0 : fetchMore.direction) === 'backward';
|
|
1760
|
+
const oldPages = ((_context$state$data = context.state.data) == null ? void 0 : _context$state$data.pages) || [];
|
|
1761
|
+
const oldPageParams = ((_context$state$data2 = context.state.data) == null ? void 0 : _context$state$data2.pageParams) || [];
|
|
1762
|
+
let newPageParams = oldPageParams;
|
|
1763
|
+
let cancelled = false;
|
|
1764
|
+
|
|
1765
|
+
const addSignalProperty = object => {
|
|
1766
|
+
Object.defineProperty(object, 'signal', {
|
|
1767
|
+
enumerable: true,
|
|
1768
|
+
get: () => {
|
|
1769
|
+
var _context$signal;
|
|
1770
|
+
|
|
1771
|
+
if ((_context$signal = context.signal) != null && _context$signal.aborted) {
|
|
1772
|
+
cancelled = true;
|
|
1773
|
+
} else {
|
|
1774
|
+
var _context$signal2;
|
|
1775
|
+
|
|
1776
|
+
(_context$signal2 = context.signal) == null ? void 0 : _context$signal2.addEventListener('abort', () => {
|
|
1777
|
+
cancelled = true;
|
|
1778
|
+
});
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1781
|
+
return context.signal;
|
|
1782
|
+
}
|
|
1783
|
+
});
|
|
1784
|
+
}; // Get query function
|
|
1785
|
+
|
|
1786
|
+
|
|
1787
|
+
const queryFn = context.options.queryFn || (() => Promise.reject("Missing queryFn for queryKey '" + context.options.queryHash + "'"));
|
|
1788
|
+
|
|
1789
|
+
const buildNewPages = (pages, param, page, previous) => {
|
|
1790
|
+
newPageParams = previous ? [param, ...newPageParams] : [...newPageParams, param];
|
|
1791
|
+
return previous ? [page, ...pages] : [...pages, page];
|
|
1792
|
+
}; // Create function to fetch a page
|
|
1793
|
+
|
|
1794
|
+
|
|
1795
|
+
const fetchPage = (pages, manual, param, previous) => {
|
|
1796
|
+
if (cancelled) {
|
|
1797
|
+
return Promise.reject('Cancelled');
|
|
1798
|
+
}
|
|
1799
|
+
|
|
1800
|
+
if (typeof param === 'undefined' && !manual && pages.length) {
|
|
1801
|
+
return Promise.resolve(pages);
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
const queryFnContext = {
|
|
1805
|
+
queryKey: context.queryKey,
|
|
1806
|
+
pageParam: param,
|
|
1807
|
+
meta: context.options.meta
|
|
1808
|
+
};
|
|
1809
|
+
addSignalProperty(queryFnContext);
|
|
1810
|
+
const queryFnResult = queryFn(queryFnContext);
|
|
1811
|
+
const promise = Promise.resolve(queryFnResult).then(page => buildNewPages(pages, param, page, previous));
|
|
1812
|
+
return promise;
|
|
1813
|
+
};
|
|
1814
|
+
|
|
1815
|
+
let promise; // Fetch first page?
|
|
1816
|
+
|
|
1817
|
+
if (!oldPages.length) {
|
|
1818
|
+
promise = fetchPage([]);
|
|
1819
|
+
} // Fetch next page?
|
|
1820
|
+
else if (isFetchingNextPage) {
|
|
1821
|
+
const manual = typeof pageParam !== 'undefined';
|
|
1822
|
+
const param = manual ? pageParam : getNextPageParam(context.options, oldPages);
|
|
1823
|
+
promise = fetchPage(oldPages, manual, param);
|
|
1824
|
+
} // Fetch previous page?
|
|
1825
|
+
else if (isFetchingPreviousPage) {
|
|
1826
|
+
const manual = typeof pageParam !== 'undefined';
|
|
1827
|
+
const param = manual ? pageParam : getPreviousPageParam(context.options, oldPages);
|
|
1828
|
+
promise = fetchPage(oldPages, manual, param, true);
|
|
1829
|
+
} // Refetch pages
|
|
1830
|
+
else {
|
|
1831
|
+
newPageParams = [];
|
|
1832
|
+
const manual = typeof context.options.getNextPageParam === 'undefined';
|
|
1833
|
+
const shouldFetchFirstPage = refetchPage && oldPages[0] ? refetchPage(oldPages[0], 0, oldPages) : true; // Fetch first page
|
|
1834
|
+
|
|
1835
|
+
promise = shouldFetchFirstPage ? fetchPage([], manual, oldPageParams[0]) : Promise.resolve(buildNewPages([], oldPageParams[0], oldPages[0])); // Fetch remaining pages
|
|
1836
|
+
|
|
1837
|
+
for (let i = 1; i < oldPages.length; i++) {
|
|
1838
|
+
promise = promise.then(pages => {
|
|
1839
|
+
const shouldFetchNextPage = refetchPage && oldPages[i] ? refetchPage(oldPages[i], i, oldPages) : true;
|
|
1840
|
+
|
|
1841
|
+
if (shouldFetchNextPage) {
|
|
1842
|
+
const param = manual ? oldPageParams[i] : getNextPageParam(context.options, pages);
|
|
1843
|
+
return fetchPage(pages, manual, param);
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
return Promise.resolve(buildNewPages(pages, oldPageParams[i], oldPages[i]));
|
|
1847
|
+
});
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
const finalPromise = promise.then(pages => ({
|
|
1852
|
+
pages,
|
|
1853
|
+
pageParams: newPageParams
|
|
1854
|
+
}));
|
|
1855
|
+
return finalPromise;
|
|
1856
|
+
};
|
|
1857
|
+
}
|
|
1858
|
+
};
|
|
1859
|
+
}
|
|
1860
|
+
function getNextPageParam(options, pages) {
|
|
1861
|
+
return options.getNextPageParam == null ? void 0 : options.getNextPageParam(pages[pages.length - 1], pages);
|
|
1862
|
+
}
|
|
1863
|
+
function getPreviousPageParam(options, pages) {
|
|
1864
|
+
return options.getPreviousPageParam == null ? void 0 : options.getPreviousPageParam(pages[0], pages);
|
|
1865
|
+
}
|
|
1866
|
+
/**
|
|
1867
|
+
* Checks if there is a next page.
|
|
1868
|
+
* Returns `undefined` if it cannot be determined.
|
|
1869
|
+
*/
|
|
1870
|
+
|
|
1871
|
+
function hasNextPage(options, pages) {
|
|
1872
|
+
if (options.getNextPageParam && Array.isArray(pages)) {
|
|
1873
|
+
const nextPageParam = getNextPageParam(options, pages);
|
|
1874
|
+
return typeof nextPageParam !== 'undefined' && nextPageParam !== null && nextPageParam !== false;
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
return;
|
|
1878
|
+
}
|
|
1879
|
+
/**
|
|
1880
|
+
* Checks if there is a previous page.
|
|
1881
|
+
* Returns `undefined` if it cannot be determined.
|
|
1882
|
+
*/
|
|
1883
|
+
|
|
1884
|
+
function hasPreviousPage(options, pages) {
|
|
1885
|
+
if (options.getPreviousPageParam && Array.isArray(pages)) {
|
|
1886
|
+
const previousPageParam = getPreviousPageParam(options, pages);
|
|
1887
|
+
return typeof previousPageParam !== 'undefined' && previousPageParam !== null && previousPageParam !== false;
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
return;
|
|
1891
|
+
}
|
|
1892
|
+
|
|
1893
|
+
// CLASS
|
|
1894
|
+
class QueryClient {
|
|
1895
|
+
constructor(config = {}) {
|
|
1896
|
+
this.queryCache = config.queryCache || new QueryCache();
|
|
1897
|
+
this.mutationCache = config.mutationCache || new MutationCache();
|
|
1898
|
+
this.logger = config.logger || defaultLogger;
|
|
1899
|
+
this.defaultOptions = config.defaultOptions || {};
|
|
1900
|
+
this.queryDefaults = [];
|
|
1901
|
+
this.mutationDefaults = [];
|
|
1902
|
+
this.mountCount = 0;
|
|
1903
|
+
|
|
1904
|
+
if (config.logger) {
|
|
1905
|
+
this.logger.error("Passing a custom logger has been deprecated and will be removed in the next major version.");
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
mount() {
|
|
1910
|
+
this.mountCount++;
|
|
1911
|
+
if (this.mountCount !== 1) return;
|
|
1912
|
+
this.unsubscribeFocus = focusManager.subscribe(() => {
|
|
1913
|
+
if (focusManager.isFocused()) {
|
|
1914
|
+
this.resumePausedMutations();
|
|
1915
|
+
this.queryCache.onFocus();
|
|
1916
|
+
}
|
|
1917
|
+
});
|
|
1918
|
+
this.unsubscribeOnline = onlineManager.subscribe(() => {
|
|
1919
|
+
if (onlineManager.isOnline()) {
|
|
1920
|
+
this.resumePausedMutations();
|
|
1921
|
+
this.queryCache.onOnline();
|
|
1922
|
+
}
|
|
1923
|
+
});
|
|
1924
|
+
}
|
|
1925
|
+
|
|
1926
|
+
unmount() {
|
|
1927
|
+
var _this$unsubscribeFocu, _this$unsubscribeOnli;
|
|
1928
|
+
|
|
1929
|
+
this.mountCount--;
|
|
1930
|
+
if (this.mountCount !== 0) return;
|
|
1931
|
+
(_this$unsubscribeFocu = this.unsubscribeFocus) == null ? void 0 : _this$unsubscribeFocu.call(this);
|
|
1932
|
+
this.unsubscribeFocus = undefined;
|
|
1933
|
+
(_this$unsubscribeOnli = this.unsubscribeOnline) == null ? void 0 : _this$unsubscribeOnli.call(this);
|
|
1934
|
+
this.unsubscribeOnline = undefined;
|
|
1935
|
+
}
|
|
1936
|
+
|
|
1937
|
+
/**
|
|
1938
|
+
* @deprecated This method should be used with only one object argument.
|
|
1939
|
+
*/
|
|
1940
|
+
isFetching(arg1, arg2) {
|
|
1941
|
+
const [filters] = parseFilterArgs(arg1, arg2);
|
|
1942
|
+
filters.fetchStatus = 'fetching';
|
|
1943
|
+
return this.queryCache.findAll(filters).length;
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
isMutating(filters) {
|
|
1947
|
+
return this.mutationCache.findAll({ ...filters,
|
|
1948
|
+
fetching: true
|
|
1949
|
+
}).length;
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
/**
|
|
1953
|
+
* @deprecated This method will accept only queryKey in the next major version.
|
|
1954
|
+
*/
|
|
1955
|
+
getQueryData(queryKey, filters) {
|
|
1956
|
+
var _this$queryCache$find;
|
|
1957
|
+
|
|
1958
|
+
return (_this$queryCache$find = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find.state.data;
|
|
1959
|
+
}
|
|
1960
|
+
|
|
1961
|
+
/**
|
|
1962
|
+
* @deprecated This method should be used with only one object argument.
|
|
1963
|
+
*/
|
|
1964
|
+
ensureQueryData(arg1, arg2, arg3) {
|
|
1965
|
+
const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
|
|
1966
|
+
const cachedData = this.getQueryData(parsedOptions.queryKey);
|
|
1967
|
+
return cachedData ? Promise.resolve(cachedData) : this.fetchQuery(parsedOptions);
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
/**
|
|
1971
|
+
* @deprecated This method should be used with only one object argument.
|
|
1972
|
+
*/
|
|
1973
|
+
getQueriesData(queryKeyOrFilters) {
|
|
1974
|
+
return this.getQueryCache().findAll(queryKeyOrFilters).map(({
|
|
1975
|
+
queryKey,
|
|
1976
|
+
state
|
|
1977
|
+
}) => {
|
|
1978
|
+
const data = state.data;
|
|
1979
|
+
return [queryKey, data];
|
|
1980
|
+
});
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
setQueryData(queryKey, updater, options) {
|
|
1984
|
+
const query = this.queryCache.find(queryKey);
|
|
1985
|
+
const prevData = query == null ? void 0 : query.state.data;
|
|
1986
|
+
const data = functionalUpdate(updater, prevData);
|
|
1987
|
+
|
|
1988
|
+
if (typeof data === 'undefined') {
|
|
1989
|
+
return undefined;
|
|
1990
|
+
}
|
|
1991
|
+
|
|
1992
|
+
const parsedOptions = parseQueryArgs(queryKey);
|
|
1993
|
+
const defaultedOptions = this.defaultQueryOptions(parsedOptions);
|
|
1994
|
+
return this.queryCache.build(this, defaultedOptions).setData(data, { ...options,
|
|
1995
|
+
manual: true
|
|
1996
|
+
});
|
|
1997
|
+
}
|
|
1998
|
+
|
|
1999
|
+
/**
|
|
2000
|
+
* @deprecated This method should be used with only one object argument.
|
|
2001
|
+
*/
|
|
2002
|
+
setQueriesData(queryKeyOrFilters, updater, options) {
|
|
2003
|
+
return notifyManager.batch(() => this.getQueryCache().findAll(queryKeyOrFilters).map(({
|
|
2004
|
+
queryKey
|
|
2005
|
+
}) => [queryKey, this.setQueryData(queryKey, updater, options)]));
|
|
2006
|
+
}
|
|
2007
|
+
|
|
2008
|
+
getQueryState(queryKey,
|
|
2009
|
+
/**
|
|
2010
|
+
* @deprecated This filters will be removed in the next major version.
|
|
2011
|
+
*/
|
|
2012
|
+
filters) {
|
|
2013
|
+
var _this$queryCache$find2;
|
|
2014
|
+
|
|
2015
|
+
return (_this$queryCache$find2 = this.queryCache.find(queryKey, filters)) == null ? void 0 : _this$queryCache$find2.state;
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
/**
|
|
2019
|
+
* @deprecated This method should be used with only one object argument.
|
|
2020
|
+
*/
|
|
2021
|
+
removeQueries(arg1, arg2) {
|
|
2022
|
+
const [filters] = parseFilterArgs(arg1, arg2);
|
|
2023
|
+
const queryCache = this.queryCache;
|
|
2024
|
+
notifyManager.batch(() => {
|
|
2025
|
+
queryCache.findAll(filters).forEach(query => {
|
|
2026
|
+
queryCache.remove(query);
|
|
2027
|
+
});
|
|
2028
|
+
});
|
|
2029
|
+
}
|
|
2030
|
+
|
|
2031
|
+
/**
|
|
2032
|
+
* @deprecated This method should be used with only one object argument.
|
|
2033
|
+
*/
|
|
2034
|
+
resetQueries(arg1, arg2, arg3) {
|
|
2035
|
+
const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
|
|
2036
|
+
const queryCache = this.queryCache;
|
|
2037
|
+
const refetchFilters = {
|
|
2038
|
+
type: 'active',
|
|
2039
|
+
...filters
|
|
2040
|
+
};
|
|
2041
|
+
return notifyManager.batch(() => {
|
|
2042
|
+
queryCache.findAll(filters).forEach(query => {
|
|
2043
|
+
query.reset();
|
|
2044
|
+
});
|
|
2045
|
+
return this.refetchQueries(refetchFilters, options);
|
|
2046
|
+
});
|
|
2047
|
+
}
|
|
2048
|
+
|
|
2049
|
+
/**
|
|
2050
|
+
* @deprecated This method should be used with only one object argument.
|
|
2051
|
+
*/
|
|
2052
|
+
cancelQueries(arg1, arg2, arg3) {
|
|
2053
|
+
const [filters, cancelOptions = {}] = parseFilterArgs(arg1, arg2, arg3);
|
|
2054
|
+
|
|
2055
|
+
if (typeof cancelOptions.revert === 'undefined') {
|
|
2056
|
+
cancelOptions.revert = true;
|
|
2057
|
+
}
|
|
2058
|
+
|
|
2059
|
+
const promises = notifyManager.batch(() => this.queryCache.findAll(filters).map(query => query.cancel(cancelOptions)));
|
|
2060
|
+
return Promise.all(promises).then(noop).catch(noop);
|
|
2061
|
+
}
|
|
2062
|
+
|
|
2063
|
+
/**
|
|
2064
|
+
* @deprecated This method should be used with only one object argument.
|
|
2065
|
+
*/
|
|
2066
|
+
invalidateQueries(arg1, arg2, arg3) {
|
|
2067
|
+
const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
|
|
2068
|
+
return notifyManager.batch(() => {
|
|
2069
|
+
var _ref, _filters$refetchType;
|
|
2070
|
+
|
|
2071
|
+
this.queryCache.findAll(filters).forEach(query => {
|
|
2072
|
+
query.invalidate();
|
|
2073
|
+
});
|
|
2074
|
+
|
|
2075
|
+
if (filters.refetchType === 'none') {
|
|
2076
|
+
return Promise.resolve();
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2079
|
+
const refetchFilters = { ...filters,
|
|
2080
|
+
type: (_ref = (_filters$refetchType = filters.refetchType) != null ? _filters$refetchType : filters.type) != null ? _ref : 'active'
|
|
2081
|
+
};
|
|
2082
|
+
return this.refetchQueries(refetchFilters, options);
|
|
2083
|
+
});
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
/**
|
|
2087
|
+
* @deprecated This method should be used with only one object argument.
|
|
2088
|
+
*/
|
|
2089
|
+
refetchQueries(arg1, arg2, arg3) {
|
|
2090
|
+
const [filters, options] = parseFilterArgs(arg1, arg2, arg3);
|
|
2091
|
+
const promises = notifyManager.batch(() => this.queryCache.findAll(filters).filter(query => !query.isDisabled()).map(query => {
|
|
2092
|
+
var _options$cancelRefetc;
|
|
2093
|
+
|
|
2094
|
+
return query.fetch(undefined, { ...options,
|
|
2095
|
+
cancelRefetch: (_options$cancelRefetc = options == null ? void 0 : options.cancelRefetch) != null ? _options$cancelRefetc : true,
|
|
2096
|
+
meta: {
|
|
2097
|
+
refetchPage: filters.refetchPage
|
|
2098
|
+
}
|
|
2099
|
+
});
|
|
2100
|
+
}));
|
|
2101
|
+
let promise = Promise.all(promises).then(noop);
|
|
2102
|
+
|
|
2103
|
+
if (!(options != null && options.throwOnError)) {
|
|
2104
|
+
promise = promise.catch(noop);
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
return promise;
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2110
|
+
/**
|
|
2111
|
+
* @deprecated This method should be used with only one object argument.
|
|
2112
|
+
*/
|
|
2113
|
+
fetchQuery(arg1, arg2, arg3) {
|
|
2114
|
+
const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
|
|
2115
|
+
const defaultedOptions = this.defaultQueryOptions(parsedOptions); // https://github.com/tannerlinsley/react-query/issues/652
|
|
2116
|
+
|
|
2117
|
+
if (typeof defaultedOptions.retry === 'undefined') {
|
|
2118
|
+
defaultedOptions.retry = false;
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
const query = this.queryCache.build(this, defaultedOptions);
|
|
2122
|
+
return query.isStaleByTime(defaultedOptions.staleTime) ? query.fetch(defaultedOptions) : Promise.resolve(query.state.data);
|
|
2123
|
+
}
|
|
2124
|
+
|
|
2125
|
+
/**
|
|
2126
|
+
* @deprecated This method should be used with only one object argument.
|
|
2127
|
+
*/
|
|
2128
|
+
prefetchQuery(arg1, arg2, arg3) {
|
|
2129
|
+
return this.fetchQuery(arg1, arg2, arg3).then(noop).catch(noop);
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
/**
|
|
2133
|
+
* @deprecated This method should be used with only one object argument.
|
|
2134
|
+
*/
|
|
2135
|
+
fetchInfiniteQuery(arg1, arg2, arg3) {
|
|
2136
|
+
const parsedOptions = parseQueryArgs(arg1, arg2, arg3);
|
|
2137
|
+
parsedOptions.behavior = infiniteQueryBehavior();
|
|
2138
|
+
return this.fetchQuery(parsedOptions);
|
|
2139
|
+
}
|
|
2140
|
+
|
|
2141
|
+
/**
|
|
2142
|
+
* @deprecated This method should be used with only one object argument.
|
|
2143
|
+
*/
|
|
2144
|
+
prefetchInfiniteQuery(arg1, arg2, arg3) {
|
|
2145
|
+
return this.fetchInfiniteQuery(arg1, arg2, arg3).then(noop).catch(noop);
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
resumePausedMutations() {
|
|
2149
|
+
return this.mutationCache.resumePausedMutations();
|
|
2150
|
+
}
|
|
2151
|
+
|
|
2152
|
+
getQueryCache() {
|
|
2153
|
+
return this.queryCache;
|
|
2154
|
+
}
|
|
2155
|
+
|
|
2156
|
+
getMutationCache() {
|
|
2157
|
+
return this.mutationCache;
|
|
2158
|
+
}
|
|
2159
|
+
|
|
2160
|
+
getLogger() {
|
|
2161
|
+
return this.logger;
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
getDefaultOptions() {
|
|
2165
|
+
return this.defaultOptions;
|
|
2166
|
+
}
|
|
2167
|
+
|
|
2168
|
+
setDefaultOptions(options) {
|
|
2169
|
+
this.defaultOptions = options;
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
setQueryDefaults(queryKey, options) {
|
|
2173
|
+
const result = this.queryDefaults.find(x => hashQueryKey(queryKey) === hashQueryKey(x.queryKey));
|
|
2174
|
+
|
|
2175
|
+
if (result) {
|
|
2176
|
+
result.defaultOptions = options;
|
|
2177
|
+
} else {
|
|
2178
|
+
this.queryDefaults.push({
|
|
2179
|
+
queryKey,
|
|
2180
|
+
defaultOptions: options
|
|
2181
|
+
});
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
|
|
2185
|
+
getQueryDefaults(queryKey) {
|
|
2186
|
+
if (!queryKey) {
|
|
2187
|
+
return undefined;
|
|
2188
|
+
} // Get the first matching defaults
|
|
2189
|
+
|
|
2190
|
+
|
|
2191
|
+
const firstMatchingDefaults = this.queryDefaults.find(x => partialMatchKey(queryKey, x.queryKey)); // Additional checks and error in dev mode
|
|
2192
|
+
|
|
2193
|
+
{
|
|
2194
|
+
// Retrieve all matching defaults for the given key
|
|
2195
|
+
const matchingDefaults = this.queryDefaults.filter(x => partialMatchKey(queryKey, x.queryKey)); // It is ok not having defaults, but it is error prone to have more than 1 default for a given key
|
|
2196
|
+
|
|
2197
|
+
if (matchingDefaults.length > 1) {
|
|
2198
|
+
this.logger.error("[QueryClient] Several query defaults match with key '" + JSON.stringify(queryKey) + "'. The first matching query defaults are used. Please check how query defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydefaults.");
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
|
|
2202
|
+
return firstMatchingDefaults == null ? void 0 : firstMatchingDefaults.defaultOptions;
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2205
|
+
setMutationDefaults(mutationKey, options) {
|
|
2206
|
+
const result = this.mutationDefaults.find(x => hashQueryKey(mutationKey) === hashQueryKey(x.mutationKey));
|
|
2207
|
+
|
|
2208
|
+
if (result) {
|
|
2209
|
+
result.defaultOptions = options;
|
|
2210
|
+
} else {
|
|
2211
|
+
this.mutationDefaults.push({
|
|
2212
|
+
mutationKey,
|
|
2213
|
+
defaultOptions: options
|
|
2214
|
+
});
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
|
|
2218
|
+
getMutationDefaults(mutationKey) {
|
|
2219
|
+
if (!mutationKey) {
|
|
2220
|
+
return undefined;
|
|
2221
|
+
} // Get the first matching defaults
|
|
2222
|
+
|
|
2223
|
+
|
|
2224
|
+
const firstMatchingDefaults = this.mutationDefaults.find(x => partialMatchKey(mutationKey, x.mutationKey)); // Additional checks and error in dev mode
|
|
2225
|
+
|
|
2226
|
+
{
|
|
2227
|
+
// Retrieve all matching defaults for the given key
|
|
2228
|
+
const matchingDefaults = this.mutationDefaults.filter(x => partialMatchKey(mutationKey, x.mutationKey)); // It is ok not having defaults, but it is error prone to have more than 1 default for a given key
|
|
2229
|
+
|
|
2230
|
+
if (matchingDefaults.length > 1) {
|
|
2231
|
+
this.logger.error("[QueryClient] Several mutation defaults match with key '" + JSON.stringify(mutationKey) + "'. The first matching mutation defaults are used. Please check how mutation defaults are registered. Order does matter here. cf. https://react-query.tanstack.com/reference/QueryClient#queryclientsetmutationdefaults.");
|
|
2232
|
+
}
|
|
2233
|
+
}
|
|
2234
|
+
|
|
2235
|
+
return firstMatchingDefaults == null ? void 0 : firstMatchingDefaults.defaultOptions;
|
|
2236
|
+
}
|
|
2237
|
+
|
|
2238
|
+
defaultQueryOptions(options) {
|
|
2239
|
+
if (options != null && options._defaulted) {
|
|
2240
|
+
return options;
|
|
2241
|
+
}
|
|
2242
|
+
|
|
2243
|
+
const defaultedOptions = { ...this.defaultOptions.queries,
|
|
2244
|
+
...this.getQueryDefaults(options == null ? void 0 : options.queryKey),
|
|
2245
|
+
...options,
|
|
2246
|
+
_defaulted: true
|
|
2247
|
+
};
|
|
2248
|
+
|
|
2249
|
+
if (!defaultedOptions.queryHash && defaultedOptions.queryKey) {
|
|
2250
|
+
defaultedOptions.queryHash = hashQueryKeyByOptions(defaultedOptions.queryKey, defaultedOptions);
|
|
2251
|
+
} // dependent default values
|
|
2252
|
+
|
|
2253
|
+
|
|
2254
|
+
if (typeof defaultedOptions.refetchOnReconnect === 'undefined') {
|
|
2255
|
+
defaultedOptions.refetchOnReconnect = defaultedOptions.networkMode !== 'always';
|
|
2256
|
+
}
|
|
2257
|
+
|
|
2258
|
+
if (typeof defaultedOptions.useErrorBoundary === 'undefined') {
|
|
2259
|
+
defaultedOptions.useErrorBoundary = !!defaultedOptions.suspense;
|
|
2260
|
+
}
|
|
2261
|
+
|
|
2262
|
+
return defaultedOptions;
|
|
2263
|
+
}
|
|
2264
|
+
|
|
2265
|
+
defaultMutationOptions(options) {
|
|
2266
|
+
if (options != null && options._defaulted) {
|
|
2267
|
+
return options;
|
|
2268
|
+
}
|
|
2269
|
+
|
|
2270
|
+
return { ...this.defaultOptions.mutations,
|
|
2271
|
+
...this.getMutationDefaults(options == null ? void 0 : options.mutationKey),
|
|
2272
|
+
...options,
|
|
2273
|
+
_defaulted: true
|
|
2274
|
+
};
|
|
2275
|
+
}
|
|
2276
|
+
|
|
2277
|
+
clear() {
|
|
2278
|
+
this.queryCache.clear();
|
|
2279
|
+
this.mutationCache.clear();
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2282
|
+
}
|
|
2283
|
+
|
|
2284
|
+
class QueryObserver extends Subscribable {
|
|
2285
|
+
constructor(client, options) {
|
|
2286
|
+
super();
|
|
2287
|
+
this.client = client;
|
|
2288
|
+
this.options = options;
|
|
2289
|
+
this.trackedProps = new Set();
|
|
2290
|
+
this.selectError = null;
|
|
2291
|
+
this.bindMethods();
|
|
2292
|
+
this.setOptions(options);
|
|
2293
|
+
}
|
|
2294
|
+
|
|
2295
|
+
bindMethods() {
|
|
2296
|
+
this.remove = this.remove.bind(this);
|
|
2297
|
+
this.refetch = this.refetch.bind(this);
|
|
2298
|
+
}
|
|
2299
|
+
|
|
2300
|
+
onSubscribe() {
|
|
2301
|
+
if (this.listeners.size === 1) {
|
|
2302
|
+
this.currentQuery.addObserver(this);
|
|
2303
|
+
|
|
2304
|
+
if (shouldFetchOnMount(this.currentQuery, this.options)) {
|
|
2305
|
+
this.executeFetch();
|
|
2306
|
+
}
|
|
2307
|
+
|
|
2308
|
+
this.updateTimers();
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
|
|
2312
|
+
onUnsubscribe() {
|
|
2313
|
+
if (!this.hasListeners()) {
|
|
2314
|
+
this.destroy();
|
|
2315
|
+
}
|
|
2316
|
+
}
|
|
2317
|
+
|
|
2318
|
+
shouldFetchOnReconnect() {
|
|
2319
|
+
return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnReconnect);
|
|
2320
|
+
}
|
|
2321
|
+
|
|
2322
|
+
shouldFetchOnWindowFocus() {
|
|
2323
|
+
return shouldFetchOn(this.currentQuery, this.options, this.options.refetchOnWindowFocus);
|
|
2324
|
+
}
|
|
2325
|
+
|
|
2326
|
+
destroy() {
|
|
2327
|
+
this.listeners = new Set();
|
|
2328
|
+
this.clearStaleTimeout();
|
|
2329
|
+
this.clearRefetchInterval();
|
|
2330
|
+
this.currentQuery.removeObserver(this);
|
|
2331
|
+
}
|
|
2332
|
+
|
|
2333
|
+
setOptions(options, notifyOptions) {
|
|
2334
|
+
const prevOptions = this.options;
|
|
2335
|
+
const prevQuery = this.currentQuery;
|
|
2336
|
+
this.options = this.client.defaultQueryOptions(options);
|
|
2337
|
+
|
|
2338
|
+
if (typeof (options == null ? void 0 : options.isDataEqual) !== 'undefined') {
|
|
2339
|
+
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");
|
|
2340
|
+
}
|
|
2341
|
+
|
|
2342
|
+
if (!shallowEqualObjects(prevOptions, this.options)) {
|
|
2343
|
+
this.client.getQueryCache().notify({
|
|
2344
|
+
type: 'observerOptionsUpdated',
|
|
2345
|
+
query: this.currentQuery,
|
|
2346
|
+
observer: this
|
|
2347
|
+
});
|
|
2348
|
+
}
|
|
2349
|
+
|
|
2350
|
+
if (typeof this.options.enabled !== 'undefined' && typeof this.options.enabled !== 'boolean') {
|
|
2351
|
+
throw new Error('Expected enabled to be a boolean');
|
|
2352
|
+
} // Keep previous query key if the user does not supply one
|
|
2353
|
+
|
|
2354
|
+
|
|
2355
|
+
if (!this.options.queryKey) {
|
|
2356
|
+
this.options.queryKey = prevOptions.queryKey;
|
|
2357
|
+
}
|
|
2358
|
+
|
|
2359
|
+
this.updateQuery();
|
|
2360
|
+
const mounted = this.hasListeners(); // Fetch if there are subscribers
|
|
2361
|
+
|
|
2362
|
+
if (mounted && shouldFetchOptionally(this.currentQuery, prevQuery, this.options, prevOptions)) {
|
|
2363
|
+
this.executeFetch();
|
|
2364
|
+
} // Update result
|
|
2365
|
+
|
|
2366
|
+
|
|
2367
|
+
this.updateResult(notifyOptions); // Update stale interval if needed
|
|
2368
|
+
|
|
2369
|
+
if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || this.options.staleTime !== prevOptions.staleTime)) {
|
|
2370
|
+
this.updateStaleTimeout();
|
|
2371
|
+
}
|
|
2372
|
+
|
|
2373
|
+
const nextRefetchInterval = this.computeRefetchInterval(); // Update refetch interval if needed
|
|
2374
|
+
|
|
2375
|
+
if (mounted && (this.currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || nextRefetchInterval !== this.currentRefetchInterval)) {
|
|
2376
|
+
this.updateRefetchInterval(nextRefetchInterval);
|
|
2377
|
+
}
|
|
2378
|
+
}
|
|
2379
|
+
|
|
2380
|
+
getOptimisticResult(options) {
|
|
2381
|
+
const query = this.client.getQueryCache().build(this.client, options);
|
|
2382
|
+
const result = this.createResult(query, options);
|
|
2383
|
+
|
|
2384
|
+
if (shouldAssignObserverCurrentProperties(this, result, options)) {
|
|
2385
|
+
// this assigns the optimistic result to the current Observer
|
|
2386
|
+
// because if the query function changes, useQuery will be performing
|
|
2387
|
+
// an effect where it would fetch again.
|
|
2388
|
+
// When the fetch finishes, we perform a deep data cloning in order
|
|
2389
|
+
// to reuse objects references. This deep data clone is performed against
|
|
2390
|
+
// the `observer.currentResult.data` property
|
|
2391
|
+
// When QueryKey changes, we refresh the query and get new `optimistic`
|
|
2392
|
+
// result, while we leave the `observer.currentResult`, so when new data
|
|
2393
|
+
// arrives, it finds the old `observer.currentResult` which is related
|
|
2394
|
+
// to the old QueryKey. Which means that currentResult and selectData are
|
|
2395
|
+
// out of sync already.
|
|
2396
|
+
// To solve this, we move the cursor of the currentResult everytime
|
|
2397
|
+
// an observer reads an optimistic value.
|
|
2398
|
+
// When keeping the previous data, the result doesn't change until new
|
|
2399
|
+
// data arrives.
|
|
2400
|
+
this.currentResult = result;
|
|
2401
|
+
this.currentResultOptions = this.options;
|
|
2402
|
+
this.currentResultState = this.currentQuery.state;
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
return result;
|
|
2406
|
+
}
|
|
2407
|
+
|
|
2408
|
+
getCurrentResult() {
|
|
2409
|
+
return this.currentResult;
|
|
2410
|
+
}
|
|
2411
|
+
|
|
2412
|
+
trackResult(result) {
|
|
2413
|
+
const trackedResult = {};
|
|
2414
|
+
Object.keys(result).forEach(key => {
|
|
2415
|
+
Object.defineProperty(trackedResult, key, {
|
|
2416
|
+
configurable: false,
|
|
2417
|
+
enumerable: true,
|
|
2418
|
+
get: () => {
|
|
2419
|
+
this.trackedProps.add(key);
|
|
2420
|
+
return result[key];
|
|
2421
|
+
}
|
|
2422
|
+
});
|
|
2423
|
+
});
|
|
2424
|
+
return trackedResult;
|
|
2425
|
+
}
|
|
2426
|
+
|
|
2427
|
+
getCurrentQuery() {
|
|
2428
|
+
return this.currentQuery;
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
remove() {
|
|
2432
|
+
this.client.getQueryCache().remove(this.currentQuery);
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2435
|
+
refetch({
|
|
2436
|
+
refetchPage,
|
|
2437
|
+
...options
|
|
2438
|
+
} = {}) {
|
|
2439
|
+
return this.fetch({ ...options,
|
|
2440
|
+
meta: {
|
|
2441
|
+
refetchPage
|
|
2442
|
+
}
|
|
2443
|
+
});
|
|
2444
|
+
}
|
|
2445
|
+
|
|
2446
|
+
fetchOptimistic(options) {
|
|
2447
|
+
const defaultedOptions = this.client.defaultQueryOptions(options);
|
|
2448
|
+
const query = this.client.getQueryCache().build(this.client, defaultedOptions);
|
|
2449
|
+
query.isFetchingOptimistic = true;
|
|
2450
|
+
return query.fetch().then(() => this.createResult(query, defaultedOptions));
|
|
2451
|
+
}
|
|
2452
|
+
|
|
2453
|
+
fetch(fetchOptions) {
|
|
2454
|
+
var _fetchOptions$cancelR;
|
|
2455
|
+
|
|
2456
|
+
return this.executeFetch({ ...fetchOptions,
|
|
2457
|
+
cancelRefetch: (_fetchOptions$cancelR = fetchOptions.cancelRefetch) != null ? _fetchOptions$cancelR : true
|
|
2458
|
+
}).then(() => {
|
|
2459
|
+
this.updateResult();
|
|
2460
|
+
return this.currentResult;
|
|
2461
|
+
});
|
|
2462
|
+
}
|
|
2463
|
+
|
|
2464
|
+
executeFetch(fetchOptions) {
|
|
2465
|
+
// Make sure we reference the latest query as the current one might have been removed
|
|
2466
|
+
this.updateQuery(); // Fetch
|
|
2467
|
+
|
|
2468
|
+
let promise = this.currentQuery.fetch(this.options, fetchOptions);
|
|
2469
|
+
|
|
2470
|
+
if (!(fetchOptions != null && fetchOptions.throwOnError)) {
|
|
2471
|
+
promise = promise.catch(noop);
|
|
2472
|
+
}
|
|
2473
|
+
|
|
2474
|
+
return promise;
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
updateStaleTimeout() {
|
|
2478
|
+
this.clearStaleTimeout();
|
|
2479
|
+
|
|
2480
|
+
if (isServer || this.currentResult.isStale || !isValidTimeout(this.options.staleTime)) {
|
|
2481
|
+
return;
|
|
2482
|
+
}
|
|
2483
|
+
|
|
2484
|
+
const time = timeUntilStale(this.currentResult.dataUpdatedAt, this.options.staleTime); // The timeout is sometimes triggered 1 ms before the stale time expiration.
|
|
2485
|
+
// To mitigate this issue we always add 1 ms to the timeout.
|
|
2486
|
+
|
|
2487
|
+
const timeout = time + 1;
|
|
2488
|
+
this.staleTimeoutId = setTimeout(() => {
|
|
2489
|
+
if (!this.currentResult.isStale) {
|
|
2490
|
+
this.updateResult();
|
|
2491
|
+
}
|
|
2492
|
+
}, timeout);
|
|
2493
|
+
}
|
|
2494
|
+
|
|
2495
|
+
computeRefetchInterval() {
|
|
2496
|
+
var _this$options$refetch;
|
|
2497
|
+
|
|
2498
|
+
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;
|
|
2499
|
+
}
|
|
2500
|
+
|
|
2501
|
+
updateRefetchInterval(nextInterval) {
|
|
2502
|
+
this.clearRefetchInterval();
|
|
2503
|
+
this.currentRefetchInterval = nextInterval;
|
|
2504
|
+
|
|
2505
|
+
if (isServer || this.options.enabled === false || !isValidTimeout(this.currentRefetchInterval) || this.currentRefetchInterval === 0) {
|
|
2506
|
+
return;
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2509
|
+
this.refetchIntervalId = setInterval(() => {
|
|
2510
|
+
if (this.options.refetchIntervalInBackground || focusManager.isFocused()) {
|
|
2511
|
+
this.executeFetch();
|
|
2512
|
+
}
|
|
2513
|
+
}, this.currentRefetchInterval);
|
|
2514
|
+
}
|
|
2515
|
+
|
|
2516
|
+
updateTimers() {
|
|
2517
|
+
this.updateStaleTimeout();
|
|
2518
|
+
this.updateRefetchInterval(this.computeRefetchInterval());
|
|
2519
|
+
}
|
|
2520
|
+
|
|
2521
|
+
clearStaleTimeout() {
|
|
2522
|
+
if (this.staleTimeoutId) {
|
|
2523
|
+
clearTimeout(this.staleTimeoutId);
|
|
2524
|
+
this.staleTimeoutId = undefined;
|
|
2525
|
+
}
|
|
2526
|
+
}
|
|
2527
|
+
|
|
2528
|
+
clearRefetchInterval() {
|
|
2529
|
+
if (this.refetchIntervalId) {
|
|
2530
|
+
clearInterval(this.refetchIntervalId);
|
|
2531
|
+
this.refetchIntervalId = undefined;
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
2534
|
+
|
|
2535
|
+
createResult(query, options) {
|
|
2536
|
+
const prevQuery = this.currentQuery;
|
|
2537
|
+
const prevOptions = this.options;
|
|
2538
|
+
const prevResult = this.currentResult;
|
|
2539
|
+
const prevResultState = this.currentResultState;
|
|
2540
|
+
const prevResultOptions = this.currentResultOptions;
|
|
2541
|
+
const queryChange = query !== prevQuery;
|
|
2542
|
+
const queryInitialState = queryChange ? query.state : this.currentQueryInitialState;
|
|
2543
|
+
const prevQueryResult = queryChange ? this.currentResult : this.previousQueryResult;
|
|
2544
|
+
const {
|
|
2545
|
+
state
|
|
2546
|
+
} = query;
|
|
2547
|
+
let {
|
|
2548
|
+
dataUpdatedAt,
|
|
2549
|
+
error,
|
|
2550
|
+
errorUpdatedAt,
|
|
2551
|
+
fetchStatus,
|
|
2552
|
+
status
|
|
2553
|
+
} = state;
|
|
2554
|
+
let isPreviousData = false;
|
|
2555
|
+
let isPlaceholderData = false;
|
|
2556
|
+
let data; // Optimistically set result in fetching state if needed
|
|
2557
|
+
|
|
2558
|
+
if (options._optimisticResults) {
|
|
2559
|
+
const mounted = this.hasListeners();
|
|
2560
|
+
const fetchOnMount = !mounted && shouldFetchOnMount(query, options);
|
|
2561
|
+
const fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions);
|
|
2562
|
+
|
|
2563
|
+
if (fetchOnMount || fetchOptionally) {
|
|
2564
|
+
fetchStatus = canFetch(query.options.networkMode) ? 'fetching' : 'paused';
|
|
2565
|
+
|
|
2566
|
+
if (!dataUpdatedAt) {
|
|
2567
|
+
status = 'loading';
|
|
2568
|
+
}
|
|
2569
|
+
}
|
|
2570
|
+
|
|
2571
|
+
if (options._optimisticResults === 'isRestoring') {
|
|
2572
|
+
fetchStatus = 'idle';
|
|
2573
|
+
}
|
|
2574
|
+
} // Keep previous data if needed
|
|
2575
|
+
|
|
2576
|
+
|
|
2577
|
+
if (options.keepPreviousData && !state.dataUpdatedAt && prevQueryResult != null && prevQueryResult.isSuccess && status !== 'error') {
|
|
2578
|
+
data = prevQueryResult.data;
|
|
2579
|
+
dataUpdatedAt = prevQueryResult.dataUpdatedAt;
|
|
2580
|
+
status = prevQueryResult.status;
|
|
2581
|
+
isPreviousData = true;
|
|
2582
|
+
} // Select data if needed
|
|
2583
|
+
else if (options.select && typeof state.data !== 'undefined') {
|
|
2584
|
+
// Memoize select result
|
|
2585
|
+
if (prevResult && state.data === (prevResultState == null ? void 0 : prevResultState.data) && options.select === this.selectFn) {
|
|
2586
|
+
data = this.selectResult;
|
|
2587
|
+
} else {
|
|
2588
|
+
try {
|
|
2589
|
+
this.selectFn = options.select;
|
|
2590
|
+
data = options.select(state.data);
|
|
2591
|
+
data = replaceData(prevResult == null ? void 0 : prevResult.data, data, options);
|
|
2592
|
+
this.selectResult = data;
|
|
2593
|
+
this.selectError = null;
|
|
2594
|
+
} catch (selectError) {
|
|
2595
|
+
{
|
|
2596
|
+
this.client.getLogger().error(selectError);
|
|
2597
|
+
}
|
|
2598
|
+
|
|
2599
|
+
this.selectError = selectError;
|
|
2600
|
+
}
|
|
2601
|
+
}
|
|
2602
|
+
} // Use query data
|
|
2603
|
+
else {
|
|
2604
|
+
data = state.data;
|
|
2605
|
+
} // Show placeholder data if needed
|
|
2606
|
+
|
|
2607
|
+
|
|
2608
|
+
if (typeof options.placeholderData !== 'undefined' && typeof data === 'undefined' && status === 'loading') {
|
|
2609
|
+
let placeholderData; // Memoize placeholder data
|
|
2610
|
+
|
|
2611
|
+
if (prevResult != null && prevResult.isPlaceholderData && options.placeholderData === (prevResultOptions == null ? void 0 : prevResultOptions.placeholderData)) {
|
|
2612
|
+
placeholderData = prevResult.data;
|
|
2613
|
+
} else {
|
|
2614
|
+
placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData() : options.placeholderData;
|
|
2615
|
+
|
|
2616
|
+
if (options.select && typeof placeholderData !== 'undefined') {
|
|
2617
|
+
try {
|
|
2618
|
+
placeholderData = options.select(placeholderData);
|
|
2619
|
+
this.selectError = null;
|
|
2620
|
+
} catch (selectError) {
|
|
2621
|
+
{
|
|
2622
|
+
this.client.getLogger().error(selectError);
|
|
2623
|
+
}
|
|
2624
|
+
|
|
2625
|
+
this.selectError = selectError;
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
}
|
|
2629
|
+
|
|
2630
|
+
if (typeof placeholderData !== 'undefined') {
|
|
2631
|
+
status = 'success';
|
|
2632
|
+
data = replaceData(prevResult == null ? void 0 : prevResult.data, placeholderData, options);
|
|
2633
|
+
isPlaceholderData = true;
|
|
2634
|
+
}
|
|
2635
|
+
}
|
|
2636
|
+
|
|
2637
|
+
if (this.selectError) {
|
|
2638
|
+
error = this.selectError;
|
|
2639
|
+
data = this.selectResult;
|
|
2640
|
+
errorUpdatedAt = Date.now();
|
|
2641
|
+
status = 'error';
|
|
2642
|
+
}
|
|
2643
|
+
|
|
2644
|
+
const isFetching = fetchStatus === 'fetching';
|
|
2645
|
+
const isLoading = status === 'loading';
|
|
2646
|
+
const isError = status === 'error';
|
|
2647
|
+
const result = {
|
|
2648
|
+
status,
|
|
2649
|
+
fetchStatus,
|
|
2650
|
+
isLoading,
|
|
2651
|
+
isSuccess: status === 'success',
|
|
2652
|
+
isError,
|
|
2653
|
+
isInitialLoading: isLoading && isFetching,
|
|
2654
|
+
data,
|
|
2655
|
+
dataUpdatedAt,
|
|
2656
|
+
error,
|
|
2657
|
+
errorUpdatedAt,
|
|
2658
|
+
failureCount: state.fetchFailureCount,
|
|
2659
|
+
failureReason: state.fetchFailureReason,
|
|
2660
|
+
errorUpdateCount: state.errorUpdateCount,
|
|
2661
|
+
isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,
|
|
2662
|
+
isFetchedAfterMount: state.dataUpdateCount > queryInitialState.dataUpdateCount || state.errorUpdateCount > queryInitialState.errorUpdateCount,
|
|
2663
|
+
isFetching,
|
|
2664
|
+
isRefetching: isFetching && !isLoading,
|
|
2665
|
+
isLoadingError: isError && state.dataUpdatedAt === 0,
|
|
2666
|
+
isPaused: fetchStatus === 'paused',
|
|
2667
|
+
isPlaceholderData,
|
|
2668
|
+
isPreviousData,
|
|
2669
|
+
isRefetchError: isError && state.dataUpdatedAt !== 0,
|
|
2670
|
+
isStale: isStale(query, options),
|
|
2671
|
+
refetch: this.refetch,
|
|
2672
|
+
remove: this.remove
|
|
2673
|
+
};
|
|
2674
|
+
return result;
|
|
2675
|
+
}
|
|
2676
|
+
|
|
2677
|
+
updateResult(notifyOptions) {
|
|
2678
|
+
const prevResult = this.currentResult;
|
|
2679
|
+
const nextResult = this.createResult(this.currentQuery, this.options);
|
|
2680
|
+
this.currentResultState = this.currentQuery.state;
|
|
2681
|
+
this.currentResultOptions = this.options; // Only notify and update result if something has changed
|
|
2682
|
+
|
|
2683
|
+
if (shallowEqualObjects(nextResult, prevResult)) {
|
|
2684
|
+
return;
|
|
2685
|
+
}
|
|
2686
|
+
|
|
2687
|
+
this.currentResult = nextResult; // Determine which callbacks to trigger
|
|
2688
|
+
|
|
2689
|
+
const defaultNotifyOptions = {
|
|
2690
|
+
cache: true
|
|
2691
|
+
};
|
|
2692
|
+
|
|
2693
|
+
const shouldNotifyListeners = () => {
|
|
2694
|
+
if (!prevResult) {
|
|
2695
|
+
return true;
|
|
2696
|
+
}
|
|
2697
|
+
|
|
2698
|
+
const {
|
|
2699
|
+
notifyOnChangeProps
|
|
2700
|
+
} = this.options;
|
|
2701
|
+
const notifyOnChangePropsValue = typeof notifyOnChangeProps === 'function' ? notifyOnChangeProps() : notifyOnChangeProps;
|
|
2702
|
+
|
|
2703
|
+
if (notifyOnChangePropsValue === 'all' || !notifyOnChangePropsValue && !this.trackedProps.size) {
|
|
2704
|
+
return true;
|
|
2705
|
+
}
|
|
2706
|
+
|
|
2707
|
+
const includedProps = new Set(notifyOnChangePropsValue != null ? notifyOnChangePropsValue : this.trackedProps);
|
|
2708
|
+
|
|
2709
|
+
if (this.options.useErrorBoundary) {
|
|
2710
|
+
includedProps.add('error');
|
|
2711
|
+
}
|
|
2712
|
+
|
|
2713
|
+
return Object.keys(this.currentResult).some(key => {
|
|
2714
|
+
const typedKey = key;
|
|
2715
|
+
const changed = this.currentResult[typedKey] !== prevResult[typedKey];
|
|
2716
|
+
return changed && includedProps.has(typedKey);
|
|
2717
|
+
});
|
|
2718
|
+
};
|
|
2719
|
+
|
|
2720
|
+
if ((notifyOptions == null ? void 0 : notifyOptions.listeners) !== false && shouldNotifyListeners()) {
|
|
2721
|
+
defaultNotifyOptions.listeners = true;
|
|
2722
|
+
}
|
|
2723
|
+
|
|
2724
|
+
this.notify({ ...defaultNotifyOptions,
|
|
2725
|
+
...notifyOptions
|
|
2726
|
+
});
|
|
2727
|
+
}
|
|
2728
|
+
|
|
2729
|
+
updateQuery() {
|
|
2730
|
+
const query = this.client.getQueryCache().build(this.client, this.options);
|
|
2731
|
+
|
|
2732
|
+
if (query === this.currentQuery) {
|
|
2733
|
+
return;
|
|
2734
|
+
}
|
|
2735
|
+
|
|
2736
|
+
const prevQuery = this.currentQuery;
|
|
2737
|
+
this.currentQuery = query;
|
|
2738
|
+
this.currentQueryInitialState = query.state;
|
|
2739
|
+
this.previousQueryResult = this.currentResult;
|
|
2740
|
+
|
|
2741
|
+
if (this.hasListeners()) {
|
|
2742
|
+
prevQuery == null ? void 0 : prevQuery.removeObserver(this);
|
|
2743
|
+
query.addObserver(this);
|
|
2744
|
+
}
|
|
2745
|
+
}
|
|
2746
|
+
|
|
2747
|
+
onQueryUpdate(action) {
|
|
2748
|
+
const notifyOptions = {};
|
|
2749
|
+
|
|
2750
|
+
if (action.type === 'success') {
|
|
2751
|
+
notifyOptions.onSuccess = !action.manual;
|
|
2752
|
+
} else if (action.type === 'error' && !isCancelledError(action.error)) {
|
|
2753
|
+
notifyOptions.onError = true;
|
|
2754
|
+
}
|
|
2755
|
+
|
|
2756
|
+
this.updateResult(notifyOptions);
|
|
2757
|
+
|
|
2758
|
+
if (this.hasListeners()) {
|
|
2759
|
+
this.updateTimers();
|
|
2760
|
+
}
|
|
2761
|
+
}
|
|
2762
|
+
|
|
2763
|
+
notify(notifyOptions) {
|
|
2764
|
+
notifyManager.batch(() => {
|
|
2765
|
+
// First trigger the configuration callbacks
|
|
2766
|
+
if (notifyOptions.onSuccess) {
|
|
2767
|
+
var _this$options$onSucce, _this$options, _this$options$onSettl, _this$options2;
|
|
2768
|
+
|
|
2769
|
+
(_this$options$onSucce = (_this$options = this.options).onSuccess) == null ? void 0 : _this$options$onSucce.call(_this$options, this.currentResult.data);
|
|
2770
|
+
(_this$options$onSettl = (_this$options2 = this.options).onSettled) == null ? void 0 : _this$options$onSettl.call(_this$options2, this.currentResult.data, null);
|
|
2771
|
+
} else if (notifyOptions.onError) {
|
|
2772
|
+
var _this$options$onError, _this$options3, _this$options$onSettl2, _this$options4;
|
|
2773
|
+
|
|
2774
|
+
(_this$options$onError = (_this$options3 = this.options).onError) == null ? void 0 : _this$options$onError.call(_this$options3, this.currentResult.error);
|
|
2775
|
+
(_this$options$onSettl2 = (_this$options4 = this.options).onSettled) == null ? void 0 : _this$options$onSettl2.call(_this$options4, undefined, this.currentResult.error);
|
|
2776
|
+
} // Then trigger the listeners
|
|
2777
|
+
|
|
2778
|
+
|
|
2779
|
+
if (notifyOptions.listeners) {
|
|
2780
|
+
this.listeners.forEach(({
|
|
2781
|
+
listener
|
|
2782
|
+
}) => {
|
|
2783
|
+
listener(this.currentResult);
|
|
2784
|
+
});
|
|
2785
|
+
} // Then the cache listeners
|
|
2786
|
+
|
|
2787
|
+
|
|
2788
|
+
if (notifyOptions.cache) {
|
|
2789
|
+
this.client.getQueryCache().notify({
|
|
2790
|
+
query: this.currentQuery,
|
|
2791
|
+
type: 'observerResultsUpdated'
|
|
2792
|
+
});
|
|
2793
|
+
}
|
|
2794
|
+
});
|
|
2795
|
+
}
|
|
2796
|
+
|
|
2797
|
+
}
|
|
2798
|
+
|
|
2799
|
+
function shouldLoadOnMount(query, options) {
|
|
2800
|
+
return options.enabled !== false && !query.state.dataUpdatedAt && !(query.state.status === 'error' && options.retryOnMount === false);
|
|
2801
|
+
}
|
|
2802
|
+
|
|
2803
|
+
function shouldFetchOnMount(query, options) {
|
|
2804
|
+
return shouldLoadOnMount(query, options) || query.state.dataUpdatedAt > 0 && shouldFetchOn(query, options, options.refetchOnMount);
|
|
2805
|
+
}
|
|
2806
|
+
|
|
2807
|
+
function shouldFetchOn(query, options, field) {
|
|
2808
|
+
if (options.enabled !== false) {
|
|
2809
|
+
const value = typeof field === 'function' ? field(query) : field;
|
|
2810
|
+
return value === 'always' || value !== false && isStale(query, options);
|
|
2811
|
+
}
|
|
2812
|
+
|
|
2813
|
+
return false;
|
|
2814
|
+
}
|
|
2815
|
+
|
|
2816
|
+
function shouldFetchOptionally(query, prevQuery, options, prevOptions) {
|
|
2817
|
+
return options.enabled !== false && (query !== prevQuery || prevOptions.enabled === false) && (!options.suspense || query.state.status !== 'error') && isStale(query, options);
|
|
2818
|
+
}
|
|
2819
|
+
|
|
2820
|
+
function isStale(query, options) {
|
|
2821
|
+
return query.isStaleByTime(options.staleTime);
|
|
2822
|
+
} // this function would decide if we will update the observer's 'current'
|
|
2823
|
+
// properties after an optimistic reading via getOptimisticResult
|
|
2824
|
+
|
|
2825
|
+
|
|
2826
|
+
function shouldAssignObserverCurrentProperties(observer, optimisticResult, options) {
|
|
2827
|
+
// it is important to keep this condition like this for three reasons:
|
|
2828
|
+
// 1. It will get removed in the v5
|
|
2829
|
+
// 2. it reads: don't update the properties if we want to keep the previous
|
|
2830
|
+
// data.
|
|
2831
|
+
// 3. The opposite condition (!options.keepPreviousData) would fallthrough
|
|
2832
|
+
// and will result in a bad decision
|
|
2833
|
+
if (options.keepPreviousData) {
|
|
2834
|
+
return false;
|
|
2835
|
+
} // this means we want to put some placeholder data when pending and queryKey
|
|
2836
|
+
// changed.
|
|
2837
|
+
|
|
2838
|
+
|
|
2839
|
+
if (options.placeholderData !== undefined) {
|
|
2840
|
+
// re-assign properties only if current data is placeholder data
|
|
2841
|
+
// which means that data did not arrive yet, so, if there is some cached data
|
|
2842
|
+
// we need to "prepare" to receive it
|
|
2843
|
+
return optimisticResult.isPlaceholderData;
|
|
2844
|
+
} // if the newly created result isn't what the observer is holding as current,
|
|
2845
|
+
// then we'll need to update the properties as well
|
|
2846
|
+
|
|
2847
|
+
|
|
2848
|
+
if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {
|
|
2849
|
+
return true;
|
|
2850
|
+
} // basically, just keep previous properties if nothing changed
|
|
2851
|
+
|
|
2852
|
+
|
|
2853
|
+
return false;
|
|
2854
|
+
}
|
|
2855
|
+
|
|
2856
|
+
class QueriesObserver extends Subscribable {
|
|
2857
|
+
constructor(client, queries) {
|
|
2858
|
+
super();
|
|
2859
|
+
this.client = client;
|
|
2860
|
+
this.queries = [];
|
|
2861
|
+
this.result = [];
|
|
2862
|
+
this.observers = [];
|
|
2863
|
+
this.observersMap = {};
|
|
2864
|
+
|
|
2865
|
+
if (queries) {
|
|
2866
|
+
this.setQueries(queries);
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
|
|
2870
|
+
onSubscribe() {
|
|
2871
|
+
if (this.listeners.size === 1) {
|
|
2872
|
+
this.observers.forEach(observer => {
|
|
2873
|
+
observer.subscribe(result => {
|
|
2874
|
+
this.onUpdate(observer, result);
|
|
2875
|
+
});
|
|
2876
|
+
});
|
|
2877
|
+
}
|
|
2878
|
+
}
|
|
2879
|
+
|
|
2880
|
+
onUnsubscribe() {
|
|
2881
|
+
if (!this.listeners.size) {
|
|
2882
|
+
this.destroy();
|
|
2883
|
+
}
|
|
2884
|
+
}
|
|
2885
|
+
|
|
2886
|
+
destroy() {
|
|
2887
|
+
this.listeners = new Set();
|
|
2888
|
+
this.observers.forEach(observer => {
|
|
2889
|
+
observer.destroy();
|
|
2890
|
+
});
|
|
2891
|
+
}
|
|
2892
|
+
|
|
2893
|
+
setQueries(queries, notifyOptions) {
|
|
2894
|
+
this.queries = queries;
|
|
2895
|
+
notifyManager.batch(() => {
|
|
2896
|
+
const prevObservers = this.observers;
|
|
2897
|
+
const newObserverMatches = this.findMatchingObservers(this.queries); // set options for the new observers to notify of changes
|
|
2898
|
+
|
|
2899
|
+
newObserverMatches.forEach(match => match.observer.setOptions(match.defaultedQueryOptions, notifyOptions));
|
|
2900
|
+
const newObservers = newObserverMatches.map(match => match.observer);
|
|
2901
|
+
const newObserversMap = Object.fromEntries(newObservers.map(observer => [observer.options.queryHash, observer]));
|
|
2902
|
+
const newResult = newObservers.map(observer => observer.getCurrentResult());
|
|
2903
|
+
const hasIndexChange = newObservers.some((observer, index) => observer !== prevObservers[index]);
|
|
2904
|
+
|
|
2905
|
+
if (prevObservers.length === newObservers.length && !hasIndexChange) {
|
|
2906
|
+
return;
|
|
2907
|
+
}
|
|
2908
|
+
|
|
2909
|
+
this.observers = newObservers;
|
|
2910
|
+
this.observersMap = newObserversMap;
|
|
2911
|
+
this.result = newResult;
|
|
2912
|
+
|
|
2913
|
+
if (!this.hasListeners()) {
|
|
2914
|
+
return;
|
|
2915
|
+
}
|
|
2916
|
+
|
|
2917
|
+
difference(prevObservers, newObservers).forEach(observer => {
|
|
2918
|
+
observer.destroy();
|
|
2919
|
+
});
|
|
2920
|
+
difference(newObservers, prevObservers).forEach(observer => {
|
|
2921
|
+
observer.subscribe(result => {
|
|
2922
|
+
this.onUpdate(observer, result);
|
|
2923
|
+
});
|
|
2924
|
+
});
|
|
2925
|
+
this.notify();
|
|
2926
|
+
});
|
|
2927
|
+
}
|
|
2928
|
+
|
|
2929
|
+
getCurrentResult() {
|
|
2930
|
+
return this.result;
|
|
2931
|
+
}
|
|
2932
|
+
|
|
2933
|
+
getQueries() {
|
|
2934
|
+
return this.observers.map(observer => observer.getCurrentQuery());
|
|
2935
|
+
}
|
|
2936
|
+
|
|
2937
|
+
getObservers() {
|
|
2938
|
+
return this.observers;
|
|
2939
|
+
}
|
|
2940
|
+
|
|
2941
|
+
getOptimisticResult(queries) {
|
|
2942
|
+
return this.findMatchingObservers(queries).map(match => match.observer.getOptimisticResult(match.defaultedQueryOptions));
|
|
2943
|
+
}
|
|
2944
|
+
|
|
2945
|
+
findMatchingObservers(queries) {
|
|
2946
|
+
const prevObservers = this.observers;
|
|
2947
|
+
const prevObserversMap = new Map(prevObservers.map(observer => [observer.options.queryHash, observer]));
|
|
2948
|
+
const defaultedQueryOptions = queries.map(options => this.client.defaultQueryOptions(options));
|
|
2949
|
+
const matchingObservers = defaultedQueryOptions.flatMap(defaultedOptions => {
|
|
2950
|
+
const match = prevObserversMap.get(defaultedOptions.queryHash);
|
|
2951
|
+
|
|
2952
|
+
if (match != null) {
|
|
2953
|
+
return [{
|
|
2954
|
+
defaultedQueryOptions: defaultedOptions,
|
|
2955
|
+
observer: match
|
|
2956
|
+
}];
|
|
2957
|
+
}
|
|
2958
|
+
|
|
2959
|
+
return [];
|
|
2960
|
+
});
|
|
2961
|
+
const matchedQueryHashes = new Set(matchingObservers.map(match => match.defaultedQueryOptions.queryHash));
|
|
2962
|
+
const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.has(defaultedOptions.queryHash));
|
|
2963
|
+
const matchingObserversSet = new Set(matchingObservers.map(match => match.observer));
|
|
2964
|
+
const unmatchedObservers = prevObservers.filter(prevObserver => !matchingObserversSet.has(prevObserver));
|
|
2965
|
+
|
|
2966
|
+
const getObserver = options => {
|
|
2967
|
+
const defaultedOptions = this.client.defaultQueryOptions(options);
|
|
2968
|
+
const currentObserver = this.observersMap[defaultedOptions.queryHash];
|
|
2969
|
+
return currentObserver != null ? currentObserver : new QueryObserver(this.client, defaultedOptions);
|
|
2970
|
+
};
|
|
2971
|
+
|
|
2972
|
+
const newOrReusedObservers = unmatchedQueries.map((options, index) => {
|
|
2973
|
+
if (options.keepPreviousData) {
|
|
2974
|
+
// return previous data from one of the observers that no longer match
|
|
2975
|
+
const previouslyUsedObserver = unmatchedObservers[index];
|
|
2976
|
+
|
|
2977
|
+
if (previouslyUsedObserver !== undefined) {
|
|
2978
|
+
return {
|
|
2979
|
+
defaultedQueryOptions: options,
|
|
2980
|
+
observer: previouslyUsedObserver
|
|
2981
|
+
};
|
|
2982
|
+
}
|
|
2983
|
+
}
|
|
2984
|
+
|
|
2985
|
+
return {
|
|
2986
|
+
defaultedQueryOptions: options,
|
|
2987
|
+
observer: getObserver(options)
|
|
2988
|
+
};
|
|
2989
|
+
});
|
|
2990
|
+
|
|
2991
|
+
const sortMatchesByOrderOfQueries = (a, b) => defaultedQueryOptions.indexOf(a.defaultedQueryOptions) - defaultedQueryOptions.indexOf(b.defaultedQueryOptions);
|
|
2992
|
+
|
|
2993
|
+
return matchingObservers.concat(newOrReusedObservers).sort(sortMatchesByOrderOfQueries);
|
|
2994
|
+
}
|
|
2995
|
+
|
|
2996
|
+
onUpdate(observer, result) {
|
|
2997
|
+
const index = this.observers.indexOf(observer);
|
|
2998
|
+
|
|
2999
|
+
if (index !== -1) {
|
|
3000
|
+
this.result = replaceAt(this.result, index, result);
|
|
3001
|
+
this.notify();
|
|
3002
|
+
}
|
|
3003
|
+
}
|
|
3004
|
+
|
|
3005
|
+
notify() {
|
|
3006
|
+
notifyManager.batch(() => {
|
|
3007
|
+
this.listeners.forEach(({
|
|
3008
|
+
listener
|
|
3009
|
+
}) => {
|
|
3010
|
+
listener(this.result);
|
|
3011
|
+
});
|
|
3012
|
+
});
|
|
3013
|
+
}
|
|
3014
|
+
|
|
3015
|
+
}
|
|
3016
|
+
|
|
3017
|
+
class InfiniteQueryObserver extends QueryObserver {
|
|
3018
|
+
// Type override
|
|
3019
|
+
// Type override
|
|
3020
|
+
// Type override
|
|
3021
|
+
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
3022
|
+
constructor(client, options) {
|
|
3023
|
+
super(client, options);
|
|
3024
|
+
}
|
|
3025
|
+
|
|
3026
|
+
bindMethods() {
|
|
3027
|
+
super.bindMethods();
|
|
3028
|
+
this.fetchNextPage = this.fetchNextPage.bind(this);
|
|
3029
|
+
this.fetchPreviousPage = this.fetchPreviousPage.bind(this);
|
|
3030
|
+
}
|
|
3031
|
+
|
|
3032
|
+
setOptions(options, notifyOptions) {
|
|
3033
|
+
super.setOptions({ ...options,
|
|
3034
|
+
behavior: infiniteQueryBehavior()
|
|
3035
|
+
}, notifyOptions);
|
|
3036
|
+
}
|
|
3037
|
+
|
|
3038
|
+
getOptimisticResult(options) {
|
|
3039
|
+
options.behavior = infiniteQueryBehavior();
|
|
3040
|
+
return super.getOptimisticResult(options);
|
|
3041
|
+
}
|
|
3042
|
+
|
|
3043
|
+
fetchNextPage({
|
|
3044
|
+
pageParam,
|
|
3045
|
+
...options
|
|
3046
|
+
} = {}) {
|
|
3047
|
+
return this.fetch({ ...options,
|
|
3048
|
+
meta: {
|
|
3049
|
+
fetchMore: {
|
|
3050
|
+
direction: 'forward',
|
|
3051
|
+
pageParam
|
|
3052
|
+
}
|
|
3053
|
+
}
|
|
3054
|
+
});
|
|
3055
|
+
}
|
|
3056
|
+
|
|
3057
|
+
fetchPreviousPage({
|
|
3058
|
+
pageParam,
|
|
3059
|
+
...options
|
|
3060
|
+
} = {}) {
|
|
3061
|
+
return this.fetch({ ...options,
|
|
3062
|
+
meta: {
|
|
3063
|
+
fetchMore: {
|
|
3064
|
+
direction: 'backward',
|
|
3065
|
+
pageParam
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
});
|
|
3069
|
+
}
|
|
3070
|
+
|
|
3071
|
+
createResult(query, options) {
|
|
3072
|
+
var _state$fetchMeta, _state$fetchMeta$fetc, _state$fetchMeta2, _state$fetchMeta2$fet, _state$data, _state$data2;
|
|
3073
|
+
|
|
3074
|
+
const {
|
|
3075
|
+
state
|
|
3076
|
+
} = query;
|
|
3077
|
+
const result = super.createResult(query, options);
|
|
3078
|
+
const {
|
|
3079
|
+
isFetching,
|
|
3080
|
+
isRefetching
|
|
3081
|
+
} = result;
|
|
3082
|
+
const isFetchingNextPage = isFetching && ((_state$fetchMeta = state.fetchMeta) == null ? void 0 : (_state$fetchMeta$fetc = _state$fetchMeta.fetchMore) == null ? void 0 : _state$fetchMeta$fetc.direction) === 'forward';
|
|
3083
|
+
const isFetchingPreviousPage = isFetching && ((_state$fetchMeta2 = state.fetchMeta) == null ? void 0 : (_state$fetchMeta2$fet = _state$fetchMeta2.fetchMore) == null ? void 0 : _state$fetchMeta2$fet.direction) === 'backward';
|
|
3084
|
+
return { ...result,
|
|
3085
|
+
fetchNextPage: this.fetchNextPage,
|
|
3086
|
+
fetchPreviousPage: this.fetchPreviousPage,
|
|
3087
|
+
hasNextPage: hasNextPage(options, (_state$data = state.data) == null ? void 0 : _state$data.pages),
|
|
3088
|
+
hasPreviousPage: hasPreviousPage(options, (_state$data2 = state.data) == null ? void 0 : _state$data2.pages),
|
|
3089
|
+
isFetchingNextPage,
|
|
3090
|
+
isFetchingPreviousPage,
|
|
3091
|
+
isRefetching: isRefetching && !isFetchingNextPage && !isFetchingPreviousPage
|
|
3092
|
+
};
|
|
3093
|
+
}
|
|
3094
|
+
|
|
3095
|
+
}
|
|
3096
|
+
|
|
3097
|
+
// CLASS
|
|
3098
|
+
class MutationObserver extends Subscribable {
|
|
3099
|
+
constructor(client, options) {
|
|
3100
|
+
super();
|
|
3101
|
+
this.client = client;
|
|
3102
|
+
this.setOptions(options);
|
|
3103
|
+
this.bindMethods();
|
|
3104
|
+
this.updateResult();
|
|
3105
|
+
}
|
|
3106
|
+
|
|
3107
|
+
bindMethods() {
|
|
3108
|
+
this.mutate = this.mutate.bind(this);
|
|
3109
|
+
this.reset = this.reset.bind(this);
|
|
3110
|
+
}
|
|
3111
|
+
|
|
3112
|
+
setOptions(options) {
|
|
3113
|
+
var _this$currentMutation;
|
|
3114
|
+
|
|
3115
|
+
const prevOptions = this.options;
|
|
3116
|
+
this.options = this.client.defaultMutationOptions(options);
|
|
3117
|
+
|
|
3118
|
+
if (!shallowEqualObjects(prevOptions, this.options)) {
|
|
3119
|
+
this.client.getMutationCache().notify({
|
|
3120
|
+
type: 'observerOptionsUpdated',
|
|
3121
|
+
mutation: this.currentMutation,
|
|
3122
|
+
observer: this
|
|
3123
|
+
});
|
|
3124
|
+
}
|
|
3125
|
+
|
|
3126
|
+
(_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.setOptions(this.options);
|
|
3127
|
+
}
|
|
3128
|
+
|
|
3129
|
+
onUnsubscribe() {
|
|
3130
|
+
if (!this.hasListeners()) {
|
|
3131
|
+
var _this$currentMutation2;
|
|
3132
|
+
|
|
3133
|
+
(_this$currentMutation2 = this.currentMutation) == null ? void 0 : _this$currentMutation2.removeObserver(this);
|
|
3134
|
+
}
|
|
3135
|
+
}
|
|
3136
|
+
|
|
3137
|
+
onMutationUpdate(action) {
|
|
3138
|
+
this.updateResult(); // Determine which callbacks to trigger
|
|
3139
|
+
|
|
3140
|
+
const notifyOptions = {
|
|
3141
|
+
listeners: true
|
|
3142
|
+
};
|
|
3143
|
+
|
|
3144
|
+
if (action.type === 'success') {
|
|
3145
|
+
notifyOptions.onSuccess = true;
|
|
3146
|
+
} else if (action.type === 'error') {
|
|
3147
|
+
notifyOptions.onError = true;
|
|
3148
|
+
}
|
|
3149
|
+
|
|
3150
|
+
this.notify(notifyOptions);
|
|
3151
|
+
}
|
|
3152
|
+
|
|
3153
|
+
getCurrentResult() {
|
|
3154
|
+
return this.currentResult;
|
|
3155
|
+
}
|
|
3156
|
+
|
|
3157
|
+
reset() {
|
|
3158
|
+
this.currentMutation = undefined;
|
|
3159
|
+
this.updateResult();
|
|
3160
|
+
this.notify({
|
|
3161
|
+
listeners: true
|
|
3162
|
+
});
|
|
3163
|
+
}
|
|
3164
|
+
|
|
3165
|
+
mutate(variables, options) {
|
|
3166
|
+
this.mutateOptions = options;
|
|
3167
|
+
|
|
3168
|
+
if (this.currentMutation) {
|
|
3169
|
+
this.currentMutation.removeObserver(this);
|
|
3170
|
+
}
|
|
3171
|
+
|
|
3172
|
+
this.currentMutation = this.client.getMutationCache().build(this.client, { ...this.options,
|
|
3173
|
+
variables: typeof variables !== 'undefined' ? variables : this.options.variables
|
|
3174
|
+
});
|
|
3175
|
+
this.currentMutation.addObserver(this);
|
|
3176
|
+
return this.currentMutation.execute();
|
|
3177
|
+
}
|
|
3178
|
+
|
|
3179
|
+
updateResult() {
|
|
3180
|
+
const state = this.currentMutation ? this.currentMutation.state : getDefaultState();
|
|
3181
|
+
const isLoading = state.status === 'loading';
|
|
3182
|
+
const result = { ...state,
|
|
3183
|
+
isLoading,
|
|
3184
|
+
isPending: isLoading,
|
|
3185
|
+
isSuccess: state.status === 'success',
|
|
3186
|
+
isError: state.status === 'error',
|
|
3187
|
+
isIdle: state.status === 'idle',
|
|
3188
|
+
mutate: this.mutate,
|
|
3189
|
+
reset: this.reset
|
|
3190
|
+
};
|
|
3191
|
+
this.currentResult = result;
|
|
3192
|
+
}
|
|
3193
|
+
|
|
3194
|
+
notify(options) {
|
|
3195
|
+
notifyManager.batch(() => {
|
|
3196
|
+
// First trigger the mutate callbacks
|
|
3197
|
+
if (this.mutateOptions && this.hasListeners()) {
|
|
3198
|
+
if (options.onSuccess) {
|
|
3199
|
+
var _this$mutateOptions$o, _this$mutateOptions, _this$mutateOptions$o2, _this$mutateOptions2;
|
|
3200
|
+
|
|
3201
|
+
(_this$mutateOptions$o = (_this$mutateOptions = this.mutateOptions).onSuccess) == null ? void 0 : _this$mutateOptions$o.call(_this$mutateOptions, this.currentResult.data, this.currentResult.variables, this.currentResult.context);
|
|
3202
|
+
(_this$mutateOptions$o2 = (_this$mutateOptions2 = this.mutateOptions).onSettled) == null ? void 0 : _this$mutateOptions$o2.call(_this$mutateOptions2, this.currentResult.data, null, this.currentResult.variables, this.currentResult.context);
|
|
3203
|
+
} else if (options.onError) {
|
|
3204
|
+
var _this$mutateOptions$o3, _this$mutateOptions3, _this$mutateOptions$o4, _this$mutateOptions4;
|
|
3205
|
+
|
|
3206
|
+
(_this$mutateOptions$o3 = (_this$mutateOptions3 = this.mutateOptions).onError) == null ? void 0 : _this$mutateOptions$o3.call(_this$mutateOptions3, this.currentResult.error, this.currentResult.variables, this.currentResult.context);
|
|
3207
|
+
(_this$mutateOptions$o4 = (_this$mutateOptions4 = this.mutateOptions).onSettled) == null ? void 0 : _this$mutateOptions$o4.call(_this$mutateOptions4, undefined, this.currentResult.error, this.currentResult.variables, this.currentResult.context);
|
|
3208
|
+
}
|
|
3209
|
+
} // Then trigger the listeners
|
|
3210
|
+
|
|
3211
|
+
|
|
3212
|
+
if (options.listeners) {
|
|
3213
|
+
this.listeners.forEach(({
|
|
3214
|
+
listener
|
|
3215
|
+
}) => {
|
|
3216
|
+
listener(this.currentResult);
|
|
3217
|
+
});
|
|
3218
|
+
}
|
|
3219
|
+
});
|
|
3220
|
+
}
|
|
3221
|
+
|
|
3222
|
+
}
|
|
3223
|
+
|
|
3224
|
+
// TYPES
|
|
3225
|
+
// FUNCTIONS
|
|
3226
|
+
function dehydrateMutation(mutation) {
|
|
3227
|
+
return {
|
|
3228
|
+
mutationKey: mutation.options.mutationKey,
|
|
3229
|
+
state: mutation.state
|
|
3230
|
+
};
|
|
3231
|
+
} // Most config is not dehydrated but instead meant to configure again when
|
|
3232
|
+
// consuming the de/rehydrated data, typically with useQuery on the client.
|
|
3233
|
+
// Sometimes it might make sense to prefetch data on the server and include
|
|
3234
|
+
// in the html-payload, but not consume it on the initial render.
|
|
3235
|
+
|
|
3236
|
+
|
|
3237
|
+
function dehydrateQuery(query) {
|
|
3238
|
+
return {
|
|
3239
|
+
state: query.state,
|
|
3240
|
+
queryKey: query.queryKey,
|
|
3241
|
+
queryHash: query.queryHash
|
|
3242
|
+
};
|
|
3243
|
+
}
|
|
3244
|
+
|
|
3245
|
+
function defaultShouldDehydrateMutation(mutation) {
|
|
3246
|
+
return mutation.state.isPaused;
|
|
3247
|
+
}
|
|
3248
|
+
function defaultShouldDehydrateQuery(query) {
|
|
3249
|
+
return query.state.status === 'success';
|
|
3250
|
+
}
|
|
3251
|
+
function dehydrate(client, options = {}) {
|
|
3252
|
+
const mutations = [];
|
|
3253
|
+
const queries = [];
|
|
3254
|
+
|
|
3255
|
+
if (options.dehydrateMutations !== false) {
|
|
3256
|
+
const shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation;
|
|
3257
|
+
client.getMutationCache().getAll().forEach(mutation => {
|
|
3258
|
+
if (shouldDehydrateMutation(mutation)) {
|
|
3259
|
+
mutations.push(dehydrateMutation(mutation));
|
|
3260
|
+
}
|
|
3261
|
+
});
|
|
3262
|
+
}
|
|
3263
|
+
|
|
3264
|
+
if (options.dehydrateQueries !== false) {
|
|
3265
|
+
const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery;
|
|
3266
|
+
client.getQueryCache().getAll().forEach(query => {
|
|
3267
|
+
if (shouldDehydrateQuery(query)) {
|
|
3268
|
+
queries.push(dehydrateQuery(query));
|
|
3269
|
+
}
|
|
3270
|
+
});
|
|
3271
|
+
}
|
|
3272
|
+
|
|
3273
|
+
return {
|
|
3274
|
+
mutations,
|
|
3275
|
+
queries
|
|
3276
|
+
};
|
|
3277
|
+
}
|
|
3278
|
+
function hydrate(client, dehydratedState, options) {
|
|
3279
|
+
if (typeof dehydratedState !== 'object' || dehydratedState === null) {
|
|
3280
|
+
return;
|
|
3281
|
+
}
|
|
3282
|
+
|
|
3283
|
+
const mutationCache = client.getMutationCache();
|
|
3284
|
+
const queryCache = client.getQueryCache(); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
3285
|
+
|
|
3286
|
+
const mutations = dehydratedState.mutations || []; // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
3287
|
+
|
|
3288
|
+
const queries = dehydratedState.queries || [];
|
|
3289
|
+
mutations.forEach(dehydratedMutation => {
|
|
3290
|
+
var _options$defaultOptio;
|
|
3291
|
+
|
|
3292
|
+
mutationCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio = options.defaultOptions) == null ? void 0 : _options$defaultOptio.mutations),
|
|
3293
|
+
mutationKey: dehydratedMutation.mutationKey
|
|
3294
|
+
}, dehydratedMutation.state);
|
|
3295
|
+
});
|
|
3296
|
+
queries.forEach(({
|
|
3297
|
+
queryKey,
|
|
3298
|
+
state,
|
|
3299
|
+
queryHash
|
|
3300
|
+
}) => {
|
|
3301
|
+
var _options$defaultOptio2;
|
|
3302
|
+
|
|
3303
|
+
const query = queryCache.get(queryHash); // Do not hydrate if an existing query exists with newer data
|
|
3304
|
+
|
|
3305
|
+
if (query) {
|
|
3306
|
+
if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
|
|
3307
|
+
// omit fetchStatus from dehydrated state
|
|
3308
|
+
// so that query stays in its current fetchStatus
|
|
3309
|
+
const {
|
|
3310
|
+
fetchStatus: _ignored,
|
|
3311
|
+
...dehydratedQueryState
|
|
3312
|
+
} = state;
|
|
3313
|
+
query.setState(dehydratedQueryState);
|
|
3314
|
+
}
|
|
3315
|
+
|
|
3316
|
+
return;
|
|
3317
|
+
} // Restore query
|
|
3318
|
+
|
|
3319
|
+
|
|
3320
|
+
queryCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries),
|
|
3321
|
+
queryKey,
|
|
3322
|
+
queryHash
|
|
3323
|
+
}, // Reset fetch status to idle to avoid
|
|
3324
|
+
// query being stuck in fetching state upon hydration
|
|
3325
|
+
{ ...state,
|
|
3326
|
+
fetchStatus: 'idle'
|
|
3327
|
+
});
|
|
3328
|
+
});
|
|
3329
|
+
}
|
|
3330
|
+
|
|
3331
|
+
exports.CancelledError = CancelledError;
|
|
3332
|
+
exports.InfiniteQueryObserver = InfiniteQueryObserver;
|
|
3333
|
+
exports.MutationCache = MutationCache;
|
|
3334
|
+
exports.MutationObserver = MutationObserver;
|
|
3335
|
+
exports.QueriesObserver = QueriesObserver;
|
|
3336
|
+
exports.Query = Query;
|
|
3337
|
+
exports.QueryCache = QueryCache;
|
|
3338
|
+
exports.QueryClient = QueryClient;
|
|
3339
|
+
exports.QueryObserver = QueryObserver;
|
|
3340
|
+
exports.defaultShouldDehydrateMutation = defaultShouldDehydrateMutation;
|
|
3341
|
+
exports.defaultShouldDehydrateQuery = defaultShouldDehydrateQuery;
|
|
3342
|
+
exports.dehydrate = dehydrate;
|
|
3343
|
+
exports.focusManager = focusManager;
|
|
3344
|
+
exports.hashQueryKey = hashQueryKey;
|
|
3345
|
+
exports.hydrate = hydrate;
|
|
3346
|
+
exports.isCancelledError = isCancelledError;
|
|
3347
|
+
exports.isError = isError;
|
|
3348
|
+
exports.isServer = isServer;
|
|
3349
|
+
exports.matchQuery = matchQuery;
|
|
3350
|
+
exports.notifyManager = notifyManager;
|
|
3351
|
+
exports.onlineManager = onlineManager;
|
|
3352
|
+
exports.parseFilterArgs = parseFilterArgs;
|
|
3353
|
+
exports.parseMutationArgs = parseMutationArgs;
|
|
3354
|
+
exports.parseMutationFilterArgs = parseMutationFilterArgs;
|
|
3355
|
+
exports.parseQueryArgs = parseQueryArgs;
|
|
3356
|
+
exports.replaceEqualDeep = replaceEqualDeep;
|
|
3357
|
+
|
|
3358
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3359
|
+
|
|
3360
|
+
}));
|
|
3361
|
+
//# sourceMappingURL=index.development.js.map
|