Skip to content

Commit 3bfd135

Browse files
committed
refactor(query-core): extract context creation to functions and move type assertions
This refactor extracts the creation of the query function context and fetch context into dedicated helper functions (e.g., `createQueryFnContext`), improving code readability. This change: - Moves type assertions and type handling higher up, reducing inline type noise and making type boundaries clearer. - Replaces ad-hoc object construction with a function that does one thing (build query contexts). No functional behavior is changed; this is a structural and type-safety improvement.
1 parent 7a6733f commit 3bfd135

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

‎packages/query-core/src/infiniteQueryBehavior.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,25 @@ export function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(
5454
return Promise.resolve(data)
5555
}
5656

57-
const queryFnContext: OmitKeyof<
58-
QueryFunctionContext<QueryKey, unknown>,
59-
'signal'
60-
> = {
61-
client: context.client,
62-
queryKey: context.queryKey,
63-
pageParam: param,
64-
direction: previous ? 'backward' : 'forward',
65-
meta: context.options.meta,
57+
const createQueryFnContext = () => {
58+
const queryFnContext: OmitKeyof<
59+
QueryFunctionContext<QueryKey, unknown>,
60+
'signal'
61+
> = {
62+
client: context.client,
63+
queryKey: context.queryKey,
64+
pageParam: param,
65+
direction: previous ? 'backward' : 'forward',
66+
meta: context.options.meta,
67+
}
68+
return queryFnContext as QueryFunctionContext<QueryKey, unknown>
6669
}
6770

71+
const queryFnContext = createQueryFnContext()
72+
6873
addSignalProperty(queryFnContext)
6974

70-
const page = await queryFn(
71-
queryFnContext as QueryFunctionContext<QueryKey, unknown>,
72-
)
75+
const page = await queryFn(queryFnContext)
7376

7477
const { maxPages } = context.options
7578
const addTo = previous ? addToStart : addToEnd

‎packages/query-core/src/query.ts

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -411,48 +411,59 @@ export class Query<
411411
const queryFn = ensureQueryFn(this.options, fetchOptions)
412412

413413
// Create query function context
414-
const queryFnContext: OmitKeyof<
415-
QueryFunctionContext<TQueryKey>,
416-
'signal'
417-
> = {
418-
client: this.#client,
419-
queryKey: this.queryKey,
420-
meta: this.meta,
414+
const createQueryFnContext = (): QueryFunctionContext<TQueryKey> => {
415+
const queryFnContext: OmitKeyof<
416+
QueryFunctionContext<TQueryKey>,
417+
'signal'
418+
> = {
419+
client: this.#client,
420+
queryKey: this.queryKey,
421+
meta: this.meta,
422+
}
423+
addSignalProperty(queryFnContext)
424+
return queryFnContext as QueryFunctionContext<TQueryKey>
421425
}
422426

423-
addSignalProperty(queryFnContext)
427+
const queryFnContext = createQueryFnContext()
424428

425429
this.#abortSignalConsumed = false
426430
if (this.options.persister) {
427431
return this.options.persister(
428-
queryFn as QueryFunction<any>,
429-
queryFnContext as QueryFunctionContext<TQueryKey>,
432+
queryFn,
433+
queryFnContext,
430434
this as unknown as Query,
431435
)
432436
}
433437

434-
return queryFn(queryFnContext as QueryFunctionContext<TQueryKey>)
438+
return queryFn(queryFnContext)
435439
}
436440

437441
// Trigger behavior hook
438-
const context: OmitKeyof<
439-
FetchContext<TQueryFnData, TError, TData, TQueryKey>,
440-
'signal'
441-
> = {
442-
fetchOptions,
443-
options: this.options,
444-
queryKey: this.queryKey,
445-
client: this.#client,
446-
state: this.state,
447-
fetchFn,
442+
const createFetchContext = (): FetchContext<
443+
TQueryFnData,
444+
TError,
445+
TData,
446+
TQueryKey
447+
> => {
448+
const context: OmitKeyof<
449+
FetchContext<TQueryFnData, TError, TData, TQueryKey>,
450+
'signal'
451+
> = {
452+
fetchOptions,
453+
options: this.options,
454+
queryKey: this.queryKey,
455+
client: this.#client,
456+
state: this.state,
457+
fetchFn,
458+
}
459+
460+
addSignalProperty(context)
461+
return context as FetchContext<TQueryFnData, TError, TData, TQueryKey>
448462
}
449463

450-
addSignalProperty(context)
464+
const context = createFetchContext()
451465

452-
this.options.behavior?.onFetch(
453-
context as FetchContext<TQueryFnData, TError, TData, TQueryKey>,
454-
this as unknown as Query,
455-
)
466+
this.options.behavior?.onFetch(context, this as unknown as Query)
456467

457468
// Store state in case the current fetch needs to be reverted
458469
this.#revertState = this.state

0 commit comments

Comments
 (0)
close