From db8940dae2b545347b918e141dec6a699750be43 Mon Sep 17 00:00:00 2001 From: Roly Fentanes Date: Fri, 24 Feb 2012 14:28:46 -0700 Subject: [PATCH] `newListener` emits correct fn when using `once` Fixes #2826. --- lib/events.js | 3 ++- test/simple/test-event-emitter-add-listeners.js | 13 ++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/events.js b/lib/events.js index 5b0b963fd7..1fff1e3616 100644 --- a/lib/events.js +++ b/lib/events.js @@ -105,7 +105,8 @@ EventEmitter.prototype.addListener = function(type, listener) { // To avoid recursion in the case that type == "newListeners"! Before // adding it to the listeners, first emit "newListeners". - this.emit('newListener', type, listener); + this.emit('newListener', type, typeof listener.listener === 'function' ? + listener.listener : listener); if (!this._events[type]) { // Optimize the case of one listener. Don't need the extra array object. diff --git a/test/simple/test-event-emitter-add-listeners.js b/test/simple/test-event-emitter-add-listeners.js index 51c48a3e98..0806e049d4 100644 --- a/test/simple/test-event-emitter-add-listeners.js +++ b/test/simple/test-event-emitter-add-listeners.js @@ -26,6 +26,7 @@ var events = require('events'); var e = new events.EventEmitter(); var events_new_listener_emited = []; +var listeners_new_listener_emited = []; var times_hello_emited = 0; // sanity check @@ -34,14 +35,19 @@ assert.equal(e.addListener, e.on); e.on('newListener', function(event, listener) { console.log('newListener: ' + event); events_new_listener_emited.push(event); + listeners_new_listener_emited.push(listener); }); -e.on('hello', function(a, b) { +function hello(a, b) { console.log('hello'); times_hello_emited += 1; assert.equal('a', a); assert.equal('b', b); -}); +} +e.on('hello', hello); + +var foo = function() {}; +e.once('foo', foo); console.log('start'); @@ -54,7 +60,8 @@ f.setMaxListeners(0); process.on('exit', function() { - assert.deepEqual(['hello'], events_new_listener_emited); + assert.deepEqual(['hello', 'foo'], events_new_listener_emited); + assert.deepEqual([hello, foo], listeners_new_listener_emited); assert.equal(1, times_hello_emited); });