redux-typed-kit
Version:
Powerful extensitions for building class-based Redux architecture powered by TypeScript.
43 lines • 1.85 kB
JavaScript
import 'reflect-metadata';
import { METADATA_KEY_ACTION } from './constants';
var Reducer = /** @class */ (function () {
function Reducer() {
this.mapping = new Map();
}
Reducer.prototype.executeReducer = function (state, action, actionType) {
var funcs = this.mapping.get(actionType || action.type);
var resState = state;
if (funcs != null) {
for (var _i = 0, funcs_1 = funcs; _i < funcs_1.length; _i++) {
var func = funcs_1[_i];
resState = func.bind(this)(resState, action);
}
}
return resState;
};
Reducer.prototype.call = function (state, action) {
if (state === void 0) { state = this.initialState; }
var resState = this.executeReducer(state, action);
// for 'any' action type
return this.executeReducer(resState, action, Object.name);
};
Reducer.prototype.create = function () {
// idk why all methods are in prototype class
var proto = Object.getPrototypeOf(this);
var methodNames = Object.getOwnPropertyNames(proto).filter(function (x) { return x != 'constructor' && !x.startsWith('_'); });
for (var _i = 0, methodNames_1 = methodNames; _i < methodNames_1.length; _i++) {
var key = methodNames_1[_i];
var metaActionType = Reflect.getMetadata(METADATA_KEY_ACTION, this, key);
if (metaActionType == null)
continue;
var func = this[key];
if (!this.mapping.has(metaActionType))
this.mapping.set(metaActionType, []);
this.mapping.get(metaActionType).push(func);
}
return this.call.bind(this);
};
return Reducer;
}());
export default Reducer;
//# sourceMappingURL=reducer.js.map