Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MaxMotovilov/6f2ea3ff6615c85a806e022c2ed97c9d to your computer and use it in GitHub Desktop.
Save MaxMotovilov/6f2ea3ff6615c85a806e022c2ed97c9d to your computer and use it in GitHub Desktop.
Middleware & reducer decorator providing cross-cutting concern solution via selectors
export function injectRootState({getState}) {
return next => action => {
action = next(action);
(action.meta || (action.meta = {})).rootState = getState();
return action;
}
}
export function withSelectors(...selectors) {
const reducer = selectors.pop();
return (state, action) => {
const {meta: rootState} = action;
return reducer(state, action, ...selectors.map( sel => sel(rootState) ));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment