Skip to content

Commit b317229

Browse files
committed
Consolidate query definition checks
1 parent dc6425d commit b317229

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

packages/toolkit/src/query/core/buildMiddleware/cacheLifecycle.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ThunkDispatch, UnknownAction } from '@reduxjs/toolkit'
22
import type { BaseQueryFn, BaseQueryMeta } from '../../baseQueryTypes'
33
import type { BaseEndpointDefinition } from '../../endpointDefinitions'
4-
import { DefinitionType } from '../../endpointDefinitions'
4+
import { DefinitionType, isAnyQueryDefinition } from '../../endpointDefinitions'
55
import type { QueryCacheKey, RootState } from '../apiState'
66
import type {
77
MutationResultSelectorResult,
@@ -328,10 +328,7 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
328328
cacheDataLoaded.catch(() => {})
329329
lifecycleMap[queryCacheKey] = lifecycle
330330
const selector = (api.endpoints[endpointName] as any).select(
331-
endpointDefinition.type === DefinitionType.query ||
332-
endpointDefinition.type === DefinitionType.infinitequery
333-
? originalArgs
334-
: queryCacheKey,
331+
isAnyQueryDefinition(endpointDefinition) ? originalArgs : queryCacheKey,
335332
)
336333

337334
const extra = mwApi.dispatch((_, __, extra) => extra)
@@ -340,8 +337,7 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
340337
getCacheEntry: () => selector(mwApi.getState()),
341338
requestId,
342339
extra,
343-
updateCachedData: (endpointDefinition.type === DefinitionType.query ||
344-
endpointDefinition.type === DefinitionType.infinitequery
340+
updateCachedData: (isAnyQueryDefinition(endpointDefinition)
345341
? (updateRecipe: Recipe<any>) =>
346342
mwApi.dispatch(
347343
api.util.updateQueryData(

packages/toolkit/src/query/core/buildMiddleware/queryLifecycle.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {
33
BaseQueryFn,
44
BaseQueryMeta,
55
} from '../../baseQueryTypes'
6-
import { DefinitionType } from '../../endpointDefinitions'
6+
import { DefinitionType, isAnyQueryDefinition } from '../../endpointDefinitions'
77
import type { Recipe } from '../buildThunks'
88
import { isFulfilled, isPending, isRejected } from '../rtkImports'
99
import type {
@@ -459,10 +459,7 @@ export const buildQueryLifecycleHandler: InternalHandlerBuilder = ({
459459
queryFulfilled.catch(() => {})
460460
lifecycleMap[requestId] = lifecycle
461461
const selector = (api.endpoints[endpointName] as any).select(
462-
endpointDefinition.type === DefinitionType.query ||
463-
endpointDefinition.type === DefinitionType.infinitequery
464-
? originalArgs
465-
: requestId,
462+
isAnyQueryDefinition(endpointDefinition) ? originalArgs : requestId,
466463
)
467464

468465
const extra = mwApi.dispatch((_, __, extra) => extra)
@@ -471,8 +468,7 @@ export const buildQueryLifecycleHandler: InternalHandlerBuilder = ({
471468
getCacheEntry: () => selector(mwApi.getState()),
472469
requestId,
473470
extra,
474-
updateCachedData: (endpointDefinition.type === DefinitionType.query ||
475-
endpointDefinition.type === DefinitionType.infinitequery
471+
updateCachedData: (isAnyQueryDefinition(endpointDefinition)
476472
? (updateRecipe: Recipe<any>) =>
477473
mwApi.dispatch(
478474
api.util.updateQueryData(

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,14 @@ export function isInfiniteQueryDefinition(
912912
return e.type === DefinitionType.infinitequery
913913
}
914914

915+
export function isAnyQueryDefinition(
916+
e: EndpointDefinition<any, any, any, any>,
917+
): e is
918+
| QueryDefinition<any, any, any, any>
919+
| InfiniteQueryDefinition<any, any, any, any, any> {
920+
return isQueryDefinition(e) || isInfiniteQueryDefinition(e)
921+
}
922+
915923
export type EndpointBuilder<
916924
BaseQuery extends BaseQueryFn,
917925
TagTypes extends string,

0 commit comments

Comments
 (0)