Skip to content

Instantly share code, notes, and snippets.

@modernserf
Created May 11, 2020 15:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save modernserf/e890a43887b1f7a80e27300481bffb4b to your computer and use it in GitHub Desktop.
Save modernserf/e890a43887b1f7a80e27300481bffb4b to your computer and use it in GitHub Desktop.
/*
A "handler" is a function with the shape `(action, store) => {}`
that is called after an action is dispatched to the Redux store,
executes side effects (e.g. logging, network calls, writing to local storage),
and potentially dispatches additional actions.
A "handler map" is an object with the shape `{[action type]: handler}`
that defines a mapping of action types with the handlers that should respond to them.
createHandlerMiddleware takes one or more handler maps
and returns a Redux middleware that routes each action to the appropriate handlers.
*/
const createHandlerMiddleware = (...handlerMaps) => {
const combinedHandlers = {};
for (const handlerMap of handlerMaps) {
for (const [actionType, handler] of Object.entries(handlerMap)) {
if (combinedHandlers[actionType]) {
combinedHandlers[actionType].push(handler);
} else {
combinedHandlers[actionType] = [handler];
}
}
}
return (store) => (next) => (action) => {
const actionProcessedByMiddleware = next(action);
if (!actionProcessedByMiddleware) return actionProcessedByMiddleware;
const handlersForAction = combinedHandlers[actionProcessedByMiddleware.type];
if (!handlersForAction) return actionProcessedByMiddleware;
handlersForAction.forEach((handler) => handler(actionProcessedByMiddleware, store));
return actionProcessedByMiddleware;
};
};
export default createHandlerMiddleware;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment