events: add type checks to once

Also cleanup unnecessary use of "self" since it will always be called
using .apply() from emit.
This commit is contained in:
Trevor Norris 2013-02-27 00:15:28 -08:00 committed by isaacs
parent e1ac2ef7cf
commit d1b4dcd6ac

View File

@ -165,18 +165,18 @@ EventEmitter.prototype.addListener = function(type, listener) {
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function(type, listener) {
if ('function' !== typeof listener) {
throw TypeError('.once only takes instances of Function');
if (typeof type !== 'string')
throw TypeError('type must be a string');
if (typeof listener !== 'function')
throw TypeError('listener must be a function');
function g() {
this.removeListener(type, g);
listener.apply(this, arguments);
}
var self = this;
function g() {
self.removeListener(type, g);
listener.apply(this, arguments);
};
g.listener = listener;
self.on(type, g);
this.on(type, g);
return this;
};