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.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Olical committed Aug 5, 2013
2 parents cc1fc01 + 8ab4403 commit a707717
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
26 changes: 19 additions & 7 deletions EventEmitter.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/*!
* EventEmitter v4.2.1 - git.io/ee
* EventEmitter v4.2.2 - git.io/ee
* Oliver Caldwell
* MIT license
* @preserve
*/

(function () {
// Place the script in strict mode
'use strict';

/**
Expand Down Expand Up @@ -41,6 +40,19 @@
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.
Expand Down Expand Up @@ -137,7 +149,7 @@
/**
* Alias of addListener
*/
proto.on = proto.addListener;
proto.on = alias('addListener');

/**
* Semi-alias of addListener. It will add a listener that will be
Expand All @@ -157,7 +169,7 @@
/**
* Alias of addOnceListener.
*/
proto.once = proto.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.
Expand Down Expand Up @@ -213,7 +225,7 @@
/**
* Alias of removeListener
*/
proto.off = proto.removeListener;
proto.off = alias('removeListener');

/**
* Adds listeners in bulk using the manipulateListeners method.
Expand Down Expand Up @@ -367,7 +379,7 @@
/**
* Alias of emitEvent
*/
proto.trigger = proto.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.
Expand Down Expand Up @@ -428,7 +440,7 @@
return EventEmitter;
});
}
else if (typeof module !== 'undefined' && module.exports){
else if (typeof module === 'object' && module.exports){
module.exports = EventEmitter;
}
else {
Expand Down
4 changes: 2 additions & 2 deletions 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.1",
"version": "4.2.2",
"main": [
"./EventEmitter.js"
],
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.1",
"version": "4.2.2",
"description": "Event based JavaScript for the browser",
"main": "EventEmitter.js",
"directories": {
Expand Down
19 changes: 19 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,25 @@
});
});

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;
});
});

// Execute the tests.
mocha.run();
}.call(this));

0 comments on commit a707717

Please sign in to comment.