@@ -14,6 +14,8 @@ import type { Api, ApiContext } from '../apiTypes'
1414import type { ApiEndpointQuery } from './module'
1515import type { BaseQueryError , QueryReturnValue } from '../baseQueryTypes'
1616import type { QueryResultSelectorResult } from './buildSelectors'
17+ import type { Dispatch } from 'redux'
18+ import { isNotNullish } from '../utils/isNotNullish'
1719
1820declare module './module' {
1921 export interface ApiEndpointQuery <
@@ -196,14 +198,14 @@ export function buildInitiate({
196198 api : Api < any , EndpointDefinitions , any , any >
197199 context : ApiContext < EndpointDefinitions >
198200} ) {
199- const runningQueries : Record <
200- string ,
201- QueryActionCreatorResult < any > | undefined
202- > = { }
203- const runningMutations : Record <
204- string ,
205- MutationActionCreatorResult < any > | undefined
206- > = { }
201+ const runningQueries : Map <
202+ Dispatch ,
203+ Record < string , QueryActionCreatorResult < any > | undefined >
204+ > = new Map ( )
205+ const runningMutations : Map <
206+ Dispatch ,
207+ Record < string , MutationActionCreatorResult < any > | undefined >
208+ > = new Map ( )
207209
208210 const {
209211 unsubscribeQueryResult,
@@ -213,32 +215,80 @@ export function buildInitiate({
213215 return {
214216 buildInitiateQuery,
215217 buildInitiateMutation,
218+ getRunningQueryThunk,
219+ getRunningMutationThunk,
220+ getRunningQueriesThunk,
221+ getRunningMutationsThunk,
216222 getRunningOperationPromises,
217- getRunningOperationPromise ,
223+ removalWarning ,
218224 }
219225
220- function getRunningOperationPromise (
221- endpointName : string ,
222- argOrRequestId : any
223- ) : any {
224- const endpointDefinition = context . endpointDefinitions [ endpointName ]
225- if ( endpointDefinition . type === DefinitionType . query ) {
226+ /** @deprecated to be removed in 2.0 */
227+ function removalWarning ( ) : never {
228+ throw new Error (
229+ `This method had to be removed due to a conceptual bug in RTK.
230+ Please see https://github.com/reduxjs/redux-toolkit/pull/2481 for details.
231+ See https://redux-toolkit.js.org/rtk-query/usage/server-side-rendering for new guidance on SSR.`
232+ )
233+ }
234+
235+ /** @deprecated to be removed in 2.0 */
236+ function getRunningOperationPromises ( ) {
237+ if (
238+ typeof process !== 'undefined' &&
239+ process . env . NODE_ENV === 'development'
240+ ) {
241+ removalWarning ( )
242+ } else {
243+ const extract = < T > (
244+ v : Map < Dispatch < AnyAction > , Record < string , T | undefined > >
245+ ) =>
246+ Array . from ( v . values ( ) ) . flatMap ( ( queriesForStore ) =>
247+ queriesForStore ? Object . values ( queriesForStore ) : [ ]
248+ )
249+ return [ ...extract ( runningQueries ) , ...extract ( runningMutations ) ] . filter (
250+ isNotNullish
251+ )
252+ }
253+ }
254+
255+ function getRunningQueryThunk ( endpointName : string , queryArgs : any ) {
256+ return ( dispatch : Dispatch ) => {
257+ const endpointDefinition = context . endpointDefinitions [ endpointName ]
226258 const queryCacheKey = serializeQueryArgs ( {
227- queryArgs : argOrRequestId ,
259+ queryArgs,
228260 endpointDefinition,
229261 endpointName,
230262 } )
231- return runningQueries [ queryCacheKey ]
232- } else {
233- return runningMutations [ argOrRequestId ]
263+ return runningQueries . get ( dispatch ) ?. [ queryCacheKey ] as
264+ | QueryActionCreatorResult < never >
265+ | undefined
234266 }
235267 }
236268
237- function getRunningOperationPromises ( ) {
238- return [
239- ...Object . values ( runningQueries ) ,
240- ...Object . values ( runningMutations ) ,
241- ] . filter ( < T > ( t : T | undefined ) : t is T => ! ! t )
269+ function getRunningMutationThunk (
270+ /**
271+ * this is only here to allow TS to infer the result type by input value
272+ * we could use it to validate the result, but it's probably not necessary
273+ */
274+ _endpointName : string ,
275+ fixedCacheKeyOrRequestId : string
276+ ) {
277+ return ( dispatch : Dispatch ) => {
278+ return runningMutations . get ( dispatch ) ?. [ fixedCacheKeyOrRequestId ] as
279+ | MutationActionCreatorResult < never >
280+ | undefined
281+ }
282+ }
283+
284+ function getRunningQueriesThunk ( ) {
285+ return ( dispatch : Dispatch ) =>
286+ Object . values ( runningQueries . get ( dispatch ) || { } ) . filter ( isNotNullish )
287+ }
288+
289+ function getRunningMutationsThunk ( ) {
290+ return ( dispatch : Dispatch ) =>
291+ Object . values ( runningMutations . get ( dispatch ) || { } ) . filter ( isNotNullish )
242292 }
243293
244294 function middlewareWarning ( getState : ( ) => RootState < { } , string , string > ) {
@@ -302,7 +352,7 @@ Features like automatic cache collection, automatic refetching etc. will not be
302352
303353 const skippedSynchronously = stateAfter . requestId !== requestId
304354
305- const runningQuery = runningQueries [ queryCacheKey ]
355+ const runningQuery = runningQueries . get ( dispatch ) ?. [ queryCacheKey ]
306356 const selectFromState = ( ) => selector ( getState ( ) )
307357
308358 const statePromise : QueryActionCreatorResult < any > = Object . assign (
@@ -360,9 +410,15 @@ Features like automatic cache collection, automatic refetching etc. will not be
360410 )
361411
362412 if ( ! runningQuery && ! skippedSynchronously && ! forceQueryFn ) {
363- runningQueries [ queryCacheKey ] = statePromise
413+ const running = runningQueries . get ( dispatch ) || { }
414+ running [ queryCacheKey ] = statePromise
415+ runningQueries . set ( dispatch , running )
416+
364417 statePromise . then ( ( ) => {
365- delete runningQueries [ queryCacheKey ]
418+ delete running [ queryCacheKey ]
419+ if ( ! Object . keys ( running ) . length ) {
420+ runningQueries . delete ( dispatch )
421+ }
366422 } )
367423 }
368424
@@ -404,15 +460,24 @@ Features like automatic cache collection, automatic refetching etc. will not be
404460 reset,
405461 } )
406462
407- runningMutations [ requestId ] = ret
463+ const running = runningMutations . get ( dispatch ) || { }
464+ runningMutations . set ( dispatch , running )
465+ running [ requestId ] = ret
408466 ret . then ( ( ) => {
409- delete runningMutations [ requestId ]
467+ delete running [ requestId ]
468+ if ( ! Object . keys ( running ) . length ) {
469+ runningMutations . delete ( dispatch )
470+ }
410471 } )
411472 if ( fixedCacheKey ) {
412- runningMutations [ fixedCacheKey ] = ret
473+ running [ fixedCacheKey ] = ret
413474 ret . then ( ( ) => {
414- if ( runningMutations [ fixedCacheKey ] === ret )
415- delete runningMutations [ fixedCacheKey ]
475+ if ( running [ fixedCacheKey ] === ret ) {
476+ delete running [ fixedCacheKey ]
477+ if ( ! Object . keys ( running ) . length ) {
478+ runningMutations . delete ( dispatch )
479+ }
480+ }
416481 } )
417482 }
418483
0 commit comments