@tanstack/query-core 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.
- package/build/lib/queryObserver.d.ts.map +1 -1
- package/build/lib/queryObserver.esm.js +56 -1
- package/build/lib/queryObserver.esm.js.map +1 -1
- package/build/lib/queryObserver.js +56 -1
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/queryObserver.mjs +56 -1
- package/build/lib/queryObserver.mjs.map +1 -1
- package/build/umd/index.development.js +56 -1
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/queryObserver.ts +72 -1
package/package.json
CHANGED
package/src/queryObserver.ts
CHANGED
|
@@ -240,7 +240,30 @@ export class QueryObserver<
|
|
|
240
240
|
): QueryObserverResult<TData, TError> {
|
|
241
241
|
const query = this.client.getQueryCache().build(this.client, options)
|
|
242
242
|
|
|
243
|
-
|
|
243
|
+
const result = this.createResult(query, options)
|
|
244
|
+
|
|
245
|
+
if (shouldAssignObserverCurrentProperties(this, result, options)) {
|
|
246
|
+
// this assigns the optimistic result to the current Observer
|
|
247
|
+
// because if the query function changes, useQuery will be performing
|
|
248
|
+
// an effect where it would fetch again.
|
|
249
|
+
// When the fetch finishes, we perform a deep data cloning in order
|
|
250
|
+
// to reuse objects references. This deep data clone is performed against
|
|
251
|
+
// the `observer.currentResult.data` property
|
|
252
|
+
// When QueryKey changes, we refresh the query and get new `optimistic`
|
|
253
|
+
// result, while we leave the `observer.currentResult`, so when new data
|
|
254
|
+
// arrives, it finds the old `observer.currentResult` which is related
|
|
255
|
+
// to the old QueryKey. Which means that currentResult and selectData are
|
|
256
|
+
// out of sync already.
|
|
257
|
+
// To solve this, we move the cursor of the currentResult everytime
|
|
258
|
+
// an observer reads an optimistic value.
|
|
259
|
+
|
|
260
|
+
// When keeping the previous data, the result doesn't change until new
|
|
261
|
+
// data arrives.
|
|
262
|
+
this.currentResult = result
|
|
263
|
+
this.currentResultOptions = this.options
|
|
264
|
+
this.currentResultState = this.currentQuery.state
|
|
265
|
+
}
|
|
266
|
+
return result
|
|
244
267
|
}
|
|
245
268
|
|
|
246
269
|
getCurrentResult(): QueryObserverResult<TData, TError> {
|
|
@@ -764,3 +787,51 @@ function isStale(
|
|
|
764
787
|
): boolean {
|
|
765
788
|
return query.isStaleByTime(options.staleTime)
|
|
766
789
|
}
|
|
790
|
+
|
|
791
|
+
// this function would decide if we will update the observer's 'current'
|
|
792
|
+
// properties after an optimistic reading via getOptimisticResult
|
|
793
|
+
function shouldAssignObserverCurrentProperties<
|
|
794
|
+
TQueryFnData = unknown,
|
|
795
|
+
TError = unknown,
|
|
796
|
+
TData = TQueryFnData,
|
|
797
|
+
TQueryData = TQueryFnData,
|
|
798
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
799
|
+
>(
|
|
800
|
+
observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
|
|
801
|
+
optimisticResult: QueryObserverResult<TData, TError>,
|
|
802
|
+
options: DefaultedQueryObserverOptions<
|
|
803
|
+
TQueryFnData,
|
|
804
|
+
TError,
|
|
805
|
+
TData,
|
|
806
|
+
TQueryData,
|
|
807
|
+
TQueryKey
|
|
808
|
+
>,
|
|
809
|
+
) {
|
|
810
|
+
// it is important to keep this condition like this for three reasons:
|
|
811
|
+
// 1. It will get removed in the v5
|
|
812
|
+
// 2. it reads: don't update the properties if we want to keep the previous
|
|
813
|
+
// data.
|
|
814
|
+
// 3. The opposite condition (!options.keepPreviousData) would fallthrough
|
|
815
|
+
// and will result in a bad decision
|
|
816
|
+
if (options.keepPreviousData) {
|
|
817
|
+
return false
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
// this means we want to put some placeholder data when pending and queryKey
|
|
821
|
+
// changed.
|
|
822
|
+
if (options.placeholderData !== undefined) {
|
|
823
|
+
// re-assign properties only if current data is placeholder data
|
|
824
|
+
// which means that data did not arrive yet, so, if there is some cached data
|
|
825
|
+
// we need to "prepare" to receive it
|
|
826
|
+
return optimisticResult.isPlaceholderData
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
// if the newly created result isn't what the observer is holding as current,
|
|
830
|
+
// then we'll need to update the properties as well
|
|
831
|
+
if (observer.getCurrentResult() !== optimisticResult) {
|
|
832
|
+
return true
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// basically, just keep previous properties if nothing changed
|
|
836
|
+
return false
|
|
837
|
+
}
|