Open History to see the last commit
1import type { QueryBehavior } from './query'
2import { addToEnd, addToStart } from './utils'
3import type {
4 InfiniteData,
5 InfiniteQueryPageParamsOptions,
6 QueryFunctionContext,
7 QueryKey,
8} from './types'
9β
10export function infiniteQueryBehavior<
11 TQueryFnData,
12 TError,
13 TData,
14>(): QueryBehavior<TQueryFnData, TError, InfiniteData<TData>> {
15 return {
16 onFetch: (context) => {
17 context.fetchFn = async () => {
18 const options = context.options as InfiniteQueryPageParamsOptions<TData>
19 const direction = context.fetchOptions?.meta?.fetchMore?.direction
20 const oldPages = context.state.data?.pages || []
21 const oldPageParams = context.state.data?.pageParams || []
22 const empty = { pages: [], pageParams: [] }
23 let cancelled = false
24β
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 // Get query function
42 const queryFn =
43 context.options.queryFn ||
44 (() => Promise.reject(new Error('Missing queryFn')))
45β
46 // Create function to fetch a page
47 const fetchPage = async (
48 data: InfiniteData<unknown>,
49 param: unknown,
50 previous?: boolean,
51 ): Promise<InfiniteData<unknown>> => {
52 if (cancelled) {
53 return Promise.reject()
54 }
55β
56 if (typeof param === 'undefined' && data.pages.length) {
57 return Promise.resolve(data)
58 }
59β
60 const queryFnContext: Omit<
61 QueryFunctionContext<QueryKey, unknown>,
62 'signal'
63 > = {
64 queryKey: context.queryKey,
65 pageParam: param,
66 direction: previous ? 'backward' : 'forward',
67 meta: context.options.meta,
68 }
69β
70 addSignalProperty(queryFnContext)
71β
72 const page = await queryFn(
73 queryFnContext as QueryFunctionContext<QueryKey, unknown>,
74 )
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 let result: InfiniteData<unknown>
86β
87 // Fetch first page?
88 if (!oldPages.length) {
89 result = await fetchPage(empty, options.defaultPageParam)
90 }
91β
92 // fetch next / previous page?
93 else if (direction) {
94 const previous = direction === 'backward'
95 const pageParamFn = previous ? getPreviousPageParam : getNextPageParam
96 const oldData = {
97 pages: oldPages,
98 pageParams: oldPageParams,
99 }
100 const param = pageParamFn(options, oldData)
101β
102 result = await fetchPage(oldData, param, previous)
103 }
104β
105 // Refetch pages
106 else {
107 // Fetch first page
108 result = await fetchPage(empty, oldPageParams[0])
109β
110 // Fetch remaining pages
111 for (let i = 1; i < oldPages.length; i++) {
112 const param = getNextPageParam(options, result)
113 result = await fetchPage(result, param)
114 }
115 }
116β
117 return result
118 }
119 },
120 }
121}
122β
123function getNextPageParam(
124 options: InfiniteQueryPageParamsOptions<any>,
125 { pages, pageParams }: InfiniteData<unknown>,
126): unknown | undefined {
127 const lastIndex = pages.length - 1
128 return options.getNextPageParam(
129 pages[lastIndex],
130 pages,
131 pageParams[lastIndex],
132 pageParams,
133 )
134}
135β
136function getPreviousPageParam(
137 options: InfiniteQueryPageParamsOptions<any>,
138 { pages, pageParams }: InfiniteData<unknown>,
139): unknown | undefined {
140 return options.getPreviousPageParam?.(
141 pages[0],
142 pages,
143 pageParams[0],
144 pageParams,
145 )
146}
147β
148/**
149 * Checks if there is a next page.
150 */
151export function hasNextPage(
152 options: InfiniteQueryPageParamsOptions<any, any>,
153 data?: InfiniteData<unknown>,
154): boolean {
155 if (!data) return false
156 return typeof getNextPageParam(options, data) !== 'undefined'
157}
158β
159/**
160 * Checks if there is a previous page.
161 */
162export function hasPreviousPage(
163 options: InfiniteQueryPageParamsOptions<any, any>,
164 data?: InfiniteData<unknown>,
165): boolean {
166 if (!data || !options.getPreviousPageParam) return false
167 return typeof getPreviousPageParam(options, data) !== 'undefined'
168}