Skip to content

Commit c16a3cd

Browse files
committed
Accept array of provided tag entries internally for perf
1 parent 25b4042 commit c16a3cd

File tree

2 files changed

+63
-68
lines changed

2 files changed

+63
-68
lines changed

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

+56-45
Original file line numberDiff line numberDiff line change
@@ -493,33 +493,39 @@ export function buildSlice({
493493
updateProvidedBy: {
494494
reducer(
495495
draft,
496-
action: PayloadAction<{
497-
queryCacheKey: QueryCacheKey
498-
providedTags: readonly FullTagDescription<string>[]
499-
}>,
496+
action: PayloadAction<
497+
Array<{
498+
queryCacheKey: QueryCacheKey
499+
providedTags: readonly FullTagDescription<string>[]
500+
}>
501+
>,
500502
) {
501-
const { queryCacheKey, providedTags } = action.payload
502-
503-
removeCacheKeyFromTags(draft, queryCacheKey)
503+
for (const { queryCacheKey, providedTags } of action.payload) {
504+
removeCacheKeyFromTags(draft, queryCacheKey)
504505

505-
for (const { type, id } of providedTags) {
506-
const subscribedQueries = ((draft.tags[type] ??= {})[
507-
id || '__internal_without_id'
508-
] ??= [])
509-
const alreadySubscribed = subscribedQueries.includes(queryCacheKey)
510-
if (!alreadySubscribed) {
511-
subscribedQueries.push(queryCacheKey)
506+
for (const { type, id } of providedTags) {
507+
const subscribedQueries = ((draft.tags[type] ??= {})[
508+
id || '__internal_without_id'
509+
] ??= [])
510+
const alreadySubscribed =
511+
subscribedQueries.includes(queryCacheKey)
512+
if (!alreadySubscribed) {
513+
subscribedQueries.push(queryCacheKey)
514+
}
512515
}
513-
}
514516

515-
// Remove readonly from the providedTags array
516-
draft.keys[queryCacheKey] =
517-
providedTags as FullTagDescription<string>[]
517+
// Remove readonly from the providedTags array
518+
draft.keys[queryCacheKey] =
519+
providedTags as FullTagDescription<string>[]
520+
}
518521
},
519-
prepare: prepareAutoBatched<{
520-
queryCacheKey: QueryCacheKey
521-
providedTags: readonly FullTagDescription<string>[]
522-
}>(),
522+
prepare:
523+
prepareAutoBatched<
524+
Array<{
525+
queryCacheKey: QueryCacheKey
526+
providedTags: readonly FullTagDescription<string>[]
527+
}>
528+
>(),
523529
},
524530
},
525531
extraReducers(builder) {
@@ -550,21 +556,26 @@ export function buildSlice({
550556
.addMatcher(
551557
isAnyOf(isFulfilled(queryThunk), isRejectedWithValue(queryThunk)),
552558
(draft, action) => {
553-
writeProvidedTagsForQuery(draft, action)
559+
writeProvidedTagsForQueries(draft, [action])
554560
},
555561
)
556562
.addMatcher(
557563
querySlice.actions.cacheEntriesUpserted.match,
558564
(draft, action) => {
559-
for (const { queryDescription: arg, value } of action.payload) {
560-
const action: CalculateProvidedByAction = {
561-
type: 'UNKNOWN',
562-
payload: value,
563-
meta: { requestStatus: 'fulfilled', requestId: 'UNKNOWN', arg },
564-
}
565-
566-
writeProvidedTagsForQuery(draft, action)
567-
}
565+
const mockActions: CalculateProvidedByAction[] = action.payload.map(
566+
({ queryDescription, value }) => {
567+
return {
568+
type: 'UNKNOWN',
569+
payload: value,
570+
meta: {
571+
requestStatus: 'fulfilled',
572+
requestId: 'UNKNOWN',
573+
arg: queryDescription,
574+
},
575+
}
576+
},
577+
)
578+
writeProvidedTagsForQueries(draft, mockActions)
568579
},
569580
)
570581
},
@@ -592,24 +603,24 @@ export function buildSlice({
592603
delete draft.keys[queryCacheKey]
593604
}
594605

595-
function writeProvidedTagsForQuery(
606+
function writeProvidedTagsForQueries(
596607
draft: InvalidationState<string>,
597-
action: CalculateProvidedByAction,
608+
actions: CalculateProvidedByAction[],
598609
) {
599-
const providedTags = calculateProvidedByThunk(
600-
action,
601-
'providesTags',
602-
definitions,
603-
assertTagType,
604-
)
605-
const { queryCacheKey } = action.meta.arg
610+
const providedByEntries = actions.map((action) => {
611+
const providedTags = calculateProvidedByThunk(
612+
action,
613+
'providesTags',
614+
definitions,
615+
assertTagType,
616+
)
617+
const { queryCacheKey } = action.meta.arg
618+
return { queryCacheKey, providedTags }
619+
})
606620

607621
invalidationSlice.caseReducers.updateProvidedBy(
608622
draft,
609-
invalidationSlice.actions.updateProvidedBy({
610-
queryCacheKey,
611-
providedTags,
612-
}),
623+
invalidationSlice.actions.updateProvidedBy(providedByEntries),
613624
)
614625
}
615626

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

+7-23
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,13 @@ type MutationThunkArg = {
166166
export type ThunkResult = unknown
167167

168168
export type ThunkApiMetaConfig = {
169-
pendingMeta: {
170-
startedTimeStamp: number
171-
[SHOULD_AUTOBATCH]: true
172-
}
169+
pendingMeta: { startedTimeStamp: number; [SHOULD_AUTOBATCH]: true }
173170
fulfilledMeta: {
174171
fulfilledTimeStamp: number
175172
baseQueryMeta: unknown
176173
[SHOULD_AUTOBATCH]: true
177174
}
178-
rejectedMeta: {
179-
baseQueryMeta: unknown
180-
[SHOULD_AUTOBATCH]: true
181-
}
175+
rejectedMeta: { baseQueryMeta: unknown; [SHOULD_AUTOBATCH]: true }
182176
}
183177
export type QueryThunk = AsyncThunk<
184178
ThunkResult,
@@ -320,10 +314,7 @@ type TransformCallback = (
320314
export const addShouldAutoBatch = <T extends Record<string, any>>(
321315
arg: T = {} as T,
322316
): T & { [SHOULD_AUTOBATCH]: true } => {
323-
return {
324-
...arg,
325-
[SHOULD_AUTOBATCH]: true,
326-
}
317+
return { ...arg, [SHOULD_AUTOBATCH]: true }
327318
}
328319

329320
export function buildThunks<
@@ -382,7 +373,7 @@ export function buildThunks<
382373
)
383374

384375
dispatch(
385-
api.internalActions.updateProvidedBy({ queryCacheKey, providedTags }),
376+
api.internalActions.updateProvidedBy([{ queryCacheKey, providedTags }]),
386377
)
387378
}
388379

@@ -466,9 +457,7 @@ export function buildThunks<
466457
).initiate(arg, {
467458
subscribe: false,
468459
forceRefetch: true,
469-
[forceQueryFnSymbol]: () => ({
470-
data: value,
471-
}),
460+
[forceQueryFnSymbol]: () => ({ data: value }),
472461
}),
473462
) as UpsertThunkResult<Definitions, EndpointName>
474463

@@ -623,10 +612,7 @@ export function buildThunks<
623612
finalQueryArg,
624613
)
625614

626-
return {
627-
...result,
628-
data: transformedResponse,
629-
}
615+
return { ...result, data: transformedResponse }
630616
}
631617

632618
if (
@@ -793,9 +779,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
793779
return addShouldAutoBatch({
794780
startedTimeStamp: Date.now(),
795781
...(isInfiniteQueryDefinition(endpointDefinition)
796-
? {
797-
direction: (arg as InfiniteQueryThunkArg<any>).direction,
798-
}
782+
? { direction: (arg as InfiniteQueryThunkArg<any>).direction }
799783
: {}),
800784
})
801785
},

0 commit comments

Comments
 (0)