Skip to content

Commit 4ad4523

Browse files
committed
Deduplicate endpoint definition lookups
1 parent 0deaefd commit 4ad4523

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

packages/toolkit/src/query/apiTypes.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { UnknownAction } from '@reduxjs/toolkit'
22
import type { BaseQueryFn } from './baseQueryTypes'
3-
import type { CombinedState, CoreModule } from './core'
3+
import type { CombinedState, CoreModule, QueryKeys } from './core'
44
import type { ApiModules } from './core/module'
55
import type { CreateApiOptions } from './createApi'
66
import type {
@@ -56,6 +56,14 @@ export interface ApiContext<Definitions extends EndpointDefinitions> {
5656
hasRehydrationInfo: (action: UnknownAction) => boolean
5757
}
5858

59+
export const getEndpointDefinition = <
60+
Definitions extends EndpointDefinitions,
61+
EndpointName extends keyof Definitions,
62+
>(
63+
context: ApiContext<Definitions>,
64+
endpointName: EndpointName,
65+
) => context.endpointDefinitions[endpointName]
66+
5967
export type Api<
6068
BaseQuery extends BaseQueryFn,
6169
Definitions extends EndpointDefinitions,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from '@reduxjs/toolkit'
88
import type { Dispatch } from 'redux'
99
import { asSafePromise } from '../../tsHelpers'
10-
import type { Api, ApiContext } from '../apiTypes'
10+
import { getEndpointDefinition, type Api, type ApiContext } from '../apiTypes'
1111
import type { BaseQueryError, QueryReturnValue } from '../baseQueryTypes'
1212
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
1313
import {
@@ -304,7 +304,7 @@ export function buildInitiate({
304304

305305
function getRunningQueryThunk(endpointName: string, queryArgs: any) {
306306
return (dispatch: Dispatch) => {
307-
const endpointDefinition = context.endpointDefinitions[endpointName]
307+
const endpointDefinition = getEndpointDefinition(context, endpointName)
308308
const queryCacheKey = serializeQueryArgs({
309309
queryArgs,
310310
endpointDefinition,

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getEndpointDefinition } from '@internal/query/apiTypes'
12
import type { QueryDefinition } from '../../endpointDefinitions'
23
import type { ConfigState, QueryCacheKey, QuerySubState } from '../apiState'
34
import { isAnyOf } from '../rtkImports'
@@ -155,9 +156,10 @@ export const buildCacheCollectionHandler: InternalHandlerBuilder = ({
155156
api: SubMiddlewareApi,
156157
config: ConfigState<string>,
157158
) {
158-
const endpointDefinition = context.endpointDefinitions[
159-
endpointName
160-
] as QueryDefinition<any, any, any, any>
159+
const endpointDefinition = getEndpointDefinition(
160+
context,
161+
endpointName,
162+
) as QueryDefinition<any, any, any, any>
161163
const keepUnusedDataFor =
162164
endpointDefinition?.keepUnusedDataFor ?? config.keepUnusedDataFor
163165

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
PromiseWithKnownReason,
2424
SubMiddlewareApi,
2525
} from './types'
26+
import { getEndpointDefinition } from '@internal/query/apiTypes'
2627

2728
export type ReferenceCacheLifecycle = never
2829

@@ -317,7 +318,7 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
317318
mwApi: SubMiddlewareApi,
318319
requestId: string,
319320
) {
320-
const endpointDefinition = context.endpointDefinitions[endpointName]
321+
const endpointDefinition = getEndpointDefinition(context, endpointName)
321322
const onCacheEntryAdded = endpointDefinition?.onCacheEntryAdded
322323
if (!onCacheEntryAdded) return
323324

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { getEndpointDefinition } from '@internal/query/apiTypes'
12
import type {
23
BaseQueryError,
34
BaseQueryFn,
45
BaseQueryMeta,
56
} from '../../baseQueryTypes'
6-
import { DefinitionType, isAnyQueryDefinition } from '../../endpointDefinitions'
7+
import { isAnyQueryDefinition } from '../../endpointDefinitions'
78
import type { Recipe } from '../buildThunks'
89
import { isFulfilled, isPending, isRejected } from '../rtkImports'
910
import type {
@@ -442,7 +443,7 @@ export const buildQueryLifecycleHandler: InternalHandlerBuilder = ({
442443
requestId,
443444
arg: { endpointName, originalArgs },
444445
} = action.meta
445-
const endpointDefinition = context.endpointDefinitions[endpointName]
446+
const endpointDefinition = getEndpointDefinition(context, endpointName)
446447
const onQueryStarted = endpointDefinition?.onQueryStarted
447448
if (onQueryStarted) {
448449
const lifecycle = {} as CacheLifecycle

packages/toolkit/src/query/createApi.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { Api, ApiContext, Module, ModuleName } from './apiTypes'
1+
import {
2+
getEndpointDefinition,
3+
type Api,
4+
type ApiContext,
5+
type Module,
6+
type ModuleName,
7+
} from './apiTypes'
28
import type { CombinedState } from './core/apiState'
39
import type { BaseQueryArg, BaseQueryFn } from './baseQueryTypes'
410
import type { SerializeQueryArgs } from './defaultSerializeQueryArgs'
@@ -421,10 +427,10 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
421427
endpoints,
422428
)) {
423429
if (typeof partialDefinition === 'function') {
424-
partialDefinition(context.endpointDefinitions[endpointName])
430+
partialDefinition(getEndpointDefinition(context, endpointName))
425431
} else {
426432
Object.assign(
427-
context.endpointDefinitions[endpointName] || {},
433+
getEndpointDefinition(context, endpointName) || {},
428434
partialDefinition,
429435
)
430436
}

0 commit comments

Comments
 (0)