Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Add skip condition on endpoint definition #4813

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
@@ -523,7 +523,8 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
getPendingMeta() {
return { startedTimeStamp: Date.now(), [SHOULD_AUTOBATCH]: true }
},
condition(queryThunkArgs, { getState }) {
condition(queryThunkArgs, options) {
const { getState } = options
const state = getState()

const requestState =
@@ -551,6 +552,14 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
return true
}

if (
isQueryDefinition(endpointDefinition) &&
endpointDefinition?.skipCondition &&
endpointDefinition?.skipCondition(state)
) {
return false
}

if (
isQueryDefinition(endpointDefinition) &&
endpointDefinition?.forceRefetch?.({
3 changes: 3 additions & 0 deletions packages/toolkit/src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ import type {
UnwrapPromise,
} from './tsHelpers'
import { isNotNullish } from './utils'
import type { QueryThunkArg, ThunkApiMetaConfig } from './core/buildThunks'

const resultType = /* @__PURE__ */ Symbol()
const baseQuery = /* @__PURE__ */ Symbol()
@@ -349,6 +350,8 @@ export interface QueryExtraOptions<
*/
invalidatesTags?: never

skipCondition?: (state: RootState<any, string, ReducerPath>) => boolean

/**
* Can be provided to return a custom cache key value based on the query arguments.
*
11 changes: 10 additions & 1 deletion packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@ import { UNINITIALIZED_VALUE } from './constants'
import type { ReactHooksModuleOptions } from './module'
import { useStableQueryArgs } from './useSerializedStableValue'
import { useShallowStableValue } from './useShallowStableValue'
import { isQueryDefinition } from '../endpointDefinitions'

// Copy-pasted from React-Redux
const canUseDOM = () =>
@@ -1316,9 +1317,17 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
},
useQuery(arg, options) {
const querySubscriptionResults = useQuerySubscription(arg, options)
const store = useStore<RootState<Definitions, any, any>>()

const endpointDefinition = context.endpointDefinitions[name]
const shouldSkipFromCondition =
isQueryDefinition(endpointDefinition) &&
endpointDefinition?.skipCondition &&
endpointDefinition?.skipCondition(store.getState())

const queryStateResults = useQueryState(arg, {
selectFromResult:
arg === skipToken || options?.skip
arg === skipToken || options?.skip || shouldSkipFromCondition
? undefined
: noPendingQueryStateSelector,
...options,