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

Fix "running thunks" types and remove unnecessary RTKQ selectors #2856

Merged
merged 2 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
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
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