UNPKG

@stand/base

Version:
150 lines (122 loc) 4.25 kB
import _Object$keys from '@babel/runtime-corejs3/core-js-stable/object/keys'; import _forEachInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/for-each'; import _concatInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/concat'; import _classCallCheck from '@babel/runtime-corejs3/helpers/classCallCheck'; import _createClass from '@babel/runtime-corejs3/helpers/createClass'; import _possibleConstructorReturn from '@babel/runtime-corejs3/helpers/possibleConstructorReturn'; import _getPrototypeOf from '@babel/runtime-corejs3/helpers/getPrototypeOf'; import _inherits from '@babel/runtime-corejs3/helpers/inherits'; import _Symbol from '@babel/runtime-corejs3/core-js-stable/symbol'; import _merge from 'lodash/merge'; import StandEvent from '@stand/event'; var symbolsSymbol = _Symbol('symbols'); var StandBase = /*#__PURE__*/ function (_StandEvent) { _inherits(StandBase, _StandEvent); function StandBase() { var _getPrototypeOf2, _context; var _this; _classCallCheck(this, StandBase); for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) { params[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(StandBase)).call.apply(_getPrototypeOf2, _concatInstanceProperty(_context = [this]).call(_context, params))); _this[symbolsSymbol] = {}; _this.addSetAndGetMethods('set', 'get'); return _this; } _createClass(StandBase, [{ key: "getSameSymbol", value: function getSameSymbol(key) { if (!this[symbolsSymbol][key]) { this[symbolsSymbol][key] = _Symbol(key); } return this[symbolsSymbol][key]; } }, { key: "addSetAndGetMethods", value: function addSetAndGetMethods(setName, getName, props) { var _this2 = this; var datas = {}; this.setReadOnlyProps(setName, props); var propsSymbol = this.getSameSymbol(setName); this[setName] = function (key, value) { var readOnlys = _this2[propsSymbol] || {}; if (typeof value === 'undefined') { var _context2; var config = key || {}; _forEachInstanceProperty(_context2 = _Object$keys(readOnlys)).call(_context2, function (prop) { if (typeof config[prop] !== 'undefined') { _this2.emit('STANDLOG', { code: 300002, message: 'can not set readonly props', detail: { method: setName, prop: prop, value: config[prop] } }); delete config[prop]; } }); _merge(datas, config); } else { if (!readOnlys[key]) { datas[key] = value; } else { _this2.emit('STANDLOG', { code: 300001, message: 'can not set readonly prop', detail: { method: setName, prop: key, value: value } }); } } return _this2; }; this[getName] = function (field) { if (field) { return datas[field]; } return datas; }; return this; } }, { key: "setReadOnlyProps", value: function setReadOnlyProps(setName) { var _context3; var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var propsSymbol = this.getSameSymbol(setName); var readOnlys = this[propsSymbol] = this[propsSymbol] || {}; _forEachInstanceProperty(_context3 = _Object$keys(props)).call(_context3, function (key) { if (props[key]) { readOnlys[key] = true; } else { delete readOnlys[key]; } }); this.emit('STANDLOG', { code: 200001, message: 'add readonly props', detail: { method: setName, value: props } }); return this; } }, { key: "rewrite", value: function rewrite(name, method) { this[name] = method; return this; } }]); return StandBase; }(StandEvent); export default StandBase;