UNPKG

red-redux-class

Version:

Use objects and composition for creating complex reducers which can be easily maintained.

197 lines 7.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var ReduxClass_types_1 = require("./ReduxClass.types"); var ReduxClassException_class_1 = require("./ReduxClassException.class"); var ReduxClassSymbol_1 = require("./ReduxClassSymbol"); var ReduxClass_constants_1 = require("./ReduxClass.constants"); var ReduxClass = (function (_super) { tslib_1.__extends(ReduxClass, _super); function ReduxClass(initialState) { if (initialState === void 0) { initialState = {}; } var _this = _super.call(this) || this; _this._initNew(); _this._initType(); _this._initialize(initialState); _this._initDefaults(); return _this; } ReduxClass.isReduxClass = function (object) { if (typeof object === 'undefined') { return false; } var _object = object; return (typeof object === 'object' && object !== null && _object[ReduxClass_constants_1.TYPEOF_KEY] === ReduxClassSymbol_1.REDUX_CLASS_SYMBOL); }; ReduxClass.propType = function () { var classConstructor = this; return function (props, key, componentName) { componentName = componentName || 'ANONYMOUS'; var _props = props; if (!(_props[key] instanceof classConstructor)) { return new Error(key + ' in ' + componentName + ' should be instanceof ' + classConstructor.toString()); } return null; }; }; ReduxClass.prototype._initNew = function () { this._initHiddenProperty(ReduxClass_constants_1.NEW_KEY, true); }; ReduxClass.prototype._initType = function () { this._initHiddenProperty(ReduxClass_constants_1.TYPEOF_KEY, ReduxClassSymbol_1.REDUX_CLASS_SYMBOL); }; ReduxClass.prototype._initialize = function (initialState) { return this.setData(initialState); }; ReduxClass.prototype._initHiddenProperty = function (key, value) { Object.defineProperty(this, key, { configurable: false, enumerable: false, value: value, writable: true, }); }; ReduxClass.prototype._initDefaults = function () { var _this = this; var defaults = this.constructor.defaults; Object.keys(defaults).forEach(function (key) { if (typeof _this[key] === 'undefined') { _this._setAttr(key, defaults[key]); } }); }; ReduxClass.prototype._new = function () { return new this.constructor(this); }; ReduxClass.prototype._setAttr = function (key, value) { var types = this.constructor.types; if (value === null) { this[key] = value; return this; } if (typeof types[key] === 'string') { if (typeof value !== types[key]) { throw new ReduxClassException_class_1.ReduxClassException('Bad value type', "Value type should be as set in static types property: '" + key + "'@" + this.constructor.name + " = " + this.constructor.types[key]); } } else if (types[key] && !(value instanceof types[key])) { value = new types[key](value); } if (types._ && !types[key] && !(value instanceof types._)) { value = new types._(value); } var oldValue = this.get(key); if (typeof oldValue !== typeof value && oldValue !== undefined && oldValue !== null && value !== undefined && value !== null) { throw new ReduxClassException_class_1.ReduxClassException('Bad value type', "Value type should be the same as previous value: '" + key + "'@" + this.constructor.name); } this[key] = value; return this; }; ReduxClass.prototype._shouldBeNew = function () { if (!this.isNew()) { throw new ReduxClassException_class_1.ReduxClassException('Set on not new', 'Create new object to set attributes'); } }; ReduxClass.prototype.forEachInstance = function (callback) { var _this = this; var keys = Object.keys(this).concat(Object.keys(this[ReduxClass_constants_1.ARRAY_KEY] || {})); keys.forEach(function (key) { var attr = _this.get(key); if (ReduxClass.isReduxClass(attr)) { callback(attr, key, _this); } }); }; ReduxClass.prototype.getNew = function () { if (this.isNew()) { return this; } return this._new(); }; ReduxClass.prototype.newPath = function (path) { var pathParts = path.split('.'); var childPath = pathParts[0]; var child = this.get(childPath); if (child === undefined) { throw new ReduxClassException_class_1.ReduxClassException("Target class doesn't exist", "Please make sure target object exists: '" + path + "'@" + this.constructor.name); } if (ReduxClass.isReduxClass(child)) { var newSelf = this.getNew(); var newPath = pathParts.slice(1).join('.'); if (newPath === '') { var newChild = child.getNew(); newSelf.set(childPath, newChild); return [ newSelf.get(childPath), newSelf, newSelf.get(childPath), ]; } var _a = tslib_1.__read(child.newPath(newPath)), target = _a[0], root = _a[1], otherTargets = _a.slice(2); newSelf.set(childPath, root); return tslib_1.__spread([ target, newSelf, newSelf.get(childPath) ], otherTargets); } else { throw new ReduxClassException_class_1.ReduxClassException('Target class is not ReduxClass', "Please make sure target object exists: '" + path + "'@" + this.constructor.name); } }; ReduxClass.prototype.new = function () { return this._new(); }; ReduxClass.prototype.get = function (key) { return this[key]; }; ReduxClass.prototype.getPath = function (path) { if (path.indexOf('.') !== -1) { var pathArr = path.split('.'); var pathArrCopy_1 = tslib_1.__spread(pathArr); return pathArr.reduce(function (accumulator) { return accumulator.get(pathArrCopy_1.shift() || ''); }, this); } var key = path; return this.get(key); }; ReduxClass.prototype.set = function (key, value) { this._shouldBeNew(); return this._setAttr(key, value); }; ReduxClass.prototype.setData = function (data) { var _this = this; var _data = data; this._shouldBeNew(); Object.keys(data).forEach(function (key) { _this._setAttr(key, _data[key]); }); return this; }; ReduxClass.prototype.isNew = function () { return this[ReduxClass_constants_1.NEW_KEY]; }; ReduxClass.prototype.$setNew = function () { this[ReduxClass_constants_1.NEW_KEY] = true; }; ReduxClass.prototype.$setNotNew = function () { this[ReduxClass_constants_1.NEW_KEY] = false; }; ReduxClass.prototype.$getType = function () { return this[ReduxClass_constants_1.TYPEOF_KEY]; }; ReduxClass.defaults = {}; ReduxClass.types = {}; return ReduxClass; }(ReduxClass_types_1.PureObject)); exports.ReduxClass = ReduxClass; //# sourceMappingURL=ReduxClass.class.js.map