create a middleware which executes before thunk middleware
See original GitHub issueHi,
We are creating a middleware that stops unnecessary requests to the backend. But this is not stopping thunks from executing. My guess is thunk middleware is running before my custom middlewares.
Here is the code
const loaderHash: { [key: string]: boolean } = {};
export const debouseAPIRequests: Middleware = store => next => (action: {
type: string;
payload: any;
}) => {
if (action.type.indexOf("@thunk") === 0) {
const key =
action.type.replace(/(.+)(\((success|fail|start)\))/, "$1") +
JSON.stringify(action.payload);
if (action.type.endsWith("(start)")) {
if (!loaderHash[key]) {
loaderHash[key] = true;
next(action);
}
}
if (action.type.endsWith("(fail)") || action.type.endsWith("(success)")) {
loaderHash[key] = false;
next(action);
}
} else {
next(action);
}
};
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Middleware - Redux
It does a bit of trickery to make sure that if you call store.dispatch(action) from your middleware instead of next(action) , the action...
Read more >Async actions in bare Redux with Thunk or custom middleware
The returned function starts dispatching the synchronous action QUOTE_REQUESTED and executes fetch() to actually start the asynchronous HTTP ...
Read more >Redux Middleware – What it is and How to Build it from Scratch
Before the action is dispatched to the store, the middleware gets executed as we can see the action logged to the console. Because...
Read more >ExpressJS Series: How can I implement before and after ...
A lot of frameworks have been using this idea, copied from ExpressJS, since. ExpressJS is basically a double pass middleware. “Double pass ...
Read more >REDUX-THUNK MIDDLEWARE - Turing: Front-End Curriculum
Middleware provides a 3rd party between dispatching an action and the moment it reaches a reducer. It basically allows us to hook into...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ve added a test case so theoretically it should all be fine. Not to worry. 😊
@ctrlplusb Thanks mate! I have searched for that middleware in the codebase to give it a try. But I guess we have taken another approach for the solution. Do you want me to validate anything? I suppose it should work.