Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
newListener emits correct fn when using once
Browse files Browse the repository at this point in the history
Fixes #2826.
  • Loading branch information
fent authored and koichik committed Feb 25, 2012
1 parent d4d45a1 commit db8940d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/events.js
Expand Up @@ -105,7 +105,8 @@ EventEmitter.prototype.addListener = function(type, listener) {


// To avoid recursion in the case that type == "newListeners"! Before // To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners". // 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]) { if (!this._events[type]) {
// Optimize the case of one listener. Don't need the extra array object. // Optimize the case of one listener. Don't need the extra array object.
Expand Down
13 changes: 10 additions & 3 deletions test/simple/test-event-emitter-add-listeners.js
Expand Up @@ -26,6 +26,7 @@ var events = require('events');
var e = new events.EventEmitter(); var e = new events.EventEmitter();


var events_new_listener_emited = []; var events_new_listener_emited = [];
var listeners_new_listener_emited = [];
var times_hello_emited = 0; var times_hello_emited = 0;


// sanity check // sanity check
Expand All @@ -34,14 +35,19 @@ assert.equal(e.addListener, e.on);
e.on('newListener', function(event, listener) { e.on('newListener', function(event, listener) {
console.log('newListener: ' + event); console.log('newListener: ' + event);
events_new_listener_emited.push(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'); console.log('hello');
times_hello_emited += 1; times_hello_emited += 1;
assert.equal('a', a); assert.equal('a', a);
assert.equal('b', b); assert.equal('b', b);
}); }
e.on('hello', hello);

var foo = function() {};
e.once('foo', foo);


console.log('start'); console.log('start');


Expand All @@ -54,7 +60,8 @@ f.setMaxListeners(0);




process.on('exit', function() { 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); assert.equal(1, times_hello_emited);
}); });


Expand Down

0 comments on commit db8940d

Please sign in to comment.