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 result = await fetchPage(result, param)
107 }
108 }
109β
110 return result
111 }
112 if (context.options.persister) {
113 context.fetchFn = () => {
114 return context.options.persister?.(
115 fetchFn as any,
116 {
117 queryKey: context.queryKey,
118 meta: context.options.meta,
119 signal: context.signal,
120 },
121 query,
122 )
123 }
124 } else {
125 context.fetchFn = fetchFn
126 }
127 },
128 }
129}
130β
131function getNextPageParam(
132 options: InfiniteQueryPageParamsOptions<any>,
133 { pages, pageParams }: InfiniteData<unknown>,
134): unknown | undefined {
135 const lastIndex = pages.length - 1
136 return pages.length > 0
137 ? options.getNextPageParam(
138 pages[lastIndex],
139 pages,
140 pageParams[lastIndex],
141 pageParams,
142 )
143 : undefined
144}
145β
146function getPreviousPageParam(
147 options: InfiniteQueryPageParamsOptions<any>,
148 { pages, pageParams }: InfiniteData<unknown>,
149): unknown | undefined {
150 return pages.length > 0
151 ? options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams)
152 : undefined
153}
154β
155/**
156 * Checks if there is a next page.
157 */
158export function hasNextPage(
159 options: InfiniteQueryPageParamsOptions<any, any>,
160 data?: InfiniteData<unknown>,
161): boolean {
162 if (!data) return false
163 return getNextPageParam(options, data) != null
164}
165β
166/**
167 * Checks if there is a previous page.
168 */
169export function hasPreviousPage(
170 options: InfiniteQueryPageParamsOptions<any, any>,
171 data?: InfiniteData<unknown>,
172): boolean {
173 if (!data || !options.getPreviousPageParam) return false
174 return getPreviousPageParam(options, data) != null
175}