12345678910111213141516171819202122232425 |
- import {cowValueModel} from './cow-value-model';
- export const subReducer = (key, reducer, ...otherKeys) => {
- const value = cowValueModel(key),
- otherParts = otherKeys.map(each => cowValueModel(each));
- return (state, action) => {
- let newSubState = reducer(value(state), action, ...otherParts.map(value => value(state)));
- if (typeof newSubState === "undefined") {
- throw new Error(`The '${key}' reducer must not return undefined.`);
- }
- return value(state, newSubState);
- };
- };
- export const composeReducers = (...reducers) => (state, action) =>
- reducers.reduce((x, r) => r(x, action), state);
- export const subMiddleware = (key, middleware) => {
- const cow = cowValueModel(key);
- return ({getState, ...rest}) => middleware({getState: () => cow(getState()), ...rest});
- };
- export const subEffex = (key, effects) =>
- effects.map(each => ({...each, effect: subMiddleware(key, each.effect)}));
|