@tanstack/react-query 4.29.19 → 4.29.23

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.
@@ -430,9 +430,10 @@
430
430
  }
431
431
 
432
432
  setFocused(focused) {
433
- this.focused = focused;
433
+ const changed = this.focused !== focused;
434
434
 
435
- if (focused) {
435
+ if (changed) {
436
+ this.focused = focused;
436
437
  this.onFocus();
437
438
  }
438
439
  }
@@ -518,9 +519,10 @@
518
519
  }
519
520
 
520
521
  setOnline(online) {
521
- this.online = online;
522
+ const changed = this.online !== online;
522
523
 
523
- if (online) {
524
+ if (changed) {
525
+ this.online = online;
524
526
  this.onOnline();
525
527
  }
526
528
  }
@@ -2348,7 +2350,30 @@
2348
2350
 
2349
2351
  getOptimisticResult(options) {
2350
2352
  const query = this.client.getQueryCache().build(this.client, options);
2351
- return this.createResult(query, options);
2353
+ const result = this.createResult(query, options);
2354
+
2355
+ if (shouldAssignObserverCurrentProperties(this, result, options)) {
2356
+ // this assigns the optimistic result to the current Observer
2357
+ // because if the query function changes, useQuery will be performing
2358
+ // an effect where it would fetch again.
2359
+ // When the fetch finishes, we perform a deep data cloning in order
2360
+ // to reuse objects references. This deep data clone is performed against
2361
+ // the `observer.currentResult.data` property
2362
+ // When QueryKey changes, we refresh the query and get new `optimistic`
2363
+ // result, while we leave the `observer.currentResult`, so when new data
2364
+ // arrives, it finds the old `observer.currentResult` which is related
2365
+ // to the old QueryKey. Which means that currentResult and selectData are
2366
+ // out of sync already.
2367
+ // To solve this, we move the cursor of the currentResult everytime
2368
+ // an observer reads an optimistic value.
2369
+ // When keeping the previous data, the result doesn't change until new
2370
+ // data arrives.
2371
+ this.currentResult = result;
2372
+ this.currentResultOptions = this.options;
2373
+ this.currentResultState = this.currentQuery.state;
2374
+ }
2375
+
2376
+ return result;
2352
2377
  }
2353
2378
 
2354
2379
  getCurrentResult() {
@@ -2764,6 +2789,38 @@
2764
2789
 
2765
2790
  function isStale(query, options) {
2766
2791
  return query.isStaleByTime(options.staleTime);
2792
+ } // this function would decide if we will update the observer's 'current'
2793
+ // properties after an optimistic reading via getOptimisticResult
2794
+
2795
+
2796
+ function shouldAssignObserverCurrentProperties(observer, optimisticResult, options) {
2797
+ // it is important to keep this condition like this for three reasons:
2798
+ // 1. It will get removed in the v5
2799
+ // 2. it reads: don't update the properties if we want to keep the previous
2800
+ // data.
2801
+ // 3. The opposite condition (!options.keepPreviousData) would fallthrough
2802
+ // and will result in a bad decision
2803
+ if (options.keepPreviousData) {
2804
+ return false;
2805
+ } // this means we want to put some placeholder data when pending and queryKey
2806
+ // changed.
2807
+
2808
+
2809
+ if (options.placeholderData !== undefined) {
2810
+ // re-assign properties only if current data is placeholder data
2811
+ // which means that data did not arrive yet, so, if there is some cached data
2812
+ // we need to "prepare" to receive it
2813
+ return optimisticResult.isPlaceholderData;
2814
+ } // if the newly created result isn't what the observer is holding as current,
2815
+ // then we'll need to update the properties as well
2816
+
2817
+
2818
+ if (observer.getCurrentResult() !== optimisticResult) {
2819
+ return true;
2820
+ } // basically, just keep previous properties if nothing changed
2821
+
2822
+
2823
+ return false;
2767
2824
  }
2768
2825
 
2769
2826
  class QueriesObserver extends Subscribable {