lin3s-event-bus
Version:
Simple but powerful event bus written in ES2015
60 lines (46 loc) • 7.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
* This file is part of the EventBusJS library.
*
* Copyright (c) 2016-present LIN3S <info@lin3s.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Beñat Espiña <benatespina@gmail.com>
* @author Mikel Tuesta <mikeltuesta@gmail.com>
*/
var _Priority = require('./Priority/Priority');
var _Priority2 = _interopRequireDefault(_Priority);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var EventSubscriber = function () {
function EventSubscriber(aCallback) {
var priority = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : new _Priority2.default();
_classCallCheck(this, EventSubscriber);
if (this.constructor.name === 'EventSubscriber') {
throw new TypeError('Abstract class EventSubscriber cannot be instantiated directly.');
}
this.callback = aCallback;
this.priority = priority;
}
_createClass(EventSubscriber, [{
key: 'handle',
value: function handle(anEvent) {
if (!this.isSubscribedTo(anEvent)) {
return;
}
this.callback(anEvent);
}
}, {
key: 'isSubscribedTo',
value: function isSubscribedTo(anEvent) {
throw new TypeError('In order to extend EventSubscriber class you must implement isSubscribedTo method.');
}
}]);
return EventSubscriber;
}();
exports.default = EventSubscriber;