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 queryFnContext: OmitKeyof<
58 QueryFunctionContext<QueryKey, unknown>,
59 'signal'
60 > = {
61 client: context.client,
62 queryKey: context.queryKey,
63 pageParam: param,
64 direction: previous ? 'backward' : 'forward',
65 meta: context.options.meta,
66 }
67β
68 addSignalProperty(queryFnContext)
69β
70 const page = await queryFn(
71 queryFnContext as QueryFunctionContext<QueryKey, unknown>,
72 )
73β
74 const { maxPages } = context.options
75 const addTo = previous ? addToStart : addToEnd
76β
77 return {
78 pages: addTo(data.pages, page, maxPages),
79 pageParams: addTo(data.pageParams, param, maxPages),
80 }
81 }
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 const remainingPages = pages ?? oldPages.length
96β
97 // Fetch all pages
98 do {
99 const param =
100 currentPage === 0
101 ? (oldPageParams[0] ?? options.initialPageParam)
102 : getNextPageParam(options, result)
103 if (currentPage > 0 && param == null) {
104 break
105 }
106 result = await fetchPage(result, param)
107 currentPage++
108 } while (currentPage < remainingPages)
109 }
110β
111 return result
112 }
113 if (context.options.persister) {
114 context.fetchFn = () => {
115 return context.options.persister?.(
116 fetchFn as any,
117 {
118 client: context.client,
119 queryKey: context.queryKey,
120 meta: context.options.meta,
121 signal: context.signal,
122 },
123 query,
124 )
125 }
126 } else {
127 context.fetchFn = fetchFn
128 }
129 },
130 }
131}
132β
133function getNextPageParam(
134 options: InfiniteQueryPageParamsOptions<any>,
135 { pages, pageParams }: InfiniteData<unknown>,
136): unknown | undefined {
137 const lastIndex = pages.length - 1
138 return pages.length > 0
139 ? options.getNextPageParam(
140 pages[lastIndex],
141 pages,
142 pageParams[lastIndex],
143 pageParams,
144 )
145 : undefined
146}
147β
148function getPreviousPageParam(
149 options: InfiniteQueryPageParamsOptions<any>,
150 { pages, pageParams }: InfiniteData<unknown>,
151): unknown | undefined {
152 return pages.length > 0
153 ? options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams)
154 : undefined
155}
156β
157/**
158 * Checks if there is a next page.
159 */
160export function hasNextPage(
161 options: InfiniteQueryPageParamsOptions<any, any>,
162 data?: InfiniteData<unknown>,
163): boolean {
164 if (!data) return false
165 return getNextPageParam(options, data) != null
166}
167β
168/**
169 * Checks if there is a previous page.
170 */
171export function hasPreviousPage(
172 options: InfiniteQueryPageParamsOptions<any, any>,
173 data?: InfiniteData<unknown>,
174): boolean {
175 if (!data || !options.getPreviousPageParam) return false
176 return getPreviousPageParam(options, data) != null
177}