Skip to content

Commit bf7c37b

Browse files
Dennis Schallermarkerikson
authored andcommitted
query/endpointDefinitions: add sideEffectForced to QueryExtraOptions
query/buildInitiate: check sideEffectForced to conditionally force query
1 parent 7c4348c commit bf7c37b

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import type {
55
QueryArgFrom,
66
ResultTypeFrom,
77
} from '../endpointDefinitions'
8-
import { DefinitionType } from '../endpointDefinitions'
8+
import { DefinitionType, isQueryDefinition } from '../endpointDefinitions'
99
import type { QueryThunk, MutationThunk, QueryThunkArg } from './buildThunks'
1010
import type { AnyAction, ThunkAction, SerializedError } from '@reduxjs/toolkit'
1111
import type { SubscriptionOptions, RootState } from './apiState'
12-
import { QueryStatus } from './apiState'
1312
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
1413
import type { Api, ApiContext } from '../apiTypes'
1514
import type { ApiEndpointQuery } from './module'
@@ -279,10 +278,21 @@ Features like automatic cache collection, automatic refetching etc. will not be
279278
endpointDefinition,
280279
endpointName,
281280
})
281+
282+
const endpointContext = context.endpointDefinitions[endpointName]
283+
const sideEffectForced =
284+
isQueryDefinition(endpointContext) &&
285+
endpointContext.sideEffectForced?.({
286+
getState,
287+
endpointState: (
288+
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
289+
).select(arg)(getState()),
290+
})
291+
282292
const thunk = queryThunk({
283293
type: 'query',
284294
subscribe,
285-
forceRefetch,
295+
forceRefetch: forceRefetch || sideEffectForced,
286296
subscriptionOptions,
287297
endpointName,
288298
originalArgs: arg,

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AnyAction, ThunkDispatch } from '@reduxjs/toolkit'
22
import { SerializeQueryArgs } from './defaultSerializeQueryArgs'
3-
import type { RootState } from './core/apiState'
3+
import type { QuerySubState, RootState } from './core/apiState'
44
import type {
55
BaseQueryExtraOptions,
66
BaseQueryFn,
@@ -343,6 +343,10 @@ export interface QueryExtraOptions<
343343
* All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
344344
*/
345345
Types?: QueryTypes<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>
346+
sideEffectForced?: (params: {
347+
getState(): RootState<any, any, string>
348+
endpointState: QuerySubState<any>
349+
}) => boolean
346350
}
347351

348352
export type QueryDefinition<

0 commit comments

Comments
 (0)