Skip to content

Commit 2bca4e0

Browse files
Georg Wicke-Arndtmarkerikson
authored andcommitted
Incorporate review suggestions
1 parent 33c6bd2 commit 2bca4e0

File tree

7 files changed

+18
-19
lines changed

7 files changed

+18
-19
lines changed

packages/toolkit/src/query/apiTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export type Module<Name extends ModuleName> = {
4545
| 'refetchOnMountOrArgChange'
4646
| 'refetchOnFocus'
4747
| 'refetchOnReconnect'
48-
| 'invalidateImmediately'
48+
| 'invalidationBehavior'
4949
| 'tagTypes'
5050
>,
5151
context: ApiContext<Definitions>

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
BaseEndpointDefinition,
88
ResultTypeFrom,
99
QueryArgFrom,
10-
FullTagDescription,
1110
} from '../endpointDefinitions'
1211
import type { Id, WithRequiredProp } from '../tsHelpers'
1312

@@ -255,7 +254,7 @@ export type ConfigState<ReducerPath> = RefetchConfigOptions & {
255254

256255
export type ModifiableConfigState = {
257256
keepUnusedDataFor: number
258-
invalidateImmediately: boolean
257+
invalidationBehavior: 'delayed' | 'immediately'
259258
} & RefetchConfigOptions
260259

261260
export type MutationState<D extends EndpointDefinitions> = {

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

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

8585
pendingTagInvalidations.push(...newTags)
8686

87-
if (!state.config.invalidateImmediately) {
88-
const hasPendingRequests = Object.values({
89-
...state.queries,
90-
...state.mutations,
91-
}).some((x) => x?.status === QueryStatus.pending)
87+
if (state.config.invalidationBehavior === 'delayed') {
88+
const hasPendingRequests = [
89+
...Object.values(state.queries),
90+
...Object.values(state.mutations),
91+
].some((x) => x?.status === QueryStatus.pending)
9292

9393
if (hasPendingRequests) return
9494
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { calculateProvidedByThunk } from './buildThunks'
2828
import type {
2929
AssertTagTypes,
3030
EndpointDefinitions,
31-
FullTagDescription,
3231
QueryDefinition,
3332
} from '../endpointDefinitions'
3433
import type { Patch } from 'immer'

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ export const coreModule = (): Module<CoreModule> => ({
452452
refetchOnMountOrArgChange,
453453
refetchOnFocus,
454454
refetchOnReconnect,
455-
invalidateImmediately,
455+
invalidationBehavior,
456456
},
457457
context
458458
) {
@@ -515,7 +515,7 @@ export const coreModule = (): Module<CoreModule> => ({
515515
refetchOnMountOrArgChange,
516516
keepUnusedDataFor,
517517
reducerPath,
518-
invalidateImmediately,
518+
invalidationBehavior,
519519
},
520520
})
521521

packages/toolkit/src/query/createApi.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,15 @@ export interface CreateApiOptions<
152152
*/
153153
refetchOnReconnect?: boolean
154154
/**
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.
155+
* Defaults to `'immediately'`. This setting allows you to control when tags are invalidated after a mutation.
156156
*
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.
157+
* - `'immediately'`: Queries are invalidated instantly after the mutation finished, even if they are running.
158+
* If the query provides tags that were invalidated while it ran, it won't be re-fetched.
159+
* - `'delayed'`: Invalidation only happens after all queries and mutations are settled.
160+
* This ensures that queries are always invalidated correctly and automatically "batches" invalidations of concurrent mutations.
161+
* Note that if you constantly have some queries (or mutations) running, this can delay tag invalidations indefinitely.
161162
*/
162-
invalidateImmediately?: boolean
163+
invalidationBehavior?: 'delayed' | 'immediately'
163164
/**
164165
* A function that is passed every dispatched action. If this returns something other than `undefined`,
165166
* that return value will be used to rehydrate fulfilled & errored queries.
@@ -264,7 +265,7 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
264265
refetchOnMountOrArgChange: false,
265266
refetchOnFocus: false,
266267
refetchOnReconnect: false,
267-
invalidateImmediately: false,
268+
invalidationBehavior: 'delayed',
268269
...options,
269270
extractRehydrationInfo,
270271
serializeQueryArgs(queryArgsApi) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('buildSlice', () => {
5151
api: {
5252
config: {
5353
focused: true,
54-
invalidateImmediately: false,
54+
invalidationBehavior: "delayed",
5555
keepUnusedDataFor: 60,
5656
middlewareRegistered: true,
5757
online: true,

0 commit comments

Comments
 (0)