From d1b4dcd6acb1d1c66e423f7992dc6eec8a35c544 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Wed, 27 Feb 2013 00:15:28 -0800 Subject: [PATCH] events: add type checks to once Also cleanup unnecessary use of "self" since it will always be called using .apply() from emit. --- lib/events.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/events.js b/lib/events.js index d9e09bbc928..4cfa7407656 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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; };