Skip to content

Commit ac01d3d

Browse files
committed
Apply SHOULD_AUTOBATCH to RTKQ thunks for opt-in perf boost
1 parent 0de2441 commit ac01d3d

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
isFulfilled,
88
isRejectedWithValue,
99
createNextState,
10+
prepareAutoBatched,
1011
} from '@reduxjs/toolkit'
1112
import type {
1213
CombinedState as CombinedQueryState,
@@ -114,11 +115,14 @@ export function buildSlice({
114115
name: `${reducerPath}/queries`,
115116
initialState: initialState as QueryState<any>,
116117
reducers: {
117-
removeQueryResult(
118-
draft,
119-
{ payload: { queryCacheKey } }: PayloadAction<QuerySubstateIdentifier>
120-
) {
121-
delete draft[queryCacheKey]
118+
removeQueryResult: {
119+
reducer(
120+
draft,
121+
{ payload: { queryCacheKey } }: PayloadAction<QuerySubstateIdentifier>
122+
) {
123+
delete draft[queryCacheKey]
124+
},
125+
prepare: prepareAutoBatched<QuerySubstateIdentifier>(),
122126
},
123127
queryResultPatched(
124128
draft,
@@ -243,14 +247,14 @@ export function buildSlice({
243247
name: `${reducerPath}/mutations`,
244248
initialState: initialState as MutationState<any>,
245249
reducers: {
246-
removeMutationResult(
247-
draft,
248-
{ payload }: PayloadAction<MutationSubstateIdentifier>
249-
) {
250-
const cacheKey = getMutationCacheKey(payload)
251-
if (cacheKey in draft) {
252-
delete draft[cacheKey]
253-
}
250+
removeMutationResult: {
251+
reducer(draft, { payload }: PayloadAction<MutationSubstateIdentifier>) {
252+
const cacheKey = getMutationCacheKey(payload)
253+
if (cacheKey in draft) {
254+
delete draft[cacheKey]
255+
}
256+
},
257+
prepare: prepareAutoBatched<MutationSubstateIdentifier>(),
254258
},
255259
},
256260
extraReducers(builder) {

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import type {
3939
ThunkDispatch,
4040
AsyncThunk,
4141
} from '@reduxjs/toolkit'
42-
import { createAsyncThunk } from '@reduxjs/toolkit'
42+
import { createAsyncThunk, SHOULD_AUTOBATCH } from '@reduxjs/toolkit'
4343

4444
import { HandledError } from '../HandledError'
4545

@@ -123,13 +123,18 @@ export interface MutationThunkArg {
123123
export type ThunkResult = unknown
124124

125125
export type ThunkApiMetaConfig = {
126-
pendingMeta: { startedTimeStamp: number }
126+
pendingMeta: {
127+
startedTimeStamp: number
128+
[SHOULD_AUTOBATCH]: true
129+
}
127130
fulfilledMeta: {
128131
fulfilledTimeStamp: number
129132
baseQueryMeta: unknown
133+
[SHOULD_AUTOBATCH]: true
130134
}
131135
rejectedMeta: {
132136
baseQueryMeta: unknown
137+
[SHOULD_AUTOBATCH]: true
133138
}
134139
}
135140
export type QueryThunk = AsyncThunk<
@@ -399,6 +404,7 @@ export function buildThunks<
399404
{
400405
fulfilledTimeStamp: Date.now(),
401406
baseQueryMeta: result.meta,
407+
[SHOULD_AUTOBATCH]: true,
402408
}
403409
)
404410
} catch (error) {
@@ -423,7 +429,7 @@ export function buildThunks<
423429
catchedError.meta,
424430
arg.originalArgs
425431
),
426-
{ baseQueryMeta: catchedError.meta }
432+
{ baseQueryMeta: catchedError.meta, [SHOULD_AUTOBATCH]: true }
427433
)
428434
} catch (e) {
429435
catchedError = e
@@ -473,7 +479,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
473479
ThunkApiMetaConfig & { state: RootState<any, string, ReducerPath> }
474480
>(`${reducerPath}/executeQuery`, executeEndpoint, {
475481
getPendingMeta() {
476-
return { startedTimeStamp: Date.now() }
482+
return { startedTimeStamp: Date.now(), [SHOULD_AUTOBATCH]: true }
477483
},
478484
condition(queryThunkArgs, { getState }) {
479485
const state = getState()
@@ -532,7 +538,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
532538
ThunkApiMetaConfig & { state: RootState<any, string, ReducerPath> }
533539
>(`${reducerPath}/executeMutation`, executeEndpoint, {
534540
getPendingMeta() {
535-
return { startedTimeStamp: Date.now() }
541+
return { startedTimeStamp: Date.now(), [SHOULD_AUTOBATCH]: true }
536542
},
537543
})
538544

0 commit comments

Comments
 (0)