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