1'use strict'; 2 3// A simple class system, more documentation to come 4function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } 5function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } 6function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } 7function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } 8function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); } 9function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } 10var EventEmitter = require('events'); 11var lib = require('./lib'); 12function parentWrap(parent, prop) { 13 if (typeof parent !== 'function' || typeof prop !== 'function') { 14 return prop; 15 } 16 return function wrap() { 17 // Save the current parent method 18 var tmp = this.parent; 19 20 // Set parent to the previous method, call, and restore 21 this.parent = parent; 22 var res = prop.apply(this, arguments); 23 this.parent = tmp; 24 return res; 25 }; 26} 27function extendClass(cls, name, props) { 28 props = props || {}; 29 lib.keys(props).forEach(function (k) { 30 props[k] = parentWrap(cls.prototype[k], props[k]); 31 }); 32 var subclass = /*#__PURE__*/function (_cls) { 33 _inheritsLoose(subclass, _cls); 34 function subclass() { 35 return _cls.apply(this, arguments) || this; 36 } 37 _createClass(subclass, [{ 38 key: "typename", 39 get: function get() { 40 return name; 41 } 42 }]); 43 return subclass; 44 }(cls); 45 lib._assign(subclass.prototype, props); 46 return subclass; 47} 48var Obj = /*#__PURE__*/function () { 49 function Obj() { 50 // Unfortunately necessary for backwards compatibility 51 this.init.apply(this, arguments); 52 } 53 var _proto = Obj.prototype; 54 _proto.init = function init() {}; 55 Obj.extend = function extend(name, props) { 56 if (typeof name === 'object') { 57 props = name; 58 name = 'anonymous'; 59 } 60 return extendClass(this, name, props); 61 }; 62 _createClass(Obj, [{ 63 key: "typename", 64 get: function get() { 65 return this.constructor.name; 66 } 67 }]); 68 return Obj; 69}(); 70var EmitterObj = /*#__PURE__*/function (_EventEmitter) { 71 _inheritsLoose(EmitterObj, _EventEmitter); 72 function EmitterObj() { 73 var _this2; 74 var _this; 75 _this = _EventEmitter.call(this) || this; 76 // Unfortunately necessary for backwards compatibility 77 (_this2 = _this).init.apply(_this2, arguments); 78 return _this; 79 } 80 var _proto2 = EmitterObj.prototype; 81 _proto2.init = function init() {}; 82 EmitterObj.extend = function extend(name, props) { 83 if (typeof name === 'object') { 84 props = name; 85 name = 'anonymous'; 86 } 87 return extendClass(this, name, props); 88 }; 89 _createClass(EmitterObj, [{ 90 key: "typename", 91 get: function get() { 92 return this.constructor.name; 93 } 94 }]); 95 return EmitterObj; 96}(EventEmitter); 97module.exports = { 98 Obj: Obj, 99 EmitterObj: EmitterObj 100};