-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2599 from reduxjs/feature/batch-rtkq-rejections
- Loading branch information
Showing
11 changed files
with
281 additions
and
60 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
packages/toolkit/src/query/core/buildMiddleware/batchActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { QueryThunk, RejectedAction } from '../buildThunks' | ||
import type { SubMiddlewareBuilder } from './types' | ||
|
||
// Copied from https://github.com/feross/queue-microtask | ||
let promise: Promise<any> | ||
const queueMicrotaskShim = | ||
typeof queueMicrotask === 'function' | ||
? queueMicrotask.bind(typeof window !== 'undefined' ? window : global) | ||
: // reuse resolved promise, and allocate it lazily | ||
(cb: () => void) => | ||
(promise || (promise = Promise.resolve())).then(cb).catch((err: any) => | ||
setTimeout(() => { | ||
throw err | ||
}, 0) | ||
) | ||
|
||
export const build: SubMiddlewareBuilder = ({ | ||
api, | ||
context: { apiUid }, | ||
queryThunk, | ||
reducerPath, | ||
}) => { | ||
return (mwApi) => { | ||
let abortedQueryActionsQueue: RejectedAction<QueryThunk, any>[] = [] | ||
let dispatchQueued = false | ||
|
||
return (next) => (action) => { | ||
if (queryThunk.rejected.match(action)) { | ||
const { condition, arg } = action.meta | ||
|
||
if (condition && arg.subscribe) { | ||
// request was aborted due to condition (another query already running) | ||
// _Don't_ dispatch right away - queue it for a debounced grouped dispatch | ||
abortedQueryActionsQueue.push(action) | ||
|
||
if (!dispatchQueued) { | ||
queueMicrotaskShim(() => { | ||
mwApi.dispatch( | ||
api.internalActions.subscriptionRequestsRejected( | ||
abortedQueryActionsQueue | ||
) | ||
) | ||
abortedQueryActionsQueue = [] | ||
}) | ||
dispatchQueued = true | ||
} | ||
// _Don't_ let the action reach the reducers now! | ||
return | ||
} | ||
} | ||
|
||
const result = next(action) | ||
|
||
return result | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.