@@ -14,6 +14,8 @@ 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 type { Dispatch } from 'redux'
18
+ import { isNotNullish } from '../utils/isNotNullish'
17
19
18
20
declare module './module' {
19
21
export interface ApiEndpointQuery <
@@ -196,14 +198,14 @@ export function buildInitiate({
196
198
api : Api < any , EndpointDefinitions , any , any >
197
199
context : ApiContext < EndpointDefinitions >
198
200
} ) {
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 ( )
207
209
208
210
const {
209
211
unsubscribeQueryResult,
@@ -213,32 +215,80 @@ export function buildInitiate({
213
215
return {
214
216
buildInitiateQuery,
215
217
buildInitiateMutation,
218
+ getRunningQueryThunk,
219
+ getRunningMutationThunk,
220
+ getRunningQueriesThunk,
221
+ getRunningMutationsThunk,
216
222
getRunningOperationPromises,
217
- getRunningOperationPromise ,
223
+ removalWarning ,
218
224
}
219
225
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 ]
226
258
const queryCacheKey = serializeQueryArgs ( {
227
- queryArgs : argOrRequestId ,
259
+ queryArgs,
228
260
endpointDefinition,
229
261
endpointName,
230
262
} )
231
- return runningQueries [ queryCacheKey ]
232
- } else {
233
- return runningMutations [ argOrRequestId ]
263
+ return runningQueries . get ( dispatch ) ?. [ queryCacheKey ] as
264
+ | QueryActionCreatorResult < never >
265
+ | undefined
234
266
}
235
267
}
236
268
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 )
242
292
}
243
293
244
294
function middlewareWarning ( getState : ( ) => RootState < { } , string , string > ) {
@@ -302,7 +352,7 @@ Features like automatic cache collection, automatic refetching etc. will not be
302
352
303
353
const skippedSynchronously = stateAfter . requestId !== requestId
304
354
305
- const runningQuery = runningQueries [ queryCacheKey ]
355
+ const runningQuery = runningQueries . get ( dispatch ) ?. [ queryCacheKey ]
306
356
const selectFromState = ( ) => selector ( getState ( ) )
307
357
308
358
const statePromise : QueryActionCreatorResult < any > = Object . assign (
@@ -360,9 +410,15 @@ Features like automatic cache collection, automatic refetching etc. will not be
360
410
)
361
411
362
412
if ( ! runningQuery && ! skippedSynchronously && ! forceQueryFn ) {
363
- runningQueries [ queryCacheKey ] = statePromise
413
+ const running = runningQueries . get ( dispatch ) || { }
414
+ running [ queryCacheKey ] = statePromise
415
+ runningQueries . set ( dispatch , running )
416
+
364
417
statePromise . then ( ( ) => {
365
- delete runningQueries [ queryCacheKey ]
418
+ delete running [ queryCacheKey ]
419
+ if ( ! Object . keys ( running ) . length ) {
420
+ runningQueries . delete ( dispatch )
421
+ }
366
422
} )
367
423
}
368
424
@@ -404,15 +460,24 @@ Features like automatic cache collection, automatic refetching etc. will not be
404
460
reset,
405
461
} )
406
462
407
- runningMutations [ requestId ] = ret
463
+ const running = runningMutations . get ( dispatch ) || { }
464
+ runningMutations . set ( dispatch , running )
465
+ running [ requestId ] = ret
408
466
ret . then ( ( ) => {
409
- delete runningMutations [ requestId ]
467
+ delete running [ requestId ]
468
+ if ( ! Object . keys ( running ) . length ) {
469
+ runningMutations . delete ( dispatch )
470
+ }
410
471
} )
411
472
if ( fixedCacheKey ) {
412
- runningMutations [ fixedCacheKey ] = ret
473
+ running [ fixedCacheKey ] = ret
413
474
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
+ }
416
481
} )
417
482
}
418
483
0 commit comments