sac.js 963 B

12345678910111213141516171819202122232425
  1. import {cowValueModel} from './cow-value-model';
  2. export const subReducer = (key, reducer, ...otherKeys) => {
  3. const value = cowValueModel(key),
  4. otherParts = otherKeys.map(each => cowValueModel(each));
  5. return (state, action) => {
  6. let newSubState = reducer(value(state), action, ...otherParts.map(value => value(state)));
  7. if (typeof newSubState === "undefined") {
  8. throw new Error(`The '${key}' reducer must not return undefined.`);
  9. }
  10. return value(state, newSubState);
  11. };
  12. };
  13. export const composeReducers = (...reducers) => (state, action) =>
  14. reducers.reduce((x, r) => r(x, action), state);
  15. export const subMiddleware = (key, middleware) => {
  16. const cow = cowValueModel(key);
  17. return ({getState, ...rest}) => middleware({getState: () => cow(getState()), ...rest});
  18. };
  19. export const subEffex = (key, effects) =>
  20. effects.map(each => ({...each, effect: subMiddleware(key, each.effect)}));