@tanstack/vue-query 4.29.21 → 4.29.22

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.
@@ -2324,7 +2324,30 @@
2324
2324
 
2325
2325
  getOptimisticResult(options) {
2326
2326
  const query = this.client.getQueryCache().build(this.client, options);
2327
- return this.createResult(query, options);
2327
+ const result = this.createResult(query, options);
2328
+
2329
+ if (shouldAssignObserverCurrentProperties(this, result, options)) {
2330
+ // this assigns the optimistic result to the current Observer
2331
+ // because if the query function changes, useQuery will be performing
2332
+ // an effect where it would fetch again.
2333
+ // When the fetch finishes, we perform a deep data cloning in order
2334
+ // to reuse objects references. This deep data clone is performed against
2335
+ // the `observer.currentResult.data` property
2336
+ // When QueryKey changes, we refresh the query and get new `optimistic`
2337
+ // result, while we leave the `observer.currentResult`, so when new data
2338
+ // arrives, it finds the old `observer.currentResult` which is related
2339
+ // to the old QueryKey. Which means that currentResult and selectData are
2340
+ // out of sync already.
2341
+ // To solve this, we move the cursor of the currentResult everytime
2342
+ // an observer reads an optimistic value.
2343
+ // When keeping the previous data, the result doesn't change until new
2344
+ // data arrives.
2345
+ this.currentResult = result;
2346
+ this.currentResultOptions = this.options;
2347
+ this.currentResultState = this.currentQuery.state;
2348
+ }
2349
+
2350
+ return result;
2328
2351
  }
2329
2352
 
2330
2353
  getCurrentResult() {
@@ -2740,6 +2763,38 @@
2740
2763
 
2741
2764
  function isStale(query, options) {
2742
2765
  return query.isStaleByTime(options.staleTime);
2766
+ } // this function would decide if we will update the observer's 'current'
2767
+ // properties after an optimistic reading via getOptimisticResult
2768
+
2769
+
2770
+ function shouldAssignObserverCurrentProperties(observer, optimisticResult, options) {
2771
+ // it is important to keep this condition like this for three reasons:
2772
+ // 1. It will get removed in the v5
2773
+ // 2. it reads: don't update the properties if we want to keep the previous
2774
+ // data.
2775
+ // 3. The opposite condition (!options.keepPreviousData) would fallthrough
2776
+ // and will result in a bad decision
2777
+ if (options.keepPreviousData) {
2778
+ return false;
2779
+ } // this means we want to put some placeholder data when pending and queryKey
2780
+ // changed.
2781
+
2782
+
2783
+ if (options.placeholderData !== undefined) {
2784
+ // re-assign properties only if current data is placeholder data
2785
+ // which means that data did not arrive yet, so, if there is some cached data
2786
+ // we need to "prepare" to receive it
2787
+ return optimisticResult.isPlaceholderData;
2788
+ } // if the newly created result isn't what the observer is holding as current,
2789
+ // then we'll need to update the properties as well
2790
+
2791
+
2792
+ if (observer.getCurrentResult() !== optimisticResult) {
2793
+ return true;
2794
+ } // basically, just keep previous properties if nothing changed
2795
+
2796
+
2797
+ return false;
2743
2798
  }
2744
2799
 
2745
2800
  class QueriesObserver extends Subscribable {