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