( pages?: number, )
| 14 | } from './types' |
| 15 | |
| 16 | export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>( |
| 17 | pages?: number, |
| 18 | ): QueryBehavior<TQueryFnData, TError, InfiniteData<TData, TPageParam>> { |
| 19 | return { |
| 20 | onFetch: (context, query) => { |
| 21 | const options = context.options as InfiniteQueryPageParamsOptions<TData> |
| 22 | const direction = context.fetchOptions?.meta?.fetchMore?.direction |
| 23 | const oldPages = context.state.data?.pages || [] |
| 24 | const oldPageParams = context.state.data?.pageParams || [] |
| 25 | let result: InfiniteData<unknown> = { pages: [], pageParams: [] } |
| 26 | let currentPage = 0 |
| 27 | |
| 28 | const fetchFn = async () => { |
| 29 | let cancelled = false |
| 30 | const addSignalProperty = (object: unknown) => { |
| 31 | addConsumeAwareSignal( |
| 32 | object, |
| 33 | () => context.signal, |
| 34 | () => (cancelled = true), |
| 35 | ) |
| 36 | } |
| 37 | |
| 38 | const queryFn = ensureQueryFn(context.options, context.fetchOptions) |
| 39 | |
| 40 | // Create function to fetch a page |
| 41 | const fetchPage = async ( |
| 42 | data: InfiniteData<unknown>, |
| 43 | param: unknown, |
| 44 | previous?: boolean, |
| 45 | ): Promise<InfiniteData<unknown>> => { |
| 46 | if (cancelled) { |
| 47 | return Promise.reject(context.signal.reason) |
| 48 | } |
| 49 | |
| 50 | if (param == null && data.pages.length) { |
| 51 | return Promise.resolve(data) |
| 52 | } |
| 53 | |
| 54 | const createQueryFnContext = () => { |
| 55 | const queryFnContext: OmitKeyof< |
| 56 | QueryFunctionContext<QueryKey, unknown>, |
| 57 | 'signal' |
| 58 | > = { |
| 59 | client: context.client, |
| 60 | queryKey: context.queryKey, |
| 61 | pageParam: param, |
| 62 | direction: previous ? 'backward' : 'forward', |
| 63 | meta: context.options.meta, |
| 64 | } |
| 65 | addSignalProperty(queryFnContext) |
| 66 | return queryFnContext as QueryFunctionContext<QueryKey, unknown> |
| 67 | } |
| 68 | |
| 69 | const queryFnContext = createQueryFnContext() |
| 70 | |
| 71 | const page = await queryFn(queryFnContext) |
| 72 | |
| 73 | const { maxPages } = context.options |
no outgoing calls
no test coverage detected