@@ -22,7 +22,7 @@ import {
2222 type QueryDefinition ,
2323 type ResultTypeFrom ,
2424} from '../endpointDefinitions'
25- import { countObjectKeys , getOrInsert , isNotNullish } from '../utils'
25+ import { filterNullishValues } from '../utils'
2626import type {
2727 InfiniteData ,
2828 InfiniteQueryConfigOptions ,
@@ -271,17 +271,20 @@ export function buildInitiate({
271271 mutationThunk,
272272 api,
273273 context,
274- internalState ,
274+ getInternalState ,
275275} : {
276276 serializeQueryArgs : InternalSerializeQueryArgs
277277 queryThunk : QueryThunk
278278 infiniteQueryThunk : InfiniteQueryThunk < any >
279279 mutationThunk : MutationThunk
280280 api : Api < any , EndpointDefinitions , any , any >
281281 context : ApiContext < EndpointDefinitions >
282- internalState : InternalMiddlewareState
282+ getInternalState : ( dispatch : Dispatch ) => InternalMiddlewareState
283283} ) {
284- const { runningQueries, runningMutations } = internalState
284+ const getRunningQueries = ( dispatch : Dispatch ) =>
285+ getInternalState ( dispatch ) ?. runningQueries
286+ const getRunningMutations = ( dispatch : Dispatch ) =>
287+ getInternalState ( dispatch ) ?. runningMutations
285288
286289 const {
287290 unsubscribeQueryResult,
@@ -306,7 +309,7 @@ export function buildInitiate({
306309 endpointDefinition,
307310 endpointName,
308311 } )
309- return runningQueries . get ( dispatch ) ?. [ queryCacheKey ] as
312+ return getRunningQueries ( dispatch ) ?. get ( queryCacheKey ) as
310313 | QueryActionCreatorResult < never >
311314 | InfiniteQueryActionCreatorResult < never >
312315 | undefined
@@ -322,20 +325,20 @@ export function buildInitiate({
322325 fixedCacheKeyOrRequestId : string ,
323326 ) {
324327 return ( dispatch : Dispatch ) => {
325- return runningMutations . get ( dispatch ) ?. [ fixedCacheKeyOrRequestId ] as
328+ return getRunningMutations ( dispatch ) ?. get ( fixedCacheKeyOrRequestId ) as
326329 | MutationActionCreatorResult < never >
327330 | undefined
328331 }
329332 }
330333
331334 function getRunningQueriesThunk ( ) {
332335 return ( dispatch : Dispatch ) =>
333- Object . values ( runningQueries . get ( dispatch ) || { } ) . filter ( isNotNullish )
336+ filterNullishValues ( getRunningQueries ( dispatch ) )
334337 }
335338
336339 function getRunningMutationsThunk ( ) {
337340 return ( dispatch : Dispatch ) =>
338- Object . values ( runningMutations . get ( dispatch ) || { } ) . filter ( isNotNullish )
341+ filterNullishValues ( getRunningMutations ( dispatch ) )
339342 }
340343
341344 function middlewareWarning ( dispatch : Dispatch ) {
@@ -429,7 +432,7 @@ You must add the middleware for RTK-Query to function correctly!`,
429432
430433 const skippedSynchronously = stateAfter . requestId !== requestId
431434
432- const runningQuery = runningQueries . get ( dispatch ) ?. [ queryCacheKey ]
435+ const runningQuery = getRunningQueries ( dispatch ) ?. get ( queryCacheKey )
433436 const selectFromState = ( ) => selector ( getState ( ) )
434437
435438 const statePromise : AnyActionCreatorResult = Object . assign (
@@ -489,14 +492,11 @@ You must add the middleware for RTK-Query to function correctly!`,
489492 )
490493
491494 if ( ! runningQuery && ! skippedSynchronously && ! forceQueryFn ) {
492- const running = getOrInsert ( runningQueries , dispatch , { } )
493- running [ queryCacheKey ] = statePromise
495+ const runningQueries = getRunningQueries ( dispatch ) !
496+ runningQueries . set ( queryCacheKey , statePromise )
494497
495498 statePromise . then ( ( ) => {
496- delete running [ queryCacheKey ]
497- if ( ! countObjectKeys ( running ) ) {
498- runningQueries . delete ( dispatch )
499- }
499+ runningQueries . delete ( queryCacheKey )
500500 } )
501501 }
502502
@@ -559,23 +559,17 @@ You must add the middleware for RTK-Query to function correctly!`,
559559 reset,
560560 } )
561561
562- const running = runningMutations . get ( dispatch ) || { }
563- runningMutations . set ( dispatch , running )
564- running [ requestId ] = ret
562+ const runningMutations = getRunningMutations ( dispatch ) !
563+
564+ runningMutations . set ( requestId , ret )
565565 ret . then ( ( ) => {
566- delete running [ requestId ]
567- if ( ! countObjectKeys ( running ) ) {
568- runningMutations . delete ( dispatch )
569- }
566+ runningMutations . delete ( requestId )
570567 } )
571568 if ( fixedCacheKey ) {
572- running [ fixedCacheKey ] = ret
569+ runningMutations . set ( fixedCacheKey , ret )
573570 ret . then ( ( ) => {
574- if ( running [ fixedCacheKey ] === ret ) {
575- delete running [ fixedCacheKey ]
576- if ( ! countObjectKeys ( running ) ) {
577- runningMutations . delete ( dispatch )
578- }
571+ if ( runningMutations . get ( fixedCacheKey ) === ret ) {
572+ runningMutations . delete ( fixedCacheKey )
579573 }
580574 } )
581575 }
0 commit comments