sac.js 1.1 KB

12345678910111213141516171819202122232425262728
  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 = (keyOrSelectorFn, middleware) => {
  16. const cow = typeof keyOrSelectorFn === "function" ? keyOrSelectorFn : cowValueModel(keyOrSelectorFn);
  17. return (store, ...rest) => {
  18. const {getState} = store;
  19. return middleware({...store, getState: () => cow(getState())}, ...rest);
  20. };
  21. };
  22. export const subEffex = (keyOrSelectorFn, effects) =>
  23. effects.map(each => ({...each, effect: subMiddleware(keyOrSelectorFn, each.effect)}));