Skip to content

Commit

Permalink
🎨 Keep track of all runningQueries
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen authored and markerikson committed Aug 16, 2022
1 parent a5e6587 commit 245f6a0
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions packages/toolkit/src/query/core/buildInitiate.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {
EndpointDefinitions,
QueryDefinition,
MutationDefinition,
QueryArgFrom,
QueryDefinition,
ResultTypeFrom,
} from '../endpointDefinitions'
import { DefinitionType } from '../endpointDefinitions'
import type { QueryThunk, MutationThunk } from './buildThunks'
import type { AnyAction, ThunkAction, SerializedError } from '@reduxjs/toolkit'
import type { SubscriptionOptions, RootState } from './apiState'
import type { MutationThunk, QueryThunk } from './buildThunks'
import type { AnyAction, SerializedError, ThunkAction } from '@reduxjs/toolkit'
import type { RootState, SubscriptionOptions } from './apiState'
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
import type { Api, ApiContext } from '../apiTypes'
import type { ApiEndpointQuery } from './module'
Expand Down Expand Up @@ -191,10 +191,12 @@ export function buildInitiate({
api: Api<any, EndpointDefinitions, any, any>
context: ApiContext<EndpointDefinitions>
}) {
// keep track of running queries by id
const runningQueries: Record<
string,
QueryActionCreatorResult<any> | undefined
Record<string, QueryActionCreatorResult<any>>
> = {}
// keep track of running mutations by id
const runningMutations: Record<
string,
MutationActionCreatorResult<any> | undefined
Expand Down Expand Up @@ -223,15 +225,20 @@ export function buildInitiate({
endpointDefinition,
endpointName,
})
return runningQueries[queryCacheKey]
// TODO(manuel) this is not really what we want, because we don't know which of those thunks will actually resolve to the correct result
return Promise.all(
Object.values(runningQueries[queryCacheKey] || {})
).then((x) => x[0])
} else {
return runningMutations[argOrRequestId]
}
}

function getRunningOperationPromises() {
return [
...Object.values(runningQueries),
...Object.values(runningQueries)
.map((x) => Object.values(x))
.reduce((x, y) => x.concat(y)),
...Object.values(runningMutations),
].filter(<T>(t: T | undefined): t is T => !!t)
}
Expand Down Expand Up @@ -279,12 +286,15 @@ Features like automatic cache collection, automatic refetching etc. will not be

const { requestId, abort } = thunkResult

const prevThunks = Object.values(runningQueries?.[queryCacheKey] || {})

let promises: Promise<any>[] = [...prevThunks, thunkResult]
const statePromise: QueryActionCreatorResult<any> = Object.assign(
Promise.all([runningQueries[queryCacheKey], thunkResult]).then(() =>
(api.endpoints[endpointName] as ApiEndpointQuery<any, any>).select(
arg
)(getState())
),
Promise.all(promises).then(() => {
return (
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
).select(arg)(getState())
}),
{
arg,
requestId,
Expand Down Expand Up @@ -328,13 +338,15 @@ Features like automatic cache collection, automatic refetching etc. will not be
}
)

if (!runningQueries[queryCacheKey]) {
runningQueries[queryCacheKey] = statePromise
statePromise.then(() => {
delete runningQueries[queryCacheKey]
})
if (!runningQueries.hasOwnProperty(queryCacheKey)) {
runningQueries[queryCacheKey] = {}
}

runningQueries[queryCacheKey][requestId] = statePromise
statePromise.then(() => {
delete runningQueries?.[queryCacheKey]?.[requestId]
})

return statePromise
}
return queryAction
Expand Down

0 comments on commit 245f6a0

Please sign in to comment.