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:
parent
e1ac2ef7cf
commit
d1b4dcd6ac
@ -165,18 +165,18 @@ EventEmitter.prototype.addListener = function(type, listener) {
|
|||||||
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
||||||
|
|
||||||
EventEmitter.prototype.once = function(type, listener) {
|
EventEmitter.prototype.once = function(type, listener) {
|
||||||
if ('function' !== typeof listener) {
|
if (typeof type !== 'string')
|
||||||
throw TypeError('.once only takes instances of Function');
|
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;
|
g.listener = listener;
|
||||||
self.on(type, g);
|
this.on(type, g);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user