From 04688614f70470dba1f7512844fd2c4008d9d8bd Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Mon, 4 Mar 2013 11:33:03 -0800 Subject: [PATCH 1/2] events: remove type check for event type Strict checking for typeof types broke backwards compatibility for other libraries. This reverts those checks. The subclass test has been changed to ensure all operations can be performed on the inherited EE before instantiation. Including the ability to set event names with numbers. --- lib/events.js | 12 ------------ test/simple/test-event-emitter-subclass.js | 7 ++++--- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/lib/events.js b/lib/events.js index 811cb90895f..9eaa36ba68a 100644 --- a/lib/events.js +++ b/lib/events.js @@ -125,8 +125,6 @@ EventEmitter.prototype.emit = function(type) { EventEmitter.prototype.addListener = function(type, listener) { var m; - if (typeof type !== 'string') - throw TypeError('type must be a string'); if (typeof listener !== 'function') throw TypeError('listener must be a function'); @@ -168,8 +166,6 @@ EventEmitter.prototype.addListener = function(type, listener) { EventEmitter.prototype.on = EventEmitter.prototype.addListener; EventEmitter.prototype.once = function(type, listener) { - if (typeof type !== 'string') - throw TypeError('type must be a string'); if (typeof listener !== 'function') throw TypeError('listener must be a function'); @@ -188,8 +184,6 @@ EventEmitter.prototype.once = function(type, listener) { EventEmitter.prototype.removeListener = function(type, listener) { var list, position, length, i; - if (typeof type !== 'string') - throw TypeError('type must be a string'); if (typeof listener !== 'function') throw TypeError('listener must be a function'); @@ -235,9 +229,6 @@ EventEmitter.prototype.removeListener = function(type, listener) { EventEmitter.prototype.removeAllListeners = function(type) { var key, listeners; - if (arguments.length > 0 && typeof type !== 'string') - throw TypeError('type must not be set or must be a string'); - if (!this._events) return this; @@ -276,9 +267,6 @@ EventEmitter.prototype.removeAllListeners = function(type) { }; EventEmitter.prototype.listeners = function(type) { - if (typeof type !== 'string') - throw TypeError('event type must be a string'); - if (!this._events || !this._events[type]) return []; if (typeof this._events[type] === 'function') diff --git a/test/simple/test-event-emitter-subclass.js b/test/simple/test-event-emitter-subclass.js index 7a2f778e22b..8a2e9c15644 100644 --- a/test/simple/test-event-emitter-subclass.js +++ b/test/simple/test-event-emitter-subclass.js @@ -27,9 +27,9 @@ var util = require('util'); util.inherits(MyEE, EventEmitter); function MyEE(cb) { - this.emit('bar'); - this.on('foo', cb); - process.nextTick(this.emit.bind(this, 'foo')); + this.once(1, cb); + this.emit(1); + this.removeAllListeners(); EventEmitter.call(this); } @@ -50,5 +50,6 @@ assert.throws(function() { process.on('exit', function() { assert(called); + assert.deepEqual(myee._events, {}); console.log('ok'); }); From d09ab61dcd4943ec543417df12487a2c8510cc1e Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Mon, 4 Mar 2013 11:59:55 -0800 Subject: [PATCH 2/2] events: code consistency v8 likes when smaller functions have a single return point, and cleaned up the single non-strict check. --- lib/events.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/events.js b/lib/events.js index 9eaa36ba68a..bee1c6132ff 100644 --- a/lib/events.js +++ b/lib/events.js @@ -84,7 +84,7 @@ EventEmitter.prototype.emit = function(type) { if (this.domain && this !== process) this.domain.enter(); - if (typeof handler == 'function') { + if (typeof handler === 'function') { switch (arguments.length) { // fast cases case 1: @@ -267,11 +267,14 @@ EventEmitter.prototype.removeAllListeners = function(type) { }; EventEmitter.prototype.listeners = function(type) { + var ret; if (!this._events || !this._events[type]) - return []; - if (typeof this._events[type] === 'function') - return [this._events[type]]; - return this._events[type].slice(); + ret = []; + else if (typeof this._events[type] === 'function') + ret = [this._events[type]]; + else + ret = this._events[type].slice(); + return ret; }; EventEmitter.listenerCount = function(emitter, type) {