-
-
Notifications
You must be signed in to change notification settings - Fork 567
/
atomWithInfiniteQuery.ts
222 lines (208 loc) 路 6.11 KB
/
atomWithInfiniteQuery.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
import { InfiniteQueryObserver, isCancelledError } from 'react-query'
import type {
InfiniteData,
InfiniteQueryObserverOptions,
InitialDataFunction,
QueryKey,
QueryObserverResult,
RefetchOptions,
RefetchQueryFilters,
} from 'react-query'
import { atom } from 'jotai'
import type { WritableAtom } from 'jotai'
import { queryClientAtom } from './queryClientAtom'
import { CreateQueryOptions, GetQueryClient } from './types'
export type AtomWithInfiniteQueryAction<TQueryFnData> =
| ({ type: 'refetch' } & Partial<
RefetchOptions & RefetchQueryFilters<TQueryFnData>
>)
| { type: 'fetchNextPage' }
| { type: 'fetchPreviousPage' }
export type AtomWithInfiniteQueryOptions<
TQueryFnData,
TError,
TData,
TQueryData
> = InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryData> & {
queryKey: QueryKey
}
export type AtomWithInfiniteQueryOptionsWithEnabled<
TQueryFnData,
TError,
TData,
TQueryData
> = Omit<
AtomWithInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryData>,
'enabled'
> & {
enabled: boolean
}
export function atomWithInfiniteQuery<
TQueryFnData,
TError,
TData = TQueryFnData,
TQueryData = TQueryFnData
>(
createQuery: CreateQueryOptions<
AtomWithInfiniteQueryOptionsWithEnabled<
TQueryFnData,
TError,
TData,
TQueryData
>
>,
getQueryClient?: GetQueryClient
): WritableAtom<
InfiniteData<TData | TQueryData> | undefined,
AtomWithInfiniteQueryAction<TQueryFnData>
>
export function atomWithInfiniteQuery<
TQueryFnData,
TError,
TData = TQueryFnData,
TQueryData = TQueryFnData
>(
createQuery: CreateQueryOptions<
AtomWithInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryData>
>,
getQueryClient?: GetQueryClient
): WritableAtom<
InfiniteData<TData | TQueryData>,
AtomWithInfiniteQueryAction<TQueryFnData>
>
export function atomWithInfiniteQuery<
TQueryFnData,
TError,
TData = TQueryFnData,
TQueryData = TQueryFnData
>(
createQuery: CreateQueryOptions<
AtomWithInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryData>
>,
getQueryClient: GetQueryClient = (get) => get(queryClientAtom)
): WritableAtom<
InfiniteData<TData | TQueryData> | undefined,
AtomWithInfiniteQueryAction<TQueryFnData>
> {
const queryDataAtom = atom(
(get) => {
const queryClient = getQueryClient(get)
const options =
typeof createQuery === 'function' ? createQuery(get) : createQuery
let settlePromise:
| ((data: InfiniteData<TData> | undefined, err?: TError) => void)
| null = null
const getInitialData = () => {
let data: InfiniteData<TQueryData | TData> | undefined =
queryClient.getQueryData<InfiniteData<TData>>(options.queryKey)
if (data === undefined && options.initialData) {
data =
typeof options.initialData === 'function'
? (
options.initialData as InitialDataFunction<
InfiniteData<TQueryData>
>
)()
: options.initialData
}
return data
}
const initialData = getInitialData()
const dataAtom = atom<
| InfiniteData<TData | TQueryData>
| Promise<InfiniteData<TData | TQueryData>>
| undefined
>(
initialData === undefined && options.enabled !== false
? new Promise<InfiniteData<TData>>((resolve, reject) => {
settlePromise = (data, err) => {
if (err) {
reject(err)
} else {
resolve(data as InfiniteData<TData>)
}
}
})
: initialData
)
let setData: (
data: InfiniteData<TData> | Promise<InfiniteData<TData>> | undefined
) => void = () => {
throw new Error('atomWithInfiniteQuery: setting data without mount')
}
const listener = (
result:
| QueryObserverResult<InfiniteData<TData>, TError>
| { data?: undefined; error: TError }
) => {
if (result.error && !isCancelledError(result.error)) {
if (settlePromise) {
settlePromise(undefined, result.error)
settlePromise = null
} else {
setData(Promise.reject<InfiniteData<TData>>(result.error))
}
return
}
if (result.data === undefined) {
return
}
if (settlePromise) {
settlePromise(result.data)
settlePromise = null
} else {
setData(result.data)
}
}
const defaultedOptions = queryClient.defaultQueryObserverOptions(options)
if (initialData === undefined && options.enabled !== false) {
if (typeof defaultedOptions.staleTime !== 'number') {
defaultedOptions.staleTime = 1000
}
}
const observer = new InfiniteQueryObserver(queryClient, defaultedOptions)
if (initialData === undefined && options.enabled !== false) {
observer
.fetchOptimistic(defaultedOptions)
.then(listener)
.catch((error) => listener({ error }))
}
dataAtom.onMount = (update) => {
setData = update
if (options.enabled !== false) {
return observer.subscribe(listener)
}
}
return { dataAtom, observer, options }
},
(get, _set, action: AtomWithInfiniteQueryAction<TQueryFnData>) => {
const { observer } = get(queryDataAtom)
switch (action.type) {
case 'refetch': {
const { type: _type, ...options } = action
void observer.refetch(options)
break
}
case 'fetchPreviousPage': {
void observer.fetchPreviousPage()
break
}
case 'fetchNextPage': {
void observer.fetchNextPage()
break
}
}
}
)
const queryAtom = atom<
InfiniteData<TData | TQueryData> | undefined,
AtomWithInfiniteQueryAction<TQueryFnData>
>(
(get) => {
const { dataAtom } = get(queryDataAtom)
return get(dataAtom)
},
(_get, set, action) => set(queryDataAtom, action) // delegate action
)
return queryAtom
}