|
1 | 1 | import type {
|
2 | 2 | EndpointDefinitions,
|
3 |
| - QueryDefinition, |
4 | 3 | MutationDefinition,
|
5 | 4 | QueryArgFrom,
|
| 5 | + QueryDefinition, |
6 | 6 | ResultTypeFrom,
|
7 | 7 | } from '../endpointDefinitions'
|
8 | 8 | 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' |
12 | 12 | import type { InternalSerializeQueryArgs } from '../defaultSerializeQueryArgs'
|
13 | 13 | import type { Api, ApiContext } from '../apiTypes'
|
14 | 14 | import type { ApiEndpointQuery } from './module'
|
@@ -191,10 +191,12 @@ export function buildInitiate({
|
191 | 191 | api: Api<any, EndpointDefinitions, any, any>
|
192 | 192 | context: ApiContext<EndpointDefinitions>
|
193 | 193 | }) {
|
| 194 | + // keep track of running queries by id |
194 | 195 | const runningQueries: Record<
|
195 | 196 | string,
|
196 |
| - QueryActionCreatorResult<any> | undefined |
| 197 | + Record<string, QueryActionCreatorResult<any>> |
197 | 198 | > = {}
|
| 199 | + // keep track of running mutations by id |
198 | 200 | const runningMutations: Record<
|
199 | 201 | string,
|
200 | 202 | MutationActionCreatorResult<any> | undefined
|
@@ -223,15 +225,20 @@ export function buildInitiate({
|
223 | 225 | endpointDefinition,
|
224 | 226 | endpointName,
|
225 | 227 | })
|
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]) |
227 | 232 | } else {
|
228 | 233 | return runningMutations[argOrRequestId]
|
229 | 234 | }
|
230 | 235 | }
|
231 | 236 |
|
232 | 237 | function getRunningOperationPromises() {
|
233 | 238 | return [
|
234 |
| - ...Object.values(runningQueries), |
| 239 | + ...Object.values(runningQueries) |
| 240 | + .map((x) => Object.values(x)) |
| 241 | + .reduce((x, y) => x.concat(y)), |
235 | 242 | ...Object.values(runningMutations),
|
236 | 243 | ].filter(<T>(t: T | undefined): t is T => !!t)
|
237 | 244 | }
|
@@ -279,12 +286,15 @@ Features like automatic cache collection, automatic refetching etc. will not be
|
279 | 286 |
|
280 | 287 | const { requestId, abort } = thunkResult
|
281 | 288 |
|
| 289 | + const prevThunks = Object.values(runningQueries?.[queryCacheKey] || {}) |
| 290 | + |
| 291 | + let promises: Promise<any>[] = [...prevThunks, thunkResult] |
282 | 292 | 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 | + }), |
288 | 298 | {
|
289 | 299 | arg,
|
290 | 300 | requestId,
|
@@ -328,13 +338,15 @@ Features like automatic cache collection, automatic refetching etc. will not be
|
328 | 338 | }
|
329 | 339 | )
|
330 | 340 |
|
331 |
| - if (!runningQueries[queryCacheKey]) { |
332 |
| - runningQueries[queryCacheKey] = statePromise |
333 |
| - statePromise.then(() => { |
334 |
| - delete runningQueries[queryCacheKey] |
335 |
| - }) |
| 341 | + if (!runningQueries.hasOwnProperty(queryCacheKey)) { |
| 342 | + runningQueries[queryCacheKey] = {} |
336 | 343 | }
|
337 | 344 |
|
| 345 | + runningQueries[queryCacheKey][requestId] = statePromise |
| 346 | + statePromise.then(() => { |
| 347 | + delete runningQueries?.[queryCacheKey]?.[requestId] |
| 348 | + }) |
| 349 | + |
338 | 350 | return statePromise
|
339 | 351 | }
|
340 | 352 | return queryAction
|
|
0 commit comments