redux-vertical
Version:
Build redux vertically
32 lines • 1.53 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const invariant_1 = __importDefault(require("invariant"));
const isPlainObject_1 = __importDefault(require("./utils/isPlainObject"));
function combineReducers(handlers) {
invariant_1.default(isPlainObject_1.default(handlers), 'combineReducers expected a dictionary object of reducers but found %s', handlers);
// check the reducers
Object.keys(handlers).forEach((ns) => {
const reducer = handlers[ns];
invariant_1.default(typeof reducer === 'function', 'reducer for %s should be functions but found %s', ns, reducer);
});
// create final reducer
function finalReducer(state = {}, action) {
let hasChanged = false;
const nextState = {};
Object.keys(handlers).forEach((ns) => {
const reducer = handlers[ns];
const previousStateForKey = state[ns];
const nextStateForKey = reducer(previousStateForKey, action);
invariant_1.default(!(typeof nextStateForKey === 'undefined'), 'expected defined state for ns %s', ns);
nextState[ns] = nextStateForKey;
hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
});
return hasChanged ? nextState : state;
}
return finalReducer;
}
exports.default = combineReducers;
//# sourceMappingURL=combine-reducers.js.map