Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
Merge branch 'release/4.2.8'
Browse files Browse the repository at this point in the history
  • Loading branch information
Olical committed Aug 31, 2014
2 parents 2a8307a + 09aed18 commit 4ad612d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 59 deletions.
8 changes: 4 additions & 4 deletions EventEmitter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* EventEmitter v4.2.7 - git.io/ee
* EventEmitter v4.2.8 - git.io/ee
* Oliver Caldwell
* MIT license
* @preserve
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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])) {
Expand Down
2 changes: 1 addition & 1 deletion EventEmitter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "eventEmitter",
"description": "Event based JavaScript for the browser",
"version": "4.2.7",
"version": "4.2.8",
"main": [
"./EventEmitter.js"
],
Expand Down
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
55 changes: 16 additions & 39 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,148 +9,125 @@ You may also be interested in [the guide](https://github.com/Wolfy87/EventEmitte
<p>Class for managing events.<br />Can be extended to provide event functionality in other classes.</p>

* **class** - [object Object]

## getListeners

<p>Returns the listener array for the specified event.<br />Will initialise the event object and listener arrays if required.<br />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.<br />Each property in the object response is an array of listener functions.</p>

* **param** (String | RegExp) _evt_ - Name of the event to return the listeners from.
* **return** (Function[] | Object) - All listener functions for the event.

* **param** (StringRegExp) _evt_ - Name of the event to return the listeners from.
* **return** (Function[]Object) - All listener functions for the event.
## flattenListeners

<p>Takes a list of listener objects and flattens it into a list of listener functions.</p>

* **param** (Object[]) _listeners_ - Raw listener objects.
* **return** (Function[]) - Just the listener functions.

## getListenersAsObject

<p>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.</p>

* **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.

## addListener

<p>Adds a listener function to the specified event.<br />The listener will not be added if it is a duplicate.<br />If the listener returns true then it will be removed after it is called.<br />If you pass a regular expression as the event name then the listener will be added to all events that match it.</p>

* **param** (String | RegExp) _evt_ - Name of the event to attach the listener to.
* **param** (StringRegExp) _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.

## on

<p>Alias of addListener</p>


## addOnceListener

<p>Semi-alias of addListener. It will add a listener that will be<br />automatically removed after it's first execution.</p>
<p>Semi-alias of addListener. It will add a listener that will be<br />automatically removed after its first execution.</p>

* **param** (String | RegExp) _evt_ - Name of the event to attach the listener to.
* **param** (StringRegExp) _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.

## once

<p>Alias of addOnceListener.</p>


## defineEvent

<p>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.<br />You need to tell it what event names should be matched by a regex.</p>
<p>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&#39;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.<br />You need to tell it what event names should be matched by a regex.</p>

* **param** (String) _evt_ - Name of the event to create.
* **return** (Object) - Current instance of EventEmitter for chaining.

## defineEvents

<p>Uses defineEvent to define multiple events.</p>

* **param** (String[]) _evts_ - An array of event names to define.
* **return** (Object) - Current instance of EventEmitter for chaining.

## removeListener

<p>Removes a listener function from the specified event.<br />When passed a regular expression as the event name, it will remove the listener from all events that match it.</p>

* **param** (String | RegExp) _evt_ - Name of the event to remove the listener from.
* **param** (StringRegExp) _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.

## off

<p>Alias of removeListener</p>


## addListeners

<p>Adds listeners in bulk using the manipulateListeners method.<br />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.<br />You can also pass it a regular expression to add the array of listeners to all events that match it.<br />Yeah, this function does quite a bit. That's probably a bad thing.</p>
<p>Adds listeners in bulk using the manipulateListeners method.<br />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.<br />You can also pass it a regular expression to add the array of listeners to all events that match it.<br />Yeah, this function does quite a bit. That&#39;s probably a bad thing.</p>

* **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** (StringObjectRegExp) _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.

## removeListeners

<p>Removes listeners in bulk using the manipulateListeners method.<br />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.<br />You can also pass it an event name and an array of listeners to be removed.<br />You can also pass it a regular expression to remove the listeners from all events that match it.</p>

* **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** (StringObjectRegExp) _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.

## manipulateListeners

<p>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.<br />The first argument will determine if the listeners are removed (true) or added (false).<br />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.<br />You can also pass it an event name and an array of listeners to be added/removed.<br />You can also pass it a regular expression to manipulate the listeners of all events that match it.</p>

* **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** (StringObjectRegExp) _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.

## removeEvent

<p>Removes all listeners from a specified event.<br />If you do not specify an event then all listeners will be removed.<br />That means every event will be emptied.<br />You can also pass a regex to remove all events that match it.</p>

* **param** (String | RegExp) _[evt]_ - Optional name of the event to remove all listeners for. Will remove from every event if not passed.
* **param** (StringRegExp) _[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.

## removeAllListeners

<p>Alias of removeEvent.</p>

<p>Added to mirror the node API.</p>

<p>Alias of removeEvent.</p><p>Added to mirror the node API.</p>

## emitEvent

<p>Emits an event of your choice.<br />When emitted, every listener attached to that event will be executed.<br />If you pass the optional argument array then those arguments will be passed to every listener upon execution.<br />Because it uses <code>apply</code>, your array of arguments will be passed as if you wrote them out separately.<br />So they will not arrive within the array on the other side, they will be separate.<br />You can also pass a regular expression to emit to all events that match it.</p>

* **param** (String | RegExp) _evt_ - Name of the event to emit and execute listeners for.
* **param** (StringRegExp) _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.

## trigger

<p>Alias of emitEvent</p>


## emit

<p>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.<br />As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it.</p>

* **param** (String | RegExp) _evt_ - Name of the event to emit and execute listeners for.
* **param** (StringRegExp) _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.

## setOnceReturnValue

<p>Sets the current value to check against when executing listeners. If a<br />listeners return value matches the one set here then it will be removed<br />after execution. This value defaults to true.</p>

* **param** (*) _value_ - The new value to check for when executing listeners.
* **return** (Object) - Current instance of EventEmitter for chaining.

## noConflict

<p>Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version.</p>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
33 changes: 21 additions & 12 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); });
Expand Down Expand Up @@ -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']);
Expand All @@ -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 () {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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); });
Expand All @@ -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);
});
Expand Down Expand Up @@ -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']);
Expand Down

0 comments on commit 4ad612d

Please sign in to comment.