UNPKG

spa-bus

Version:

Tools for multilevel components to pass values in any SPA

68 lines (55 loc) 2.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _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, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var SPAEventBus = /*#__PURE__*/ function () { function SPAEventBus() { _classCallCheck(this, SPAEventBus); this.events = {}; this.addEventListener = this.addEventListener.bind(this); this.emit = this.emit.bind(this); this.removeEventListener = this.removeEventListener.bind(this); } _createClass(SPAEventBus, [{ key: "addEventListener", value: function addEventListener(type, callBack) { if (typeof type !== 'string') { throw new Error('event type must be string'); } if (typeof callBack !== 'function') { throw new Error('callBack type must be function'); } if (!(type in this.events)) { this.events[type] = callBack; } else { throw new Error('this event is listening'); } } }, { key: "removeEventListener", value: function removeEventListener(type) { if (typeof type !== 'string') { throw new Error('event type must be string'); } delete this.events[type]; } }, { key: "emit", value: function emit(type, params) { if (typeof this.events[type] === 'function') { this.events[type](params); } else { console.error('unknown event type'); } } }]); return SPAEventBus; }(); var _default = new SPAEventBus(); exports["default"] = _default;