forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfiniteQueryBehavior.ts
171 lines (148 loc) · 4.93 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
import type { QueryBehavior } from './query'
import { isCancelable } from './retryer'
import type { InfiniteData, QueryFunctionContext, QueryOptions } from './types'
export function infiniteQueryBehavior<
TQueryFnData,
TError,
TData
>(): QueryBehavior<TQueryFnData, TError, InfiniteData<TData>> {
return {
onFetch: context => {
context.fetchFn = () => {
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 || []
let newPageParams = oldPageParams
// Get query function
const queryFn =
context.options.queryFn || (() => Promise.reject('Missing queryFn'))
// Create function to fetch a page
const fetchPage = (
pages: unknown[],
manual?: boolean,
param?: unknown,
previous?: boolean
): Promise<unknown[]> => {
if (typeof param === 'undefined' && !manual && pages.length) {
return Promise.resolve(pages)
}
const queryFnContext: QueryFunctionContext = {
queryKey: context.queryKey,
pageParam: param,
}
let cancelFn: undefined | (() => any)
const queryFnResult = queryFn(queryFnContext)
if ((queryFnResult as any).cancel) {
cancelFn = (queryFnResult as any).cancel
}
const promise = Promise.resolve(queryFnResult).then(page => {
newPageParams = previous
? [param, ...newPageParams]
: [...newPageParams, param]
return previous ? [page, ...pages] : [...pages, page]
})
if (cancelFn) {
const promiseAsAny = promise as any
promiseAsAny.cancel = cancelFn
}
return promise
}
let promise
// 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'
// Fetch first page
promise = fetchPage([], manual, oldPageParams[0])
// Fetch remaining pages
for (let i = 1; i < oldPages.length; i++) {
promise = promise.then(pages => {
const param = manual
? oldPageParams[i]
: getNextPageParam(context.options, pages)
return fetchPage(pages, manual, param)
})
}
}
const finalPromise = promise.then(pages => ({
pages,
pageParams: newPageParams,
}))
if (isCancelable(promise)) {
const finalPromiseAsAny = finalPromise as any
finalPromiseAsAny.cancel = promise.cancel
}
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
)
}
}