red-redux-class
Version:
Use objects and composition for creating complex reducers which can be easily maintained.
59 lines • 2.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var ReduxClass_class_1 = require("./ReduxClass.class");
function searchObjectForNew(state, parentKey, statePaths) {
if (parentKey === void 0) { parentKey = ''; }
if (statePaths === void 0) { statePaths = []; }
state.forEachInstance(function (attr, key) {
var path = parentKey + '.' + key;
searchObjectForNew(attr, path, statePaths);
statePaths.push(path.slice(1));
});
return statePaths;
}
function traverseStateForNew(state, paths) {
if (!paths) {
paths = searchObjectForNew(state);
}
state.$setNotNew();
paths.forEach(function (path) {
var target = state.getPath(path);
target.$setNotNew();
});
return paths;
}
function ReduxClassWrapper(reducer) {
var decorated = createReducer;
function stateReducerHandler(newState) {
if (!ReduxClass_class_1.ReduxClass.isReduxClass(newState)) {
throw new Error('Reducer should be of ReduxClass type');
}
traverseStateForNew(newState);
return newState;
}
function decoratedStateReducer(state, action) {
var newState = reducer(state, action);
return stateReducerHandler(newState);
}
function createReducer(state, action) {
var newState = reducer(state, action);
if (ReduxClass_class_1.ReduxClass.isReduxClass(state) || ReduxClass_class_1.ReduxClass.isReduxClass(newState)) {
stateReducerHandler(newState);
decorated = decoratedStateReducer;
}
else {
decorated = reducer;
}
return newState;
}
function main(state, action) {
return decorated(state, action);
}
return main;
}
exports.ReduxClassWrapper = ReduxClassWrapper;
exports.privateMethods = {
searchObjectForNew: searchObjectForNew,
traverseStateForNew: traverseStateForNew,
};
//# sourceMappingURL=ReduxClass.decorator.js.map