| /** | |||||
| * Event caller/listener system. Intended to be extended by classes which require | |||||
| * an event callback system. | |||||
| */ | |||||
| export class EventCaller{ | export class EventCaller{ | ||||
| constructor(){ | constructor(){ | ||||
| this.__listeners = {}; | this.__listeners = {}; | ||||
| } | } | ||||
| /** | |||||
| * @type {number} | |||||
| */ | |||||
| get totalListeners(){ | get totalListeners(){ | ||||
| var count = 0; | var count = 0; | ||||
| for (key in Object.keys(this.__listeners)){ | for (key in Object.keys(this.__listeners)){ | ||||
| return count; | return count; | ||||
| } | } | ||||
| /** | |||||
| * Returns true if the given callback is listening for the given event, and false otherwise. | |||||
| * @param {string} eventName - The name of the event to check. | |||||
| * @param {Function} callback - The function/method to check for | |||||
| * @param {Object} [owner=null] - The object to use as "this" when calling the callback function. | |||||
| * @returns {bool} | |||||
| */ | |||||
| is_listening(eventName, callback, owner=null){ | is_listening(eventName, callback, owner=null){ | ||||
| if (typeof(eventName) !== 'string') | if (typeof(eventName) !== 'string') | ||||
| throw new TypeError("Expected eventName to be string."); | throw new TypeError("Expected eventName to be string."); | ||||
| return false; | return false; | ||||
| } | } | ||||
| /** | |||||
| * Sets the given callback to listen for the given event. | |||||
| * If [once] is true, the callback will be cleared from the listeners list after the named | |||||
| * event has been emitted. | |||||
| * @param {string} eventName - The name of the event to listen for. | |||||
| * @param {Function} callback - The function to call when the given event is emitted. | |||||
| * @param {Object} [owner=null] - The owner to use when calling the callback. | |||||
| * @param {bool} [once=false] - If true, the callback will be cleared after the next emit of the given event. | |||||
| * @returns {this} | |||||
| */ | |||||
| listen(eventName, callback, owner=null, once=false){ | listen(eventName, callback, owner=null, once=false){ | ||||
| try{ | try{ | ||||
| if (!this.is_listening(eventName, callback, owner)){ | if (!this.is_listening(eventName, callback, owner)){ | ||||
| return this; | return this; | ||||
| } | } | ||||
| /** | |||||
| * Removes the given callback from the given event. | |||||
| * NOTE: If the listener callback was set with it's owner, that owner must be included | |||||
| * to remove the listener callback successfully. | |||||
| * @param {string} eventName - The name of the event to remove from. | |||||
| * @param {Function} callback - The function/method to remove. | |||||
| * @param {Object} [owner=null] - The owner of the callback. | |||||
| * @returns {this} | |||||
| */ | |||||
| unlisten(eventName, callback, owner=null){ | unlisten(eventName, callback, owner=null){ | ||||
| if (typeof(eventName) !== 'string') | if (typeof(eventName) !== 'string') | ||||
| throw new TypeError("Expected eventName to be string."); | throw new TypeError("Expected eventName to be string."); | ||||
| return this; | return this; | ||||
| } | } | ||||
| /** | |||||
| * Removes all listeners from the given event. | |||||
| * @param {string} eventName - The name of the event to clear listeners from. | |||||
| * @returns {this} | |||||
| */ | |||||
| unlisten_event(eventName){ | unlisten_event(eventName){ | ||||
| if (typeof(eventName) !== 'string') | if (typeof(eventName) !== 'string') | ||||
| throw new TypeError("Expected eventName to be string."); | throw new TypeError("Expected eventName to be string."); | ||||
| return this; | return this; | ||||
| } | } | ||||
| /** | |||||
| * Removes all listeners. | |||||
| * @returns {this} | |||||
| */ | |||||
| unlisten_all(){ | unlisten_all(){ | ||||
| // NOTE: Perhaps it's better to loop through and delete each property? This should do for now though. | // NOTE: Perhaps it's better to loop through and delete each property? This should do for now though. | ||||
| this.__listeners = {}; | this.__listeners = {}; | ||||
| return this; | |||||
| } | } | ||||
| /** | |||||
| * Emits the given event, calling all listener callbacks attached to the event and passing each | |||||
| * the arguments (if any) given. | |||||
| * NOTE: All listeners of the given event designated to only listen once will be removed after | |||||
| * this call. | |||||
| * @param {string} eventName - The name of the event to emit. | |||||
| * @param {...*} args - The arguments to pass to every listener of this event. | |||||
| * @returns {this} | |||||
| */ | |||||
| emit(){ | emit(){ | ||||
| var args = Array.from(arguments); | var args = Array.from(arguments); | ||||
| if (args.length <= 0) | if (args.length <= 0) |