Created
April 16, 2020 02:27
-
-
Save MaxMotovilov/6f2ea3ff6615c85a806e022c2ed97c9d to your computer and use it in GitHub Desktop.
Middleware & reducer decorator providing cross-cutting concern solution via selectors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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