Skip to content

Commit a7fd57c

Browse files
phryneasmarkerikson
authored andcommitted
scope getRunningOperationPromise to store instance
1 parent 76cedeb commit a7fd57c

File tree

2 files changed

+74
-37
lines changed

2 files changed

+74
-37
lines changed

packages/toolkit/src/query/core/buildInitiate.ts

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { Api, ApiContext } from '../apiTypes'
1414
import type { ApiEndpointQuery } from './module'
1515
import type { BaseQueryError, QueryReturnValue } from '../baseQueryTypes'
1616
import type { QueryResultSelectorResult } from './buildSelectors'
17+
import { Dispatch } from 'redux'
1718

1819
declare module './module' {
1920
export interface ApiEndpointQuery<
@@ -196,14 +197,14 @@ export function buildInitiate({
196197
api: Api<any, EndpointDefinitions, any, any>
197198
context: ApiContext<EndpointDefinitions>
198199
}) {
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()
207208

208209
const {
209210
unsubscribeQueryResult,
@@ -220,25 +221,33 @@ export function buildInitiate({
220221
function getRunningOperationPromise(
221222
endpointName: string,
222223
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+
}
234237
}
235238
}
236239

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)
242251
}
243252

244253
function middlewareWarning(getState: () => RootState<{}, string, string>) {
@@ -302,7 +311,7 @@ Features like automatic cache collection, automatic refetching etc. will not be
302311

303312
const skippedSynchronously = stateAfter.requestId !== requestId
304313

305-
const runningQuery = runningQueries[queryCacheKey]
314+
const runningQuery = runningQueries.get(dispatch)?.[queryCacheKey]
306315
const selectFromState = () => selector(getState())
307316

308317
const statePromise: QueryActionCreatorResult<any> = Object.assign(
@@ -360,9 +369,15 @@ Features like automatic cache collection, automatic refetching etc. will not be
360369
)
361370

362371
if (!runningQuery && !skippedSynchronously && !forceQueryFn) {
363-
runningQueries[queryCacheKey] = statePromise
372+
const running = runningQueries.get(dispatch) || {}
373+
running[queryCacheKey] = statePromise
374+
runningQueries.set(dispatch, running)
375+
364376
statePromise.then(() => {
365-
delete runningQueries[queryCacheKey]
377+
delete running[queryCacheKey]
378+
if (!Object.keys(running).length) {
379+
runningQueries.delete(dispatch)
380+
}
366381
})
367382
}
368383

@@ -404,15 +419,24 @@ Features like automatic cache collection, automatic refetching etc. will not be
404419
reset,
405420
})
406421

407-
runningMutations[requestId] = ret
422+
const running = runningMutations.get(dispatch) || {}
423+
runningMutations.set(dispatch, running)
424+
running[requestId] = ret
408425
ret.then(() => {
409-
delete runningMutations[requestId]
426+
delete running[requestId]
427+
if (!Object.keys(running).length) {
428+
runningMutations.delete(dispatch)
429+
}
410430
})
411431
if (fixedCacheKey) {
412-
runningMutations[fixedCacheKey] = ret
432+
running[fixedCacheKey] = ret
413433
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+
}
416440
})
417441
}
418442

packages/toolkit/src/query/core/module.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ declare module '../apiTypes' {
142142
* Useful for SSR scenarios to await everything triggered in any way,
143143
* including via hook calls, or manually dispatching `initiate` actions.
144144
*/
145-
getRunningOperationPromises: () => Array<Promise<unknown>>
145+
getRunningOperationPromises: () => ThunkAction<
146+
Array<Promise<unknown>>,
147+
any,
148+
any,
149+
AnyAction
150+
>
146151
/**
147152
* If a promise is running for a given endpoint name + argument combination,
148153
* returns that promise. Otherwise, returns `undefined`.
@@ -152,21 +157,29 @@ declare module '../apiTypes' {
152157
getRunningOperationPromise<EndpointName extends QueryKeys<Definitions>>(
153158
endpointName: EndpointName,
154159
args: QueryArgFrom<Definitions[EndpointName]>
155-
):
160+
): ThunkAction<
156161
| QueryActionCreatorResult<
157162
Definitions[EndpointName] & { type: 'query' }
158163
>
159-
| undefined
164+
| undefined,
165+
any,
166+
any,
167+
AnyAction
168+
>
160169
getRunningOperationPromise<
161170
EndpointName extends MutationKeys<Definitions>
162171
>(
163172
endpointName: EndpointName,
164173
fixedCacheKeyOrRequestId: string
165-
):
174+
): ThunkAction<
166175
| MutationActionCreatorResult<
167176
Definitions[EndpointName] & { type: 'mutation' }
168177
>
169-
| undefined
178+
| undefined,
179+
any,
180+
any,
181+
AnyAction
182+
>
170183

171184
/**
172185
* A Redux thunk that can be used to manually trigger pre-fetching of data.

0 commit comments

Comments
 (0)