redux-dynamic-modules icon indicating copy to clipboard operation
redux-dynamic-modules copied to clipboard

Setting the a (static) first and last middleware

Open hagai-reuveni opened this issue 5 years ago • 5 comments

Hi,

Is it possible to make sure that a "static" middleware will always be the first/last in the middleware array?

In my case, I have an "action splitter" middleware that I use in order to send multiple actions in array. If the middleware gets an array, it dispatches each one of actions - (with 'next'). The last middleware is always a custom logger middleware and I need to make sure it's the last...

I would like to be able to add/remove middleware without having to worry about the index of the above middleware. Thanks, H.

hagai-reuveni avatar May 22 '19 13:05 hagai-reuveni

You can add a module with just the middleware while creating the store, that will ensure it is the first one in your chain of middle wares. And it can do following to achieve being the last one as well.

// customMiddleware.js
  // 1. action splitter code
 // 2. next (...)
 // 3. logger

Please let me know if I am missing something.

navneet-g avatar May 22 '19 14:05 navneet-g

Thanks @navneet-g .

I'll try to describe what I'm aiming for and hope it will be clearer:

  1. When the app starts I load the 'core redux-module' which has (among other) the splitter and the logger.
  2. After loading a certain path, another redux-module (feature redux-module) is being loaded and it has a middleware of its own.

Will the new feature middleware, that is being added to the store, will be pushed to the end of the existing middleware array or is it possible to leave the logger in the last index ?

Hope it's more clear now.


(A side note) - I'm not using redux only to read/write from/to the state. I'm using it as kind of a "messaging system" so there are a lot of messages (actions) being thrown and handled by different middlewares until data is ready to be written to the state.

hagai-reuveni avatar May 22 '19 18:05 hagai-reuveni

@navneet-g This could be supported by introducing a "finalMiddleware" option or something?

abettadapur avatar Jun 20 '19 02:06 abettadapur

@hagai-reuveni yes the feature middleware will be pushed to the end of the middle ware. There is no way to keep the middleware at last. But the middleware calls next so it has control over to be the last. Can you create a codesandbox to explain what scenario is not possible and we can see what is the best way to enable it.

navneet-g avatar Jun 27 '19 20:06 navneet-g

is this issue solved?

ignascepelis avatar Mar 02 '21 14:03 ignascepelis