diff --git a/EventEmitter.js b/EventEmitter.js index 1a4c7ca..728a43c 100644 --- a/EventEmitter.js +++ b/EventEmitter.js @@ -1,5 +1,5 @@ /*! - * EventEmitter v4.2.7 - git.io/ee + * EventEmitter v4.2.8 - git.io/ee * Oliver Caldwell * MIT license * @preserve @@ -22,7 +22,7 @@ var originalGlobalValue = exports.EventEmitter; /** - * Finds the index of the listener for the event in it's storage array. + * 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. @@ -153,7 +153,7 @@ /** * Semi-alias of addListener. It will add a listener that will be - * automatically removed after it's first execution. + * 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. @@ -275,7 +275,7 @@ var single = remove ? this.removeListener : this.addListener; var multiple = remove ? this.removeListeners : this.addListeners; - // If evt is an object then pass each of it's properties to this method + // 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])) { diff --git a/EventEmitter.min.js b/EventEmitter.min.js index 12fb8a1..5abc004 100644 --- a/EventEmitter.min.js +++ b/EventEmitter.min.js @@ -1,5 +1,5 @@ /*! - * EventEmitter v4.2.7 - git.io/ee + * EventEmitter v4.2.8 - git.io/ee * Oliver Caldwell * MIT license * @preserve diff --git a/bower.json b/bower.json index c703af7..31dc483 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "eventEmitter", "description": "Event based JavaScript for the browser", - "version": "4.2.7", + "version": "4.2.8", "main": [ "./EventEmitter.js" ], diff --git a/component.json b/component.json index 59a983f..80b0d40 100644 --- a/component.json +++ b/component.json @@ -2,7 +2,7 @@ "name": "eventEmitter", "repo": "Wolfy87/EventEmitter", "description": "Event based JavaScript for the browser.", - "version": "4.2.7", + "version": "4.2.8", "scripts": ["EventEmitter.js"], "main": "EventEmitter.js", "license": "MIT" diff --git a/docs/api.md b/docs/api.md index e57cee1..8be9425 100644 --- a/docs/api.md +++ b/docs/api.md @@ -9,148 +9,125 @@ You may also be interested in [the guide](https://github.com/Wolfy87/EventEmitte
Class for managing events.
Can be extended to provide event functionality in other classes.
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.
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. - ## getListenersAsObjectFetches 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. + * **param** (StringRegExp) _evt_ - Name of the event to return the listeners from. * **return** (Object) - All listener functions for an event in an object. - ## addListenerAdds 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.
Alias of addListener
- ## addOnceListener -Semi-alias of addListener. It will add a listener that will be
automatically removed after it's first execution.
Semi-alias of addListener. It will add a listener that will be
automatically removed after its first execution.
Alias of addOnceListener.
- ## defineEvent -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.
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.
Uses defineEvent to define multiple events.
* **param** (String[]) _evts_ - An array of event names to define. * **return** (Object) - Current instance of EventEmitter for chaining. - ## removeListenerRemoves 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.
Alias of removeListener
- ## addListeners -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.
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.
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.
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.
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.
Alias of removeEvent.
- -Added to mirror the node API.
- +Alias of removeEvent.
Added to mirror the node API.
## emitEventEmits 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.
Alias of emitEvent
- ## emitSubtly 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.
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.
Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version.
diff --git a/package.json b/package.json index c676c12..18c4db9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wolfy87-eventemitter", - "version": "4.2.7", + "version": "4.2.8", "description": "Event based JavaScript for the browser", "main": "EventEmitter.js", "directories": { diff --git a/tests/tests.js b/tests/tests.js index c9a8ad6..6f7dadf 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -44,8 +44,7 @@ }); }); - test('allows you to fetch listeners by regex', function () - { + test('allows you to fetch listeners by regex', function () { var check = []; ee.addListener('foo', function() { check.push(1); }); @@ -122,8 +121,7 @@ assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn1, fn2]); }); - test('allows you to add listeners by regex', function () - { + test('allows you to add listeners by regex', function () { var check = []; ee.defineEvents(['bar', 'baz']); @@ -133,6 +131,21 @@ 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 () { @@ -351,8 +364,7 @@ assert.deepEqual(ee.flattenListeners(ee.getListeners('baz')), []); }); - test('removes listeners when passed a regex', function () - { + test('removes listeners when passed a regex', function () { var check = []; ee.removeEvent(); @@ -493,8 +505,7 @@ assert.strictEqual(flattenCheck(check), '1,1,2,3,4,4,5,5,6'); }); - test('executes all listeners that match a regular expression', function () - { + test('executes all listeners that match a regular expression', function () { var check = []; ee.addListener('foo', function() { check.push(1); }); @@ -505,8 +516,7 @@ assert.strictEqual(flattenCheck(check), '2,3'); }); - test('global object is defined', function() - { + test('global object is defined', function() { ee.addListener('foo', function() { assert.equal(this, ee); }); @@ -629,8 +639,7 @@ assert.deepEqual(ee.flattenListeners(ee.getListeners('bar')), [fn3, fn2, fn5]); }); - test('allows you to add listeners by regex', function () - { + test('allows you to add listeners by regex', function () { var check = []; ee.defineEvents(['bar', 'baz']);