Skip to content

Commit

Permalink
add api.getRunningQueryPromise
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Sep 17, 2021
1 parent cc73ab0 commit 262094e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
45 changes: 31 additions & 14 deletions packages/toolkit/src/query/core/buildInitiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { QueryThunk, MutationThunk } from './buildThunks'
import type { AnyAction, ThunkAction, SerializedError } from '@reduxjs/toolkit'
import type { QuerySubState, SubscriptionOptions, RootState } from './apiState'
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
import type { Api } from '../apiTypes'
import type { Api, ApiContext } from '../apiTypes'
import type { ApiEndpointQuery } from './module'
import type { BaseQueryError } from '../baseQueryTypes'

Expand Down Expand Up @@ -177,14 +177,22 @@ export function buildInitiate({
queryThunk,
mutationThunk,
api,
context,
}: {
serializeQueryArgs: InternalSerializeQueryArgs
queryThunk: QueryThunk
mutationThunk: MutationThunk
api: Api<any, EndpointDefinitions, any, any>
context: ApiContext<EndpointDefinitions>
}) {
const runningQueries: Record<string, Promise<unknown> | undefined> = {}
const runningMutations: Record<string, Promise<unknown> | undefined> = {}
const runningQueries: Record<
string,
QueryActionCreatorResult<any> | undefined
> = {}
const runningMutations: Record<
string,
MutationActionCreatorResult<any> | undefined
> = {}

const {
unsubscribeQueryResult,
Expand All @@ -195,12 +203,23 @@ export function buildInitiate({
buildInitiateQuery,
buildInitiateMutation,
getRunningOperationPromises,
getRunningQueryPromise,
}

function getRunningOperationPromises() {
return Object.values(runningQueries)
.concat(Object.values(runningMutations))
.filter((t): t is Promise<unknown> => !!t)
return [
...Object.values(runningQueries),
...Object.values(runningMutations),
].filter(<T>(t: T | undefined): t is T => !!t)
}

function getRunningQueryPromise(endpointName: string, queryArgs: any) {
const queryCacheKey = serializeQueryArgs({
queryArgs,
endpointDefinition: context.endpointDefinitions[endpointName],
endpointName,
})
return runningQueries[queryCacheKey]
}

function middlewareWarning(getState: () => RootState<{}, string, string>) {
Expand Down Expand Up @@ -243,7 +262,7 @@ Features like automatic cache collection, automatic refetching etc. will not be
const thunkResult = dispatch(thunk)
middlewareWarning(getState)
const { requestId, abort } = thunkResult
const statePromise = Object.assign(
const statePromise: QueryActionCreatorResult<any> = Object.assign(
Promise.all([runningQueries[queryCacheKey], thunkResult]).then(() =>
(api.endpoints[endpointName] as ApiEndpointQuery<any, any>).select(
arg
Expand Down Expand Up @@ -287,8 +306,8 @@ Features like automatic cache collection, automatic refetching etc. will not be
})

if (!runningQueries[queryCacheKey]) {
runningQueries[queryCacheKey] = thunkResult
thunkResult.then(() => {
runningQueries[queryCacheKey] = statePromise
statePromise.then(() => {
delete runningQueries[queryCacheKey]
})
}
Expand Down Expand Up @@ -325,13 +344,11 @@ Features like automatic cache collection, automatic refetching etc. will not be
if (track) dispatch(unsubscribeMutationResult({ requestId }))
},
})
ret.then(() => {
ret.resolved = true
})

runningMutations[requestId] = thunkResult
thunkResult.then(() => {
runningMutations[requestId] = ret
ret.then(() => {
delete runningMutations[requestId]
ret.resolved = true
})

return ret
Expand Down
9 changes: 8 additions & 1 deletion packages/toolkit/src/query/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { onFocus, onFocusLost, onOnline, onOffline } from './setupListeners'
import { buildSlice } from './buildSlice'
import { buildMiddleware } from './buildMiddleware'
import { buildSelectors } from './buildSelectors'
import type { QueryActionCreatorResult } from './buildInitiate'
import { buildInitiate } from './buildInitiate'
import { assertCast, safeAssign } from '../tsHelpers'
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
Expand Down Expand Up @@ -264,6 +265,10 @@ declare module '../apiTypes' {
: never
}
getRunningOperationPromises: () => Array<Promise<unknown>>
getRunningQueryPromise: <EndpointName extends QueryKeys<Definitions>>(
endpointName: EndpointName,
args: QueryArgFrom<Definitions[EndpointName]>
) => QueryActionCreatorResult<Definitions[EndpointName]> | undefined
}
}
}
Expand Down Expand Up @@ -443,14 +448,16 @@ export const coreModule = (): Module<CoreModule> => ({
buildInitiateQuery,
buildInitiateMutation,
getRunningOperationPromises,
getRunningQueryPromise,
} = buildInitiate({
queryThunk,
mutationThunk,
api,
serializeQueryArgs: serializeQueryArgs as any,
context,
})

api.getRunningOperationPromises = getRunningOperationPromises
safeAssign(api, { getRunningOperationPromises, getRunningQueryPromise })

return {
name: coreModuleName,
Expand Down

0 comments on commit 262094e

Please sign in to comment.