@@ -14,6 +14,7 @@ import type { Api, ApiContext } from '../apiTypes'
14
14
import type { ApiEndpointQuery } from './module'
15
15
import type { BaseQueryError , QueryReturnValue } from '../baseQueryTypes'
16
16
import type { QueryResultSelectorResult } from './buildSelectors'
17
+ import { Dispatch } from 'redux'
17
18
18
19
declare module './module' {
19
20
export interface ApiEndpointQuery <
@@ -196,14 +197,14 @@ export function buildInitiate({
196
197
api : Api < any , EndpointDefinitions , any , any >
197
198
context : ApiContext < EndpointDefinitions >
198
199
} ) {
199
- const runningQueries : Record <
200
- string ,
201
- QueryActionCreatorResult < any > | undefined
202
- > = { }
203
- const runningMutations : Record <
204
- string ,
205
- MutationActionCreatorResult < any > | undefined
206
- > = { }
200
+ const runningQueries : Map <
201
+ Dispatch ,
202
+ Record < string , QueryActionCreatorResult < any > | undefined >
203
+ > = new Map ( )
204
+ const runningMutations : Map <
205
+ Dispatch ,
206
+ Record < string , MutationActionCreatorResult < any > | undefined >
207
+ > = new Map ( )
207
208
208
209
const {
209
210
unsubscribeQueryResult,
@@ -220,25 +221,33 @@ export function buildInitiate({
220
221
function getRunningOperationPromise (
221
222
endpointName : string ,
222
223
argOrRequestId : any
223
- ) : any {
224
- const endpointDefinition = context . endpointDefinitions [ endpointName ]
225
- if ( endpointDefinition . type === DefinitionType . query ) {
226
- const queryCacheKey = serializeQueryArgs ( {
227
- queryArgs : argOrRequestId ,
228
- endpointDefinition,
229
- endpointName,
230
- } )
231
- return runningQueries [ queryCacheKey ]
232
- } else {
233
- return runningMutations [ argOrRequestId ]
224
+ ) : ThunkAction < any , any , any , AnyAction > {
225
+ return ( dispatch : Dispatch ) => {
226
+ const endpointDefinition = context . endpointDefinitions [ endpointName ]
227
+ if ( endpointDefinition . type === DefinitionType . query ) {
228
+ const queryCacheKey = serializeQueryArgs ( {
229
+ queryArgs : argOrRequestId ,
230
+ endpointDefinition,
231
+ endpointName,
232
+ } )
233
+ return runningQueries . get ( dispatch ) ?. [ queryCacheKey ]
234
+ } else {
235
+ return runningMutations . get ( dispatch ) ?. [ argOrRequestId ]
236
+ }
234
237
}
235
238
}
236
239
237
- function getRunningOperationPromises ( ) {
238
- return [
239
- ...Object . values ( runningQueries ) ,
240
- ...Object . values ( runningMutations ) ,
241
- ] . filter ( < T > ( t : T | undefined ) : t is T => ! ! t )
240
+ function getRunningOperationPromises ( ) : ThunkAction <
241
+ Promise < unknown > [ ] ,
242
+ any ,
243
+ any ,
244
+ AnyAction
245
+ > {
246
+ return ( dispatch : Dispatch ) =>
247
+ [
248
+ ...Object . values ( runningQueries . get ( dispatch ) || { } ) ,
249
+ ...Object . values ( runningMutations . get ( dispatch ) || { } ) ,
250
+ ] . filter ( < T > ( t : T | undefined ) : t is T => ! ! t )
242
251
}
243
252
244
253
function middlewareWarning ( getState : ( ) => RootState < { } , string , string > ) {
@@ -302,7 +311,7 @@ Features like automatic cache collection, automatic refetching etc. will not be
302
311
303
312
const skippedSynchronously = stateAfter . requestId !== requestId
304
313
305
- const runningQuery = runningQueries [ queryCacheKey ]
314
+ const runningQuery = runningQueries . get ( dispatch ) ?. [ queryCacheKey ]
306
315
const selectFromState = ( ) => selector ( getState ( ) )
307
316
308
317
const statePromise : QueryActionCreatorResult < any > = Object . assign (
@@ -360,9 +369,15 @@ Features like automatic cache collection, automatic refetching etc. will not be
360
369
)
361
370
362
371
if ( ! runningQuery && ! skippedSynchronously && ! forceQueryFn ) {
363
- runningQueries [ queryCacheKey ] = statePromise
372
+ const running = runningQueries . get ( dispatch ) || { }
373
+ running [ queryCacheKey ] = statePromise
374
+ runningQueries . set ( dispatch , running )
375
+
364
376
statePromise . then ( ( ) => {
365
- delete runningQueries [ queryCacheKey ]
377
+ delete running [ queryCacheKey ]
378
+ if ( ! Object . keys ( running ) . length ) {
379
+ runningQueries . delete ( dispatch )
380
+ }
366
381
} )
367
382
}
368
383
@@ -404,15 +419,24 @@ Features like automatic cache collection, automatic refetching etc. will not be
404
419
reset,
405
420
} )
406
421
407
- runningMutations [ requestId ] = ret
422
+ const running = runningMutations . get ( dispatch ) || { }
423
+ runningMutations . set ( dispatch , running )
424
+ running [ requestId ] = ret
408
425
ret . then ( ( ) => {
409
- delete runningMutations [ requestId ]
426
+ delete running [ requestId ]
427
+ if ( ! Object . keys ( running ) . length ) {
428
+ runningMutations . delete ( dispatch )
429
+ }
410
430
} )
411
431
if ( fixedCacheKey ) {
412
- runningMutations [ fixedCacheKey ] = ret
432
+ running [ fixedCacheKey ] = ret
413
433
ret . then ( ( ) => {
414
- if ( runningMutations [ fixedCacheKey ] === ret )
415
- delete runningMutations [ fixedCacheKey ]
434
+ if ( running [ fixedCacheKey ] === ret ) {
435
+ delete running [ fixedCacheKey ]
436
+ if ( ! Object . keys ( running ) . length ) {
437
+ runningMutations . delete ( dispatch )
438
+ }
439
+ }
416
440
} )
417
441
}
418
442
0 commit comments