Skip to content

Commit

Permalink
Merge pull request #2856 from reduxjs/feature/1.9-rc-tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson authored Nov 1, 2022
2 parents 4c32e29 + e672cd8 commit 38d65c0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
46 changes: 24 additions & 22 deletions packages/toolkit/src/query/core/buildSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ export function buildSelectors<
}) {
type RootState = _RootState<Definitions, string, string>

const selectSkippedQuery = (state: RootState) => defaultQuerySubState
const selectSkippedMutation = (state: RootState) => defaultMutationSubState

return { buildQuerySelector, buildMutationSelector, selectInvalidatedBy }

function withRequestFlags<T extends { status: QueryStatus }>(
Expand Down Expand Up @@ -159,20 +162,18 @@ export function buildSelectors<
endpointDefinition: QueryDefinition<any, any, any, any>
) {
return ((queryArgs: any) => {
const selectQuerySubState = createSelector(
selectInternalState,
(internalState) =>
(queryArgs === skipToken
? undefined
: internalState?.queries?.[
serializeQueryArgs({
queryArgs,
endpointDefinition,
endpointName,
})
]) ?? defaultQuerySubState
)
return createSelector(selectQuerySubState, withRequestFlags)
const serializedArgs = serializeQueryArgs({
queryArgs,
endpointDefinition,
endpointName,
})
const selectQuerySubstate = (state: RootState) =>
selectInternalState(state)?.queries?.[serializedArgs] ??
defaultQuerySubState
const finalSelectQuerySubState =
queryArgs === skipToken ? selectSkippedQuery : selectQuerySubstate

return createSelector(finalSelectQuerySubState, withRequestFlags)
}) as QueryResultSelectorFactory<any, RootState>
}

Expand All @@ -184,14 +185,15 @@ export function buildSelectors<
} else {
mutationId = id
}
const selectMutationSubstate = createSelector(
selectInternalState,
(internalState) =>
(mutationId === skipToken
? undefined
: internalState?.mutations?.[mutationId]) ?? defaultMutationSubState
)
return createSelector(selectMutationSubstate, withRequestFlags)
const selectMutationSubstate = (state: RootState) =>
selectInternalState(state)?.mutations?.[mutationId as string] ??
defaultMutationSubState
const finalSelectMutationSubstate =
mutationId === skipToken
? selectSkippedMutation
: selectMutationSubstate

return createSelector(finalSelectMutationSubstate, withRequestFlags)
}) as MutationResultSelectorFactory<any, RootState>
}

Expand Down
3 changes: 1 addition & 2 deletions packages/toolkit/src/query/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ export type CoreModule =
| ReferenceQueryLifecycle
| ReferenceCacheCollection

interface ThunkWithReturnValue<T>
extends ThunkAction<T | undefined, any, any, AnyAction> {}
interface ThunkWithReturnValue<T> extends ThunkAction<T, any, any, AnyAction> {}

declare module '../apiTypes' {
export interface ApiModules<
Expand Down
7 changes: 5 additions & 2 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({

const useQueryState: UseQueryState<any> = (
arg: any,
{ skip = false, selectFromResult = defaultQueryStateSelector } = {}
{ skip = false, selectFromResult } = {}
) => {
const { select } = api.endpoints[name] as ApiEndpointQuery<
QueryDefinition<any, any, any, any, any>,
Expand Down Expand Up @@ -916,7 +916,10 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
)

const querySelector: Selector<ApiRootState, any, [any]> = useMemo(
() => createSelector([selectDefaultResult], selectFromResult),
() =>
selectFromResult
? createSelector([selectDefaultResult], selectFromResult)
: selectDefaultResult,
[selectDefaultResult, selectFromResult]
)

Expand Down

0 comments on commit 38d65c0

Please sign in to comment.