diff --git a/EventEmitter.js b/EventEmitter.js index 728a43c..b1ce229 100644 --- a/EventEmitter.js +++ b/EventEmitter.js @@ -1,472 +1,472 @@ /*! - * EventEmitter v4.2.8 - git.io/ee + * EventEmitter v4.2.9 - git.io/ee * Oliver Caldwell * MIT license * @preserve */ (function () { - 'use strict'; - - /** - * Class for managing events. - * Can be extended to provide event functionality in other classes. - * - * @class EventEmitter Manages event registering and emitting. - */ - function EventEmitter() {} - - // Shortcuts to improve speed and size - var proto = EventEmitter.prototype; - var exports = this; - var originalGlobalValue = exports.EventEmitter; - - /** - * Finds the index of the listener for the event in its storage array. - * - * @param {Function[]} listeners Array of listeners to search through. - * @param {Function} listener Method to look for. - * @return {Number} Index of the specified listener, -1 if not found - * @api private - */ - function indexOfListener(listeners, listener) { - var i = listeners.length; - while (i--) { - if (listeners[i].listener === listener) { - return i; - } - } - - return -1; - } - - /** - * Alias a method while keeping the context correct, to allow for overwriting of target method. - * - * @param {String} name The name of the target method. - * @return {Function} The aliased method - * @api private - */ - function alias(name) { - return function aliasClosure() { - return this[name].apply(this, arguments); - }; - } - - /** - * Returns the listener array for the specified event. - * Will initialise the event object and listener arrays if required. - * Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them. - * Each property in the object response is an array of listener functions. - * - * @param {String|RegExp} evt Name of the event to return the listeners from. - * @return {Function[]|Object} All listener functions for the event. - */ - proto.getListeners = function getListeners(evt) { - var events = this._getEvents(); - var response; - var key; - - // Return a concatenated array of all matching events if - // the selector is a regular expression. - if (evt instanceof RegExp) { - response = {}; - for (key in events) { - if (events.hasOwnProperty(key) && evt.test(key)) { - response[key] = events[key]; - } - } - } - else { - response = events[evt] || (events[evt] = []); - } - - return response; - }; - - /** - * Takes a list of listener objects and flattens it into a list of listener functions. - * - * @param {Object[]} listeners Raw listener objects. - * @return {Function[]} Just the listener functions. - */ - proto.flattenListeners = function flattenListeners(listeners) { - var flatListeners = []; - var i; - - for (i = 0; i < listeners.length; i += 1) { - flatListeners.push(listeners[i].listener); - } - - return flatListeners; - }; - - /** - * Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful. - * - * @param {String|RegExp} evt Name of the event to return the listeners from. - * @return {Object} All listener functions for an event in an object. - */ - proto.getListenersAsObject = function getListenersAsObject(evt) { - var listeners = this.getListeners(evt); - var response; - - if (listeners instanceof Array) { - response = {}; - response[evt] = listeners; - } - - return response || listeners; - }; - - /** - * Adds a listener function to the specified event. - * The listener will not be added if it is a duplicate. - * If the listener returns true then it will be removed after it is called. - * If you pass a regular expression as the event name then the listener will be added to all events that match it. - * - * @param {String|RegExp} evt Name of the event to attach the listener to. - * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.addListener = function addListener(evt, listener) { - var listeners = this.getListenersAsObject(evt); - var listenerIsWrapped = typeof listener === 'object'; - var key; - - for (key in listeners) { - if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) { - listeners[key].push(listenerIsWrapped ? listener : { - listener: listener, - once: false - }); - } - } - - return this; - }; - - /** - * Alias of addListener - */ - proto.on = alias('addListener'); - - /** - * Semi-alias of addListener. It will add a listener that will be - * automatically removed after its first execution. - * - * @param {String|RegExp} evt Name of the event to attach the listener to. - * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.addOnceListener = function addOnceListener(evt, listener) { - return this.addListener(evt, { - listener: listener, - once: true - }); - }; - - /** - * Alias of addOnceListener. - */ - proto.once = alias('addOnceListener'); - - /** - * Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad. - * You need to tell it what event names should be matched by a regex. - * - * @param {String} evt Name of the event to create. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.defineEvent = function defineEvent(evt) { - this.getListeners(evt); - return this; - }; - - /** - * Uses defineEvent to define multiple events. - * - * @param {String[]} evts An array of event names to define. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.defineEvents = function defineEvents(evts) { - for (var i = 0; i < evts.length; i += 1) { - this.defineEvent(evts[i]); - } - return this; - }; - - /** - * Removes a listener function from the specified event. - * When passed a regular expression as the event name, it will remove the listener from all events that match it. - * - * @param {String|RegExp} evt Name of the event to remove the listener from. - * @param {Function} listener Method to remove from the event. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.removeListener = function removeListener(evt, listener) { - var listeners = this.getListenersAsObject(evt); - var index; - var key; - - for (key in listeners) { - if (listeners.hasOwnProperty(key)) { - index = indexOfListener(listeners[key], listener); - - if (index !== -1) { - listeners[key].splice(index, 1); - } - } - } - - return this; - }; - - /** - * Alias of removeListener - */ - proto.off = alias('removeListener'); - - /** - * Adds listeners in bulk using the manipulateListeners method. - * If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added. - * You can also pass it a regular expression to add the array of listeners to all events that match it. - * Yeah, this function does quite a bit. That's probably a bad thing. - * - * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once. - * @param {Function[]} [listeners] An optional array of listener functions to add. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.addListeners = function addListeners(evt, listeners) { - // Pass through to manipulateListeners - return this.manipulateListeners(false, evt, listeners); - }; - - /** - * Removes listeners in bulk using the manipulateListeners method. - * If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. - * You can also pass it an event name and an array of listeners to be removed. - * You can also pass it a regular expression to remove the listeners from all events that match it. - * - * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once. - * @param {Function[]} [listeners] An optional array of listener functions to remove. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.removeListeners = function removeListeners(evt, listeners) { - // Pass through to manipulateListeners - return this.manipulateListeners(true, evt, listeners); - }; - - /** - * Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level. - * The first argument will determine if the listeners are removed (true) or added (false). - * If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. - * You can also pass it an event name and an array of listeners to be added/removed. - * You can also pass it a regular expression to manipulate the listeners of all events that match it. - * - * @param {Boolean} remove True if you want to remove listeners, false if you want to add. - * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once. - * @param {Function[]} [listeners] An optional array of listener functions to add/remove. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) { - var i; - var value; - var single = remove ? this.removeListener : this.addListener; - var multiple = remove ? this.removeListeners : this.addListeners; - - // If evt is an object then pass each of its properties to this method - if (typeof evt === 'object' && !(evt instanceof RegExp)) { - for (i in evt) { - if (evt.hasOwnProperty(i) && (value = evt[i])) { - // Pass the single listener straight through to the singular method - if (typeof value === 'function') { - single.call(this, i, value); - } - else { - // Otherwise pass back to the multiple function - multiple.call(this, i, value); - } - } - } - } - else { - // So evt must be a string - // And listeners must be an array of listeners - // Loop over it and pass each one to the multiple method - i = listeners.length; - while (i--) { - single.call(this, evt, listeners[i]); - } - } - - return this; - }; - - /** - * Removes all listeners from a specified event. - * If you do not specify an event then all listeners will be removed. - * That means every event will be emptied. - * You can also pass a regex to remove all events that match it. - * - * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.removeEvent = function removeEvent(evt) { - var type = typeof evt; - var events = this._getEvents(); - var key; - - // Remove different things depending on the state of evt - if (type === 'string') { - // Remove all listeners for the specified event - delete events[evt]; - } - else if (evt instanceof RegExp) { - // Remove all events matching the regex. - for (key in events) { - if (events.hasOwnProperty(key) && evt.test(key)) { - delete events[key]; - } - } - } - else { - // Remove all listeners in all events - delete this._events; - } - - return this; - }; - - /** - * Alias of removeEvent. - * - * Added to mirror the node API. - */ - proto.removeAllListeners = alias('removeEvent'); - - /** - * Emits an event of your choice. - * When emitted, every listener attached to that event will be executed. - * If you pass the optional argument array then those arguments will be passed to every listener upon execution. - * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately. - * So they will not arrive within the array on the other side, they will be separate. - * You can also pass a regular expression to emit to all events that match it. - * - * @param {String|RegExp} evt Name of the event to emit and execute listeners for. - * @param {Array} [args] Optional array of arguments to be passed to each listener. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.emitEvent = function emitEvent(evt, args) { - var listeners = this.getListenersAsObject(evt); - var listener; - var i; - var key; - var response; - - for (key in listeners) { - if (listeners.hasOwnProperty(key)) { - i = listeners[key].length; - - while (i--) { - // If the listener returns true then it shall be removed from the event - // The function is executed either with a basic call or an apply if there is an args array - listener = listeners[key][i]; - - if (listener.once === true) { - this.removeListener(evt, listener.listener); - } - - response = listener.listener.apply(this, args || []); - - if (response === this._getOnceReturnValue()) { - this.removeListener(evt, listener.listener); - } - } - } - } - - return this; - }; - - /** - * Alias of emitEvent - */ - proto.trigger = alias('emitEvent'); - - /** - * Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on. - * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it. - * - * @param {String|RegExp} evt Name of the event to emit and execute listeners for. - * @param {...*} Optional additional arguments to be passed to each listener. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.emit = function emit(evt) { - var args = Array.prototype.slice.call(arguments, 1); - return this.emitEvent(evt, args); - }; - - /** - * Sets the current value to check against when executing listeners. If a - * listeners return value matches the one set here then it will be removed - * after execution. This value defaults to true. - * - * @param {*} value The new value to check for when executing listeners. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.setOnceReturnValue = function setOnceReturnValue(value) { - this._onceReturnValue = value; - return this; - }; - - /** - * Fetches the current value to check against when executing listeners. If - * the listeners return value matches this one then it should be removed - * automatically. It will return true by default. - * - * @return {*|Boolean} The current value to check for or the default, true. - * @api private - */ - proto._getOnceReturnValue = function _getOnceReturnValue() { - if (this.hasOwnProperty('_onceReturnValue')) { - return this._onceReturnValue; - } - else { - return true; - } - }; - - /** - * Fetches the events object and creates one if required. - * - * @return {Object} The events storage object. - * @api private - */ - proto._getEvents = function _getEvents() { - return this._events || (this._events = {}); - }; - - /** - * Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version. - * - * @return {Function} Non conflicting EventEmitter class. - */ - EventEmitter.noConflict = function noConflict() { - exports.EventEmitter = originalGlobalValue; - return EventEmitter; - }; - - // Expose the class either via AMD, CommonJS or the global object - if (typeof define === 'function' && define.amd) { - define(function () { - return EventEmitter; - }); - } - else if (typeof module === 'object' && module.exports){ - module.exports = EventEmitter; - } - else { - this.EventEmitter = EventEmitter; - } + 'use strict'; + + /** + * Class for managing events. + * Can be extended to provide event functionality in other classes. + * + * @class EventEmitter Manages event registering and emitting. + */ + function EventEmitter() {} + + // Shortcuts to improve speed and size + var proto = EventEmitter.prototype; + var exports = this; + var originalGlobalValue = exports.EventEmitter; + + /** + * Finds the index of the listener for the event in its storage array. + * + * @param {Function[]} listeners Array of listeners to search through. + * @param {Function} listener Method to look for. + * @return {Number} Index of the specified listener, -1 if not found + * @api private + */ + function indexOfListener(listeners, listener) { + var i = listeners.length; + while (i--) { + if (listeners[i].listener === listener) { + return i; + } + } + + return -1; + } + + /** + * Alias a method while keeping the context correct, to allow for overwriting of target method. + * + * @param {String} name The name of the target method. + * @return {Function} The aliased method + * @api private + */ + function alias(name) { + return function aliasClosure() { + return this[name].apply(this, arguments); + }; + } + + /** + * Returns the listener array for the specified event. + * Will initialise the event object and listener arrays if required. + * Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them. + * Each property in the object response is an array of listener functions. + * + * @param {String|RegExp} evt Name of the event to return the listeners from. + * @return {Function[]|Object} All listener functions for the event. + */ + proto.getListeners = function getListeners(evt) { + var events = this._getEvents(); + var response; + var key; + + // Return a concatenated array of all matching events if + // the selector is a regular expression. + if (evt instanceof RegExp) { + response = {}; + for (key in events) { + if (events.hasOwnProperty(key) && evt.test(key)) { + response[key] = events[key]; + } + } + } + else { + response = events[evt] || (events[evt] = []); + } + + return response; + }; + + /** + * Takes a list of listener objects and flattens it into a list of listener functions. + * + * @param {Object[]} listeners Raw listener objects. + * @return {Function[]} Just the listener functions. + */ + proto.flattenListeners = function flattenListeners(listeners) { + var flatListeners = []; + var i; + + for (i = 0; i < listeners.length; i += 1) { + flatListeners.push(listeners[i].listener); + } + + return flatListeners; + }; + + /** + * Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful. + * + * @param {String|RegExp} evt Name of the event to return the listeners from. + * @return {Object} All listener functions for an event in an object. + */ + proto.getListenersAsObject = function getListenersAsObject(evt) { + var listeners = this.getListeners(evt); + var response; + + if (listeners instanceof Array) { + response = {}; + response[evt] = listeners; + } + + return response || listeners; + }; + + /** + * Adds a listener function to the specified event. + * The listener will not be added if it is a duplicate. + * If the listener returns true then it will be removed after it is called. + * If you pass a regular expression as the event name then the listener will be added to all events that match it. + * + * @param {String|RegExp} evt Name of the event to attach the listener to. + * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.addListener = function addListener(evt, listener) { + var listeners = this.getListenersAsObject(evt); + var listenerIsWrapped = typeof listener === 'object'; + var key; + + for (key in listeners) { + if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) { + listeners[key].push(listenerIsWrapped ? listener : { + listener: listener, + once: false + }); + } + } + + return this; + }; + + /** + * Alias of addListener + */ + proto.on = alias('addListener'); + + /** + * Semi-alias of addListener. It will add a listener that will be + * automatically removed after its first execution. + * + * @param {String|RegExp} evt Name of the event to attach the listener to. + * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.addOnceListener = function addOnceListener(evt, listener) { + return this.addListener(evt, { + listener: listener, + once: true + }); + }; + + /** + * Alias of addOnceListener. + */ + proto.once = alias('addOnceListener'); + + /** + * Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad. + * You need to tell it what event names should be matched by a regex. + * + * @param {String} evt Name of the event to create. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.defineEvent = function defineEvent(evt) { + this.getListeners(evt); + return this; + }; + + /** + * Uses defineEvent to define multiple events. + * + * @param {String[]} evts An array of event names to define. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.defineEvents = function defineEvents(evts) { + for (var i = 0; i < evts.length; i += 1) { + this.defineEvent(evts[i]); + } + return this; + }; + + /** + * Removes a listener function from the specified event. + * When passed a regular expression as the event name, it will remove the listener from all events that match it. + * + * @param {String|RegExp} evt Name of the event to remove the listener from. + * @param {Function} listener Method to remove from the event. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.removeListener = function removeListener(evt, listener) { + var listeners = this.getListenersAsObject(evt); + var index; + var key; + + for (key in listeners) { + if (listeners.hasOwnProperty(key)) { + index = indexOfListener(listeners[key], listener); + + if (index !== -1) { + listeners[key].splice(index, 1); + } + } + } + + return this; + }; + + /** + * Alias of removeListener + */ + proto.off = alias('removeListener'); + + /** + * Adds listeners in bulk using the manipulateListeners method. + * If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added. + * You can also pass it a regular expression to add the array of listeners to all events that match it. + * Yeah, this function does quite a bit. That's probably a bad thing. + * + * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once. + * @param {Function[]} [listeners] An optional array of listener functions to add. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.addListeners = function addListeners(evt, listeners) { + // Pass through to manipulateListeners + return this.manipulateListeners(false, evt, listeners); + }; + + /** + * Removes listeners in bulk using the manipulateListeners method. + * If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. + * You can also pass it an event name and an array of listeners to be removed. + * You can also pass it a regular expression to remove the listeners from all events that match it. + * + * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once. + * @param {Function[]} [listeners] An optional array of listener functions to remove. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.removeListeners = function removeListeners(evt, listeners) { + // Pass through to manipulateListeners + return this.manipulateListeners(true, evt, listeners); + }; + + /** + * Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level. + * The first argument will determine if the listeners are removed (true) or added (false). + * If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. + * You can also pass it an event name and an array of listeners to be added/removed. + * You can also pass it a regular expression to manipulate the listeners of all events that match it. + * + * @param {Boolean} remove True if you want to remove listeners, false if you want to add. + * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once. + * @param {Function[]} [listeners] An optional array of listener functions to add/remove. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) { + var i; + var value; + var single = remove ? this.removeListener : this.addListener; + var multiple = remove ? this.removeListeners : this.addListeners; + + // If evt is an object then pass each of its properties to this method + if (typeof evt === 'object' && !(evt instanceof RegExp)) { + for (i in evt) { + if (evt.hasOwnProperty(i) && (value = evt[i])) { + // Pass the single listener straight through to the singular method + if (typeof value === 'function') { + single.call(this, i, value); + } + else { + // Otherwise pass back to the multiple function + multiple.call(this, i, value); + } + } + } + } + else { + // So evt must be a string + // And listeners must be an array of listeners + // Loop over it and pass each one to the multiple method + i = listeners.length; + while (i--) { + single.call(this, evt, listeners[i]); + } + } + + return this; + }; + + /** + * Removes all listeners from a specified event. + * If you do not specify an event then all listeners will be removed. + * That means every event will be emptied. + * You can also pass a regex to remove all events that match it. + * + * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.removeEvent = function removeEvent(evt) { + var type = typeof evt; + var events = this._getEvents(); + var key; + + // Remove different things depending on the state of evt + if (type === 'string') { + // Remove all listeners for the specified event + delete events[evt]; + } + else if (evt instanceof RegExp) { + // Remove all events matching the regex. + for (key in events) { + if (events.hasOwnProperty(key) && evt.test(key)) { + delete events[key]; + } + } + } + else { + // Remove all listeners in all events + delete this._events; + } + + return this; + }; + + /** + * Alias of removeEvent. + * + * Added to mirror the node API. + */ + proto.removeAllListeners = alias('removeEvent'); + + /** + * Emits an event of your choice. + * When emitted, every listener attached to that event will be executed. + * If you pass the optional argument array then those arguments will be passed to every listener upon execution. + * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately. + * So they will not arrive within the array on the other side, they will be separate. + * You can also pass a regular expression to emit to all events that match it. + * + * @param {String|RegExp} evt Name of the event to emit and execute listeners for. + * @param {Array} [args] Optional array of arguments to be passed to each listener. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.emitEvent = function emitEvent(evt, args) { + var listeners = this.getListenersAsObject(evt); + var listener; + var i; + var key; + var response; + + for (key in listeners) { + if (listeners.hasOwnProperty(key)) { + i = listeners[key].length; + + while (i--) { + // If the listener returns true then it shall be removed from the event + // The function is executed either with a basic call or an apply if there is an args array + listener = listeners[key][i]; + + if (listener.once === true) { + this.removeListener(evt, listener.listener); + } + + response = listener.listener.apply(this, args || []); + + if (response === this._getOnceReturnValue()) { + this.removeListener(evt, listener.listener); + } + } + } + } + + return this; + }; + + /** + * Alias of emitEvent + */ + proto.trigger = alias('emitEvent'); + + /** + * Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on. + * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it. + * + * @param {String|RegExp} evt Name of the event to emit and execute listeners for. + * @param {...*} Optional additional arguments to be passed to each listener. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.emit = function emit(evt) { + var args = Array.prototype.slice.call(arguments, 1); + return this.emitEvent(evt, args); + }; + + /** + * Sets the current value to check against when executing listeners. If a + * listeners return value matches the one set here then it will be removed + * after execution. This value defaults to true. + * + * @param {*} value The new value to check for when executing listeners. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.setOnceReturnValue = function setOnceReturnValue(value) { + this._onceReturnValue = value; + return this; + }; + + /** + * Fetches the current value to check against when executing listeners. If + * the listeners return value matches this one then it should be removed + * automatically. It will return true by default. + * + * @return {*|Boolean} The current value to check for or the default, true. + * @api private + */ + proto._getOnceReturnValue = function _getOnceReturnValue() { + if (this.hasOwnProperty('_onceReturnValue')) { + return this._onceReturnValue; + } + else { + return true; + } + }; + + /** + * Fetches the events object and creates one if required. + * + * @return {Object} The events storage object. + * @api private + */ + proto._getEvents = function _getEvents() { + return this._events || (this._events = {}); + }; + + /** + * Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version. + * + * @return {Function} Non conflicting EventEmitter class. + */ + EventEmitter.noConflict = function noConflict() { + exports.EventEmitter = originalGlobalValue; + return EventEmitter; + }; + + // Expose the class either via AMD, CommonJS or the global object + if (typeof define === 'function' && define.amd) { + define(function () { + return EventEmitter; + }); + } + else if (typeof module === 'object' && module.exports){ + module.exports = EventEmitter; + } + else { + exports.EventEmitter = EventEmitter; + } }.call(this)); diff --git a/EventEmitter.min.js b/EventEmitter.min.js index 5abc004..e4ebd95 100644 --- a/EventEmitter.min.js +++ b/EventEmitter.min.js @@ -1,7 +1,7 @@ /*! - * EventEmitter v4.2.8 - git.io/ee + * EventEmitter v4.2.9 - git.io/ee * Oliver Caldwell * MIT license * @preserve */ -(function(){"use strict";function t(){}function r(t,n){for(var e=t.length;e--;)if(t[e].listener===n)return e;return-1}function n(e){return function(){return this[e].apply(this,arguments)}}var e=t.prototype,i=this,s=i.EventEmitter;e.getListeners=function(n){var r,e,t=this._getEvents();if(n instanceof RegExp){r={};for(e in t)t.hasOwnProperty(e)&&n.test(e)&&(r[e]=t[e])}else r=t[n]||(t[n]=[]);return r},e.flattenListeners=function(t){var e,n=[];for(e=0;e - - EventEmitter tests - - - + + EventEmitter tests + + + - -
- - - - - + +
+ + + + + \ No newline at end of file diff --git a/tests/tests.js b/tests/tests.js index 6f7dadf..6cff17a 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,809 +1,809 @@ (function () { - /*global mocha,chai,EventEmitter*/ - 'use strict'; - - // Setup Mocha and Chai. - mocha.setup('tdd'); - var assert = chai.assert; - - function flattenCheck(check) { - var sorted = check.slice(0); - sorted.sort(function (a, b) { - return a < b ? -1 : 1; - }); - return sorted.join(); - } - - // Configure the tests - suite('getListeners', function() { - var ee; - - setup(function() { - ee = new EventEmitter(); - }); - - test('initialises the event object and a listener array', function() { - ee.getListeners('foo'); - assert.deepEqual(ee._events, { - foo: [] - }); - }); - - test('does not overwrite listener arrays', function() { - var listeners = ee.getListeners('foo'); - listeners.push('bar'); - - assert.deepEqual(ee._events, { - foo: ['bar'] - }); - - ee.getListeners('foo'); - - assert.deepEqual(ee._events, { - foo: ['bar'] - }); - }); - - test('allows you to fetch listeners by regex', function () { - var check = []; - - ee.addListener('foo', function() { check.push(1); }); - ee.addListener('bar', function() { check.push(2); return 'bar'; }); - ee.addListener('baz', function() { check.push(3); return 'baz'; }); - - var listeners = ee.getListeners(/ba[rz]/); - - assert.strictEqual(listeners.bar.length + listeners.baz.length, 2); - assert.strictEqual(listeners.bar[0].listener(), 'bar'); - assert.strictEqual(listeners.baz[0].listener(), 'baz'); - }); - - test('does not return matched sub-strings', function () { - var check = function () {}; - - ee.addListener('foo', function () {}); - ee.addListener('fooBar', check); - - var listeners = ee.getListeners('fooBar'); - assert.strictEqual(listeners.length, 1); - assert.strictEqual(listeners[0].listener, check); - }); - }); - - suite('flattenListeners', function () { - var ee; - var fn1 = function(){}; - var fn2 = function(){}; - var fn3 = function(){}; - - setup(function () { - ee = new EventEmitter(); - }); - - test('takes an array of objects and returns an array of functions', function () { - var input = [ - {listener: fn1}, - {listener: fn2}, - {listener: fn3} - ]; - var output = ee.flattenListeners(input); - assert.deepEqual(output, [fn1, fn2, fn3]); - }); - - test('if given an empty array, an empty array is returned', function () { - var output = ee.flattenListeners([]); - assert.deepEqual(output, []); - }); - }); - - suite('addListener', function() { - var ee; - var fn1 = function(){}; - var fn2 = function(){}; - - setup(function() { - ee = new EventEmitter(); - }); - - test('adds a listener to the specified event', function() { - ee.addListener('foo', fn1); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1]); - }); - - test('does not allow duplicate listeners', function() { - ee.addListener('bar', fn1); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn1]); - - ee.addListener('bar', fn2); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn1, fn2]); - - ee.addListener('bar', fn1); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn1, fn2]); - }); - - test('allows you to add listeners by regex', function () { - var check = []; - - ee.defineEvents(['bar', 'baz']); - ee.addListener('foo', function() { check.push(1); }); - ee.addListener(/ba[rz]/, function() { check.push(2); }); - ee.emitEvent(/ba[rz]/); - - assert.strictEqual(flattenCheck(check), '2,2'); - }); - - test('prevents you from adding duplicate listeners', function () { - var count = 0; - - function adder() { - count += 1; - } - - ee.addListener('foo', adder); - ee.addListener('foo', adder); - ee.addListener('foo', adder); - ee.emitEvent('foo'); - - assert.strictEqual(count, 1); - }); - }); - - suite('addOnceListener', function () { - var ee; - var counter; - var fn1 = function() { counter++; }; - - setup(function () { - ee = new EventEmitter(); - counter = 0; - }); - - test('once listeners can be added', function () { - ee.addOnceListener('foo', fn1); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1]); - }); - - test('listeners are only executed once', function () { - ee.addOnceListener('foo', fn1); - ee.emitEvent('foo'); - ee.emitEvent('foo'); - ee.emitEvent('foo'); - assert.strictEqual(counter, 1); - }); - - test('listeners can be removed', function () { - ee.addOnceListener('foo', fn1); - ee.removeListener('foo', fn1); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), []); - }); - - test('can not cause infinite recursion', function () { - ee.addOnceListener('foo', function() { - counter += 1; - this.emitEvent('foo'); - }); - ee.trigger('foo'); - assert.strictEqual(counter, 1); - }); - }); - - suite('removeListener', function() { - var ee; - var fn1 = function(){}; - var fn2 = function(){}; - var fn3 = function(){}; - var fn4 = function(){}; - var fn5 = function(){}; - var fnX = function(){}; - - setup(function() { - ee = new EventEmitter(); - }); - - test('does nothing when the listener is not found', function() { - var orig = ee.getListeners('foo').length; - ee.removeListener('foo', fn1); - assert.lengthOf(ee.getListeners('foo'), orig); - }); - - test('can handle removing events that have not been added', function() { - assert.notProperty(ee, '_events'); - ee.removeEvent('foo'); - assert.property(ee, '_events'); - assert.isObject(ee._events); - }); - - test('actually removes events', function() { - ee.removeEvent('foo'); - assert.notDeepProperty(ee, '_events.foo'); - }); - - test('removes listeners', function() { - var listeners = ee.getListeners('bar'); - - ee.addListener('bar', fn1); - ee.addListener('bar', fn2); - ee.addListener('bar', fn3); - ee.addListener('bar', fn3); // Make sure doubling up does nothing - ee.addListener('bar', fn4); - assert.deepEqual(ee.flattenListeners(listeners), [fn1, fn2, fn3, fn4]); - - ee.removeListener('bar', fn3); - assert.deepEqual(ee.flattenListeners(listeners), [fn1, fn2, fn4]); - - ee.removeListener('bar', fnX); - assert.deepEqual(ee.flattenListeners(listeners), [fn1, fn2, fn4]); - - ee.removeListener('bar', fn1); - assert.deepEqual(ee.flattenListeners(listeners), [fn2, fn4]); - - ee.removeListener('bar', fn4); - assert.deepEqual(ee.flattenListeners(listeners), [fn2]); - - ee.removeListener('bar', fn2); - assert.deepEqual(ee.flattenListeners(ee._events.bar), []); - }); - - test('removes with a regex', function() { - ee.addListeners({ - foo: [fn1, fn2, fn3, fn4, fn5], - bar: [fn1, fn2, fn3, fn4, fn5], - baz: [fn1, fn2, fn3, fn4, fn5] - }); - - ee.removeListener(/ba[rz]/, fn3); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn3, fn2, fn1]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn5, fn4, fn2, fn1]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), [fn5, fn4, fn2, fn1]); - }); - }); - - suite('getListenersAsObject', function () { - var ee; - - setup(function() { - ee = new EventEmitter(); - ee.addListener('bar', function(){}); - ee.addListener('baz', function(){}); - }); - - test('returns an object for strings', function () { - var listeners = ee.getListenersAsObject('bar'); - assert.isObject(listeners); - assert.lengthOf(listeners.bar, 1); - }); - - test('returns an object for regexs', function () { - var listeners = ee.getListenersAsObject(/ba[rz]/); - assert.isObject(listeners); - assert.lengthOf(listeners.bar, 1); - assert.lengthOf(listeners.baz, 1); - }); - }); - - suite('defineEvent', function () { - var ee; - - setup(function() { - ee = new EventEmitter(); - }); - - test('defines an event when there is nothing else inside', function () { - ee.defineEvent('foo'); - assert.isArray(ee._events.foo); - }); - - test('defines an event when there are other events already', function () { - var f = function(){}; - ee.addListener('foo', f); - ee.defineEvent('bar'); - - assert.deepEqual(ee.flattenListeners(ee._events.foo), [f]); - assert.isArray(ee._events.bar); - }); - - test('does not overwrite existing events', function () { - var f = function(){}; - ee.addListener('foo', f); - ee.defineEvent('foo'); - assert.deepEqual(ee.flattenListeners(ee._events.foo), [f]); - }); - }); - - suite('defineEvents', function () { - var ee; - - setup(function() { - ee = new EventEmitter(); - }); - - test('defines multiple events', function () { - ee.defineEvents(['foo', 'bar']); - assert.isArray(ee._events.foo, []); - assert.isArray(ee._events.bar, []); - }); - }); - - suite('removeEvent', function() { - var ee; - var fn1 = function(){}; - var fn2 = function(){}; - var fn3 = function(){}; - var fn4 = function(){}; - var fn5 = function(){}; - - setup(function() { - ee = new EventEmitter(); - - ee.addListener('foo', fn1); - ee.addListener('foo', fn2); - ee.addListener('bar', fn3); - ee.addListener('bar', fn4); - ee.addListener('baz', fn5); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn2]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn3, fn4]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), [fn5]); - }); - - test('removes all listeners for the specified event', function() { - ee.removeEvent('bar'); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn2]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); - assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), [fn5]); - - ee.removeEvent('baz'); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn2]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); - assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), []); - }); - - test('removes all events when no event is specified', function() { - ee.removeEvent(); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), []); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); - assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), []); - }); - - test('removes listeners when passed a regex', function () { - var check = []; - ee.removeEvent(); - - ee.addListener('foo', function() { check.push(1); return 'foo'; }); - ee.addListener('bar', function() { check.push(2); return 'bar'; }); - ee.addListener('baz', function() { check.push(3); return 'baz'; }); - - ee.removeEvent(/ba[rz]/); - var listeners = ee.getListeners('foo'); - - assert.lengthOf(listeners, 1); - assert.strictEqual(listeners[0].listener(), 'foo'); - }); - - test('can be used through the alias, removeAllListeners', function() { - ee.removeAllListeners('bar'); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn2]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); - assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), [fn5]); - - ee.removeAllListeners('baz'); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn2]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); - assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), []); - }); - }); - - suite('emitEvent', function() { - var ee; - - setup(function() { - ee = new EventEmitter(); - }); - - test('executes attached listeners', function() { - var run = false; - - ee.addListener('foo', function() { - run = true; - }); - ee.emitEvent('foo'); - - assert.isTrue(run); - }); - - test('executes attached with a single argument', function() { - var key = null; - - ee.addListener('bar', function(a) { - key = a; - }); - ee.emitEvent('bar', [50]); - - assert.strictEqual(key, 50); - - ee.emit('bar', 60); - assert.strictEqual(key, 60); - }); - - test('executes attached with arguments', function() { - var key = null; - - ee.addListener('bar2', function(a, b) { - key = a + b; - }); - ee.emitEvent('bar2', [40, 2]); - - assert.strictEqual(key, 42); - }); - - test('executes multiple listeners', function() { - var check = []; - - ee.addListener('baz', function() { check.push(1); }); - ee.addListener('baz', function() { check.push(2); }); - ee.addListener('baz', function() { check.push(3); }); - ee.addListener('baz', function() { check.push(4); }); - ee.addListener('baz', function() { check.push(5); }); + /*global mocha,chai,EventEmitter*/ + 'use strict'; + + // Setup Mocha and Chai. + mocha.setup('tdd'); + var assert = chai.assert; + + function flattenCheck(check) { + var sorted = check.slice(0); + sorted.sort(function (a, b) { + return a < b ? -1 : 1; + }); + return sorted.join(); + } + + // Configure the tests + suite('getListeners', function() { + var ee; + + setup(function() { + ee = new EventEmitter(); + }); + + test('initialises the event object and a listener array', function() { + ee.getListeners('foo'); + assert.deepEqual(ee._events, { + foo: [] + }); + }); + + test('does not overwrite listener arrays', function() { + var listeners = ee.getListeners('foo'); + listeners.push('bar'); + + assert.deepEqual(ee._events, { + foo: ['bar'] + }); + + ee.getListeners('foo'); + + assert.deepEqual(ee._events, { + foo: ['bar'] + }); + }); + + test('allows you to fetch listeners by regex', function () { + var check = []; + + ee.addListener('foo', function() { check.push(1); }); + ee.addListener('bar', function() { check.push(2); return 'bar'; }); + ee.addListener('baz', function() { check.push(3); return 'baz'; }); + + var listeners = ee.getListeners(/ba[rz]/); + + assert.strictEqual(listeners.bar.length + listeners.baz.length, 2); + assert.strictEqual(listeners.bar[0].listener(), 'bar'); + assert.strictEqual(listeners.baz[0].listener(), 'baz'); + }); + + test('does not return matched sub-strings', function () { + var check = function () {}; + + ee.addListener('foo', function () {}); + ee.addListener('fooBar', check); + + var listeners = ee.getListeners('fooBar'); + assert.strictEqual(listeners.length, 1); + assert.strictEqual(listeners[0].listener, check); + }); + }); + + suite('flattenListeners', function () { + var ee; + var fn1 = function(){}; + var fn2 = function(){}; + var fn3 = function(){}; + + setup(function () { + ee = new EventEmitter(); + }); + + test('takes an array of objects and returns an array of functions', function () { + var input = [ + {listener: fn1}, + {listener: fn2}, + {listener: fn3} + ]; + var output = ee.flattenListeners(input); + assert.deepEqual(output, [fn1, fn2, fn3]); + }); + + test('if given an empty array, an empty array is returned', function () { + var output = ee.flattenListeners([]); + assert.deepEqual(output, []); + }); + }); + + suite('addListener', function() { + var ee; + var fn1 = function(){}; + var fn2 = function(){}; + + setup(function() { + ee = new EventEmitter(); + }); + + test('adds a listener to the specified event', function() { + ee.addListener('foo', fn1); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1]); + }); + + test('does not allow duplicate listeners', function() { + ee.addListener('bar', fn1); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn1]); + + ee.addListener('bar', fn2); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn1, fn2]); + + ee.addListener('bar', fn1); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn1, fn2]); + }); + + test('allows you to add listeners by regex', function () { + var check = []; + + ee.defineEvents(['bar', 'baz']); + ee.addListener('foo', function() { check.push(1); }); + ee.addListener(/ba[rz]/, function() { check.push(2); }); + ee.emitEvent(/ba[rz]/); + + assert.strictEqual(flattenCheck(check), '2,2'); + }); + + test('prevents you from adding duplicate listeners', function () { + var count = 0; + + function adder() { + count += 1; + } + + ee.addListener('foo', adder); + ee.addListener('foo', adder); + ee.addListener('foo', adder); + ee.emitEvent('foo'); + + assert.strictEqual(count, 1); + }); + }); + + suite('addOnceListener', function () { + var ee; + var counter; + var fn1 = function() { counter++; }; + + setup(function () { + ee = new EventEmitter(); + counter = 0; + }); + + test('once listeners can be added', function () { + ee.addOnceListener('foo', fn1); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1]); + }); + + test('listeners are only executed once', function () { + ee.addOnceListener('foo', fn1); + ee.emitEvent('foo'); + ee.emitEvent('foo'); + ee.emitEvent('foo'); + assert.strictEqual(counter, 1); + }); + + test('listeners can be removed', function () { + ee.addOnceListener('foo', fn1); + ee.removeListener('foo', fn1); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), []); + }); + + test('can not cause infinite recursion', function () { + ee.addOnceListener('foo', function() { + counter += 1; + this.emitEvent('foo'); + }); + ee.trigger('foo'); + assert.strictEqual(counter, 1); + }); + }); + + suite('removeListener', function() { + var ee; + var fn1 = function(){}; + var fn2 = function(){}; + var fn3 = function(){}; + var fn4 = function(){}; + var fn5 = function(){}; + var fnX = function(){}; + + setup(function() { + ee = new EventEmitter(); + }); + + test('does nothing when the listener is not found', function() { + var orig = ee.getListeners('foo').length; + ee.removeListener('foo', fn1); + assert.lengthOf(ee.getListeners('foo'), orig); + }); + + test('can handle removing events that have not been added', function() { + assert.notProperty(ee, '_events'); + ee.removeEvent('foo'); + assert.property(ee, '_events'); + assert.isObject(ee._events); + }); + + test('actually removes events', function() { + ee.removeEvent('foo'); + assert.notDeepProperty(ee, '_events.foo'); + }); + + test('removes listeners', function() { + var listeners = ee.getListeners('bar'); + + ee.addListener('bar', fn1); + ee.addListener('bar', fn2); + ee.addListener('bar', fn3); + ee.addListener('bar', fn3); // Make sure doubling up does nothing + ee.addListener('bar', fn4); + assert.deepEqual(ee.flattenListeners(listeners), [fn1, fn2, fn3, fn4]); + + ee.removeListener('bar', fn3); + assert.deepEqual(ee.flattenListeners(listeners), [fn1, fn2, fn4]); + + ee.removeListener('bar', fnX); + assert.deepEqual(ee.flattenListeners(listeners), [fn1, fn2, fn4]); + + ee.removeListener('bar', fn1); + assert.deepEqual(ee.flattenListeners(listeners), [fn2, fn4]); + + ee.removeListener('bar', fn4); + assert.deepEqual(ee.flattenListeners(listeners), [fn2]); + + ee.removeListener('bar', fn2); + assert.deepEqual(ee.flattenListeners(ee._events.bar), []); + }); + + test('removes with a regex', function() { + ee.addListeners({ + foo: [fn1, fn2, fn3, fn4, fn5], + bar: [fn1, fn2, fn3, fn4, fn5], + baz: [fn1, fn2, fn3, fn4, fn5] + }); + + ee.removeListener(/ba[rz]/, fn3); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn3, fn2, fn1]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn5, fn4, fn2, fn1]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), [fn5, fn4, fn2, fn1]); + }); + }); + + suite('getListenersAsObject', function () { + var ee; + + setup(function() { + ee = new EventEmitter(); + ee.addListener('bar', function(){}); + ee.addListener('baz', function(){}); + }); + + test('returns an object for strings', function () { + var listeners = ee.getListenersAsObject('bar'); + assert.isObject(listeners); + assert.lengthOf(listeners.bar, 1); + }); + + test('returns an object for regexs', function () { + var listeners = ee.getListenersAsObject(/ba[rz]/); + assert.isObject(listeners); + assert.lengthOf(listeners.bar, 1); + assert.lengthOf(listeners.baz, 1); + }); + }); + + suite('defineEvent', function () { + var ee; + + setup(function() { + ee = new EventEmitter(); + }); + + test('defines an event when there is nothing else inside', function () { + ee.defineEvent('foo'); + assert.isArray(ee._events.foo); + }); + + test('defines an event when there are other events already', function () { + var f = function(){}; + ee.addListener('foo', f); + ee.defineEvent('bar'); + + assert.deepEqual(ee.flattenListeners(ee._events.foo), [f]); + assert.isArray(ee._events.bar); + }); + + test('does not overwrite existing events', function () { + var f = function(){}; + ee.addListener('foo', f); + ee.defineEvent('foo'); + assert.deepEqual(ee.flattenListeners(ee._events.foo), [f]); + }); + }); + + suite('defineEvents', function () { + var ee; + + setup(function() { + ee = new EventEmitter(); + }); + + test('defines multiple events', function () { + ee.defineEvents(['foo', 'bar']); + assert.isArray(ee._events.foo, []); + assert.isArray(ee._events.bar, []); + }); + }); + + suite('removeEvent', function() { + var ee; + var fn1 = function(){}; + var fn2 = function(){}; + var fn3 = function(){}; + var fn4 = function(){}; + var fn5 = function(){}; + + setup(function() { + ee = new EventEmitter(); + + ee.addListener('foo', fn1); + ee.addListener('foo', fn2); + ee.addListener('bar', fn3); + ee.addListener('bar', fn4); + ee.addListener('baz', fn5); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn2]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn3, fn4]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), [fn5]); + }); + + test('removes all listeners for the specified event', function() { + ee.removeEvent('bar'); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn2]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); + assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), [fn5]); + + ee.removeEvent('baz'); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn2]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); + assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), []); + }); + + test('removes all events when no event is specified', function() { + ee.removeEvent(); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), []); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); + assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), []); + }); + + test('removes listeners when passed a regex', function () { + var check = []; + ee.removeEvent(); + + ee.addListener('foo', function() { check.push(1); return 'foo'; }); + ee.addListener('bar', function() { check.push(2); return 'bar'; }); + ee.addListener('baz', function() { check.push(3); return 'baz'; }); + + ee.removeEvent(/ba[rz]/); + var listeners = ee.getListeners('foo'); + + assert.lengthOf(listeners, 1); + assert.strictEqual(listeners[0].listener(), 'foo'); + }); + + test('can be used through the alias, removeAllListeners', function() { + ee.removeAllListeners('bar'); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn2]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); + assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), [fn5]); + + ee.removeAllListeners('baz'); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn2]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); + assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), []); + }); + }); + + suite('emitEvent', function() { + var ee; + + setup(function() { + ee = new EventEmitter(); + }); + + test('executes attached listeners', function() { + var run = false; + + ee.addListener('foo', function() { + run = true; + }); + ee.emitEvent('foo'); + + assert.isTrue(run); + }); + + test('executes attached with a single argument', function() { + var key = null; + + ee.addListener('bar', function(a) { + key = a; + }); + ee.emitEvent('bar', [50]); + + assert.strictEqual(key, 50); + + ee.emit('bar', 60); + assert.strictEqual(key, 60); + }); + + test('executes attached with arguments', function() { + var key = null; + + ee.addListener('bar2', function(a, b) { + key = a + b; + }); + ee.emitEvent('bar2', [40, 2]); + + assert.strictEqual(key, 42); + }); + + test('executes multiple listeners', function() { + var check = []; + + ee.addListener('baz', function() { check.push(1); }); + ee.addListener('baz', function() { check.push(2); }); + ee.addListener('baz', function() { check.push(3); }); + ee.addListener('baz', function() { check.push(4); }); + ee.addListener('baz', function() { check.push(5); }); - ee.emitEvent('baz'); + ee.emitEvent('baz'); - assert.strictEqual(flattenCheck(check), '1,2,3,4,5'); - }); + assert.strictEqual(flattenCheck(check), '1,2,3,4,5'); + }); - test('executes multiple listeners after one has been removed', function() { - var check = []; - var toRemove = function() { check.push('R'); }; + test('executes multiple listeners after one has been removed', function() { + var check = []; + var toRemove = function() { check.push('R'); }; - ee.addListener('baz', function() { check.push(1); }); - ee.addListener('baz', function() { check.push(2); }); - ee.addListener('baz', toRemove); - ee.addListener('baz', function() { check.push(3); }); - ee.addListener('baz', function() { check.push(4); }); + ee.addListener('baz', function() { check.push(1); }); + ee.addListener('baz', function() { check.push(2); }); + ee.addListener('baz', toRemove); + ee.addListener('baz', function() { check.push(3); }); + ee.addListener('baz', function() { check.push(4); }); - ee.removeListener('baz', toRemove); + ee.removeListener('baz', toRemove); - ee.emitEvent('baz'); + ee.emitEvent('baz'); - assert.strictEqual(flattenCheck(check), '1,2,3,4'); - }); + assert.strictEqual(flattenCheck(check), '1,2,3,4'); + }); - test('executes multiple listeners and removes those that return true', function() { - var check = []; + test('executes multiple listeners and removes those that return true', function() { + var check = []; - ee.addListener('baz', function() { check.push(1); }); - ee.addListener('baz', function() { check.push(2); return true; }); - ee.addListener('baz', function() { check.push(3); return false; }); - ee.addListener('baz', function() { check.push(4); return 1; }); - ee.addListener('baz', function() { check.push(5); return true; }); + ee.addListener('baz', function() { check.push(1); }); + ee.addListener('baz', function() { check.push(2); return true; }); + ee.addListener('baz', function() { check.push(3); return false; }); + ee.addListener('baz', function() { check.push(4); return 1; }); + ee.addListener('baz', function() { check.push(5); return true; }); - ee.emitEvent('baz'); - ee.emitEvent('baz'); + ee.emitEvent('baz'); + ee.emitEvent('baz'); - assert.strictEqual(flattenCheck(check), '1,1,2,3,3,4,4,5'); - }); + assert.strictEqual(flattenCheck(check), '1,1,2,3,3,4,4,5'); + }); - test('can remove listeners that return true and also define another listener within them', function () { - var check = []; + test('can remove listeners that return true and also define another listener within them', function () { + var check = []; - ee.addListener('baz', function() { check.push(1); }); + ee.addListener('baz', function() { check.push(1); }); - ee.addListener('baz', function() { - ee.addListener('baz', function() { - check.push(2); - }); + ee.addListener('baz', function() { + ee.addListener('baz', function() { + check.push(2); + }); - check.push(3); - return true; - }); + check.push(3); + return true; + }); - ee.addListener('baz', function() { check.push(4); return false; }); - ee.addListener('baz', function() { check.push(5); return 1; }); - ee.addListener('baz', function() { check.push(6); return true; }); + ee.addListener('baz', function() { check.push(4); return false; }); + ee.addListener('baz', function() { check.push(5); return 1; }); + ee.addListener('baz', function() { check.push(6); return true; }); - ee.emitEvent('baz'); - ee.emitEvent('baz'); + ee.emitEvent('baz'); + ee.emitEvent('baz'); - assert.strictEqual(flattenCheck(check), '1,1,2,3,4,4,5,5,6'); - }); + assert.strictEqual(flattenCheck(check), '1,1,2,3,4,4,5,5,6'); + }); - test('executes all listeners that match a regular expression', function () { - var check = []; + test('executes all listeners that match a regular expression', function () { + var check = []; - ee.addListener('foo', function() { check.push(1); }); - ee.addListener('bar', function() { check.push(2); }); - ee.addListener('baz', function() { check.push(3); }); + ee.addListener('foo', function() { check.push(1); }); + ee.addListener('bar', function() { check.push(2); }); + ee.addListener('baz', function() { check.push(3); }); - ee.emitEvent(/ba[rz]/); - assert.strictEqual(flattenCheck(check), '2,3'); - }); + ee.emitEvent(/ba[rz]/); + assert.strictEqual(flattenCheck(check), '2,3'); + }); - test('global object is defined', function() { - ee.addListener('foo', function() { - assert.equal(this, ee); - }); + test('global object is defined', function() { + ee.addListener('foo', function() { + assert.equal(this, ee); + }); - ee.emitEvent('foo'); - }); - }); + ee.emitEvent('foo'); + }); + }); - suite('manipulateListeners', function() { - var ee; - var fn1 = function(){}; - var fn2 = function(){}; - var fn3 = function(){}; - var fn4 = function(){}; - var fn5 = function(){}; - - setup(function() { - ee = new EventEmitter(); - }); - - test('manipulates multiple with an array', function() { - ee.manipulateListeners(false, 'foo', [fn1, fn2, fn3, fn4, fn5]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn3, fn2, fn1]); - - ee.manipulateListeners(true, 'foo', [fn1, fn2]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn3]); - - ee.manipulateListeners(true, 'foo', [fn3, fn5]); - ee.manipulateListeners(false, 'foo', [fn4, fn1]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn4, fn1]); - - ee.manipulateListeners(true, 'foo', [fn4, fn1]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), []); - }); - - test('manipulates with an object', function() { - ee.manipulateListeners(false, { - foo: [fn1, fn2, fn3], - bar: fn4 - }); - - ee.manipulateListeners(false, { - bar: [fn5, fn1] - }); - - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn3, fn2, fn1]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn4, fn1, fn5]); - - ee.manipulateListeners(true, { - foo: fn1, - bar: [fn5, fn4] - }); - - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn3, fn2]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn1]); - - ee.manipulateListeners(true, { - foo: [fn3, fn2], - bar: fn1 - }); - - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), []); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); - }); - - test('does not execute listeners just after they are added in another listeners', function() { - var check = []; - - ee.addListener('baz', function() { check.push(1); }); - ee.addListener('baz', function() { check.push(2); }); - ee.addListener('baz', function() { - check.push(3); - - ee.addListener('baz', function() { - check.push(4); - }); - }); - ee.addListener('baz', function() { check.push(5); }); - ee.addListener('baz', function() { check.push(6); }); - - ee.emitEvent('baz'); - - assert.strictEqual(flattenCheck(check), '1,2,3,5,6'); - }); - }); - - suite('addListeners', function() { - var ee; - var fn1 = function(){}; - var fn2 = function(){}; - var fn3 = function(){}; - var fn4 = function(){}; - var fn5 = function(){}; - - setup(function() { - ee = new EventEmitter(); - }); - - test('adds with an array', function() { - ee.addListeners('foo', [fn1, fn2, fn3]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn3, fn2, fn1]); - - ee.addListeners('foo', [fn4, fn5]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn3, fn2, fn1, fn5, fn4]); - }); - - test('adds with an object', function() { - ee.addListeners({ - foo: fn1, - bar: [fn2, fn3] - }); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn3, fn2]); - - ee.addListeners({ - foo: [fn4], - bar: fn5 - }); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn4]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn3, fn2, fn5]); - }); - - test('allows you to add listeners by regex', function () { - var check = []; - - ee.defineEvents(['bar', 'baz']); - ee.addListeners('foo', [function() { check.push(1); }]); - ee.addListeners(/ba[rz]/, [function() { check.push(2); }, function() { check.push(3); }]); - ee.emitEvent(/ba[rz]/); - - assert.strictEqual(flattenCheck(check), '2,2,3,3'); - }); - }); - - suite('removeListeners', function() { - var ee; - var fn1 = function(){}; - var fn2 = function(){}; - var fn3 = function(){}; - var fn4 = function(){}; - var fn5 = function(){}; - - setup(function() { - ee = new EventEmitter(); - }); - - test('removes with an array', function() { - ee.addListeners('foo', [fn1, fn2, fn3, fn4, fn5]); - ee.removeListeners('foo', [fn2, fn3]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn1]); - - ee.removeListeners('foo', [fn5, fn4]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1]); - - ee.removeListeners('foo', [fn1]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), []); - }); - - test('removes with an object', function() { - ee.addListeners({ - foo: [fn1, fn2, fn3, fn4, fn5], - bar: [fn1, fn2, fn3, fn4, fn5] - }); - - ee.removeListeners({ - foo: fn2, - bar: [fn3, fn4, fn5] - }); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn3, fn1]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn2, fn1]); - - ee.removeListeners({ - foo: [fn3], - bar: [fn2, fn1] - }); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn1]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); - }); - - test('removes with a regex', function() { - ee.addListeners({ - foo: [fn1, fn2, fn3, fn4, fn5], - bar: [fn1, fn2, fn3, fn4, fn5], - baz: [fn1, fn2, fn3, fn4, fn5] - }); - - ee.removeListeners(/ba[rz]/, [fn3, fn4]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn3, fn2, fn1]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn5, fn2, fn1]); - assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), [fn5, fn2, fn1]); - }); - }); - - suite('setOnceReturnValue', function() { - var ee; - - setup(function () { - ee = new EventEmitter(); - }); - - test('will remove if left as default and returning true', function () { - var check = []; - - ee.addListener('baz', function() { check.push(1); }); - ee.addListener('baz', function() { check.push(2); return true; }); - ee.addListener('baz', function() { check.push(3); return false; }); - ee.addListener('baz', function() { check.push(4); return 1; }); - ee.addListener('baz', function() { check.push(5); return true; }); - - ee.emitEvent('baz'); - ee.emitEvent('baz'); - - assert.strictEqual(flattenCheck(check), '1,1,2,3,3,4,4,5'); - }); - - test('will remove those that return a string when set to that string', function () { - var check = []; - - ee.setOnceReturnValue('only-once'); - ee.addListener('baz', function() { check.push(1); }); - ee.addListener('baz', function() { check.push(2); return true; }); - ee.addListener('baz', function() { check.push(3); return 'only-once'; }); - ee.addListener('baz', function() { check.push(4); return 1; }); - ee.addListener('baz', function() { check.push(5); return 'only-once'; }); - ee.addListener('baz', function() { check.push(6); return true; }); - - ee.emitEvent('baz'); - ee.emitEvent('baz'); - - assert.strictEqual(flattenCheck(check), '1,1,2,2,3,4,4,5,6,6'); - }); - - test('will not remove those that return a different string to the one that is set', function () { - var check = []; - - ee.setOnceReturnValue('only-once'); - ee.addListener('baz', function() { check.push(1); }); - ee.addListener('baz', function() { check.push(2); return true; }); - ee.addListener('baz', function() { check.push(3); return 'not-only-once'; }); - ee.addListener('baz', function() { check.push(4); return 1; }); - ee.addListener('baz', function() { check.push(5); return 'only-once'; }); - ee.addListener('baz', function() { check.push(6); return true; }); - - ee.emitEvent('baz'); - ee.emitEvent('baz'); - - assert.strictEqual(flattenCheck(check), '1,1,2,2,3,3,4,4,5,6,6'); - }); - }); - - suite('alias', function () { - test('that it works when overwriting target method', function () { - var addListener = EventEmitter.prototype.addListener; - var res; - var rand = Math.random(); - - EventEmitter.prototype.addListener = function () { - res = rand; - }; - - var ee = new EventEmitter(); - ee.on(); - - assert.strictEqual(res, rand); - - EventEmitter.prototype.addListener = addListener; - }); - }); - - suite('noConflict', function () { - var _EventEmitter = EventEmitter; - - teardown(function () { - EventEmitter = _EventEmitter; - }); - - test('reverts the global `EventEmitter` to its previous value', function () { - EventEmitter.noConflict(); + suite('manipulateListeners', function() { + var ee; + var fn1 = function(){}; + var fn2 = function(){}; + var fn3 = function(){}; + var fn4 = function(){}; + var fn5 = function(){}; + + setup(function() { + ee = new EventEmitter(); + }); + + test('manipulates multiple with an array', function() { + ee.manipulateListeners(false, 'foo', [fn1, fn2, fn3, fn4, fn5]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn3, fn2, fn1]); + + ee.manipulateListeners(true, 'foo', [fn1, fn2]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn3]); + + ee.manipulateListeners(true, 'foo', [fn3, fn5]); + ee.manipulateListeners(false, 'foo', [fn4, fn1]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn4, fn1]); + + ee.manipulateListeners(true, 'foo', [fn4, fn1]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), []); + }); + + test('manipulates with an object', function() { + ee.manipulateListeners(false, { + foo: [fn1, fn2, fn3], + bar: fn4 + }); + + ee.manipulateListeners(false, { + bar: [fn5, fn1] + }); + + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn3, fn2, fn1]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn4, fn1, fn5]); + + ee.manipulateListeners(true, { + foo: fn1, + bar: [fn5, fn4] + }); + + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn3, fn2]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn1]); + + ee.manipulateListeners(true, { + foo: [fn3, fn2], + bar: fn1 + }); + + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), []); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); + }); + + test('does not execute listeners just after they are added in another listeners', function() { + var check = []; + + ee.addListener('baz', function() { check.push(1); }); + ee.addListener('baz', function() { check.push(2); }); + ee.addListener('baz', function() { + check.push(3); + + ee.addListener('baz', function() { + check.push(4); + }); + }); + ee.addListener('baz', function() { check.push(5); }); + ee.addListener('baz', function() { check.push(6); }); + + ee.emitEvent('baz'); + + assert.strictEqual(flattenCheck(check), '1,2,3,5,6'); + }); + }); + + suite('addListeners', function() { + var ee; + var fn1 = function(){}; + var fn2 = function(){}; + var fn3 = function(){}; + var fn4 = function(){}; + var fn5 = function(){}; + + setup(function() { + ee = new EventEmitter(); + }); + + test('adds with an array', function() { + ee.addListeners('foo', [fn1, fn2, fn3]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn3, fn2, fn1]); + + ee.addListeners('foo', [fn4, fn5]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn3, fn2, fn1, fn5, fn4]); + }); + + test('adds with an object', function() { + ee.addListeners({ + foo: fn1, + bar: [fn2, fn3] + }); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn3, fn2]); + + ee.addListeners({ + foo: [fn4], + bar: fn5 + }); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1, fn4]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn3, fn2, fn5]); + }); + + test('allows you to add listeners by regex', function () { + var check = []; + + ee.defineEvents(['bar', 'baz']); + ee.addListeners('foo', [function() { check.push(1); }]); + ee.addListeners(/ba[rz]/, [function() { check.push(2); }, function() { check.push(3); }]); + ee.emitEvent(/ba[rz]/); + + assert.strictEqual(flattenCheck(check), '2,2,3,3'); + }); + }); + + suite('removeListeners', function() { + var ee; + var fn1 = function(){}; + var fn2 = function(){}; + var fn3 = function(){}; + var fn4 = function(){}; + var fn5 = function(){}; + + setup(function() { + ee = new EventEmitter(); + }); + + test('removes with an array', function() { + ee.addListeners('foo', [fn1, fn2, fn3, fn4, fn5]); + ee.removeListeners('foo', [fn2, fn3]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn1]); + + ee.removeListeners('foo', [fn5, fn4]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn1]); + + ee.removeListeners('foo', [fn1]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), []); + }); + + test('removes with an object', function() { + ee.addListeners({ + foo: [fn1, fn2, fn3, fn4, fn5], + bar: [fn1, fn2, fn3, fn4, fn5] + }); + + ee.removeListeners({ + foo: fn2, + bar: [fn3, fn4, fn5] + }); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn3, fn1]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn2, fn1]); + + ee.removeListeners({ + foo: [fn3], + bar: [fn2, fn1] + }); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn1]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), []); + }); + + test('removes with a regex', function() { + ee.addListeners({ + foo: [fn1, fn2, fn3, fn4, fn5], + bar: [fn1, fn2, fn3, fn4, fn5], + baz: [fn1, fn2, fn3, fn4, fn5] + }); + + ee.removeListeners(/ba[rz]/, [fn3, fn4]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('foo')), [fn5, fn4, fn3, fn2, fn1]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn5, fn2, fn1]); + assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), [fn5, fn2, fn1]); + }); + }); + + suite('setOnceReturnValue', function() { + var ee; + + setup(function () { + ee = new EventEmitter(); + }); + + test('will remove if left as default and returning true', function () { + var check = []; + + ee.addListener('baz', function() { check.push(1); }); + ee.addListener('baz', function() { check.push(2); return true; }); + ee.addListener('baz', function() { check.push(3); return false; }); + ee.addListener('baz', function() { check.push(4); return 1; }); + ee.addListener('baz', function() { check.push(5); return true; }); + + ee.emitEvent('baz'); + ee.emitEvent('baz'); + + assert.strictEqual(flattenCheck(check), '1,1,2,3,3,4,4,5'); + }); + + test('will remove those that return a string when set to that string', function () { + var check = []; + + ee.setOnceReturnValue('only-once'); + ee.addListener('baz', function() { check.push(1); }); + ee.addListener('baz', function() { check.push(2); return true; }); + ee.addListener('baz', function() { check.push(3); return 'only-once'; }); + ee.addListener('baz', function() { check.push(4); return 1; }); + ee.addListener('baz', function() { check.push(5); return 'only-once'; }); + ee.addListener('baz', function() { check.push(6); return true; }); + + ee.emitEvent('baz'); + ee.emitEvent('baz'); + + assert.strictEqual(flattenCheck(check), '1,1,2,2,3,4,4,5,6,6'); + }); + + test('will not remove those that return a different string to the one that is set', function () { + var check = []; + + ee.setOnceReturnValue('only-once'); + ee.addListener('baz', function() { check.push(1); }); + ee.addListener('baz', function() { check.push(2); return true; }); + ee.addListener('baz', function() { check.push(3); return 'not-only-once'; }); + ee.addListener('baz', function() { check.push(4); return 1; }); + ee.addListener('baz', function() { check.push(5); return 'only-once'; }); + ee.addListener('baz', function() { check.push(6); return true; }); + + ee.emitEvent('baz'); + ee.emitEvent('baz'); + + assert.strictEqual(flattenCheck(check), '1,1,2,2,3,3,4,4,5,6,6'); + }); + }); + + suite('alias', function () { + test('that it works when overwriting target method', function () { + var addListener = EventEmitter.prototype.addListener; + var res; + var rand = Math.random(); + + EventEmitter.prototype.addListener = function () { + res = rand; + }; + + var ee = new EventEmitter(); + ee.on(); + + assert.strictEqual(res, rand); + + EventEmitter.prototype.addListener = addListener; + }); + }); + + suite('noConflict', function () { + var _EventEmitter = EventEmitter; + + teardown(function () { + EventEmitter = _EventEmitter; + }); + + test('reverts the global `EventEmitter` to its previous value', function () { + EventEmitter.noConflict(); - assert.isUndefined(EventEmitter); - }); - - test('returns `EventEmitter`', function () { - assert.strictEqual(EventEmitter.noConflict(), _EventEmitter); - }); - }); + assert.isUndefined(EventEmitter); + }); + + test('returns `EventEmitter`', function () { + assert.strictEqual(EventEmitter.noConflict(), _EventEmitter); + }); + }); - // Execute the tests. - mocha.run(); + // Execute the tests. + mocha.run(); }.call(this)); \ No newline at end of file diff --git a/tools/dist.sh b/tools/dist.sh index b1733af..45ef047 100755 --- a/tools/dist.sh +++ b/tools/dist.sh @@ -1,6 +1,6 @@ #!/bin/bash node_modules/.bin/uglifyjs\ - --comments\ - --mangle sort=true\ - --compress\ - --output EventEmitter.min.js EventEmitter.js \ No newline at end of file + --comments\ + --mangle sort=true\ + --compress\ + --output EventEmitter.min.js EventEmitter.js \ No newline at end of file