dva-core
Version:
The core lightweight library for dva, based on redux and redux-saga.
29 lines (23 loc) • 785 B
JavaScript
import invariant from 'invariant';
function identify(value) {
return value;
}
function handleAction(actionType, reducer = identify) {
return (state, action) => {
const { type } = action;
invariant(type, 'dispatch: action should be a plain Object with type');
if (actionType === type) {
return reducer(state, action);
}
return state;
};
}
function reduceReducers(...reducers) {
return (previous, current) => reducers.reduce((p, r) => r(p, current), previous);
}
function handleActions(handlers, defaultState) {
const reducers = Object.keys(handlers).map(type => handleAction(type, handlers[type]));
const reducer = reduceReducers(...reducers);
return (state = defaultState, action) => reducer(state, action);
}
export default handleActions;