@tanstack/query-core 5.0.0-alpha.49 → 5.0.0-alpha.50

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.
Files changed (39) hide show
  1. package/build/lib/focusManager.js +15 -31
  2. package/build/lib/focusManager.js.map +1 -1
  3. package/build/lib/hydration.js +4 -7
  4. package/build/lib/hydration.js.map +1 -1
  5. package/build/lib/infiniteQueryBehavior.js +4 -5
  6. package/build/lib/infiniteQueryBehavior.js.map +1 -1
  7. package/build/lib/infiniteQueryObserver.js +2 -3
  8. package/build/lib/infiniteQueryObserver.js.map +1 -1
  9. package/build/lib/mutation.js +102 -127
  10. package/build/lib/mutation.js.map +1 -1
  11. package/build/lib/mutationCache.js +17 -30
  12. package/build/lib/mutationCache.js.map +1 -1
  13. package/build/lib/mutationObserver.js +51 -81
  14. package/build/lib/mutationObserver.js.map +1 -1
  15. package/build/lib/onlineManager.js +14 -29
  16. package/build/lib/onlineManager.js.map +1 -1
  17. package/build/lib/queriesObserver.js +79 -125
  18. package/build/lib/queriesObserver.js.map +1 -1
  19. package/build/lib/query.js +161 -210
  20. package/build/lib/query.js.map +1 -1
  21. package/build/lib/queryCache.js +10 -15
  22. package/build/lib/queryCache.js.map +1 -1
  23. package/build/lib/queryClient.js +61 -98
  24. package/build/lib/queryClient.js.map +1 -1
  25. package/build/lib/queryObserver.js +197 -297
  26. package/build/lib/queryObserver.js.map +1 -1
  27. package/build/lib/removable.js +6 -13
  28. package/build/lib/removable.js.map +1 -1
  29. package/build/lib/retryer.js +14 -15
  30. package/build/lib/retryer.js.map +1 -1
  31. package/build/lib/utils.js +1 -1
  32. package/build/lib/utils.js.map +1 -1
  33. package/package.json +1 -2
  34. package/build/lib/_virtual/_rollupPluginBabelHelpers.js +0 -16
  35. package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  36. package/build/umd/index.development.js +0 -2691
  37. package/build/umd/index.development.js.map +0 -1
  38. package/build/umd/index.production.js +0 -2
  39. package/build/umd/index.production.js.map +0 -1
@@ -1,2691 +0,0 @@
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
- subscribe(listener) {
13
- this.listeners.add(listener);
14
- this.onSubscribe();
15
- return () => {
16
- this.listeners.delete(listener);
17
- this.onUnsubscribe();
18
- };
19
- }
20
- hasListeners() {
21
- return this.listeners.size > 0;
22
- }
23
- onSubscribe() {
24
- // Do nothing
25
- }
26
- onUnsubscribe() {
27
- // Do nothing
28
- }
29
- }
30
-
31
- // TYPES
32
-
33
- // UTILS
34
-
35
- const isServer = typeof window === 'undefined' || 'Deno' in window;
36
- function noop() {
37
- return undefined;
38
- }
39
- function functionalUpdate(updater, input) {
40
- return typeof updater === 'function' ? updater(input) : updater;
41
- }
42
- function isValidTimeout(value) {
43
- return typeof value === 'number' && value >= 0 && value !== Infinity;
44
- }
45
- function timeUntilStale(updatedAt, staleTime) {
46
- return Math.max(updatedAt + (staleTime || 0) - Date.now(), 0);
47
- }
48
- function matchQuery(filters, query) {
49
- const {
50
- type = 'all',
51
- exact,
52
- fetchStatus,
53
- predicate,
54
- queryKey,
55
- stale
56
- } = filters;
57
- if (queryKey) {
58
- if (exact) {
59
- if (query.queryHash !== hashQueryKeyByOptions(queryKey, query.options)) {
60
- return false;
61
- }
62
- } else if (!partialMatchKey(query.queryKey, queryKey)) {
63
- return false;
64
- }
65
- }
66
- if (type !== 'all') {
67
- const isActive = query.isActive();
68
- if (type === 'active' && !isActive) {
69
- return false;
70
- }
71
- if (type === 'inactive' && isActive) {
72
- return false;
73
- }
74
- }
75
- if (typeof stale === 'boolean' && query.isStale() !== stale) {
76
- return false;
77
- }
78
- if (typeof fetchStatus !== 'undefined' && fetchStatus !== query.state.fetchStatus) {
79
- return false;
80
- }
81
- if (predicate && !predicate(query)) {
82
- return false;
83
- }
84
- return true;
85
- }
86
- function matchMutation(filters, mutation) {
87
- const {
88
- exact,
89
- status,
90
- predicate,
91
- mutationKey
92
- } = filters;
93
- if (mutationKey) {
94
- if (!mutation.options.mutationKey) {
95
- return false;
96
- }
97
- if (exact) {
98
- if (hashKey(mutation.options.mutationKey) !== hashKey(mutationKey)) {
99
- return false;
100
- }
101
- } else if (!partialMatchKey(mutation.options.mutationKey, mutationKey)) {
102
- return false;
103
- }
104
- }
105
- if (status && mutation.state.status !== status) {
106
- return false;
107
- }
108
- if (predicate && !predicate(mutation)) {
109
- return false;
110
- }
111
- return true;
112
- }
113
- function hashQueryKeyByOptions(queryKey, options) {
114
- const hashFn = options?.queryKeyHashFn || hashKey;
115
- return hashFn(queryKey);
116
- }
117
-
118
- /**
119
- * Default query & mutation keys hash function.
120
- * Hashes the value into a stable hash.
121
- */
122
- function hashKey(queryKey) {
123
- return JSON.stringify(queryKey, (_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
124
- result[key] = val[key];
125
- return result;
126
- }, {}) : val);
127
- }
128
-
129
- /**
130
- * Checks if key `b` partially matches with key `a`.
131
- */
132
-
133
- function partialMatchKey(a, b) {
134
- if (a === b) {
135
- return true;
136
- }
137
- if (typeof a !== typeof b) {
138
- return false;
139
- }
140
- if (a && b && typeof a === 'object' && typeof b === 'object') {
141
- return !Object.keys(b).some(key => !partialMatchKey(a[key], b[key]));
142
- }
143
- return false;
144
- }
145
-
146
- /**
147
- * This function returns `a` if `b` is deeply equal.
148
- * If not, it will replace any deeply equal children of `b` with those of `a`.
149
- * This can be used for structural sharing between JSON values for example.
150
- */
151
-
152
- function replaceEqualDeep(a, b) {
153
- if (a === b) {
154
- return a;
155
- }
156
- const array = isPlainArray(a) && isPlainArray(b);
157
- if (array || isPlainObject(a) && isPlainObject(b)) {
158
- const aSize = array ? a.length : Object.keys(a).length;
159
- const bItems = array ? b : Object.keys(b);
160
- const bSize = bItems.length;
161
- const copy = array ? [] : {};
162
- let equalItems = 0;
163
- for (let i = 0; i < bSize; i++) {
164
- const key = array ? i : bItems[i];
165
- copy[key] = replaceEqualDeep(a[key], b[key]);
166
- if (copy[key] === a[key]) {
167
- equalItems++;
168
- }
169
- }
170
- return aSize === bSize && equalItems === aSize ? a : copy;
171
- }
172
- return b;
173
- }
174
-
175
- /**
176
- * Shallow compare objects. Only works with objects that always have the same properties.
177
- */
178
- function shallowEqualObjects(a, b) {
179
- if (a && !b || b && !a) {
180
- return false;
181
- }
182
- for (const key in a) {
183
- if (a[key] !== b[key]) {
184
- return false;
185
- }
186
- }
187
- return true;
188
- }
189
- function isPlainArray(value) {
190
- return Array.isArray(value) && value.length === Object.keys(value).length;
191
- }
192
-
193
- // Copied from: https://github.com/jonschlinkert/is-plain-object
194
- function isPlainObject(o) {
195
- if (!hasObjectPrototype(o)) {
196
- return false;
197
- }
198
-
199
- // If has modified constructor
200
- const ctor = o.constructor;
201
- if (typeof ctor === 'undefined') {
202
- return true;
203
- }
204
-
205
- // If has modified prototype
206
- const prot = ctor.prototype;
207
- if (!hasObjectPrototype(prot)) {
208
- return false;
209
- }
210
-
211
- // If constructor does not have an Object-specific method
212
- if (!prot.hasOwnProperty('isPrototypeOf')) {
213
- return false;
214
- }
215
-
216
- // Most likely a plain Object
217
- return true;
218
- }
219
- function hasObjectPrototype(o) {
220
- return Object.prototype.toString.call(o) === '[object Object]';
221
- }
222
- function sleep(timeout) {
223
- return new Promise(resolve => {
224
- setTimeout(resolve, timeout);
225
- });
226
- }
227
-
228
- /**
229
- * Schedules a microtask.
230
- * This can be useful to schedule state updates after rendering.
231
- */
232
- function scheduleMicrotask(callback) {
233
- sleep(0).then(callback);
234
- }
235
- function replaceData(prevData, data, options) {
236
- if (typeof options.structuralSharing === 'function') {
237
- return options.structuralSharing(prevData, data);
238
- } else if (options.structuralSharing !== false) {
239
- // Structurally share data between prev and new data if needed
240
- return replaceEqualDeep(prevData, data);
241
- }
242
- return data;
243
- }
244
- function keepPreviousData(previousData) {
245
- return previousData;
246
- }
247
- function addToEnd(items, item, max = 0) {
248
- const newItems = [...items, item];
249
- return max && newItems.length > max ? newItems.slice(1) : newItems;
250
- }
251
- function addToStart(items, item, max = 0) {
252
- const newItems = [item, ...items];
253
- return max && newItems.length > max ? newItems.slice(0, -1) : newItems;
254
- }
255
-
256
- class FocusManager extends Subscribable {
257
- #focused;
258
- #cleanup;
259
- #setup;
260
- constructor() {
261
- super();
262
- this.#setup = onFocus => {
263
- // addEventListener does not exist in React Native, but window does
264
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
265
- if (!isServer && window.addEventListener) {
266
- const listener = () => onFocus();
267
- // Listen to visibilitychange
268
- window.addEventListener('visibilitychange', listener, false);
269
- return () => {
270
- // Be sure to unsubscribe if a new handler is set
271
- window.removeEventListener('visibilitychange', listener);
272
- };
273
- }
274
- return;
275
- };
276
- }
277
- onSubscribe() {
278
- if (!this.#cleanup) {
279
- this.setEventListener(this.#setup);
280
- }
281
- }
282
- onUnsubscribe() {
283
- if (!this.hasListeners()) {
284
- this.#cleanup?.();
285
- this.#cleanup = undefined;
286
- }
287
- }
288
- setEventListener(setup) {
289
- this.#setup = setup;
290
- this.#cleanup?.();
291
- this.#cleanup = setup(focused => {
292
- if (typeof focused === 'boolean') {
293
- this.setFocused(focused);
294
- } else {
295
- this.onFocus();
296
- }
297
- });
298
- }
299
- setFocused(focused) {
300
- this.#focused = focused;
301
- if (focused) {
302
- this.onFocus();
303
- }
304
- }
305
- onFocus() {
306
- this.listeners.forEach(listener => {
307
- listener();
308
- });
309
- }
310
- isFocused() {
311
- if (typeof this.#focused === 'boolean') {
312
- return this.#focused;
313
- }
314
-
315
- // document global can be unavailable in react native
316
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
317
- return globalThis.document?.visibilityState !== 'hidden';
318
- }
319
- }
320
- const focusManager = new FocusManager();
321
-
322
- class OnlineManager extends Subscribable {
323
- #online;
324
- #cleanup;
325
- #setup;
326
- constructor() {
327
- super();
328
- this.#setup = onOnline => {
329
- // addEventListener does not exist in React Native, but window does
330
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
331
- if (!isServer && window.addEventListener) {
332
- const listener = () => onOnline();
333
- // Listen to online
334
- window.addEventListener('online', listener, false);
335
- window.addEventListener('offline', listener, false);
336
- return () => {
337
- // Be sure to unsubscribe if a new handler is set
338
- window.removeEventListener('online', listener);
339
- window.removeEventListener('offline', listener);
340
- };
341
- }
342
- return;
343
- };
344
- }
345
- onSubscribe() {
346
- if (!this.#cleanup) {
347
- this.setEventListener(this.#setup);
348
- }
349
- }
350
- onUnsubscribe() {
351
- if (!this.hasListeners()) {
352
- this.#cleanup?.();
353
- this.#cleanup = undefined;
354
- }
355
- }
356
- setEventListener(setup) {
357
- this.#setup = setup;
358
- this.#cleanup?.();
359
- this.#cleanup = setup(online => {
360
- if (typeof online === 'boolean') {
361
- this.setOnline(online);
362
- } else {
363
- this.onOnline();
364
- }
365
- });
366
- }
367
- setOnline(online) {
368
- this.#online = online;
369
- if (online) {
370
- this.onOnline();
371
- }
372
- }
373
- onOnline() {
374
- this.listeners.forEach(listener => {
375
- listener();
376
- });
377
- }
378
- isOnline() {
379
- if (typeof this.#online === 'boolean') {
380
- return this.#online;
381
- }
382
- if (typeof navigator === 'undefined' || typeof navigator.onLine === 'undefined') {
383
- return true;
384
- }
385
- return navigator.onLine;
386
- }
387
- }
388
- const onlineManager = new OnlineManager();
389
-
390
- // TYPES
391
-
392
- function defaultRetryDelay(failureCount) {
393
- return Math.min(1000 * 2 ** failureCount, 30000);
394
- }
395
- function canFetch(networkMode) {
396
- return (networkMode ?? 'online') === 'online' ? onlineManager.isOnline() : true;
397
- }
398
- class CancelledError {
399
- constructor(options) {
400
- this.revert = options?.revert;
401
- this.silent = options?.silent;
402
- }
403
- }
404
- function isCancelledError(value) {
405
- return value instanceof CancelledError;
406
- }
407
- function createRetryer(config) {
408
- let isRetryCancelled = false;
409
- let failureCount = 0;
410
- let isResolved = false;
411
- let continueFn;
412
- let promiseResolve;
413
- let promiseReject;
414
- const promise = new Promise((outerResolve, outerReject) => {
415
- promiseResolve = outerResolve;
416
- promiseReject = outerReject;
417
- });
418
- const cancel = cancelOptions => {
419
- if (!isResolved) {
420
- reject(new CancelledError(cancelOptions));
421
- config.abort?.();
422
- }
423
- };
424
- const cancelRetry = () => {
425
- isRetryCancelled = true;
426
- };
427
- const continueRetry = () => {
428
- isRetryCancelled = false;
429
- };
430
- const shouldPause = () => !focusManager.isFocused() || config.networkMode !== 'always' && !onlineManager.isOnline();
431
- const resolve = value => {
432
- if (!isResolved) {
433
- isResolved = true;
434
- config.onSuccess?.(value);
435
- continueFn?.();
436
- promiseResolve(value);
437
- }
438
- };
439
- const reject = value => {
440
- if (!isResolved) {
441
- isResolved = true;
442
- config.onError?.(value);
443
- continueFn?.();
444
- promiseReject(value);
445
- }
446
- };
447
- const pause = () => {
448
- return new Promise(continueResolve => {
449
- continueFn = value => {
450
- const canContinue = isResolved || !shouldPause();
451
- if (canContinue) {
452
- continueResolve(value);
453
- }
454
- return canContinue;
455
- };
456
- config.onPause?.();
457
- }).then(() => {
458
- continueFn = undefined;
459
- if (!isResolved) {
460
- config.onContinue?.();
461
- }
462
- });
463
- };
464
-
465
- // Create loop function
466
- const run = () => {
467
- // Do nothing if already resolved
468
- if (isResolved) {
469
- return;
470
- }
471
- let promiseOrValue;
472
-
473
- // Execute query
474
- try {
475
- promiseOrValue = config.fn();
476
- } catch (error) {
477
- promiseOrValue = Promise.reject(error);
478
- }
479
- Promise.resolve(promiseOrValue).then(resolve).catch(error => {
480
- // Stop if the fetch is already resolved
481
- if (isResolved) {
482
- return;
483
- }
484
-
485
- // Do we need to retry the request?
486
- const retry = config.retry ?? 3;
487
- const retryDelay = config.retryDelay ?? defaultRetryDelay;
488
- const delay = typeof retryDelay === 'function' ? retryDelay(failureCount, error) : retryDelay;
489
- const shouldRetry = retry === true || typeof retry === 'number' && failureCount < retry || typeof retry === 'function' && retry(failureCount, error);
490
- if (isRetryCancelled || !shouldRetry) {
491
- // We are done if the query does not need to be retried
492
- reject(error);
493
- return;
494
- }
495
- failureCount++;
496
-
497
- // Notify on fail
498
- config.onFail?.(failureCount, error);
499
-
500
- // Delay
501
- sleep(delay)
502
- // Pause if the document is not visible or when the device is offline
503
- .then(() => {
504
- if (shouldPause()) {
505
- return pause();
506
- }
507
- return;
508
- }).then(() => {
509
- if (isRetryCancelled) {
510
- reject(error);
511
- } else {
512
- run();
513
- }
514
- });
515
- });
516
- };
517
-
518
- // Start loop
519
- if (canFetch(config.networkMode)) {
520
- run();
521
- } else {
522
- pause().then(run);
523
- }
524
- return {
525
- promise,
526
- cancel,
527
- continue: () => {
528
- const didContinue = continueFn?.();
529
- return didContinue ? promise : Promise.resolve();
530
- },
531
- cancelRetry,
532
- continueRetry
533
- };
534
- }
535
-
536
- // TYPES
537
-
538
- function createNotifyManager() {
539
- let queue = [];
540
- let transactions = 0;
541
- let notifyFn = callback => {
542
- callback();
543
- };
544
- let batchNotifyFn = callback => {
545
- callback();
546
- };
547
- const batch = callback => {
548
- let result;
549
- transactions++;
550
- try {
551
- result = callback();
552
- } finally {
553
- transactions--;
554
- if (!transactions) {
555
- flush();
556
- }
557
- }
558
- return result;
559
- };
560
- const schedule = callback => {
561
- if (transactions) {
562
- queue.push(callback);
563
- } else {
564
- scheduleMicrotask(() => {
565
- notifyFn(callback);
566
- });
567
- }
568
- };
569
-
570
- /**
571
- * All calls to the wrapped function will be batched.
572
- */
573
- const batchCalls = callback => {
574
- return (...args) => {
575
- schedule(() => {
576
- callback(...args);
577
- });
578
- };
579
- };
580
- const flush = () => {
581
- const originalQueue = queue;
582
- queue = [];
583
- if (originalQueue.length) {
584
- scheduleMicrotask(() => {
585
- batchNotifyFn(() => {
586
- originalQueue.forEach(callback => {
587
- notifyFn(callback);
588
- });
589
- });
590
- });
591
- }
592
- };
593
-
594
- /**
595
- * Use this method to set a custom notify function.
596
- * This can be used to for example wrap notifications with `React.act` while running tests.
597
- */
598
- const setNotifyFunction = fn => {
599
- notifyFn = fn;
600
- };
601
-
602
- /**
603
- * Use this method to set a custom function to batch notifications together into a single tick.
604
- * By default React Query will use the batch function provided by ReactDOM or React Native.
605
- */
606
- const setBatchNotifyFunction = fn => {
607
- batchNotifyFn = fn;
608
- };
609
- return {
610
- batch,
611
- batchCalls,
612
- schedule,
613
- setNotifyFunction,
614
- setBatchNotifyFunction
615
- };
616
- }
617
-
618
- // SINGLETON
619
- const notifyManager = createNotifyManager();
620
-
621
- class Removable {
622
- #gcTimeout;
623
- destroy() {
624
- this.clearGcTimeout();
625
- }
626
- scheduleGc() {
627
- this.clearGcTimeout();
628
- if (isValidTimeout(this.gcTime)) {
629
- this.#gcTimeout = setTimeout(() => {
630
- this.optionalRemove();
631
- }, this.gcTime);
632
- }
633
- }
634
- updateGcTime(newGcTime) {
635
- // Default to 5 minutes (Infinity for server-side) if no gcTime is set
636
- this.gcTime = Math.max(this.gcTime || 0, newGcTime ?? (isServer ? Infinity : 5 * 60 * 1000));
637
- }
638
- clearGcTimeout() {
639
- if (this.#gcTimeout) {
640
- clearTimeout(this.#gcTimeout);
641
- this.#gcTimeout = undefined;
642
- }
643
- }
644
- }
645
-
646
- // TYPES
647
-
648
- // CLASS
649
-
650
- class Query extends Removable {
651
- #initialState;
652
- #revertState;
653
- #cache;
654
- #promise;
655
- #retryer;
656
- #observers;
657
- #defaultOptions;
658
- #abortSignalConsumed;
659
- constructor(config) {
660
- super();
661
- this.#abortSignalConsumed = false;
662
- this.#defaultOptions = config.defaultOptions;
663
- this.#setOptions(config.options);
664
- this.#observers = [];
665
- this.#cache = config.cache;
666
- this.queryKey = config.queryKey;
667
- this.queryHash = config.queryHash;
668
- this.#initialState = config.state || getDefaultState$1(this.options);
669
- this.state = this.#initialState;
670
- this.scheduleGc();
671
- }
672
- get meta() {
673
- return this.options.meta;
674
- }
675
- #setOptions(options) {
676
- this.options = {
677
- ...this.#defaultOptions,
678
- ...options
679
- };
680
- this.updateGcTime(this.options.gcTime);
681
- }
682
- optionalRemove() {
683
- if (!this.#observers.length && this.state.fetchStatus === 'idle') {
684
- this.#cache.remove(this);
685
- }
686
- }
687
- setData(newData, options) {
688
- const data = replaceData(this.state.data, newData, this.options);
689
-
690
- // Set data and mark it as cached
691
- this.#dispatch({
692
- data,
693
- type: 'success',
694
- dataUpdatedAt: options?.updatedAt,
695
- manual: options?.manual
696
- });
697
- return data;
698
- }
699
- setState(state, setStateOptions) {
700
- this.#dispatch({
701
- type: 'setState',
702
- state,
703
- setStateOptions
704
- });
705
- }
706
- cancel(options) {
707
- const promise = this.#promise;
708
- this.#retryer?.cancel(options);
709
- return promise ? promise.then(noop).catch(noop) : Promise.resolve();
710
- }
711
- destroy() {
712
- super.destroy();
713
- this.cancel({
714
- silent: true
715
- });
716
- }
717
- reset() {
718
- this.destroy();
719
- this.setState(this.#initialState);
720
- }
721
- isActive() {
722
- return this.#observers.some(observer => observer.options.enabled !== false);
723
- }
724
- isDisabled() {
725
- return this.getObserversCount() > 0 && !this.isActive();
726
- }
727
- isStale() {
728
- return this.state.isInvalidated || !this.state.dataUpdatedAt || this.#observers.some(observer => observer.getCurrentResult().isStale);
729
- }
730
- isStaleByTime(staleTime = 0) {
731
- return this.state.isInvalidated || !this.state.dataUpdatedAt || !timeUntilStale(this.state.dataUpdatedAt, staleTime);
732
- }
733
- onFocus() {
734
- const observer = this.#observers.find(x => x.shouldFetchOnWindowFocus());
735
- observer?.refetch({
736
- cancelRefetch: false
737
- });
738
-
739
- // Continue fetch if currently paused
740
- this.#retryer?.continue();
741
- }
742
- onOnline() {
743
- const observer = this.#observers.find(x => x.shouldFetchOnReconnect());
744
- observer?.refetch({
745
- cancelRefetch: false
746
- });
747
-
748
- // Continue fetch if currently paused
749
- this.#retryer?.continue();
750
- }
751
- addObserver(observer) {
752
- if (this.#observers.indexOf(observer) === -1) {
753
- this.#observers.push(observer);
754
-
755
- // Stop the query from being garbage collected
756
- this.clearGcTimeout();
757
- this.#cache.notify({
758
- type: 'observerAdded',
759
- query: this,
760
- observer
761
- });
762
- }
763
- }
764
- removeObserver(observer) {
765
- if (this.#observers.indexOf(observer) !== -1) {
766
- this.#observers = this.#observers.filter(x => x !== observer);
767
- if (!this.#observers.length) {
768
- // If the transport layer does not support cancellation
769
- // we'll let the query continue so the result can be cached
770
- if (this.#retryer) {
771
- if (this.#abortSignalConsumed) {
772
- this.#retryer.cancel({
773
- revert: true
774
- });
775
- } else {
776
- this.#retryer.cancelRetry();
777
- }
778
- }
779
- this.scheduleGc();
780
- }
781
- this.#cache.notify({
782
- type: 'observerRemoved',
783
- query: this,
784
- observer
785
- });
786
- }
787
- }
788
- getObserversCount() {
789
- return this.#observers.length;
790
- }
791
- invalidate() {
792
- if (!this.state.isInvalidated) {
793
- this.#dispatch({
794
- type: 'invalidate'
795
- });
796
- }
797
- }
798
- fetch(options, fetchOptions) {
799
- if (this.state.fetchStatus !== 'idle') {
800
- if (this.state.dataUpdatedAt && fetchOptions?.cancelRefetch) {
801
- // Silently cancel current fetch if the user wants to cancel refetches
802
- this.cancel({
803
- silent: true
804
- });
805
- } else if (this.#promise) {
806
- // make sure that retries that were potentially cancelled due to unmounts can continue
807
- this.#retryer?.continueRetry();
808
- // Return current promise if we are already fetching
809
- return this.#promise;
810
- }
811
- }
812
-
813
- // Update config if passed, otherwise the config from the last execution is used
814
- if (options) {
815
- this.#setOptions(options);
816
- }
817
-
818
- // Use the options from the first observer with a query function if no function is found.
819
- // This can happen when the query is hydrated or created with setQueryData.
820
- if (!this.options.queryFn) {
821
- const observer = this.#observers.find(x => x.options.queryFn);
822
- if (observer) {
823
- this.#setOptions(observer.options);
824
- }
825
- }
826
- {
827
- if (!Array.isArray(this.options.queryKey)) {
828
- console.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']`);
829
- }
830
- }
831
- const abortController = new AbortController();
832
-
833
- // Create query function context
834
- const queryFnContext = {
835
- queryKey: this.queryKey,
836
- meta: this.meta
837
- };
838
-
839
- // Adds an enumerable signal property to the object that
840
- // which sets abortSignalConsumed to true when the signal
841
- // is read.
842
- const addSignalProperty = object => {
843
- Object.defineProperty(object, 'signal', {
844
- enumerable: true,
845
- get: () => {
846
- this.#abortSignalConsumed = true;
847
- return abortController.signal;
848
- }
849
- });
850
- };
851
- addSignalProperty(queryFnContext);
852
-
853
- // Create fetch function
854
- const fetchFn = () => {
855
- if (!this.options.queryFn) {
856
- return Promise.reject(new Error('Missing queryFn'));
857
- }
858
- this.#abortSignalConsumed = false;
859
- return this.options.queryFn(queryFnContext);
860
- };
861
-
862
- // Trigger behavior hook
863
- const context = {
864
- fetchOptions,
865
- options: this.options,
866
- queryKey: this.queryKey,
867
- state: this.state,
868
- fetchFn
869
- };
870
- addSignalProperty(context);
871
- this.options.behavior?.onFetch(context);
872
-
873
- // Store state in case the current fetch needs to be reverted
874
- this.#revertState = this.state;
875
-
876
- // Set to fetching state if not already in it
877
- if (this.state.fetchStatus === 'idle' || this.state.fetchMeta !== context.fetchOptions?.meta) {
878
- this.#dispatch({
879
- type: 'fetch',
880
- meta: context.fetchOptions?.meta
881
- });
882
- }
883
- const onError = error => {
884
- // Optimistically update state if needed
885
- if (!(isCancelledError(error) && error.silent)) {
886
- this.#dispatch({
887
- type: 'error',
888
- error: error
889
- });
890
- }
891
- if (!isCancelledError(error)) {
892
- // Notify cache callback
893
- this.#cache.config.onError?.(error, this);
894
- this.#cache.config.onSettled?.(this.state.data, error, this);
895
- }
896
- if (!this.isFetchingOptimistic) {
897
- // Schedule query gc after fetching
898
- this.scheduleGc();
899
- }
900
- this.isFetchingOptimistic = false;
901
- };
902
-
903
- // Try to fetch the data
904
- this.#retryer = createRetryer({
905
- fn: context.fetchFn,
906
- abort: abortController.abort.bind(abortController),
907
- onSuccess: data => {
908
- if (typeof data === 'undefined') {
909
- {
910
- console.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}`);
911
- }
912
- onError(new Error(`${this.queryHash} data is undefined`));
913
- return;
914
- }
915
- this.setData(data);
916
-
917
- // Notify cache callback
918
- this.#cache.config.onSuccess?.(data, this);
919
- this.#cache.config.onSettled?.(data, this.state.error, this);
920
- if (!this.isFetchingOptimistic) {
921
- // Schedule query gc after fetching
922
- this.scheduleGc();
923
- }
924
- this.isFetchingOptimistic = false;
925
- },
926
- onError,
927
- onFail: (failureCount, error) => {
928
- this.#dispatch({
929
- type: 'failed',
930
- failureCount,
931
- error
932
- });
933
- },
934
- onPause: () => {
935
- this.#dispatch({
936
- type: 'pause'
937
- });
938
- },
939
- onContinue: () => {
940
- this.#dispatch({
941
- type: 'continue'
942
- });
943
- },
944
- retry: context.options.retry,
945
- retryDelay: context.options.retryDelay,
946
- networkMode: context.options.networkMode
947
- });
948
- this.#promise = this.#retryer.promise;
949
- return this.#promise;
950
- }
951
- #dispatch(action) {
952
- const reducer = state => {
953
- switch (action.type) {
954
- case 'failed':
955
- return {
956
- ...state,
957
- fetchFailureCount: action.failureCount,
958
- fetchFailureReason: action.error
959
- };
960
- case 'pause':
961
- return {
962
- ...state,
963
- fetchStatus: 'paused'
964
- };
965
- case 'continue':
966
- return {
967
- ...state,
968
- fetchStatus: 'fetching'
969
- };
970
- case 'fetch':
971
- return {
972
- ...state,
973
- fetchFailureCount: 0,
974
- fetchFailureReason: null,
975
- fetchMeta: action.meta ?? null,
976
- fetchStatus: canFetch(this.options.networkMode) ? 'fetching' : 'paused',
977
- ...(!state.dataUpdatedAt && {
978
- error: null,
979
- status: 'pending'
980
- })
981
- };
982
- case 'success':
983
- return {
984
- ...state,
985
- data: action.data,
986
- dataUpdateCount: state.dataUpdateCount + 1,
987
- dataUpdatedAt: action.dataUpdatedAt ?? Date.now(),
988
- error: null,
989
- isInvalidated: false,
990
- status: 'success',
991
- ...(!action.manual && {
992
- fetchStatus: 'idle',
993
- fetchFailureCount: 0,
994
- fetchFailureReason: null
995
- })
996
- };
997
- case 'error':
998
- const error = action.error;
999
- if (isCancelledError(error) && error.revert && this.#revertState) {
1000
- return {
1001
- ...this.#revertState
1002
- };
1003
- }
1004
- return {
1005
- ...state,
1006
- error: error,
1007
- errorUpdateCount: state.errorUpdateCount + 1,
1008
- errorUpdatedAt: Date.now(),
1009
- fetchFailureCount: state.fetchFailureCount + 1,
1010
- fetchFailureReason: error,
1011
- fetchStatus: 'idle',
1012
- status: 'error'
1013
- };
1014
- case 'invalidate':
1015
- return {
1016
- ...state,
1017
- isInvalidated: true
1018
- };
1019
- case 'setState':
1020
- return {
1021
- ...state,
1022
- ...action.state
1023
- };
1024
- }
1025
- };
1026
- this.state = reducer(this.state);
1027
- notifyManager.batch(() => {
1028
- this.#observers.forEach(observer => {
1029
- observer.onQueryUpdate();
1030
- });
1031
- this.#cache.notify({
1032
- query: this,
1033
- type: 'updated',
1034
- action
1035
- });
1036
- });
1037
- }
1038
- }
1039
- function getDefaultState$1(options) {
1040
- const data = typeof options.initialData === 'function' ? options.initialData() : options.initialData;
1041
- const hasData = typeof data !== 'undefined';
1042
- const initialDataUpdatedAt = hasData ? typeof options.initialDataUpdatedAt === 'function' ? options.initialDataUpdatedAt() : options.initialDataUpdatedAt : 0;
1043
- return {
1044
- data,
1045
- dataUpdateCount: 0,
1046
- dataUpdatedAt: hasData ? initialDataUpdatedAt ?? Date.now() : 0,
1047
- error: null,
1048
- errorUpdateCount: 0,
1049
- errorUpdatedAt: 0,
1050
- fetchFailureCount: 0,
1051
- fetchFailureReason: null,
1052
- fetchMeta: null,
1053
- isInvalidated: false,
1054
- status: hasData ? 'success' : 'pending',
1055
- fetchStatus: 'idle'
1056
- };
1057
- }
1058
-
1059
- // TYPES
1060
-
1061
- // CLASS
1062
-
1063
- class QueryCache extends Subscribable {
1064
- #queries;
1065
- constructor(config = {}) {
1066
- super();
1067
- this.config = config;
1068
- this.#queries = new Map();
1069
- }
1070
- build(client, options, state) {
1071
- const queryKey = options.queryKey;
1072
- const queryHash = options.queryHash ?? hashQueryKeyByOptions(queryKey, options);
1073
- let query = this.get(queryHash);
1074
- if (!query) {
1075
- query = new Query({
1076
- cache: this,
1077
- queryKey,
1078
- queryHash,
1079
- options: client.defaultQueryOptions(options),
1080
- state,
1081
- defaultOptions: client.getQueryDefaults(queryKey)
1082
- });
1083
- this.add(query);
1084
- }
1085
- return query;
1086
- }
1087
- add(query) {
1088
- if (!this.#queries.has(query.queryHash)) {
1089
- this.#queries.set(query.queryHash, query);
1090
- this.notify({
1091
- type: 'added',
1092
- query
1093
- });
1094
- }
1095
- }
1096
- remove(query) {
1097
- const queryInMap = this.#queries.get(query.queryHash);
1098
- if (queryInMap) {
1099
- query.destroy();
1100
- if (queryInMap === query) {
1101
- this.#queries.delete(query.queryHash);
1102
- }
1103
- this.notify({
1104
- type: 'removed',
1105
- query
1106
- });
1107
- }
1108
- }
1109
- clear() {
1110
- notifyManager.batch(() => {
1111
- this.getAll().forEach(query => {
1112
- this.remove(query);
1113
- });
1114
- });
1115
- }
1116
- get(queryHash) {
1117
- return this.#queries.get(queryHash);
1118
- }
1119
- getAll() {
1120
- return [...this.#queries.values()];
1121
- }
1122
- find(filters) {
1123
- const defaultedFilters = {
1124
- exact: true,
1125
- ...filters
1126
- };
1127
- return this.getAll().find(query => matchQuery(defaultedFilters, query));
1128
- }
1129
- findAll(filters = {}) {
1130
- const queries = this.getAll();
1131
- return Object.keys(filters).length > 0 ? queries.filter(query => matchQuery(filters, query)) : queries;
1132
- }
1133
- notify(event) {
1134
- notifyManager.batch(() => {
1135
- this.listeners.forEach(listener => {
1136
- listener(event);
1137
- });
1138
- });
1139
- }
1140
- onFocus() {
1141
- notifyManager.batch(() => {
1142
- this.getAll().forEach(query => {
1143
- query.onFocus();
1144
- });
1145
- });
1146
- }
1147
- onOnline() {
1148
- notifyManager.batch(() => {
1149
- this.getAll().forEach(query => {
1150
- query.onOnline();
1151
- });
1152
- });
1153
- }
1154
- }
1155
-
1156
- // TYPES
1157
-
1158
- // CLASS
1159
-
1160
- class Mutation extends Removable {
1161
- #observers;
1162
- #defaultOptions;
1163
- #mutationCache;
1164
- #retryer;
1165
- constructor(config) {
1166
- super();
1167
- this.mutationId = config.mutationId;
1168
- this.#defaultOptions = config.defaultOptions;
1169
- this.#mutationCache = config.mutationCache;
1170
- this.#observers = [];
1171
- this.state = config.state || getDefaultState();
1172
- this.setOptions(config.options);
1173
- this.scheduleGc();
1174
- }
1175
- setOptions(options) {
1176
- this.options = {
1177
- ...this.#defaultOptions,
1178
- ...options
1179
- };
1180
- this.updateGcTime(this.options.gcTime);
1181
- }
1182
- get meta() {
1183
- return this.options.meta;
1184
- }
1185
- addObserver(observer) {
1186
- if (this.#observers.indexOf(observer) === -1) {
1187
- this.#observers.push(observer);
1188
-
1189
- // Stop the mutation from being garbage collected
1190
- this.clearGcTimeout();
1191
- this.#mutationCache.notify({
1192
- type: 'observerAdded',
1193
- mutation: this,
1194
- observer
1195
- });
1196
- }
1197
- }
1198
- removeObserver(observer) {
1199
- this.#observers = this.#observers.filter(x => x !== observer);
1200
- this.scheduleGc();
1201
- this.#mutationCache.notify({
1202
- type: 'observerRemoved',
1203
- mutation: this,
1204
- observer
1205
- });
1206
- }
1207
- optionalRemove() {
1208
- if (!this.#observers.length) {
1209
- if (this.state.status === 'pending') {
1210
- this.scheduleGc();
1211
- } else {
1212
- this.#mutationCache.remove(this);
1213
- }
1214
- }
1215
- }
1216
- continue() {
1217
- return this.#retryer?.continue() ??
1218
- // continuing a mutation assumes that variables are set, mutation must have been dehydrated before
1219
- this.execute(this.state.variables);
1220
- }
1221
- async execute(variables) {
1222
- const executeMutation = () => {
1223
- this.#retryer = createRetryer({
1224
- fn: () => {
1225
- if (!this.options.mutationFn) {
1226
- return Promise.reject(new Error('No mutationFn found'));
1227
- }
1228
- return this.options.mutationFn(variables);
1229
- },
1230
- onFail: (failureCount, error) => {
1231
- this.#dispatch({
1232
- type: 'failed',
1233
- failureCount,
1234
- error
1235
- });
1236
- },
1237
- onPause: () => {
1238
- this.#dispatch({
1239
- type: 'pause'
1240
- });
1241
- },
1242
- onContinue: () => {
1243
- this.#dispatch({
1244
- type: 'continue'
1245
- });
1246
- },
1247
- retry: this.options.retry ?? 0,
1248
- retryDelay: this.options.retryDelay,
1249
- networkMode: this.options.networkMode
1250
- });
1251
- return this.#retryer.promise;
1252
- };
1253
- const restored = this.state.status === 'pending';
1254
- try {
1255
- if (!restored) {
1256
- this.#dispatch({
1257
- type: 'pending',
1258
- variables
1259
- });
1260
- // Notify cache callback
1261
- await this.#mutationCache.config.onMutate?.(variables, this);
1262
- const context = await this.options.onMutate?.(variables);
1263
- if (context !== this.state.context) {
1264
- this.#dispatch({
1265
- type: 'pending',
1266
- context,
1267
- variables
1268
- });
1269
- }
1270
- }
1271
- const data = await executeMutation();
1272
-
1273
- // Notify cache callback
1274
- await this.#mutationCache.config.onSuccess?.(data, variables, this.state.context, this);
1275
- await this.options.onSuccess?.(data, variables, this.state.context);
1276
-
1277
- // Notify cache callback
1278
- await this.#mutationCache.config.onSettled?.(data, null, this.state.variables, this.state.context, this);
1279
- await this.options.onSettled?.(data, null, variables, this.state.context);
1280
- this.#dispatch({
1281
- type: 'success',
1282
- data
1283
- });
1284
- return data;
1285
- } catch (error) {
1286
- try {
1287
- // Notify cache callback
1288
- await this.#mutationCache.config.onError?.(error, variables, this.state.context, this);
1289
- await this.options.onError?.(error, variables, this.state.context);
1290
-
1291
- // Notify cache callback
1292
- await this.#mutationCache.config.onSettled?.(undefined, error, this.state.variables, this.state.context, this);
1293
- await this.options.onSettled?.(undefined, error, variables, this.state.context);
1294
- throw error;
1295
- } finally {
1296
- this.#dispatch({
1297
- type: 'error',
1298
- error: error
1299
- });
1300
- }
1301
- }
1302
- }
1303
- #dispatch(action) {
1304
- const reducer = state => {
1305
- switch (action.type) {
1306
- case 'failed':
1307
- return {
1308
- ...state,
1309
- failureCount: action.failureCount,
1310
- failureReason: action.error
1311
- };
1312
- case 'pause':
1313
- return {
1314
- ...state,
1315
- isPaused: true
1316
- };
1317
- case 'continue':
1318
- return {
1319
- ...state,
1320
- isPaused: false
1321
- };
1322
- case 'pending':
1323
- return {
1324
- ...state,
1325
- context: action.context,
1326
- data: undefined,
1327
- failureCount: 0,
1328
- failureReason: null,
1329
- error: null,
1330
- isPaused: !canFetch(this.options.networkMode),
1331
- status: 'pending',
1332
- variables: action.variables,
1333
- submittedAt: Date.now()
1334
- };
1335
- case 'success':
1336
- return {
1337
- ...state,
1338
- data: action.data,
1339
- failureCount: 0,
1340
- failureReason: null,
1341
- error: null,
1342
- status: 'success',
1343
- isPaused: false
1344
- };
1345
- case 'error':
1346
- return {
1347
- ...state,
1348
- data: undefined,
1349
- error: action.error,
1350
- failureCount: state.failureCount + 1,
1351
- failureReason: action.error,
1352
- isPaused: false,
1353
- status: 'error'
1354
- };
1355
- }
1356
- };
1357
- this.state = reducer(this.state);
1358
- notifyManager.batch(() => {
1359
- this.#observers.forEach(observer => {
1360
- observer.onMutationUpdate(action);
1361
- });
1362
- this.#mutationCache.notify({
1363
- mutation: this,
1364
- type: 'updated',
1365
- action
1366
- });
1367
- });
1368
- }
1369
- }
1370
- function getDefaultState() {
1371
- return {
1372
- context: undefined,
1373
- data: undefined,
1374
- error: null,
1375
- failureCount: 0,
1376
- failureReason: null,
1377
- isPaused: false,
1378
- status: 'idle',
1379
- variables: undefined,
1380
- submittedAt: 0
1381
- };
1382
- }
1383
-
1384
- // TYPES
1385
-
1386
- // CLASS
1387
-
1388
- class MutationCache extends Subscribable {
1389
- #mutations;
1390
- #mutationId;
1391
- #resuming;
1392
- constructor(config = {}) {
1393
- super();
1394
- this.config = config;
1395
- this.#mutations = [];
1396
- this.#mutationId = 0;
1397
- }
1398
- build(client, options, state) {
1399
- const mutation = new Mutation({
1400
- mutationCache: this,
1401
- mutationId: ++this.#mutationId,
1402
- options: client.defaultMutationOptions(options),
1403
- state
1404
- });
1405
- this.add(mutation);
1406
- return mutation;
1407
- }
1408
- add(mutation) {
1409
- this.#mutations.push(mutation);
1410
- this.notify({
1411
- type: 'added',
1412
- mutation
1413
- });
1414
- }
1415
- remove(mutation) {
1416
- this.#mutations = this.#mutations.filter(x => x !== mutation);
1417
- this.notify({
1418
- type: 'removed',
1419
- mutation
1420
- });
1421
- }
1422
- clear() {
1423
- notifyManager.batch(() => {
1424
- this.#mutations.forEach(mutation => {
1425
- this.remove(mutation);
1426
- });
1427
- });
1428
- }
1429
- getAll() {
1430
- return this.#mutations;
1431
- }
1432
- find(filters) {
1433
- const defaultedFilters = {
1434
- exact: true,
1435
- ...filters
1436
- };
1437
- return this.#mutations.find(mutation => matchMutation(defaultedFilters, mutation));
1438
- }
1439
- findAll(filters = {}) {
1440
- return this.#mutations.filter(mutation => matchMutation(filters, mutation));
1441
- }
1442
- notify(event) {
1443
- notifyManager.batch(() => {
1444
- this.listeners.forEach(listener => {
1445
- listener(event);
1446
- });
1447
- });
1448
- }
1449
- resumePausedMutations() {
1450
- this.#resuming = (this.#resuming ?? Promise.resolve()).then(() => {
1451
- const pausedMutations = this.#mutations.filter(x => x.state.isPaused);
1452
- return notifyManager.batch(() => pausedMutations.reduce((promise, mutation) => promise.then(() => mutation.continue().catch(noop)), Promise.resolve()));
1453
- }).then(() => {
1454
- this.#resuming = undefined;
1455
- });
1456
- return this.#resuming;
1457
- }
1458
- }
1459
-
1460
- function infiniteQueryBehavior() {
1461
- return {
1462
- onFetch: context => {
1463
- context.fetchFn = async () => {
1464
- const options = context.options;
1465
- const direction = context.fetchOptions?.meta?.fetchMore?.direction;
1466
- const oldPages = context.state.data?.pages || [];
1467
- const oldPageParams = context.state.data?.pageParams || [];
1468
- const empty = {
1469
- pages: [],
1470
- pageParams: []
1471
- };
1472
- let cancelled = false;
1473
- const addSignalProperty = object => {
1474
- Object.defineProperty(object, 'signal', {
1475
- enumerable: true,
1476
- get: () => {
1477
- if (context.signal.aborted) {
1478
- cancelled = true;
1479
- } else {
1480
- context.signal.addEventListener('abort', () => {
1481
- cancelled = true;
1482
- });
1483
- }
1484
- return context.signal;
1485
- }
1486
- });
1487
- };
1488
-
1489
- // Get query function
1490
- const queryFn = context.options.queryFn || (() => Promise.reject(new Error('Missing queryFn')));
1491
-
1492
- // Create function to fetch a page
1493
- const fetchPage = async (data, param, previous) => {
1494
- if (cancelled) {
1495
- return Promise.reject();
1496
- }
1497
- if (typeof param === 'undefined' && data.pages.length) {
1498
- return Promise.resolve(data);
1499
- }
1500
- const queryFnContext = {
1501
- queryKey: context.queryKey,
1502
- pageParam: param,
1503
- direction: previous ? 'backward' : 'forward',
1504
- meta: context.options.meta
1505
- };
1506
- addSignalProperty(queryFnContext);
1507
- const page = await queryFn(queryFnContext);
1508
- const {
1509
- maxPages
1510
- } = context.options;
1511
- const addTo = previous ? addToStart : addToEnd;
1512
- return {
1513
- pages: addTo(data.pages, page, maxPages),
1514
- pageParams: addTo(data.pageParams, param, maxPages)
1515
- };
1516
- };
1517
- let result;
1518
-
1519
- // Fetch first page?
1520
- if (!oldPages.length) {
1521
- result = await fetchPage(empty, options.defaultPageParam);
1522
- }
1523
-
1524
- // fetch next / previous page?
1525
- else if (direction) {
1526
- const previous = direction === 'backward';
1527
- const pageParamFn = previous ? getPreviousPageParam : getNextPageParam;
1528
- const oldData = {
1529
- pages: oldPages,
1530
- pageParams: oldPageParams
1531
- };
1532
- const param = pageParamFn(options, oldData);
1533
- result = await fetchPage(oldData, param, previous);
1534
- }
1535
-
1536
- // Refetch pages
1537
- else {
1538
- // Fetch first page
1539
- result = await fetchPage(empty, oldPageParams[0]);
1540
-
1541
- // Fetch remaining pages
1542
- for (let i = 1; i < oldPages.length; i++) {
1543
- const param = getNextPageParam(options, result);
1544
- result = await fetchPage(result, param);
1545
- }
1546
- }
1547
- return result;
1548
- };
1549
- }
1550
- };
1551
- }
1552
- function getNextPageParam(options, {
1553
- pages,
1554
- pageParams
1555
- }) {
1556
- const lastIndex = pages.length - 1;
1557
- return options.getNextPageParam(pages[lastIndex], pages, pageParams[lastIndex], pageParams);
1558
- }
1559
- function getPreviousPageParam(options, {
1560
- pages,
1561
- pageParams
1562
- }) {
1563
- return options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams);
1564
- }
1565
-
1566
- /**
1567
- * Checks if there is a next page.
1568
- */
1569
- function hasNextPage(options, data) {
1570
- if (!data) return false;
1571
- return typeof getNextPageParam(options, data) !== 'undefined';
1572
- }
1573
-
1574
- /**
1575
- * Checks if there is a previous page.
1576
- */
1577
- function hasPreviousPage(options, data) {
1578
- if (!data || !options.getPreviousPageParam) return false;
1579
- return typeof getPreviousPageParam(options, data) !== 'undefined';
1580
- }
1581
-
1582
- // TYPES
1583
-
1584
- // CLASS
1585
-
1586
- class QueryClient {
1587
- #queryCache;
1588
- #mutationCache;
1589
- #defaultOptions;
1590
- #queryDefaults;
1591
- #mutationDefaults;
1592
- #mountCount;
1593
- #unsubscribeFocus;
1594
- #unsubscribeOnline;
1595
- constructor(config = {}) {
1596
- this.#queryCache = config.queryCache || new QueryCache();
1597
- this.#mutationCache = config.mutationCache || new MutationCache();
1598
- this.#defaultOptions = config.defaultOptions || {};
1599
- this.#queryDefaults = new Map();
1600
- this.#mutationDefaults = new Map();
1601
- this.#mountCount = 0;
1602
- }
1603
- mount() {
1604
- this.#mountCount++;
1605
- if (this.#mountCount !== 1) return;
1606
- this.#unsubscribeFocus = focusManager.subscribe(() => {
1607
- if (focusManager.isFocused()) {
1608
- this.resumePausedMutations();
1609
- this.#queryCache.onFocus();
1610
- }
1611
- });
1612
- this.#unsubscribeOnline = onlineManager.subscribe(() => {
1613
- if (onlineManager.isOnline()) {
1614
- this.resumePausedMutations();
1615
- this.#queryCache.onOnline();
1616
- }
1617
- });
1618
- }
1619
- unmount() {
1620
- this.#mountCount--;
1621
- if (this.#mountCount !== 0) return;
1622
- this.#unsubscribeFocus?.();
1623
- this.#unsubscribeFocus = undefined;
1624
- this.#unsubscribeOnline?.();
1625
- this.#unsubscribeOnline = undefined;
1626
- }
1627
- isFetching(filters) {
1628
- return this.#queryCache.findAll({
1629
- ...filters,
1630
- fetchStatus: 'fetching'
1631
- }).length;
1632
- }
1633
- isMutating(filters) {
1634
- return this.#mutationCache.findAll({
1635
- ...filters,
1636
- status: 'pending'
1637
- }).length;
1638
- }
1639
- getQueryData(queryKey) {
1640
- return this.#queryCache.find({
1641
- queryKey
1642
- })?.state.data;
1643
- }
1644
- ensureQueryData(options) {
1645
- const cachedData = this.getQueryData(options.queryKey);
1646
- return cachedData ? Promise.resolve(cachedData) : this.fetchQuery(options);
1647
- }
1648
- getQueriesData(filters) {
1649
- return this.getQueryCache().findAll(filters).map(({
1650
- queryKey,
1651
- state
1652
- }) => {
1653
- const data = state.data;
1654
- return [queryKey, data];
1655
- });
1656
- }
1657
- setQueryData(queryKey, updater, options) {
1658
- const query = this.#queryCache.find({
1659
- queryKey
1660
- });
1661
- const prevData = query?.state.data;
1662
- const data = functionalUpdate(updater, prevData);
1663
- if (typeof data === 'undefined') {
1664
- return undefined;
1665
- }
1666
- const defaultedOptions = this.defaultQueryOptions({
1667
- queryKey
1668
- });
1669
- return this.#queryCache.build(this, defaultedOptions).setData(data, {
1670
- ...options,
1671
- manual: true
1672
- });
1673
- }
1674
- setQueriesData(filters, updater, options) {
1675
- return notifyManager.batch(() => this.getQueryCache().findAll(filters).map(({
1676
- queryKey
1677
- }) => [queryKey, this.setQueryData(queryKey, updater, options)]));
1678
- }
1679
- getQueryState(queryKey) {
1680
- return this.#queryCache.find({
1681
- queryKey
1682
- })?.state;
1683
- }
1684
- removeQueries(filters) {
1685
- const queryCache = this.#queryCache;
1686
- notifyManager.batch(() => {
1687
- queryCache.findAll(filters).forEach(query => {
1688
- queryCache.remove(query);
1689
- });
1690
- });
1691
- }
1692
- resetQueries(filters, options) {
1693
- const queryCache = this.#queryCache;
1694
- const refetchFilters = {
1695
- type: 'active',
1696
- ...filters
1697
- };
1698
- return notifyManager.batch(() => {
1699
- queryCache.findAll(filters).forEach(query => {
1700
- query.reset();
1701
- });
1702
- return this.refetchQueries(refetchFilters, options);
1703
- });
1704
- }
1705
- cancelQueries(filters = {}, cancelOptions = {}) {
1706
- const defaultedCancelOptions = {
1707
- revert: true,
1708
- ...cancelOptions
1709
- };
1710
- const promises = notifyManager.batch(() => this.#queryCache.findAll(filters).map(query => query.cancel(defaultedCancelOptions)));
1711
- return Promise.all(promises).then(noop).catch(noop);
1712
- }
1713
- invalidateQueries(filters = {}, options = {}) {
1714
- return notifyManager.batch(() => {
1715
- this.#queryCache.findAll(filters).forEach(query => {
1716
- query.invalidate();
1717
- });
1718
- if (filters.refetchType === 'none') {
1719
- return Promise.resolve();
1720
- }
1721
- const refetchFilters = {
1722
- ...filters,
1723
- type: filters.refetchType ?? filters.type ?? 'active'
1724
- };
1725
- return this.refetchQueries(refetchFilters, options);
1726
- });
1727
- }
1728
- refetchQueries(filters = {}, options) {
1729
- const fetchOptions = {
1730
- ...options,
1731
- cancelRefetch: options?.cancelRefetch ?? true
1732
- };
1733
- const promises = notifyManager.batch(() => this.#queryCache.findAll(filters).filter(query => !query.isDisabled()).map(query => {
1734
- let promise = query.fetch(undefined, fetchOptions);
1735
- if (!fetchOptions.throwOnError) {
1736
- promise = promise.catch(noop);
1737
- }
1738
- return query.state.fetchStatus === 'paused' ? Promise.resolve() : promise;
1739
- }));
1740
- return Promise.all(promises).then(noop);
1741
- }
1742
- fetchQuery(options) {
1743
- const defaultedOptions = this.defaultQueryOptions(options);
1744
-
1745
- // https://github.com/tannerlinsley/react-query/issues/652
1746
- if (typeof defaultedOptions.retry === 'undefined') {
1747
- defaultedOptions.retry = false;
1748
- }
1749
- const query = this.#queryCache.build(this, defaultedOptions);
1750
- return query.isStaleByTime(defaultedOptions.staleTime) ? query.fetch(defaultedOptions) : Promise.resolve(query.state.data);
1751
- }
1752
- prefetchQuery(options) {
1753
- return this.fetchQuery(options).then(noop).catch(noop);
1754
- }
1755
- fetchInfiniteQuery(options) {
1756
- options.behavior = infiniteQueryBehavior();
1757
- return this.fetchQuery(options);
1758
- }
1759
- prefetchInfiniteQuery(options) {
1760
- return this.fetchInfiniteQuery(options).then(noop).catch(noop);
1761
- }
1762
- resumePausedMutations() {
1763
- return this.#mutationCache.resumePausedMutations();
1764
- }
1765
- getQueryCache() {
1766
- return this.#queryCache;
1767
- }
1768
- getMutationCache() {
1769
- return this.#mutationCache;
1770
- }
1771
- getDefaultOptions() {
1772
- return this.#defaultOptions;
1773
- }
1774
- setDefaultOptions(options) {
1775
- this.#defaultOptions = options;
1776
- }
1777
- setQueryDefaults(queryKey, options) {
1778
- this.#queryDefaults.set(hashKey(queryKey), {
1779
- queryKey,
1780
- defaultOptions: options
1781
- });
1782
- }
1783
- getQueryDefaults(queryKey) {
1784
- const defaults = [...this.#queryDefaults.values()];
1785
- let result = {};
1786
- defaults.forEach(queryDefault => {
1787
- if (partialMatchKey(queryKey, queryDefault.queryKey)) {
1788
- result = {
1789
- ...result,
1790
- ...queryDefault.defaultOptions
1791
- };
1792
- }
1793
- });
1794
- return result;
1795
- }
1796
- setMutationDefaults(mutationKey, options) {
1797
- this.#mutationDefaults.set(hashKey(mutationKey), {
1798
- mutationKey,
1799
- defaultOptions: options
1800
- });
1801
- }
1802
- getMutationDefaults(mutationKey) {
1803
- const defaults = [...this.#mutationDefaults.values()];
1804
- let result = {};
1805
- defaults.forEach(queryDefault => {
1806
- if (partialMatchKey(mutationKey, queryDefault.mutationKey)) {
1807
- result = {
1808
- ...result,
1809
- ...queryDefault.defaultOptions
1810
- };
1811
- }
1812
- });
1813
- return result;
1814
- }
1815
- defaultQueryOptions(options) {
1816
- if (options?._defaulted) {
1817
- return options;
1818
- }
1819
- const defaultedOptions = {
1820
- ...this.#defaultOptions.queries,
1821
- ...(options?.queryKey && this.getQueryDefaults(options.queryKey)),
1822
- ...options,
1823
- _defaulted: true
1824
- };
1825
- if (!defaultedOptions.queryHash) {
1826
- defaultedOptions.queryHash = hashQueryKeyByOptions(defaultedOptions.queryKey, defaultedOptions);
1827
- }
1828
-
1829
- // dependent default values
1830
- if (typeof defaultedOptions.refetchOnReconnect === 'undefined') {
1831
- defaultedOptions.refetchOnReconnect = defaultedOptions.networkMode !== 'always';
1832
- }
1833
- if (typeof defaultedOptions.throwOnError === 'undefined') {
1834
- defaultedOptions.throwOnError = !!defaultedOptions.suspense;
1835
- }
1836
- return defaultedOptions;
1837
- }
1838
- defaultMutationOptions(options) {
1839
- if (options?._defaulted) {
1840
- return options;
1841
- }
1842
- return {
1843
- ...this.#defaultOptions.mutations,
1844
- ...(options?.mutationKey && this.getMutationDefaults(options.mutationKey)),
1845
- ...options,
1846
- _defaulted: true
1847
- };
1848
- }
1849
- clear() {
1850
- this.#queryCache.clear();
1851
- this.#mutationCache.clear();
1852
- }
1853
- }
1854
-
1855
- class QueryObserver extends Subscribable {
1856
- #client;
1857
- #currentQuery = undefined;
1858
- #currentQueryInitialState = undefined;
1859
- #currentResult = undefined;
1860
- #currentResultState;
1861
- #currentResultOptions;
1862
- #selectError;
1863
- #selectFn;
1864
- #selectResult;
1865
- // This property keeps track of the last query with defined data.
1866
- // It will be used to pass the previous data and query to the placeholder function between renders.
1867
- #lastQueryWithDefinedData;
1868
- #staleTimeoutId;
1869
- #refetchIntervalId;
1870
- #currentRefetchInterval;
1871
- #trackedProps = new Set();
1872
- constructor(client, options) {
1873
- super();
1874
- this.#client = client;
1875
- this.options = options;
1876
- this.#selectError = null;
1877
- this.bindMethods();
1878
- this.setOptions(options);
1879
- }
1880
- bindMethods() {
1881
- this.refetch = this.refetch.bind(this);
1882
- }
1883
- onSubscribe() {
1884
- if (this.listeners.size === 1) {
1885
- this.#currentQuery.addObserver(this);
1886
- if (shouldFetchOnMount(this.#currentQuery, this.options)) {
1887
- this.#executeFetch();
1888
- }
1889
- this.#updateTimers();
1890
- }
1891
- }
1892
- onUnsubscribe() {
1893
- if (!this.hasListeners()) {
1894
- this.destroy();
1895
- }
1896
- }
1897
- shouldFetchOnReconnect() {
1898
- return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnReconnect);
1899
- }
1900
- shouldFetchOnWindowFocus() {
1901
- return shouldFetchOn(this.#currentQuery, this.options, this.options.refetchOnWindowFocus);
1902
- }
1903
- destroy() {
1904
- this.listeners = new Set();
1905
- this.#clearStaleTimeout();
1906
- this.#clearRefetchInterval();
1907
- this.#currentQuery.removeObserver(this);
1908
- }
1909
- setOptions(options, notifyOptions) {
1910
- const prevOptions = this.options;
1911
- const prevQuery = this.#currentQuery;
1912
- this.options = this.#client.defaultQueryOptions(options);
1913
- if (!shallowEqualObjects(prevOptions, this.options)) {
1914
- this.#client.getQueryCache().notify({
1915
- type: 'observerOptionsUpdated',
1916
- query: this.#currentQuery,
1917
- observer: this
1918
- });
1919
- }
1920
- if (typeof this.options.enabled !== 'undefined' && typeof this.options.enabled !== 'boolean') {
1921
- throw new Error('Expected enabled to be a boolean');
1922
- }
1923
-
1924
- // Keep previous query key if the user does not supply one
1925
- if (!this.options.queryKey) {
1926
- this.options.queryKey = prevOptions.queryKey;
1927
- }
1928
- this.#updateQuery();
1929
- const mounted = this.hasListeners();
1930
-
1931
- // Fetch if there are subscribers
1932
- if (mounted && shouldFetchOptionally(this.#currentQuery, prevQuery, this.options, prevOptions)) {
1933
- this.#executeFetch();
1934
- }
1935
-
1936
- // Update result
1937
- this.#updateResult(notifyOptions);
1938
-
1939
- // Update stale interval if needed
1940
- if (mounted && (this.#currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || this.options.staleTime !== prevOptions.staleTime)) {
1941
- this.#updateStaleTimeout();
1942
- }
1943
- const nextRefetchInterval = this.#computeRefetchInterval();
1944
-
1945
- // Update refetch interval if needed
1946
- if (mounted && (this.#currentQuery !== prevQuery || this.options.enabled !== prevOptions.enabled || nextRefetchInterval !== this.#currentRefetchInterval)) {
1947
- this.#updateRefetchInterval(nextRefetchInterval);
1948
- }
1949
- }
1950
- getOptimisticResult(options) {
1951
- const query = this.#client.getQueryCache().build(this.#client, options);
1952
- return this.createResult(query, options);
1953
- }
1954
- getCurrentResult() {
1955
- return this.#currentResult;
1956
- }
1957
- trackResult(result) {
1958
- const trackedResult = {};
1959
- Object.keys(result).forEach(key => {
1960
- Object.defineProperty(trackedResult, key, {
1961
- configurable: false,
1962
- enumerable: true,
1963
- get: () => {
1964
- this.#trackedProps.add(key);
1965
- return result[key];
1966
- }
1967
- });
1968
- });
1969
- return trackedResult;
1970
- }
1971
- getCurrentQuery() {
1972
- return this.#currentQuery;
1973
- }
1974
- refetch({
1975
- ...options
1976
- } = {}) {
1977
- return this.fetch({
1978
- ...options
1979
- });
1980
- }
1981
- fetchOptimistic(options) {
1982
- const defaultedOptions = this.#client.defaultQueryOptions(options);
1983
- const query = this.#client.getQueryCache().build(this.#client, defaultedOptions);
1984
- query.isFetchingOptimistic = true;
1985
- return query.fetch().then(() => this.createResult(query, defaultedOptions));
1986
- }
1987
- fetch(fetchOptions) {
1988
- return this.#executeFetch({
1989
- ...fetchOptions,
1990
- cancelRefetch: fetchOptions.cancelRefetch ?? true
1991
- }).then(() => {
1992
- this.#updateResult();
1993
- return this.#currentResult;
1994
- });
1995
- }
1996
- #executeFetch(fetchOptions) {
1997
- // Make sure we reference the latest query as the current one might have been removed
1998
- this.#updateQuery();
1999
-
2000
- // Fetch
2001
- let promise = this.#currentQuery.fetch(this.options, fetchOptions);
2002
- if (!fetchOptions?.throwOnError) {
2003
- promise = promise.catch(noop);
2004
- }
2005
- return promise;
2006
- }
2007
- #updateStaleTimeout() {
2008
- this.#clearStaleTimeout();
2009
- if (isServer || this.#currentResult.isStale || !isValidTimeout(this.options.staleTime)) {
2010
- return;
2011
- }
2012
- const time = timeUntilStale(this.#currentResult.dataUpdatedAt, this.options.staleTime);
2013
-
2014
- // The timeout is sometimes triggered 1 ms before the stale time expiration.
2015
- // To mitigate this issue we always add 1 ms to the timeout.
2016
- const timeout = time + 1;
2017
- this.#staleTimeoutId = setTimeout(() => {
2018
- if (!this.#currentResult.isStale) {
2019
- this.#updateResult();
2020
- }
2021
- }, timeout);
2022
- }
2023
- #computeRefetchInterval() {
2024
- return (typeof this.options.refetchInterval === 'function' ? this.options.refetchInterval(this.#currentResult.data, this.#currentQuery) : this.options.refetchInterval) ?? false;
2025
- }
2026
- #updateRefetchInterval(nextInterval) {
2027
- this.#clearRefetchInterval();
2028
- this.#currentRefetchInterval = nextInterval;
2029
- if (isServer || this.options.enabled === false || !isValidTimeout(this.#currentRefetchInterval) || this.#currentRefetchInterval === 0) {
2030
- return;
2031
- }
2032
- this.#refetchIntervalId = setInterval(() => {
2033
- if (this.options.refetchIntervalInBackground || focusManager.isFocused()) {
2034
- this.#executeFetch();
2035
- }
2036
- }, this.#currentRefetchInterval);
2037
- }
2038
- #updateTimers() {
2039
- this.#updateStaleTimeout();
2040
- this.#updateRefetchInterval(this.#computeRefetchInterval());
2041
- }
2042
- #clearStaleTimeout() {
2043
- if (this.#staleTimeoutId) {
2044
- clearTimeout(this.#staleTimeoutId);
2045
- this.#staleTimeoutId = undefined;
2046
- }
2047
- }
2048
- #clearRefetchInterval() {
2049
- if (this.#refetchIntervalId) {
2050
- clearInterval(this.#refetchIntervalId);
2051
- this.#refetchIntervalId = undefined;
2052
- }
2053
- }
2054
- createResult(query, options) {
2055
- const prevQuery = this.#currentQuery;
2056
- const prevOptions = this.options;
2057
- const prevResult = this.#currentResult;
2058
- const prevResultState = this.#currentResultState;
2059
- const prevResultOptions = this.#currentResultOptions;
2060
- const queryChange = query !== prevQuery;
2061
- const queryInitialState = queryChange ? query.state : this.#currentQueryInitialState;
2062
- const {
2063
- state
2064
- } = query;
2065
- let {
2066
- error,
2067
- errorUpdatedAt,
2068
- fetchStatus,
2069
- status
2070
- } = state;
2071
- let isPlaceholderData = false;
2072
- let data;
2073
-
2074
- // Optimistically set result in fetching state if needed
2075
- if (options._optimisticResults) {
2076
- const mounted = this.hasListeners();
2077
- const fetchOnMount = !mounted && shouldFetchOnMount(query, options);
2078
- const fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions);
2079
- if (fetchOnMount || fetchOptionally) {
2080
- fetchStatus = canFetch(query.options.networkMode) ? 'fetching' : 'paused';
2081
- if (!state.dataUpdatedAt) {
2082
- status = 'pending';
2083
- }
2084
- }
2085
- if (options._optimisticResults === 'isRestoring') {
2086
- fetchStatus = 'idle';
2087
- }
2088
- }
2089
-
2090
- // Select data if needed
2091
- if (options.select && typeof state.data !== 'undefined') {
2092
- // Memoize select result
2093
- if (prevResult && state.data === prevResultState?.data && options.select === this.#selectFn) {
2094
- data = this.#selectResult;
2095
- } else {
2096
- try {
2097
- this.#selectFn = options.select;
2098
- data = options.select(state.data);
2099
- data = replaceData(prevResult?.data, data, options);
2100
- this.#selectResult = data;
2101
- this.#selectError = null;
2102
- } catch (selectError) {
2103
- this.#selectError = selectError;
2104
- }
2105
- }
2106
- }
2107
- // Use query data
2108
- else {
2109
- data = state.data;
2110
- }
2111
-
2112
- // Show placeholder data if needed
2113
- if (typeof options.placeholderData !== 'undefined' && typeof data === 'undefined' && status === 'pending') {
2114
- let placeholderData;
2115
-
2116
- // Memoize placeholder data
2117
- if (prevResult?.isPlaceholderData && options.placeholderData === prevResultOptions?.placeholderData) {
2118
- placeholderData = prevResult.data;
2119
- } else {
2120
- placeholderData = typeof options.placeholderData === 'function' ? options.placeholderData(this.#lastQueryWithDefinedData?.state.data, this.#lastQueryWithDefinedData) : options.placeholderData;
2121
- if (options.select && typeof placeholderData !== 'undefined') {
2122
- try {
2123
- placeholderData = options.select(placeholderData);
2124
- this.#selectError = null;
2125
- } catch (selectError) {
2126
- this.#selectError = selectError;
2127
- }
2128
- }
2129
- }
2130
- if (typeof placeholderData !== 'undefined') {
2131
- status = 'success';
2132
- data = replaceData(prevResult?.data, placeholderData, options);
2133
- isPlaceholderData = true;
2134
- }
2135
- }
2136
- if (this.#selectError) {
2137
- error = this.#selectError;
2138
- data = this.#selectResult;
2139
- errorUpdatedAt = Date.now();
2140
- status = 'error';
2141
- }
2142
- const isFetching = fetchStatus === 'fetching';
2143
- const isPending = status === 'pending';
2144
- const isError = status === 'error';
2145
- const isLoading = isPending && isFetching;
2146
- const result = {
2147
- status,
2148
- fetchStatus,
2149
- isPending,
2150
- isSuccess: status === 'success',
2151
- isError,
2152
- isInitialLoading: isLoading,
2153
- isLoading,
2154
- data,
2155
- dataUpdatedAt: state.dataUpdatedAt,
2156
- error,
2157
- errorUpdatedAt,
2158
- failureCount: state.fetchFailureCount,
2159
- failureReason: state.fetchFailureReason,
2160
- errorUpdateCount: state.errorUpdateCount,
2161
- isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,
2162
- isFetchedAfterMount: state.dataUpdateCount > queryInitialState.dataUpdateCount || state.errorUpdateCount > queryInitialState.errorUpdateCount,
2163
- isFetching,
2164
- isRefetching: isFetching && !isPending,
2165
- isLoadingError: isError && state.dataUpdatedAt === 0,
2166
- isPaused: fetchStatus === 'paused',
2167
- isPlaceholderData,
2168
- isRefetchError: isError && state.dataUpdatedAt !== 0,
2169
- isStale: isStale(query, options),
2170
- refetch: this.refetch
2171
- };
2172
- return result;
2173
- }
2174
- #updateResult(notifyOptions) {
2175
- const prevResult = this.#currentResult;
2176
- const nextResult = this.createResult(this.#currentQuery, this.options);
2177
- this.#currentResultState = this.#currentQuery.state;
2178
- this.#currentResultOptions = this.options;
2179
-
2180
- // Only notify and update result if something has changed
2181
- if (shallowEqualObjects(nextResult, prevResult)) {
2182
- return;
2183
- }
2184
- if (this.#currentResultState.data !== undefined) {
2185
- this.#lastQueryWithDefinedData = this.#currentQuery;
2186
- }
2187
- this.#currentResult = nextResult;
2188
-
2189
- // Determine which callbacks to trigger
2190
- const defaultNotifyOptions = {};
2191
- const shouldNotifyListeners = () => {
2192
- if (!prevResult) {
2193
- return true;
2194
- }
2195
- const {
2196
- notifyOnChangeProps
2197
- } = this.options;
2198
- if (notifyOnChangeProps === 'all' || !notifyOnChangeProps && !this.#trackedProps.size) {
2199
- return true;
2200
- }
2201
- const includedProps = new Set(notifyOnChangeProps ?? this.#trackedProps);
2202
- if (this.options.throwOnError) {
2203
- includedProps.add('error');
2204
- }
2205
- return Object.keys(this.#currentResult).some(key => {
2206
- const typedKey = key;
2207
- const changed = this.#currentResult[typedKey] !== prevResult[typedKey];
2208
- return changed && includedProps.has(typedKey);
2209
- });
2210
- };
2211
- if (notifyOptions?.listeners !== false && shouldNotifyListeners()) {
2212
- defaultNotifyOptions.listeners = true;
2213
- }
2214
- this.#notify({
2215
- ...defaultNotifyOptions,
2216
- ...notifyOptions
2217
- });
2218
- }
2219
- #updateQuery() {
2220
- const query = this.#client.getQueryCache().build(this.#client, this.options);
2221
- if (query === this.#currentQuery) {
2222
- return;
2223
- }
2224
- const prevQuery = this.#currentQuery;
2225
- this.#currentQuery = query;
2226
- this.#currentQueryInitialState = query.state;
2227
- if (this.hasListeners()) {
2228
- prevQuery?.removeObserver(this);
2229
- query.addObserver(this);
2230
- }
2231
- }
2232
- onQueryUpdate() {
2233
- this.#updateResult();
2234
- if (this.hasListeners()) {
2235
- this.#updateTimers();
2236
- }
2237
- }
2238
- #notify(notifyOptions) {
2239
- notifyManager.batch(() => {
2240
- // First, trigger the listeners
2241
- if (notifyOptions.listeners) {
2242
- this.listeners.forEach(listener => {
2243
- listener(this.#currentResult);
2244
- });
2245
- }
2246
-
2247
- // Then the cache listeners
2248
- this.#client.getQueryCache().notify({
2249
- query: this.#currentQuery,
2250
- type: 'observerResultsUpdated'
2251
- });
2252
- });
2253
- }
2254
- }
2255
- function shouldLoadOnMount(query, options) {
2256
- return options.enabled !== false && !query.state.dataUpdatedAt && !(query.state.status === 'error' && options.retryOnMount === false);
2257
- }
2258
- function shouldFetchOnMount(query, options) {
2259
- return shouldLoadOnMount(query, options) || query.state.dataUpdatedAt > 0 && shouldFetchOn(query, options, options.refetchOnMount);
2260
- }
2261
- function shouldFetchOn(query, options, field) {
2262
- if (options.enabled !== false) {
2263
- const value = typeof field === 'function' ? field(query) : field;
2264
- return value === 'always' || value !== false && isStale(query, options);
2265
- }
2266
- return false;
2267
- }
2268
- function shouldFetchOptionally(query, prevQuery, options, prevOptions) {
2269
- return options.enabled !== false && (query !== prevQuery || prevOptions.enabled === false) && (!options.suspense || query.state.status !== 'error') && isStale(query, options);
2270
- }
2271
- function isStale(query, options) {
2272
- return query.isStaleByTime(options.staleTime);
2273
- }
2274
-
2275
- function difference(array1, array2) {
2276
- return array1.filter(x => array2.indexOf(x) === -1);
2277
- }
2278
- function replaceAt(array, index, value) {
2279
- const copy = array.slice(0);
2280
- copy[index] = value;
2281
- return copy;
2282
- }
2283
- class QueriesObserver extends Subscribable {
2284
- #client;
2285
- #result;
2286
- #queries;
2287
- #observers;
2288
- #options;
2289
- #combinedResult;
2290
- constructor(client, queries, options) {
2291
- super();
2292
- this.#client = client;
2293
- this.#queries = [];
2294
- this.#observers = [];
2295
- this.#setResult([]);
2296
- this.setQueries(queries, options);
2297
- }
2298
- #setResult(value) {
2299
- this.#result = value;
2300
- this.#combinedResult = this.#combineResult(value);
2301
- }
2302
- onSubscribe() {
2303
- if (this.listeners.size === 1) {
2304
- this.#observers.forEach(observer => {
2305
- observer.subscribe(result => {
2306
- this.#onUpdate(observer, result);
2307
- });
2308
- });
2309
- }
2310
- }
2311
- onUnsubscribe() {
2312
- if (!this.listeners.size) {
2313
- this.destroy();
2314
- }
2315
- }
2316
- destroy() {
2317
- this.listeners = new Set();
2318
- this.#observers.forEach(observer => {
2319
- observer.destroy();
2320
- });
2321
- }
2322
- setQueries(queries, options, notifyOptions) {
2323
- this.#queries = queries;
2324
- this.#options = options;
2325
- notifyManager.batch(() => {
2326
- const prevObservers = this.#observers;
2327
- const newObserverMatches = this.#findMatchingObservers(this.#queries);
2328
-
2329
- // set options for the new observers to notify of changes
2330
- newObserverMatches.forEach(match => match.observer.setOptions(match.defaultedQueryOptions, notifyOptions));
2331
- const newObservers = newObserverMatches.map(match => match.observer);
2332
- const newResult = newObservers.map(observer => observer.getCurrentResult());
2333
- const hasIndexChange = newObservers.some((observer, index) => observer !== prevObservers[index]);
2334
- if (prevObservers.length === newObservers.length && !hasIndexChange) {
2335
- return;
2336
- }
2337
- this.#observers = newObservers;
2338
- this.#setResult(newResult);
2339
- if (!this.hasListeners()) {
2340
- return;
2341
- }
2342
- difference(prevObservers, newObservers).forEach(observer => {
2343
- observer.destroy();
2344
- });
2345
- difference(newObservers, prevObservers).forEach(observer => {
2346
- observer.subscribe(result => {
2347
- this.#onUpdate(observer, result);
2348
- });
2349
- });
2350
- this.#notify();
2351
- });
2352
- }
2353
- getCurrentResult() {
2354
- return this.#combinedResult;
2355
- }
2356
- getQueries() {
2357
- return this.#observers.map(observer => observer.getCurrentQuery());
2358
- }
2359
- getObservers() {
2360
- return this.#observers;
2361
- }
2362
- getOptimisticResult(queries) {
2363
- const matches = this.#findMatchingObservers(queries);
2364
- const result = matches.map(match => match.observer.getOptimisticResult(match.defaultedQueryOptions));
2365
- return [result, r => {
2366
- return this.#combineResult(r ?? result);
2367
- }, () => {
2368
- return matches.map((match, index) => {
2369
- const observerResult = result[index];
2370
- return !match.defaultedQueryOptions.notifyOnChangeProps ? match.observer.trackResult(observerResult) : observerResult;
2371
- });
2372
- }];
2373
- }
2374
- #combineResult(input) {
2375
- const combine = this.#options?.combine;
2376
- if (combine) {
2377
- return replaceEqualDeep(this.#combinedResult, combine(input));
2378
- }
2379
- return input;
2380
- }
2381
- #findMatchingObservers(queries) {
2382
- const prevObservers = this.#observers;
2383
- const prevObserversMap = new Map(prevObservers.map(observer => [observer.options.queryHash, observer]));
2384
- const defaultedQueryOptions = queries.map(options => this.#client.defaultQueryOptions(options));
2385
- const matchingObservers = defaultedQueryOptions.flatMap(defaultedOptions => {
2386
- const match = prevObserversMap.get(defaultedOptions.queryHash);
2387
- if (match != null) {
2388
- return [{
2389
- defaultedQueryOptions: defaultedOptions,
2390
- observer: match
2391
- }];
2392
- }
2393
- return [];
2394
- });
2395
- const matchedQueryHashes = new Set(matchingObservers.map(match => match.defaultedQueryOptions.queryHash));
2396
- const unmatchedQueries = defaultedQueryOptions.filter(defaultedOptions => !matchedQueryHashes.has(defaultedOptions.queryHash));
2397
- const getObserver = options => {
2398
- const defaultedOptions = this.#client.defaultQueryOptions(options);
2399
- const currentObserver = this.#observers.find(o => o.options.queryHash === defaultedOptions.queryHash);
2400
- return currentObserver ?? new QueryObserver(this.#client, defaultedOptions);
2401
- };
2402
- const newOrReusedObservers = unmatchedQueries.map(options => {
2403
- return {
2404
- defaultedQueryOptions: options,
2405
- observer: getObserver(options)
2406
- };
2407
- });
2408
- const sortMatchesByOrderOfQueries = (a, b) => defaultedQueryOptions.indexOf(a.defaultedQueryOptions) - defaultedQueryOptions.indexOf(b.defaultedQueryOptions);
2409
- return matchingObservers.concat(newOrReusedObservers).sort(sortMatchesByOrderOfQueries);
2410
- }
2411
- #onUpdate(observer, result) {
2412
- const index = this.#observers.indexOf(observer);
2413
- if (index !== -1) {
2414
- this.#setResult(replaceAt(this.#result, index, result));
2415
- this.#notify();
2416
- }
2417
- }
2418
- #notify() {
2419
- notifyManager.batch(() => {
2420
- this.listeners.forEach(listener => {
2421
- listener(this.#result);
2422
- });
2423
- });
2424
- }
2425
- }
2426
-
2427
- class InfiniteQueryObserver extends QueryObserver {
2428
- // Type override
2429
-
2430
- // Type override
2431
-
2432
- // Type override
2433
-
2434
- // eslint-disable-next-line @typescript-eslint/no-useless-constructor
2435
- constructor(client, options) {
2436
- super(client, options);
2437
- }
2438
- bindMethods() {
2439
- super.bindMethods();
2440
- this.fetchNextPage = this.fetchNextPage.bind(this);
2441
- this.fetchPreviousPage = this.fetchPreviousPage.bind(this);
2442
- }
2443
- setOptions(options, notifyOptions) {
2444
- super.setOptions({
2445
- ...options,
2446
- behavior: infiniteQueryBehavior()
2447
- }, notifyOptions);
2448
- }
2449
- getOptimisticResult(options) {
2450
- options.behavior = infiniteQueryBehavior();
2451
- return super.getOptimisticResult(options);
2452
- }
2453
- fetchNextPage(options) {
2454
- return this.fetch({
2455
- ...options,
2456
- meta: {
2457
- fetchMore: {
2458
- direction: 'forward'
2459
- }
2460
- }
2461
- });
2462
- }
2463
- fetchPreviousPage(options) {
2464
- return this.fetch({
2465
- ...options,
2466
- meta: {
2467
- fetchMore: {
2468
- direction: 'backward'
2469
- }
2470
- }
2471
- });
2472
- }
2473
- createResult(query, options) {
2474
- const {
2475
- state
2476
- } = query;
2477
- const result = super.createResult(query, options);
2478
- const {
2479
- isFetching,
2480
- isRefetching
2481
- } = result;
2482
- const isFetchingNextPage = isFetching && state.fetchMeta?.fetchMore?.direction === 'forward';
2483
- const isFetchingPreviousPage = isFetching && state.fetchMeta?.fetchMore?.direction === 'backward';
2484
- return {
2485
- ...result,
2486
- fetchNextPage: this.fetchNextPage,
2487
- fetchPreviousPage: this.fetchPreviousPage,
2488
- hasNextPage: hasNextPage(options, state.data),
2489
- hasPreviousPage: hasPreviousPage(options, state.data),
2490
- isFetchingNextPage,
2491
- isFetchingPreviousPage,
2492
- isRefetching: isRefetching && !isFetchingNextPage && !isFetchingPreviousPage
2493
- };
2494
- }
2495
- }
2496
-
2497
- // TYPES
2498
-
2499
- // CLASS
2500
-
2501
- class MutationObserver extends Subscribable {
2502
- #client;
2503
- #currentResult = undefined;
2504
- #currentMutation;
2505
- #mutateOptions;
2506
- constructor(client, options) {
2507
- super();
2508
- this.#client = client;
2509
- this.setOptions(options);
2510
- this.bindMethods();
2511
- this.#updateResult();
2512
- }
2513
- bindMethods() {
2514
- this.mutate = this.mutate.bind(this);
2515
- this.reset = this.reset.bind(this);
2516
- }
2517
- setOptions(options) {
2518
- const prevOptions = this.options;
2519
- this.options = this.#client.defaultMutationOptions(options);
2520
- if (!shallowEqualObjects(prevOptions, this.options)) {
2521
- this.#client.getMutationCache().notify({
2522
- type: 'observerOptionsUpdated',
2523
- mutation: this.#currentMutation,
2524
- observer: this
2525
- });
2526
- }
2527
- this.#currentMutation?.setOptions(this.options);
2528
- }
2529
- onUnsubscribe() {
2530
- if (!this.hasListeners()) {
2531
- this.#currentMutation?.removeObserver(this);
2532
- }
2533
- }
2534
- onMutationUpdate(action) {
2535
- this.#updateResult();
2536
- this.#notify(action);
2537
- }
2538
- getCurrentResult() {
2539
- return this.#currentResult;
2540
- }
2541
- reset() {
2542
- this.#currentMutation = undefined;
2543
- this.#updateResult();
2544
- this.#notify();
2545
- }
2546
- mutate(variables, options) {
2547
- this.#mutateOptions = options;
2548
- this.#currentMutation?.removeObserver(this);
2549
- this.#currentMutation = this.#client.getMutationCache().build(this.#client, this.options);
2550
- this.#currentMutation.addObserver(this);
2551
- return this.#currentMutation.execute(variables);
2552
- }
2553
- #updateResult() {
2554
- const state = this.#currentMutation?.state ?? getDefaultState();
2555
- this.#currentResult = {
2556
- ...state,
2557
- isPending: state.status === 'pending',
2558
- isSuccess: state.status === 'success',
2559
- isError: state.status === 'error',
2560
- isIdle: state.status === 'idle',
2561
- mutate: this.mutate,
2562
- reset: this.reset
2563
- };
2564
- }
2565
- #notify(action) {
2566
- notifyManager.batch(() => {
2567
- // First trigger the mutate callbacks
2568
- if (this.#mutateOptions && this.hasListeners()) {
2569
- if (action?.type === 'success') {
2570
- this.#mutateOptions.onSuccess?.(action.data, this.#currentResult.variables, this.#currentResult.context);
2571
- this.#mutateOptions.onSettled?.(action.data, null, this.#currentResult.variables, this.#currentResult.context);
2572
- } else if (action?.type === 'error') {
2573
- this.#mutateOptions.onError?.(action.error, this.#currentResult.variables, this.#currentResult.context);
2574
- this.#mutateOptions.onSettled?.(undefined, action.error, this.#currentResult.variables, this.#currentResult.context);
2575
- }
2576
- }
2577
-
2578
- // Then trigger the listeners
2579
- this.listeners.forEach(listener => {
2580
- listener(this.#currentResult);
2581
- });
2582
- });
2583
- }
2584
- }
2585
-
2586
- // TYPES
2587
-
2588
- // FUNCTIONS
2589
-
2590
- function dehydrateMutation(mutation) {
2591
- return {
2592
- mutationKey: mutation.options.mutationKey,
2593
- state: mutation.state
2594
- };
2595
- }
2596
-
2597
- // Most config is not dehydrated but instead meant to configure again when
2598
- // consuming the de/rehydrated data, typically with useQuery on the client.
2599
- // Sometimes it might make sense to prefetch data on the server and include
2600
- // in the html-payload, but not consume it on the initial render.
2601
- function dehydrateQuery(query) {
2602
- return {
2603
- state: query.state,
2604
- queryKey: query.queryKey,
2605
- queryHash: query.queryHash
2606
- };
2607
- }
2608
- function defaultShouldDehydrateMutation(mutation) {
2609
- return mutation.state.isPaused;
2610
- }
2611
- function defaultShouldDehydrateQuery(query) {
2612
- return query.state.status === 'success';
2613
- }
2614
- function dehydrate(client, options = {}) {
2615
- const filterMutation = options.shouldDehydrateMutation ?? defaultShouldDehydrateMutation;
2616
- const mutations = client.getMutationCache().getAll().flatMap(mutation => filterMutation(mutation) ? [dehydrateMutation(mutation)] : []);
2617
- const filterQuery = options.shouldDehydrateQuery ?? defaultShouldDehydrateQuery;
2618
- const queries = client.getQueryCache().getAll().flatMap(query => filterQuery(query) ? [dehydrateQuery(query)] : []);
2619
- return {
2620
- mutations,
2621
- queries
2622
- };
2623
- }
2624
- function hydrate(client, dehydratedState, options) {
2625
- if (typeof dehydratedState !== 'object' || dehydratedState === null) {
2626
- return;
2627
- }
2628
- const mutationCache = client.getMutationCache();
2629
- const queryCache = client.getQueryCache();
2630
-
2631
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
2632
- const mutations = dehydratedState.mutations || [];
2633
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
2634
- const queries = dehydratedState.queries || [];
2635
- mutations.forEach(dehydratedMutation => {
2636
- mutationCache.build(client, {
2637
- ...options?.defaultOptions?.mutations,
2638
- mutationKey: dehydratedMutation.mutationKey
2639
- }, dehydratedMutation.state);
2640
- });
2641
- queries.forEach(dehydratedQuery => {
2642
- const query = queryCache.get(dehydratedQuery.queryHash);
2643
-
2644
- // Reset fetch status to idle in the dehydrated state to avoid
2645
- // query being stuck in fetching state upon hydration
2646
- const dehydratedQueryState = {
2647
- ...dehydratedQuery.state,
2648
- fetchStatus: 'idle'
2649
- };
2650
-
2651
- // Do not hydrate if an existing query exists with newer data
2652
- if (query) {
2653
- if (query.state.dataUpdatedAt < dehydratedQueryState.dataUpdatedAt) {
2654
- query.setState(dehydratedQueryState);
2655
- }
2656
- return;
2657
- }
2658
-
2659
- // Restore query
2660
- queryCache.build(client, {
2661
- ...options?.defaultOptions?.queries,
2662
- queryKey: dehydratedQuery.queryKey,
2663
- queryHash: dehydratedQuery.queryHash
2664
- }, dehydratedQueryState);
2665
- });
2666
- }
2667
-
2668
- exports.CancelledError = CancelledError;
2669
- exports.InfiniteQueryObserver = InfiniteQueryObserver;
2670
- exports.MutationCache = MutationCache;
2671
- exports.MutationObserver = MutationObserver;
2672
- exports.QueriesObserver = QueriesObserver;
2673
- exports.QueryCache = QueryCache;
2674
- exports.QueryClient = QueryClient;
2675
- exports.QueryObserver = QueryObserver;
2676
- exports.defaultShouldDehydrateMutation = defaultShouldDehydrateMutation;
2677
- exports.defaultShouldDehydrateQuery = defaultShouldDehydrateQuery;
2678
- exports.dehydrate = dehydrate;
2679
- exports.focusManager = focusManager;
2680
- exports.hashKey = hashKey;
2681
- exports.hydrate = hydrate;
2682
- exports.isCancelledError = isCancelledError;
2683
- exports.isServer = isServer;
2684
- exports.keepPreviousData = keepPreviousData;
2685
- exports.matchQuery = matchQuery;
2686
- exports.notifyManager = notifyManager;
2687
- exports.onlineManager = onlineManager;
2688
- exports.replaceEqualDeep = replaceEqualDeep;
2689
-
2690
- }));
2691
- //# sourceMappingURL=index.development.js.map