@tanstack/query-core 5.0.0-alpha.26 → 5.0.0-alpha.31
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/focusManager.d.ts.map +1 -0
- package/build/lib/hydration.d.ts.map +1 -0
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/infiniteQueryBehavior.d.ts.map +1 -0
- package/build/lib/infiniteQueryObserver.d.ts.map +1 -0
- package/build/lib/mutation.d.ts.map +1 -0
- package/build/lib/mutationCache.d.ts.map +1 -0
- package/build/lib/mutationObserver.d.ts.map +1 -0
- package/build/lib/notifyManager.d.ts.map +1 -0
- package/build/lib/onlineManager.d.ts.map +1 -0
- package/build/lib/queriesObserver.d.ts.map +1 -0
- package/build/lib/query.d.ts.map +1 -0
- package/build/lib/queryCache.d.ts.map +1 -0
- package/build/lib/queryClient.d.ts.map +1 -0
- package/build/lib/queryObserver.d.ts.map +1 -0
- package/build/lib/queryObserver.esm.js +7 -6
- package/build/lib/queryObserver.esm.js.map +1 -1
- package/build/lib/queryObserver.js +7 -6
- package/build/lib/queryObserver.js.map +1 -1
- package/build/lib/queryObserver.mjs +5 -5
- package/build/lib/queryObserver.mjs.map +1 -1
- package/build/lib/removable.d.ts.map +1 -0
- package/build/lib/retryer.d.ts.map +1 -0
- package/build/lib/subscribable.d.ts.map +1 -0
- package/build/lib/tests/focusManager.test.d.ts.map +1 -0
- package/build/lib/tests/hydration.test.d.ts.map +1 -0
- package/build/lib/tests/infiniteQueryBehavior.test.d.ts.map +1 -0
- package/build/lib/tests/infiniteQueryObserver.test.d.ts.map +1 -0
- package/build/lib/tests/mutationCache.test.d.ts.map +1 -0
- package/build/lib/tests/mutationObserver.test.d.ts.map +1 -0
- package/build/lib/tests/mutations.test.d.ts.map +1 -0
- package/build/lib/tests/notifyManager.test.d.ts.map +1 -0
- package/build/lib/tests/onlineManager.test.d.ts.map +1 -0
- package/build/lib/tests/queriesObserver.test.d.ts.map +1 -0
- package/build/lib/tests/query.test.d.ts.map +1 -0
- package/build/lib/tests/queryCache.test.d.ts.map +1 -0
- package/build/lib/tests/queryClient.test.d.ts.map +1 -0
- package/build/lib/tests/queryObserver.test.d.ts.map +1 -0
- package/build/lib/tests/utils.d.ts.map +1 -0
- package/build/lib/tests/utils.test.d.ts.map +1 -0
- package/build/lib/types.d.ts +1 -1
- package/build/lib/types.d.ts.map +1 -0
- package/build/lib/utils.d.ts.map +1 -0
- package/build/umd/index.development.js +5 -5
- 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 +8 -5
- package/src/tests/queryObserver.test.tsx +66 -0
- package/src/types.ts +7 -1
package/package.json
CHANGED
package/src/queryObserver.ts
CHANGED
|
@@ -65,9 +65,9 @@ export class QueryObserver<
|
|
|
65
65
|
#selectError: TError | null
|
|
66
66
|
#selectFn?: (data: TQueryData) => TData
|
|
67
67
|
#selectResult?: TData
|
|
68
|
-
// This property keeps track of the last defined
|
|
69
|
-
// It will be used to pass the previous data to the placeholder function between renders.
|
|
70
|
-
#
|
|
68
|
+
// This property keeps track of the last query with defined data.
|
|
69
|
+
// It will be used to pass the previous data and query to the placeholder function between renders.
|
|
70
|
+
#lastQueryWithDefinedData?: Query<TQueryFnData, TError, TQueryData, TQueryKey>
|
|
71
71
|
#staleTimeoutId?: ReturnType<typeof setTimeout>
|
|
72
72
|
#refetchIntervalId?: ReturnType<typeof setInterval>
|
|
73
73
|
#currentRefetchInterval?: number | false
|
|
@@ -489,7 +489,10 @@ export class QueryObserver<
|
|
|
489
489
|
typeof options.placeholderData === 'function'
|
|
490
490
|
? (
|
|
491
491
|
options.placeholderData as unknown as PlaceholderDataFunction<TQueryData>
|
|
492
|
-
)(
|
|
492
|
+
)(
|
|
493
|
+
this.#lastQueryWithDefinedData?.state.data,
|
|
494
|
+
this.#lastQueryWithDefinedData as any,
|
|
495
|
+
)
|
|
493
496
|
: options.placeholderData
|
|
494
497
|
if (options.select && typeof placeholderData !== 'undefined') {
|
|
495
498
|
try {
|
|
@@ -572,7 +575,7 @@ export class QueryObserver<
|
|
|
572
575
|
}
|
|
573
576
|
|
|
574
577
|
if (this.#currentResultState.data !== undefined) {
|
|
575
|
-
this.#
|
|
578
|
+
this.#lastQueryWithDefinedData = this.#currentQuery
|
|
576
579
|
}
|
|
577
580
|
this.#currentResult = nextResult
|
|
578
581
|
|
|
@@ -691,6 +691,72 @@ describe('queryObserver', () => {
|
|
|
691
691
|
expect(observer.getCurrentResult().isPlaceholderData).toBe(false)
|
|
692
692
|
})
|
|
693
693
|
|
|
694
|
+
test('should pass the correct previous queryKey (from prevQuery) to placeholderData function params with select', async () => {
|
|
695
|
+
const results: QueryObserverResult[] = []
|
|
696
|
+
const keys: Array<readonly unknown[] | null> = []
|
|
697
|
+
|
|
698
|
+
const key1 = queryKey()
|
|
699
|
+
const key2 = queryKey()
|
|
700
|
+
|
|
701
|
+
const data1 = { value: 'data1' }
|
|
702
|
+
const data2 = { value: 'data2' }
|
|
703
|
+
|
|
704
|
+
const observer = new QueryObserver(queryClient, {
|
|
705
|
+
queryKey: key1,
|
|
706
|
+
queryFn: () => data1,
|
|
707
|
+
placeholderData: (prev, prevQuery) => {
|
|
708
|
+
keys.push(prevQuery?.queryKey || null)
|
|
709
|
+
return prev
|
|
710
|
+
},
|
|
711
|
+
select: (data) => data.value,
|
|
712
|
+
})
|
|
713
|
+
|
|
714
|
+
const unsubscribe = observer.subscribe((result) => {
|
|
715
|
+
results.push(result)
|
|
716
|
+
})
|
|
717
|
+
|
|
718
|
+
await sleep(1)
|
|
719
|
+
|
|
720
|
+
observer.setOptions({
|
|
721
|
+
queryKey: key2,
|
|
722
|
+
queryFn: () => data2,
|
|
723
|
+
placeholderData: (prev, prevQuery) => {
|
|
724
|
+
keys.push(prevQuery?.queryKey || null)
|
|
725
|
+
return prev
|
|
726
|
+
},
|
|
727
|
+
select: (data) => data.value,
|
|
728
|
+
})
|
|
729
|
+
|
|
730
|
+
await sleep(1)
|
|
731
|
+
unsubscribe()
|
|
732
|
+
expect(results.length).toBe(4)
|
|
733
|
+
expect(keys.length).toBe(3)
|
|
734
|
+
expect(keys[0]).toBe(null) // First Query - status: 'pending', fetchStatus: 'idle'
|
|
735
|
+
expect(keys[1]).toBe(null) // First Query - status: 'pending', fetchStatus: 'fetching'
|
|
736
|
+
expect(keys[2]).toBe(key1) // Second Query - status: 'pending', fetchStatus: 'fetching'
|
|
737
|
+
|
|
738
|
+
expect(results[0]).toMatchObject({
|
|
739
|
+
data: undefined,
|
|
740
|
+
status: 'pending',
|
|
741
|
+
fetchStatus: 'fetching',
|
|
742
|
+
}) // Initial fetch
|
|
743
|
+
expect(results[1]).toMatchObject({
|
|
744
|
+
data: 'data1',
|
|
745
|
+
status: 'success',
|
|
746
|
+
fetchStatus: 'idle',
|
|
747
|
+
}) // Successful fetch
|
|
748
|
+
expect(results[2]).toMatchObject({
|
|
749
|
+
data: 'data1',
|
|
750
|
+
status: 'success',
|
|
751
|
+
fetchStatus: 'fetching',
|
|
752
|
+
}) // Fetch for new key, but using previous data as placeholder
|
|
753
|
+
expect(results[3]).toMatchObject({
|
|
754
|
+
data: 'data2',
|
|
755
|
+
status: 'success',
|
|
756
|
+
fetchStatus: 'idle',
|
|
757
|
+
}) // Successful fetch for new key
|
|
758
|
+
})
|
|
759
|
+
|
|
694
760
|
test('should pass the correct previous data to placeholderData function params when select function is used in conjunction', async () => {
|
|
695
761
|
const results: QueryObserverResult[] = []
|
|
696
762
|
|
package/src/types.ts
CHANGED
|
@@ -49,8 +49,14 @@ export type InitialDataFunction<T> = () => T | undefined
|
|
|
49
49
|
|
|
50
50
|
type NonFunctionGuard<T> = T extends Function ? never : T
|
|
51
51
|
|
|
52
|
-
export type PlaceholderDataFunction<
|
|
52
|
+
export type PlaceholderDataFunction<
|
|
53
|
+
TQueryFnData = unknown,
|
|
54
|
+
TError = DefaultError,
|
|
55
|
+
TQueryData = TQueryFnData,
|
|
56
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
57
|
+
> = (
|
|
53
58
|
previousData: TQueryData | undefined,
|
|
59
|
+
previousQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> | undefined,
|
|
54
60
|
) => TQueryData | undefined
|
|
55
61
|
|
|
56
62
|
export type QueriesPlaceholderDataFunction<TQueryData> = () =>
|