-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
infiniteQueryBehavior.ts
205 lines (178 loc) · 5.89 KB
/
infiniteQueryBehavior.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
import type { QueryBehavior } from './query'
import type {
InfiniteData,
QueryFunctionContext,
QueryOptions,
RefetchQueryFilters,
} from './types'
import { getAbortController } from './utils'
export function infiniteQueryBehavior<
TQueryFnData,
TError,
TData
>(): QueryBehavior<TQueryFnData, TError, InfiniteData<TData>> {
return {
onFetch: context => {
context.fetchFn = () => {
const refetchPage: RefetchQueryFilters['refetchPage'] | undefined =
context.fetchOptions?.meta?.refetchPage
const fetchMore = context.fetchOptions?.meta?.fetchMore
const pageParam = fetchMore?.pageParam
const isFetchingNextPage = fetchMore?.direction === 'forward'
const isFetchingPreviousPage = fetchMore?.direction === 'backward'
const oldPages = context.state.data?.pages || []
const oldPageParams = context.state.data?.pageParams || []
const abortController = getAbortController()
const abortSignal = abortController?.signal
let newPageParams = oldPageParams
let cancelled = false
// Get query function
const queryFn =
context.options.queryFn || (() => Promise.reject('Missing queryFn'))
const buildNewPages = (
pages: unknown[],
param: unknown,
page: unknown,
previous?: boolean
) => {
newPageParams = previous
? [param, ...newPageParams]
: [...newPageParams, param]
return previous ? [page, ...pages] : [...pages, page]
}
// Create function to fetch a page
const fetchPage = (
pages: unknown[],
manual?: boolean,
param?: unknown,
previous?: boolean
): Promise<unknown[]> => {
if (cancelled) {
return Promise.reject('Cancelled')
}
if (typeof param === 'undefined' && !manual && pages.length) {
return Promise.resolve(pages)
}
const queryFnContext: QueryFunctionContext = {
queryKey: context.queryKey,
signal: abortSignal,
pageParam: param,
meta: context.meta,
}
const queryFnResult = queryFn(queryFnContext)
const promise = Promise.resolve(queryFnResult).then(page =>
buildNewPages(pages, param, page, previous)
)
return promise
}
let promise: Promise<unknown[]>
// Fetch first page?
if (!oldPages.length) {
promise = fetchPage([])
}
// Fetch next page?
else if (isFetchingNextPage) {
const manual = typeof pageParam !== 'undefined'
const param = manual
? pageParam
: getNextPageParam(context.options, oldPages)
promise = fetchPage(oldPages, manual, param)
}
// Fetch previous page?
else if (isFetchingPreviousPage) {
const manual = typeof pageParam !== 'undefined'
const param = manual
? pageParam
: getPreviousPageParam(context.options, oldPages)
promise = fetchPage(oldPages, manual, param, true)
}
// Refetch pages
else {
newPageParams = []
const manual = typeof context.options.getNextPageParam === 'undefined'
const shouldFetchFirstPage =
refetchPage && oldPages[0]
? refetchPage(oldPages[0], 0, oldPages)
: true
// Fetch first page
promise = shouldFetchFirstPage
? fetchPage([], manual, oldPageParams[0])
: Promise.resolve(buildNewPages([], oldPageParams[0], oldPages[0]))
// Fetch remaining pages
for (let i = 1; i < oldPages.length; i++) {
promise = promise.then(pages => {
const shouldFetchNextPage =
refetchPage && oldPages[i]
? refetchPage(oldPages[i], i, oldPages)
: true
if (shouldFetchNextPage) {
const param = manual
? oldPageParams[i]
: getNextPageParam(context.options, pages)
return fetchPage(pages, manual, param)
}
return Promise.resolve(
buildNewPages(pages, oldPageParams[i], oldPages[i])
)
})
}
}
const finalPromise = promise.then(pages => ({
pages,
pageParams: newPageParams,
}))
context.signal?.addEventListener('abort', () => {
cancelled = true
abortController?.abort()
})
return finalPromise
}
},
}
}
export function getNextPageParam(
options: QueryOptions<any, any>,
pages: unknown[]
): unknown | undefined {
return options.getNextPageParam?.(pages[pages.length - 1], pages)
}
export function getPreviousPageParam(
options: QueryOptions<any, any>,
pages: unknown[]
): unknown | undefined {
return options.getPreviousPageParam?.(pages[0], pages)
}
/**
* Checks if there is a next page.
* Returns `undefined` if it cannot be determined.
*/
export function hasNextPage(
options: QueryOptions<any, any>,
pages?: unknown
): boolean | undefined {
if (options.getNextPageParam && Array.isArray(pages)) {
const nextPageParam = getNextPageParam(options, pages)
return (
typeof nextPageParam !== 'undefined' &&
nextPageParam !== null &&
nextPageParam !== false
)
}
}
/**
* Checks if there is a previous page.
* Returns `undefined` if it cannot be determined.
*/
export function hasPreviousPage(
options: QueryOptions<any, any>,
pages?: unknown
): boolean | undefined {
if (options.getPreviousPageParam && Array.isArray(pages)) {
const previousPageParam = getPreviousPageParam(options, pages)
return (
typeof previousPageParam !== 'undefined' &&
previousPageParam !== null &&
previousPageParam !== false
)
}
}