forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseQueries.ts
247 lines (224 loc) · 8.57 KB
/
useQueries.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'use client'
import * as React from 'react'
import { useSyncExternalStore } from './useSyncExternalStore'
import type { QueryKey, QueryFunction } from '@tanstack/query-core'
import { notifyManager, QueriesObserver } from '@tanstack/query-core'
import { useQueryClient } from './QueryClientProvider'
import type { UseQueryOptions, UseQueryResult } from './types'
import { useIsRestoring } from './isRestoring'
import { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'
import {
ensurePreventErrorBoundaryRetry,
getHasError,
useClearResetErrorBoundary,
} from './errorBoundaryUtils'
import {
ensureStaleTime,
shouldSuspend,
fetchOptimistic,
willFetch,
} from './suspense'
// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
// - `context` is omitted as it is passed as a root-level option to `useQueries` instead.
type UseQueryOptionsForUseQueries<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'context'>
// Avoid TS depth-limit error in case of large array literal
type MAXIMUM_DEPTH = 20
type GetOptions<T> =
// Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }
T extends {
queryFnData: infer TQueryFnData
error?: infer TError
data: infer TData
}
? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>
: T extends { queryFnData: infer TQueryFnData; error?: infer TError }
? UseQueryOptionsForUseQueries<TQueryFnData, TError>
: T extends { data: infer TData; error?: infer TError }
? UseQueryOptionsForUseQueries<unknown, TError, TData>
: // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]
T extends [infer TQueryFnData, infer TError, infer TData]
? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>
: T extends [infer TQueryFnData, infer TError]
? UseQueryOptionsForUseQueries<TQueryFnData, TError>
: T extends [infer TQueryFnData]
? UseQueryOptionsForUseQueries<TQueryFnData>
: // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided
T extends {
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey>
select: (data: any) => infer TData
}
? UseQueryOptionsForUseQueries<TQueryFnData, unknown, TData, TQueryKey>
: T extends { queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> }
? UseQueryOptionsForUseQueries<
TQueryFnData,
unknown,
TQueryFnData,
TQueryKey
>
: // Fallback
UseQueryOptionsForUseQueries
type GetResults<T> =
// Part 1: responsible for mapping explicit type parameter to function result, if object
T extends { queryFnData: any; error?: infer TError; data: infer TData }
? UseQueryResult<TData, TError>
: T extends { queryFnData: infer TQueryFnData; error?: infer TError }
? UseQueryResult<TQueryFnData, TError>
: T extends { data: infer TData; error?: infer TError }
? UseQueryResult<TData, TError>
: // Part 2: responsible for mapping explicit type parameter to function result, if tuple
T extends [any, infer TError, infer TData]
? UseQueryResult<TData, TError>
: T extends [infer TQueryFnData, infer TError]
? UseQueryResult<TQueryFnData, TError>
: T extends [infer TQueryFnData]
? UseQueryResult<TQueryFnData>
: // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided
T extends {
queryFn?: QueryFunction<unknown, any>
select: (data: any) => infer TData
}
? UseQueryResult<TData>
: T extends { queryFn?: QueryFunction<infer TQueryFnData, any> }
? UseQueryResult<TQueryFnData>
: // Fallback
UseQueryResult
/**
* QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
*/
export type QueriesOptions<
T extends any[],
Result extends any[] = [],
Depth extends ReadonlyArray<number> = [],
> = Depth['length'] extends MAXIMUM_DEPTH
? UseQueryOptionsForUseQueries[]
: T extends []
? []
: T extends [infer Head]
? [...Result, GetOptions<Head>]
: T extends [infer Head, ...infer Tail]
? QueriesOptions<[...Tail], [...Result, GetOptions<Head>], [...Depth, 1]>
: unknown[] extends T
? T
: // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogenous type!
// use this to infer the param types in the case of Array.map() argument
T extends UseQueryOptionsForUseQueries<
infer TQueryFnData,
infer TError,
infer TData,
infer TQueryKey
>[]
? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData, TQueryKey>[]
: // Fallback
UseQueryOptionsForUseQueries[]
/**
* QueriesResults reducer recursively maps type param to results
*/
export type QueriesResults<
T extends any[],
Result extends any[] = [],
Depth extends ReadonlyArray<number> = [],
> = Depth['length'] extends MAXIMUM_DEPTH
? UseQueryResult[]
: T extends []
? []
: T extends [infer Head]
? [...Result, GetResults<Head>]
: T extends [infer Head, ...infer Tail]
? QueriesResults<[...Tail], [...Result, GetResults<Head>], [...Depth, 1]>
: T extends UseQueryOptionsForUseQueries<
infer TQueryFnData,
infer TError,
infer TData,
any
>[]
? // Dynamic-size (homogenous) UseQueryOptions array: map directly to array of results
UseQueryResult<unknown extends TData ? TQueryFnData : TData, TError>[]
: // Fallback
UseQueryResult[]
export function useQueries<T extends any[]>({
queries,
context,
}: {
queries: readonly [...QueriesOptions<T>]
context?: UseQueryOptions['context']
}): QueriesResults<T> {
const queryClient = useQueryClient({ context })
const isRestoring = useIsRestoring()
const errorResetBoundary = useQueryErrorResetBoundary()
const defaultedQueries = React.useMemo(
() =>
queries.map((options) => {
const defaultedOptions = queryClient.defaultQueryOptions(options)
// Make sure the results are already in fetching state before subscribing or updating options
defaultedOptions._optimisticResults = isRestoring
? 'isRestoring'
: 'optimistic'
return defaultedOptions
}),
[queries, queryClient, isRestoring],
)
defaultedQueries.forEach((query) => {
ensureStaleTime(query)
ensurePreventErrorBoundaryRetry(query, errorResetBoundary)
})
useClearResetErrorBoundary(errorResetBoundary)
const [observer] = React.useState(
() => new QueriesObserver(queryClient, defaultedQueries),
)
const optimisticResult = observer.getOptimisticResult(defaultedQueries)
useSyncExternalStore(
React.useCallback(
(onStoreChange) =>
isRestoring
? () => undefined
: observer.subscribe(notifyManager.batchCalls(onStoreChange)),
[observer, isRestoring],
),
() => observer.getCurrentResult(),
() => observer.getCurrentResult(),
)
React.useEffect(() => {
// Do not notify on updates because of changes in the options because
// these changes should already be reflected in the optimistic result.
observer.setQueries(defaultedQueries, { listeners: false })
}, [defaultedQueries, observer])
const shouldAtLeastOneSuspend = optimisticResult.some((result, index) =>
shouldSuspend(defaultedQueries[index], result, isRestoring),
)
const suspensePromises = shouldAtLeastOneSuspend
? optimisticResult.flatMap((result, index) => {
const options = defaultedQueries[index]
const queryObserver = observer.getObservers()[index]
if (options && queryObserver) {
if (shouldSuspend(options, result, isRestoring)) {
return fetchOptimistic(options, queryObserver, errorResetBoundary)
} else if (willFetch(result, isRestoring)) {
void fetchOptimistic(options, queryObserver, errorResetBoundary)
}
}
return []
})
: []
if (suspensePromises.length > 0) {
throw Promise.all(suspensePromises)
}
const observerQueries = observer.getQueries()
const firstSingleResultWhichShouldThrow = optimisticResult.find(
(result, index) =>
getHasError({
result,
errorResetBoundary,
useErrorBoundary: defaultedQueries[index]?.useErrorBoundary ?? false,
query: observerQueries[index]!,
}),
)
if (firstSingleResultWhichShouldThrow?.error) {
throw firstSingleResultWhichShouldThrow.error
}
return optimisticResult as QueriesResults<T>
}