Skip to content

Commit 672c319

Browse files
kuishou68TkDodo
andauthored
fix(query-core): propagate AbortSignal reason in infiniteQueryBehavior fetchPage cancellation (TanStack#10476)
* fix: propagate AbortSignal reason when cancelling infinite query fetchPage (Closes TanStack#10475) Signed-off-by: Cocoon-Break <54054995+kuishou68@users.noreply.github.com> * add a test --------- Signed-off-by: Cocoon-Break <54054995+kuishou68@users.noreply.github.com> Co-authored-by: Dominik Dorfmeister 🔮 <office@dorfmeister.cc>
1 parent 55afb3e commit 672c319

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

packages/query-core/src/__tests__/infiniteQueryBehavior.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22
import { queryKey, sleep } from '@tanstack/query-test-utils'
33
import { CancelledError, InfiniteQueryObserver, QueryClient } from '..'
4+
import { infiniteQueryBehavior } from '../infiniteQueryBehavior'
45
import type { InfiniteData, InfiniteQueryObserverResult, QueryCache } from '..'
56

67
describe('InfiniteQueryBehavior', () => {
@@ -332,6 +333,46 @@ describe('InfiniteQueryBehavior', () => {
332333
unsubscribe()
333334
})
334335

336+
it('should surface the abort reason when cancellation happens between refetched pages', async () => {
337+
const key = queryKey()
338+
const abortController = new AbortController()
339+
const queryFn = vi.fn().mockImplementation(({ pageParam, signal }) => {
340+
void signal.aborted
341+
342+
if (pageParam === 1) {
343+
abortController.abort()
344+
}
345+
346+
return pageParam
347+
})
348+
349+
const behavior = infiniteQueryBehavior<number, Error, number, number>()
350+
const context = {
351+
client: queryClient,
352+
queryKey: key,
353+
fetchOptions: undefined,
354+
options: {
355+
queryKey: key,
356+
queryFn,
357+
initialPageParam: 1,
358+
getNextPageParam: (lastPage: number) => lastPage + 1,
359+
},
360+
state: {
361+
data: {
362+
pages: [1, 2],
363+
pageParams: [1, 2],
364+
},
365+
},
366+
fetchFn: () => Promise.resolve(),
367+
signal: abortController.signal,
368+
}
369+
370+
behavior.onFetch(context as any, {} as any)
371+
372+
await expect(context.fetchFn()).rejects.toBe(abortController.signal.reason)
373+
expect(queryFn).toHaveBeenCalledTimes(1)
374+
})
375+
335376
it('should not enter an infinite loop when a page errors while retry is on #8046', async () => {
336377
let errorCount = 0
337378
const key = queryKey()

packages/query-core/src/infiniteQueryBehavior.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(
4444
previous?: boolean,
4545
): Promise<InfiniteData<unknown>> => {
4646
if (cancelled) {
47-
return Promise.reject()
47+
return Promise.reject(context.signal.reason)
4848
}
4949

5050
if (param == null && data.pages.length) {

0 commit comments

Comments
 (0)