Skip to content

Commit 0dedba0

Browse files
Dennis Schallermarkerikson
authored andcommitted
rename sideEffectForced to forceRefetch
make forceRefetch be read from endpoint config instead of passing it into thunk as forced
1 parent bf7c37b commit 0dedba0

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,10 @@ Features like automatic cache collection, automatic refetching etc. will not be
279279
endpointName,
280280
})
281281

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-
292282
const thunk = queryThunk({
293283
type: 'query',
294284
subscribe,
295-
forceRefetch: forceRefetch || sideEffectForced,
285+
forceRefetch: forceRefetch,
296286
subscriptionOptions,
297287
endpointName,
298288
originalArgs: arg,

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import type {
1212
QueryActionCreatorResult,
1313
} from './buildInitiate'
1414
import { forceQueryFnSymbol, isUpsertQuery } from './buildInitiate'
15-
import type {
15+
import {
1616
AssertTagTypes,
1717
EndpointDefinition,
1818
EndpointDefinitions,
19+
isQueryDefinition,
1920
MutationDefinition,
2021
QueryArgFrom,
2122
QueryDefinition,
@@ -478,6 +479,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
478479
const state = getState()
479480
const requestState = state[reducerPath]?.queries?.[arg.queryCacheKey]
480481
const fulfilledVal = requestState?.fulfilledTimeStamp
482+
const endpointDefinition = endpointDefinitions[arg.endpointName]
481483

482484
// Order of these checks matters.
483485
// In order for `upsertQueryData` to successfully run while an existing request is in flight,
@@ -490,6 +492,15 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
490492
// if this is forced, continue
491493
if (isForcedQuery(arg, state)) return true
492494

495+
if (
496+
isQueryDefinition(endpointDefinition) &&
497+
endpointDefinition?.forceRefetch?.({
498+
endpointState: requestState,
499+
state,
500+
})
501+
)
502+
return true
503+
493504
// Pull from the cache unless we explicitly force refetch or qualify based on time
494505
if (fulfilledVal)
495506
// Value is cached and we didn't specify to refresh, skip it.

packages/toolkit/src/query/endpointDefinitions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,15 @@ export interface QueryExtraOptions<
339339
responseData: ResultType
340340
): ResultType | void
341341

342+
forceRefetch?(params: {
343+
state: RootState<any, any, string>
344+
endpointState?: QuerySubState<any>
345+
}): boolean
346+
342347
/**
343348
* All of these are `undefined` at runtime, purely to be used in TypeScript declarations!
344349
*/
345350
Types?: QueryTypes<QueryArg, BaseQuery, TagTypes, ResultType, ReducerPath>
346-
sideEffectForced?: (params: {
347-
getState(): RootState<any, any, string>
348-
endpointState: QuerySubState<any>
349-
}) => boolean
350351
}
351352

352353
export type QueryDefinition<

0 commit comments

Comments
 (0)