Skip to content

Commit 118b914

Browse files
committed
fix: made queryFn of streamedQuery not consume signal directly (so that only streamFn can consume it)
1 parent b23c4f4 commit 118b914

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ describe('streamedQuery', () => {
456456

457457
expect(queryClient.getQueryState(key)).toMatchObject({
458458
status: 'success',
459-
fetchStatus: 'idle',
459+
fetchStatus: 'fetching',
460460
data: [0, 1],
461461
})
462462
})

packages/query-core/src/infiniteQueryBehavior.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(
3030
const addSignalProperty = (object: unknown) => {
3131
addConsumeAwareSignal(
3232
object,
33-
context.signal,
33+
() => context.signal,
3434
() => (cancelled = true),
3535
)
3636
}

packages/query-core/src/streamedQuery.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { addConsumeAwareSignal, addToEnd } from './utils'
2-
import type { QueryFunction, QueryFunctionContext, QueryKey } from './types'
2+
import type {
3+
OmitKeyof,
4+
QueryFunction,
5+
QueryFunctionContext,
6+
QueryKey,
7+
} from './types'
38

49
type BaseStreamedQueryParams<TQueryFnData, TQueryKey extends QueryKey> = {
510
streamFn: (
@@ -74,10 +79,17 @@ export function streamedQuery<
7479
let result = initialValue
7580

7681
let cancelled = false
77-
const streamFnContext = { ...context }
78-
addConsumeAwareSignal(
79-
streamFnContext,
80-
context.signal,
82+
const streamFnContext = addConsumeAwareSignal<
83+
OmitKeyof<typeof context, 'signal'>
84+
>(
85+
{
86+
client: context.client,
87+
meta: context.meta,
88+
queryKey: context.queryKey,
89+
pageParam: context.pageParam,
90+
direction: context.direction,
91+
},
92+
() => context.signal,
8193
() => (cancelled = true),
8294
)
8395

packages/query-core/src/utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,15 @@ export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
466466
return !!throwOnError
467467
}
468468

469-
export function addConsumeAwareSignal(
470-
object: unknown,
471-
signal: AbortSignal,
469+
export function addConsumeAwareSignal<T>(
470+
object: T,
471+
getSignal: () => AbortSignal,
472472
onCancelled: VoidFunction,
473-
) {
473+
): T & { signal: AbortSignal } {
474474
Object.defineProperty(object, 'signal', {
475475
enumerable: true,
476476
get: () => {
477+
const signal = getSignal()
477478
if (signal.aborted) {
478479
onCancelled()
479480
} else {
@@ -484,4 +485,6 @@ export function addConsumeAwareSignal(
484485
return signal
485486
},
486487
})
488+
489+
return object as T & { signal: AbortSignal }
487490
}

0 commit comments

Comments
 (0)