UNPKG

lin3s-event-bus

Version:

Simple but powerful event bus written in ES2015

62 lines (49 loc) 7.51 kB
'use strict'; 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 Mikel Tuesta <mikeltuesta@gmail.com> */ var _EventSubscriberPriorityQueue = require('./../Priority/EventSubscriberPriorityQueue'); var _EventSubscriberPriorityQueue2 = _interopRequireDefault(_EventSubscriberPriorityQueue); 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 EventPublisher = function () { function EventPublisher() { _classCallCheck(this, EventPublisher); if (this.constructor.name === 'EventPublisher') { throw new TypeError('Abstract class EventSubscriber cannot be instantiated directly.'); } this.subscribers = new _EventSubscriberPriorityQueue2.default(); } _createClass(EventPublisher, [{ key: 'subscribe', value: function subscribe() { throw new TypeError('In order to extend EventPublisher class you must implement subscribe method.'); } }, { key: 'publish', value: function publish(anEvent) { this.prePublish(anEvent); var subscribers = this.subscribers.getSubscribers(); subscribers.forEach(function (subscriber) { subscriber.handle(anEvent); }); } }, { key: 'prePublish', value: function prePublish() { throw new TypeError('In order to extend EventPublisher class you must implement prePublish method.'); } }]); return EventPublisher; }(); exports.default = EventPublisher;