import {decow, deget} from './cow-value-model'; export const subReducer = (key, reducer, ...otherKeys) => { const selector = deget(key), modifier = decow(key), otherParts = otherKeys.map(deget); return (state, action) => { let newSubState = reducer(selector(state), action, ...otherParts.map(eachSelector => eachSelector(state))); if (typeof newSubState === "undefined") { throw new Error(`The '${key}' reducer must not return undefined.`); } return modifier(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 : deget(keyOrSelectorFn); 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)}));