Permalink
Browse files

events: Allow non-string event names

As crazy as it may sound, there are examples of this in the wild.

Since this is a locked portion of the API, we cannot introduce a
breaking change.
  • Loading branch information...
1 parent 119cbf4 commit 61e0e3d395f26664677822c967a4a630953835fd @isaacs committed Mar 4, 2013
Showing with 12 additions and 1 deletion.
  1. +1 −1 lib/events.js
  2. +11 −0 test/simple/test-event-emitter-add-listeners.js
View
2 lib/events.js
@@ -126,7 +126,7 @@ EventEmitter.prototype.addListener = function(type, listener) {
var m;
if (typeof type !== 'string')
- throw TypeError('type must be a string');
+ type = '' + type;
if (typeof listener !== 'function')
throw TypeError('listener must be a function');
View
11 test/simple/test-event-emitter-add-listeners.js
@@ -59,6 +59,17 @@ var f = new events.EventEmitter();
f.setMaxListeners(0);
+// people actually do this.
+// if it was not a locked part of node's API,
+// then this would throw. Perhaps in 2.0.
+var e = new events.EventEmitter();
+var nonStringEventFired = false;
+e.on(1, function() {
+ nonStringEventFired = true;
+});
+e.emit({toString: function() { return '1'; }});
+assert(nonStringEventFired);
+
process.on('exit', function() {
assert.deepEqual(['hello', 'foo'], events_new_listener_emited);
assert.deepEqual([hello, foo], listeners_new_listener_emited);

0 comments on commit 61e0e3d

Please sign in to comment.