Skip to content

Commit 0148e46

Browse files
GeorchWmarkerikson
authored andcommitted
Add invalidateImmediately setting
1 parent 001d7a1 commit 0148e46

File tree

6 files changed

+21
-0
lines changed

6 files changed

+21
-0
lines changed

packages/toolkit/src/query/apiTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export type Module<Name extends ModuleName> = {
4545
| 'refetchOnMountOrArgChange'
4646
| 'refetchOnFocus'
4747
| 'refetchOnReconnect'
48+
| 'invalidateImmediately'
4849
| 'tagTypes'
4950
>,
5051
context: ApiContext<Definitions>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export type QuerySubState<D extends BaseEndpointDefinition<any, any, any>> = Id<
167167
> & { error: undefined })
168168
| ({
169169
status: QueryStatus.pending
170+
pendingTagInvalidations: FullTagDescription<string>[]
170171
} & BaseQuerySubState<D>)
171172
| ({
172173
status: QueryStatus.rejected
@@ -256,6 +257,7 @@ export type ConfigState<ReducerPath> = RefetchConfigOptions & {
256257

257258
export type ModifiableConfigState = {
258259
keepUnusedDataFor: number
260+
invalidateImmediately: boolean
259261
} & RefetchConfigOptions
260262

261263
export type MutationState<D extends EndpointDefinitions> = {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ export const buildInvalidationByTagsHandler: InternalHandlerBuilder = ({
8585

8686
const state = rootState[reducerPath]
8787

88+
if (state.config.invalidateImmediately) {
89+
handleInvalidatedTags(tags, mwApi)
90+
return
91+
}
92+
8893
const hasPendingQueries = Object.values(state.queries).some(
8994
(x) => x?.status === QueryStatus.pending
9095
)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ export const coreModule = (): Module<CoreModule> => ({
452452
refetchOnMountOrArgChange,
453453
refetchOnFocus,
454454
refetchOnReconnect,
455+
invalidateImmediately,
455456
},
456457
context
457458
) {
@@ -514,6 +515,7 @@ export const coreModule = (): Module<CoreModule> => ({
514515
refetchOnMountOrArgChange,
515516
keepUnusedDataFor,
516517
reducerPath,
518+
invalidateImmediately,
517519
},
518520
})
519521

packages/toolkit/src/query/createApi.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ export interface CreateApiOptions<
151151
* Note: requires [`setupListeners`](./setupListeners) to have been called.
152152
*/
153153
refetchOnReconnect?: boolean
154+
/**
155+
* Defaults to `false`. This setting allows you to disable a consistency measure that delay tag invalidations until all pending queries and mutations are settled.
156+
*
157+
* Context: If a mutation finishes while a query is pending, then RTK-query has to wait until the query is finished until it can invalidate any tags, since the query itself could provide a tag that the mutation will invalidate.
158+
* RTK-query also waits with the tag invalidations if another mutation is pending, to "batch" the tag invalidations if two mutations invalidate the same tag, avoiding unnecessary re-fetching.
159+
*
160+
* If you constantly have some queries running, this can delay the tag invalidations. In this case, you can enable this setting to opt-out of the "correct" behavior and refetch immediately when a mutation finishes instead.
161+
*/
162+
invalidateImmediately?: boolean
154163
/**
155164
* A function that is passed every dispatched action. If this returns something other than `undefined`,
156165
* that return value will be used to rehydrate fulfilled & errored queries.
@@ -255,6 +264,7 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
255264
refetchOnMountOrArgChange: false,
256265
refetchOnFocus: false,
257266
refetchOnReconnect: false,
267+
invalidateImmediately: false,
258268
...options,
259269
extractRehydrationInfo,
260270
serializeQueryArgs(queryArgsApi) {

packages/toolkit/src/query/tests/buildSlice.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe('buildSlice', () => {
5151
api: {
5252
config: {
5353
focused: true,
54+
invalidateImmediately: false,
5455
keepUnusedDataFor: 60,
5556
middlewareRegistered: true,
5657
online: true,

0 commit comments

Comments
 (0)