Skip to content

Commit a89883b

Browse files
committed
Add basic maxPages support
1 parent e634241 commit a89883b

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

packages/toolkit/src/query/core/apiState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export type GetPreviousPageParamFunction<TPageParam, TQueryFnData> = (
4646

4747
export type InfiniteQueryConfigOptions<TQueryFnData, TPageParam> = {
4848
initialPageParam: TPageParam
49+
maxPages?: number
4950
/**
5051
* This function can be set to automatically get the previous cursor for infinite queries.
5152
* The result will also be used to determine the value of `hasPreviousPage`.

packages/toolkit/src/query/core/buildThunks.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ export function buildThunks<
431431
const fetchPage = async (
432432
data: InfiniteData<unknown, unknown>,
433433
param: unknown,
434+
maxPages: number,
434435
previous?: boolean,
435436
): Promise<QueryReturnValue> => {
436437
// This should handle cases where there is no `getPrevPageParam`,
@@ -442,7 +443,6 @@ export function buildThunks<
442443
const pageResponse = await executeRequest(param)
443444

444445
// TODO Get maxPages from endpoint config
445-
const maxPages = 20
446446
const addTo = previous ? addToStart : addToEnd
447447

448448
return {
@@ -535,6 +535,10 @@ export function buildThunks<
535535
'infiniteQueryOptions' in endpointDefinition
536536
) {
537537
// This is an infinite query endpoint
538+
const { infiniteQueryOptions } = endpointDefinition
539+
540+
// Runtime checks should guarantee this is a positive number if provided
541+
const { maxPages = Infinity } = infiniteQueryOptions
538542

539543
let result: QueryReturnValue
540544

@@ -551,24 +555,20 @@ export function buildThunks<
551555
if ('direction' in arg && arg.direction && existingData.pages.length) {
552556
const previous = arg.direction === 'backward'
553557
const pageParamFn = previous ? getPreviousPageParam : getNextPageParam
554-
const param = pageParamFn(
555-
endpointDefinition.infiniteQueryOptions,
556-
existingData,
557-
)
558+
const param = pageParamFn(infiniteQueryOptions, existingData)
558559

559-
result = await fetchPage(existingData, param, previous)
560+
result = await fetchPage(existingData, param, maxPages, previous)
560561
} else {
561562
// Otherwise, fetch the first page and then any remaining pages
562563

563-
const {
564-
initialPageParam = endpointDefinition.infiniteQueryOptions
565-
.initialPageParam,
566-
} = arg as InfiniteQueryThunkArg<any>
564+
const { initialPageParam = infiniteQueryOptions.initialPageParam } =
565+
arg as InfiniteQueryThunkArg<any>
567566

568567
// Fetch first page
569568
result = await fetchPage(
570569
existingData,
571570
existingData.pageParams[0] ?? initialPageParam,
571+
maxPages,
572572
)
573573

574574
//original
@@ -587,6 +587,7 @@ export function buildThunks<
587587
result = await fetchPage(
588588
result.data as InfiniteData<unknown, unknown>,
589589
param,
590+
maxPages,
590591
)
591592
}
592593
}

0 commit comments

Comments
 (0)