Skip to content

Commit 0deaefd

Browse files
committed
Deduplicate cache lifecycle action fields
1 parent 45876a2 commit 0deaefd

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
205205
}
206206
const lifecycleMap: Record<string, CacheLifecycle> = {}
207207

208+
const { removeQueryResult, removeMutationResult, cacheEntriesUpserted } =
209+
api.internalActions
210+
208211
function resolveLifecycleEntry(
209212
cacheKey: string,
210213
data: unknown,
@@ -229,6 +232,16 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
229232
}
230233
}
231234

235+
function getActionMetaFields(
236+
action:
237+
| ReturnType<typeof queryThunk.pending>
238+
| ReturnType<typeof mutationThunk.pending>,
239+
) {
240+
const { arg, requestId } = action.meta
241+
const { endpointName, originalArgs } = arg
242+
return [endpointName, originalArgs, requestId] as const
243+
}
244+
232245
const handler: ApiMiddlewareInternalHandler = (
233246
action,
234247
mwApi,
@@ -250,13 +263,10 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
250263
}
251264

252265
if (queryThunk.pending.match(action)) {
253-
checkForNewCacheKey(
254-
action.meta.arg.endpointName,
255-
cacheKey,
256-
action.meta.requestId,
257-
action.meta.arg.originalArgs,
258-
)
259-
} else if (api.internalActions.cacheEntriesUpserted.match(action)) {
266+
const [endpointName, originalArgs, requestId] =
267+
getActionMetaFields(action)
268+
checkForNewCacheKey(endpointName, cacheKey, requestId, originalArgs)
269+
} else if (cacheEntriesUpserted.match(action)) {
260270
for (const { queryDescription, value } of action.payload) {
261271
const { endpointName, originalArgs, queryCacheKey } = queryDescription
262272
checkForNewCacheKey(
@@ -271,19 +281,15 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
271281
} else if (mutationThunk.pending.match(action)) {
272282
const state = mwApi.getState()[reducerPath].mutations[cacheKey]
273283
if (state) {
274-
handleNewKey(
275-
action.meta.arg.endpointName,
276-
action.meta.arg.originalArgs,
277-
cacheKey,
278-
mwApi,
279-
action.meta.requestId,
280-
)
284+
const [endpointName, originalArgs, requestId] =
285+
getActionMetaFields(action)
286+
handleNewKey(endpointName, originalArgs, cacheKey, mwApi, requestId)
281287
}
282288
} else if (isFulfilledThunk(action)) {
283289
resolveLifecycleEntry(cacheKey, action.payload, action.meta.baseQueryMeta)
284290
} else if (
285-
api.internalActions.removeQueryResult.match(action) ||
286-
api.internalActions.removeMutationResult.match(action)
291+
removeQueryResult.match(action) ||
292+
removeMutationResult.match(action)
287293
) {
288294
removeLifecycleEntry(cacheKey)
289295
} else if (api.util.resetApiState.match(action)) {
@@ -298,9 +304,8 @@ export const buildCacheLifecycleHandler: InternalHandlerBuilder = ({
298304
if (isMutationThunk(action)) {
299305
return action.meta.arg.fixedCacheKey ?? action.meta.requestId
300306
}
301-
if (api.internalActions.removeQueryResult.match(action))
302-
return action.payload.queryCacheKey
303-
if (api.internalActions.removeMutationResult.match(action))
307+
if (removeQueryResult.match(action)) return action.payload.queryCacheKey
308+
if (removeMutationResult.match(action))
304309
return getMutationCacheKey(action.payload)
305310
return ''
306311
}

0 commit comments

Comments
 (0)