question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

create a middleware which executes before thunk middleware

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ctrlplusbcommented, Apr 13, 2020

I’ve added a test case so theoretically it should all be fine. Not to worry. 😊

0reactions
alpatescommented, Apr 13, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found