lin3s-event-bus
Version:
Simple but powerful event bus written in ES2015
61 lines (46 loc) • 1.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/*
* 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 = function EventSubscriberPriorityQueue() {
var _this = this;
_classCallCheck(this, EventSubscriberPriorityQueue);
var queue = [];
this.isSorted = true;
this.sort = function () {
queue.sort(function (aSubscriber, anotherSubscriber) {
return anotherSubscriber.priority.getPriority() - aSubscriber.priority.getPriority();
});
_this.isSorted = true;
};
this.push = function (aSubscriber) {
_this.isSorted = false;
queue.push(aSubscriber);
};
this.remove = function (aSubscriber) {
var aSubscriberIndex = queue.indexOf(aSubscriber);
if (aSubscriberIndex < 0) {
return;
}
_this.isSorted = false;
queue.splice(aSubscriberIndex, 1);
};
this.getSubscribers = function () {
if (!_this.isSorted) {
_this.sort();
}
return [].concat(queue);
};
};
exports.default = EventSubscriberPriorityQueue;