redux-dynamic-modules
redux-dynamic-modules copied to clipboard
Setting the a (static) first and last middleware
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.
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.
Thanks @navneet-g .
I'll try to describe what I'm aiming for and hope it will be clearer:
- When the app starts I load the 'core redux-module' which has (among other) the splitter and the logger.
- 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.
@navneet-g This could be supported by introducing a "finalMiddleware" option or something?
@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.
is this issue solved?