Skip to content

Commit

Permalink
rename sideEffectForced to forceRefetch
Browse files Browse the repository at this point in the history
make forceRefetch be read from endpoint config instead of passing it into thunk as forced
  • Loading branch information
Dennis Schaller committed Sep 13, 2022
1 parent 35efbda commit 0bb5f2b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
8 changes: 1 addition & 7 deletions packages/toolkit/src/query/core/buildInitiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,10 @@ Features like automatic cache collection, automatic refetching etc. will not be
endpointName,
})

const endpointContext = context.endpointDefinitions[endpointName]
const sideEffectForced = isQueryDefinition(endpointContext) && endpointContext.sideEffectForced?.({
getState,
endpointState: (api.endpoints[endpointName] as ApiEndpointQuery<any, any>).select(arg)(getState())
})

const thunk = queryThunk({
type: 'query',
subscribe,
forceRefetch: forceRefetch || sideEffectForced,
forceRefetch: forceRefetch,
subscriptionOptions,
endpointName,
originalArgs: arg,
Expand Down
5 changes: 4 additions & 1 deletion packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import type {
import type { RootState, QueryKeys, QuerySubstateIdentifier } from './apiState'
import { QueryStatus } from './apiState'
import type { StartQueryActionCreatorOptions } from './buildInitiate'
import type {
import {
AssertTagTypes,
EndpointDefinition,
EndpointDefinitions,
isQueryDefinition,
MutationDefinition,
QueryArgFrom,
QueryDefinition,
Expand Down Expand Up @@ -397,12 +398,14 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
const state = getState()
const requestState = state[reducerPath]?.queries?.[arg.queryCacheKey]
const fulfilledVal = requestState?.fulfilledTimeStamp
const endpointDefinition = endpointDefinitions[arg.endpointName]

// Don't retry a request that's currently in-flight
if (requestState?.status === 'pending') return false

// if this is forced, continue
if (isForcedQuery(arg, state)) return true
if (isQueryDefinition(endpointDefinition) && endpointDefinition?.forceRefetch?.({ endpointState: requestState, state })) return true

// Pull from the cache unless we explicitly force refetch or qualify based on time
if (fulfilledVal)
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export interface QueryExtraOptions<
* Not to be used. A query should not invalidate tags in the cache.
*/
invalidatesTags?: never
sideEffectForced?: (params: {getState(): RootState<any, any, string>, endpointState: QuerySubState<any>}) => boolean
forceRefetch?(params: {state: RootState<any, any, string>, endpointState?: QuerySubState<any>}): boolean
}

export type QueryDefinition<
Expand Down

0 comments on commit 0bb5f2b

Please sign in to comment.