UNPKG

irrelon-reactor-autonet

Version:

A module for automatically creating groups of self-networking services.

507 lines (428 loc) 13.3 kB
"use strict"; import BaseClass from './BaseClass'; import Overload from './Overload'; var EventMethods = { on: new Overload({ /** * Attach an event listener to the passed event. * @param {String} event The name of the event to listen for. * @param {Function} listener The method to call when the event is fired. */ 'string, function': function (event, listener) { this._listeners = this._listeners || {}; this._listeners[event] = this._listeners[event] || {}; this._listeners[event]['*'] = this._listeners[event]['*'] || []; this._listeners[event]['*'].push(listener); return this; }, /** * Attach an event listener to the passed event only if the passed * id matches the document id for the event being fired. * @param {String} event The name of the event to listen for. * @param {*} id The document id to match against. * @param {Function} listener The method to call when the event is fired. */ 'string, *, function': function (event, id, listener) { this._listeners = this._listeners || {}; this._listeners[event] = this._listeners[event] || {}; this._listeners[event][id] = this._listeners[event][id] || []; this._listeners[event][id].push(listener); return this; } }), once: new Overload({ /** * Attach an event listener to the passed event which will only fire once. * @param {String} event The name of the event to listen for. * @param {Function} listener The method to call when the event is fired. */ 'string, function': function (event, listener) { var self = this, fired = false, internalCallback = function () { if (!fired) { fired = true; self.off(event, internalCallback); listener.apply(self, arguments); } }; return this.on(event, internalCallback); }, /** * Attach an event listener to the passed event only if the passed * id matches the document id for the event being fired. * @param {String} event The name of the event to listen for. * @param {*} id The document id to match against. * @param {Function} listener The method to call when the event is fired. */ 'string, *, function': function (event, id, listener) { var self = this, fired = false, internalCallback = function () { if (!fired) { fired = true; self.off(event, id, internalCallback); listener.apply(self, arguments); } }; return this.on(event, id, internalCallback); } }), off: new Overload({ /** * Cancels all event listeners for the passed event. * @param {String} event The name of the event. * @returns {*} */ 'string': function (event) { var self = this; if (this._emitting) { this._eventRemovalQueue = this._eventRemovalQueue || []; this._eventRemovalQueue.push(function () { self.off(event); }); } else { if (this._listeners && this._listeners[event]) { delete this._listeners[event]; } } return this; }, /** * Cancels the event listener for the passed event and listener function. * @param {String} event The event to cancel listener for. * @param {Function} listener The event listener function used in the on() * or once() call to cancel. * @returns {*} */ 'string, function': function (event, listener) { var self = this, arr, index; if (this._emitting) { this._eventRemovalQueue = this._eventRemovalQueue || []; this._eventRemovalQueue.push(function () { self.off(event, listener); }); } else { if (typeof(listener) === 'string') { if (this._listeners && this._listeners[event] && this._listeners[event][listener]) { delete this._listeners[event][listener]; } } else { if (this._listeners && this._listeners[event]) { arr = this._listeners[event]['*']; index = arr.indexOf(listener); if (index > -1) { arr.splice(index, 1); } } } } return this; }, /** * Cancels an event listener based on an event name, id and listener function. * @param {String} event The event to cancel listener for. * @param {String} id The ID of the event to cancel listening for. * @param {Function} listener The event listener function used in the on() * or once() call to cancel. */ 'string, *, function': function (event, id, listener) { var self = this; if (this._emitting) { this._eventRemovalQueue = this._eventRemovalQueue || []; this._eventRemovalQueue.push(function () { self.off(event, id, listener); }); } else { if (this._listeners && this._listeners[event] && this._listeners[event][id]) { var arr = this._listeners[event][id], index = arr.indexOf(listener); if (index > -1) { arr.splice(index, 1); } } } }, /** * Cancels all listeners for an event based on the passed event name and id. * @param {String} event The event name to cancel listeners for. * @param {*} id The ID to cancel all listeners for. */ 'string, *': function (event, id) { var self = this; if (this._emitting) { this._eventRemovalQueue = this._eventRemovalQueue || []; this._eventRemovalQueue.push(function () { self.off(event, id); }); } else { if (this._listeners && this._listeners[event] && this._listeners[event][id]) { // Kill all listeners for this event id delete this._listeners[event][id]; } } } }), emit: new Overload({ /** * Emit an event. * @param {String} event The event to emit. * @returns {*} */ 'string': function (event) { // Fire global listeners return this.$main(event); }, /** * Emit an event with data. * @param {String} event The event to emit. * @param {*} data Data to emit with the event. * @returns {*} */ 'string, ...': function (event, data) { // Fire global listeners first this.$main.apply(this, arguments); return this; }, /** * Handles emitting events, is an internal method not called directly. * @param {String} event The name of the event to emit. * @param {*} data The data to emit with the event. * @returns {*} * @private */ '$main': function (event, data) { var id = '*'; this._listeners = this._listeners || {}; this._emitting = true; if (this._listeners[event]) { var arrIndex, arrCount, tmpFunc, arr; // Handle global emit if (this._listeners[event][id]) { arr = this._listeners[event][id]; arrCount = arr.length; for (arrIndex = 0; arrIndex < arrCount; arrIndex++) { // Check we have a function to execute tmpFunc = arr[arrIndex]; if (typeof tmpFunc === 'function') { tmpFunc.apply(this, Array.prototype.slice.call(arguments, 1)); } } } } this._emitting = false; this._processRemovalQueue(); return this; } }), emitId: new Overload({ 'string': function (event) { throw('Missing id from emitId call!'); }, 'string, *': function (event, id) { return this.$main(event, id); }, 'string, *, ...': function (event, id) { // Fire global listeners first this.$main.apply(this, arguments); return this; }, '$main': function (event, id, data) { this._listeners = this._listeners || {}; this._emitting = true; if (this._listeners[event]) { var arrIndex, arrCount, tmpFunc, arr; // Handle global emit if (this._listeners[event]['*']) { arr = this._listeners[event]['*']; arrCount = arr.length; for (arrIndex = 0; arrIndex < arrCount; arrIndex++) { // Check we have a function to execute tmpFunc = arr[arrIndex]; if (typeof tmpFunc === 'function') { tmpFunc.apply(this, Array.prototype.slice.call(arguments, 2)); } } } // Handle id emit if (this._listeners[event][id]) { arr = this._listeners[event][id]; arrCount = arr.length; for (arrIndex = 0; arrIndex < arrCount; arrIndex++) { // Check we have a function to execute tmpFunc = arr[arrIndex]; if (typeof tmpFunc === 'function') { tmpFunc.apply(this, Array.prototype.slice.call(arguments, 2)); } } } } this._emitting = false; this._processRemovalQueue(); return this; } }), /** * Checks if an event has any event listeners or not. * @param {String} event The name of the event to check for. * @returns {boolean} True if one or more event listeners are registered for * the event. False if none are found. */ willEmit: function (event) { var id = '*'; if (this._listeners && this._listeners[event]) { var arrIndex, arrCount, tmpFunc, arr; // Handle global emit if (this._listeners[event][id]) { arr = this._listeners[event][id]; arrCount = arr.length; for (arrIndex = 0; arrIndex < arrCount; arrIndex++) { // Check we have a function to execute tmpFunc = arr[arrIndex]; if (typeof tmpFunc === 'function') { return true; } } } } return false }, /** * Checks if an event has any event listeners or not based on the passed id. * @param {String} event The name of the event to check for. * @param {String} id The event ID to check for. * @returns {boolean} True if one or more event listeners are registered for * the event. False if none are found. */ willEmitId: function (event, id) { if (this._listeners && this._listeners[event]) { var arrIndex, arrCount, tmpFunc, arr; // Handle global emit if (this._listeners[event]['*']) { arr = this._listeners[event]['*']; arrCount = arr.length; for (arrIndex = 0; arrIndex < arrCount; arrIndex++) { // Check we have a function to execute tmpFunc = arr[arrIndex]; if (typeof tmpFunc === 'function') { return true; } } } // Handle id emit if (this._listeners[event][id]) { arr = this._listeners[event][id]; arrCount = arr.length; for (arrIndex = 0; arrIndex < arrCount; arrIndex++) { // Check we have a function to execute tmpFunc = arr[arrIndex]; if (typeof tmpFunc === 'function') { return true; } } } } return false }, /** * If events are cleared with the off() method while the event emitter is * actively processing any events then the off() calls get added to a * queue to be executed after the event emitter is finished. This stops * errors that might occur by potentially modifying the event queue while * the emitter is running through them. This method is called after the * event emitter is finished processing. * @private */ _processRemovalQueue: function () { var i; if (this._eventRemovalQueue && this._eventRemovalQueue.length) { // Execute each removal call for (i = 0; i < this._eventRemovalQueue.length; i++) { this._eventRemovalQueue[i](); } // Clear the removal queue this._eventRemovalQueue = []; } }, /** * Queues an event to be fired. This has automatic de-bouncing so that any * events of the same type that occur within 100 milliseconds of a previous * one will all be wrapped into a single emit rather than emitting tons of * events for lots of chained inserts etc. Only the data from the last * de-bounced event will be emitted. * @param {String} eventName The name of the event to emit. * @param {*=} data Optional data to emit with the event. */ deferEmit: function (eventName, data) { var self = this, args; if (!this._noEmitDefer && (!this._db || (this._db && !this._db._noEmitDefer))) { args = arguments; // Check for an existing timeout this._deferTimeout = this._deferTimeout || {}; if (this._deferTimeout[eventName]) { clearTimeout(this._deferTimeout[eventName]); } // Set a timeout this._deferTimeout[eventName] = setTimeout(function () { self.emit.apply(self, args); }, 1); } else { this.emit.apply(this, arguments); } return this; } }; /** * Provides event emitter functionality including the methods: * on, off, once, emit, deferEmit. */ class EventEmitter extends BaseClass { constructor (name) { super(name); // Define the object that will keep track of listeners this._listeners = {}; } get on () { return EventMethods.on; } get once () { return EventMethods.once; } get off () { return EventMethods.off; } get emit () { return EventMethods.emit; } get emitId () { return EventMethods.emitId; } get deferEmit () { return EventMethods.deferEmit; } get willEmit () { return EventMethods.willEmit; } get willEmitId () { return EventMethods.willEmitId; } get _processRemovalQueue () { return EventMethods._processRemovalQueue; } } export default EventEmitter;