Skip to content

Commit 245f6a0

Browse files
wesenmarkerikson
authored andcommitted
🎨 Keep track of all runningQueries
1 parent a5e6587 commit 245f6a0

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

packages/toolkit/src/query/core/buildInitiate.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type {
22
EndpointDefinitions,
3-
QueryDefinition,
43
MutationDefinition,
54
QueryArgFrom,
5+
QueryDefinition,
66
ResultTypeFrom,
77
} from '../endpointDefinitions'
88
import { DefinitionType } from '../endpointDefinitions'
9-
import type { QueryThunk, MutationThunk } from './buildThunks'
10-
import type { AnyAction, ThunkAction, SerializedError } from '@reduxjs/toolkit'
11-
import type { SubscriptionOptions, RootState } from './apiState'
9+
import type { MutationThunk, QueryThunk } from './buildThunks'
10+
import type { AnyAction, SerializedError, ThunkAction } from '@reduxjs/toolkit'
11+
import type { RootState, SubscriptionOptions } from './apiState'
1212
import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
1313
import type { Api, ApiContext } from '../apiTypes'
1414
import type { ApiEndpointQuery } from './module'
@@ -191,10 +191,12 @@ export function buildInitiate({
191191
api: Api<any, EndpointDefinitions, any, any>
192192
context: ApiContext<EndpointDefinitions>
193193
}) {
194+
// keep track of running queries by id
194195
const runningQueries: Record<
195196
string,
196-
QueryActionCreatorResult<any> | undefined
197+
Record<string, QueryActionCreatorResult<any>>
197198
> = {}
199+
// keep track of running mutations by id
198200
const runningMutations: Record<
199201
string,
200202
MutationActionCreatorResult<any> | undefined
@@ -223,15 +225,20 @@ export function buildInitiate({
223225
endpointDefinition,
224226
endpointName,
225227
})
226-
return runningQueries[queryCacheKey]
228+
// 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
229+
return Promise.all(
230+
Object.values(runningQueries[queryCacheKey] || {})
231+
).then((x) => x[0])
227232
} else {
228233
return runningMutations[argOrRequestId]
229234
}
230235
}
231236

232237
function getRunningOperationPromises() {
233238
return [
234-
...Object.values(runningQueries),
239+
...Object.values(runningQueries)
240+
.map((x) => Object.values(x))
241+
.reduce((x, y) => x.concat(y)),
235242
...Object.values(runningMutations),
236243
].filter(<T>(t: T | undefined): t is T => !!t)
237244
}
@@ -279,12 +286,15 @@ Features like automatic cache collection, automatic refetching etc. will not be
279286

280287
const { requestId, abort } = thunkResult
281288

289+
const prevThunks = Object.values(runningQueries?.[queryCacheKey] || {})
290+
291+
let promises: Promise<any>[] = [...prevThunks, thunkResult]
282292
const statePromise: QueryActionCreatorResult<any> = Object.assign(
283-
Promise.all([runningQueries[queryCacheKey], thunkResult]).then(() =>
284-
(api.endpoints[endpointName] as ApiEndpointQuery<any, any>).select(
285-
arg
286-
)(getState())
287-
),
293+
Promise.all(promises).then(() => {
294+
return (
295+
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
296+
).select(arg)(getState())
297+
}),
288298
{
289299
arg,
290300
requestId,
@@ -328,13 +338,15 @@ Features like automatic cache collection, automatic refetching etc. will not be
328338
}
329339
)
330340

331-
if (!runningQueries[queryCacheKey]) {
332-
runningQueries[queryCacheKey] = statePromise
333-
statePromise.then(() => {
334-
delete runningQueries[queryCacheKey]
335-
})
341+
if (!runningQueries.hasOwnProperty(queryCacheKey)) {
342+
runningQueries[queryCacheKey] = {}
336343
}
337344

345+
runningQueries[queryCacheKey][requestId] = statePromise
346+
statePromise.then(() => {
347+
delete runningQueries?.[queryCacheKey]?.[requestId]
348+
})
349+
338350
return statePromise
339351
}
340352
return queryAction

0 commit comments

Comments
 (0)