Open History to see the last commit
1import { addToEnd, addToStart, ensureQueryFn } from './utils'
2import type { QueryBehavior } from './query'
3import type {
4 InfiniteData,
5 InfiniteQueryPageParamsOptions,
6 OmitKeyof,
7 QueryFunctionContext,
8 QueryKey,
9} from './types'
10β
11export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(
12 pages?: number,
13): QueryBehavior<TQueryFnData, TError, InfiniteData<TData, TPageParam>> {
14 return {
15 onFetch: (context, query) => {
16 const options = context.options as InfiniteQueryPageParamsOptions<TData>
17 const direction = context.fetchOptions?.meta?.fetchMore?.direction
18 const oldPages = context.state.data?.pages || []
19 const oldPageParams = context.state.data?.pageParams || []
20 let result: InfiniteData<unknown> = { pages: [], pageParams: [] }
21 let currentPage = 0
22β
23 const fetchFn = async () => {
24 let cancelled = false
25 const addSignalProperty = (object: unknown) => {
26 Object.defineProperty(object, 'signal', {
27 enumerable: true,
28 get: () => {
29 if (context.signal.aborted) {
30 cancelled = true
31 } else {
32 context.signal.addEventListener('abort', () => {
33 cancelled = true
34 })
35 }
36 return context.signal
37 },
38 })
39 }
40β
41 const queryFn = ensureQueryFn(context.options, context.fetchOptions)
42β
43 // Create function to fetch a page
44 const fetchPage = async (
45 data: InfiniteData<unknown>,
46 param: unknown,
47 previous?: boolean,
48 ): Promise<InfiniteData<unknown>> => {
49 if (cancelled) {
50 return Promise.reject()
51 }
52β
53 if (param == null && data.pages.length) {
54 return Promise.resolve(data)
55 }
56β
57 const createQueryFnContext = () => {
58 const queryFnContext: OmitKeyof<
59 QueryFunctionContext<QueryKey, unknown>,
60 'signal'
61 > = {
62 client: context.client,
63 queryKey: context.queryKey,
64 pageParam: param,
65 direction: previous ? 'backward' : 'forward',
66 meta: context.options.meta,
67 }
68 addSignalProperty(queryFnContext)
69 return queryFnContext as QueryFunctionContext<QueryKey, unknown>
70 }
71β
72 const queryFnContext = createQueryFnContext()
73β
74 const page = await queryFn(queryFnContext)
75β
76 const { maxPages } = context.options
77 const addTo = previous ? addToStart : addToEnd
78β
79 return {
80 pages: addTo(data.pages, page, maxPages),
81 pageParams: addTo(data.pageParams, param, maxPages),
82 }
83 }
84β
85 // fetch next / previous page?
86 if (direction && oldPages.length) {
87 const previous = direction === 'backward'
88 const pageParamFn = previous ? getPreviousPageParam : getNextPageParam
89 const oldData = {
90 pages: oldPages,
91 pageParams: oldPageParams,
92 }
93 const param = pageParamFn(options, oldData)
94β
95 result = await fetchPage(oldData, param, previous)
96 } else {
97 const remainingPages = pages ?? oldPages.length
98β
99 // Fetch all pages
100 do {
101 const param =
102 currentPage === 0
103 ? (oldPageParams[0] ?? options.initialPageParam)
104 : getNextPageParam(options, result)
105 if (currentPage > 0 && param == null) {
106 break
107 }
108 result = await fetchPage(result, param)
109 currentPage++
110 } while (currentPage < remainingPages)
111 }
112β
113 return result
114 }
115 if (context.options.persister) {
116 context.fetchFn = () => {
117 return context.options.persister?.(
118 fetchFn as any,
119 {
120 client: context.client,
121 queryKey: context.queryKey,
122 meta: context.options.meta,
123 signal: context.signal,
124 },
125 query,
126 )
127 }
128 } else {
129 context.fetchFn = fetchFn
130 }
131 },
132 }
133}
134β
135function getNextPageParam(
136 options: InfiniteQueryPageParamsOptions<any>,
137 { pages, pageParams }: InfiniteData<unknown>,
138): unknown | undefined {
139 const lastIndex = pages.length - 1
140 return pages.length > 0
141 ? options.getNextPageParam(
142 pages[lastIndex],
143 pages,
144 pageParams[lastIndex],
145 pageParams,
146 )
147 : undefined
148}
149β
150function getPreviousPageParam(
151 options: InfiniteQueryPageParamsOptions<any>,
152 { pages, pageParams }: InfiniteData<unknown>,
153): unknown | undefined {
154 return pages.length > 0
155 ? options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams)
156 : undefined
157}
158β
159/**
160 * Checks if there is a next page.
161 */
162export function hasNextPage(
163 options: InfiniteQueryPageParamsOptions<any, any>,
164 data?: InfiniteData<unknown>,
165): boolean {
166 if (!data) return false
167 return getNextPageParam(options, data) != null
168}
169β
170/**
171 * Checks if there is a previous page.
172 */
173export function hasPreviousPage(
174 options: InfiniteQueryPageParamsOptions<any, any>,
175 data?: InfiniteData<unknown>,
176): boolean {
177 if (!data || !options.getPreviousPageParam) return false
178 return getPreviousPageParam(options, data) != null
179}