12345678910111213141516171819202122232425262728 |
- import {atPath} from 'atpath';
- export const subReducer = (key, reducer, ...otherKeys) => {
- const atKey = atPath(key),
- otherParts = otherKeys.map(atPath);
- return (state, action) => {
- const newSubState = reducer(atKey.get(state), action, ...otherParts.map(eachSelector => eachSelector.get(state)));
- if (typeof newSubState === "undefined") {
- throw new Error(`The '${key}' reducer must not return undefined.`);
- }
- return atKey.put(newSubState)(state);
- };
- };
- export const composeReducers = (...reducers) => (state, action) =>
- reducers.reduce((x, r) => r(x, action), state);
- export const subMiddleware = (keyOrSelectorFn, middleware) => {
- const selector = typeof keyOrSelectorFn === "function" ? keyOrSelectorFn : atPath(keyOrSelectorFn).get;
- return (store, ...rest) => {
- const {getState} = store;
- return middleware({...store, getState: () => selector(getState())}, ...rest);
- };
- };
- export const subEffex = (keyOrSelectorFn, effects) =>
- effects.map(each => ({...each, effect: subMiddleware(keyOrSelectorFn, each.effect)}));
|