node.js 698 B

1234567891011121314151617
  1. import {cowValueModel} from './cow-value-model';
  2. export const subReducer = (key, reducer, ...otherKeys) => {
  3. const {get: getValue, set: setValue} = cowValueModel(key),
  4. otherParts = otherKeys.map(each => cowValueModel(each).get);
  5. return (state, action) => {
  6. let newSubState = reducer(getValue(state), action, ...otherParts.map(getValue => getValue(state)));
  7. if (typeof newSubState === "undefined") {
  8. throw new Error(`The '${key}' reducer must not return undefined.`);
  9. }
  10. return setValue(state, newSubState);
  11. };
  12. };
  13. export const composeReducers = (...reducers) => (state, action) =>
  14. reducers.reduce((x, r) => r(x, action), state);