Browse Source

events: unwrap #once listeners in #listeners

Fixes: https://github.com/nodejs/node/issues/6873
PR-URL: https://github.com/nodejs/node/pull/6881
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
v7.x
Owen Smith 9 years ago
committed by James M Snell
parent
commit
b7a8a691b4
  1. 12
      lib/events.js
  2. 13
      test/parallel/test-event-emitter-listeners.js

12
lib/events.js

@ -425,9 +425,9 @@ EventEmitter.prototype.listeners = function listeners(type) {
if (!evlistener) if (!evlistener)
ret = []; ret = [];
else if (typeof evlistener === 'function') else if (typeof evlistener === 'function')
ret = [evlistener]; ret = [evlistener.listener || evlistener];
else else
ret = arrayClone(evlistener, evlistener.length); ret = unwrapListeners(evlistener);
} }
return ret; return ret;
@ -475,3 +475,11 @@ function arrayClone(arr, i) {
copy[i] = arr[i]; copy[i] = arr[i];
return copy; return copy;
} }
function unwrapListeners(arr) {
const ret = new Array(arr.length);
for (var i = 0; i < ret.length; ++i) {
ret[i] = arr[i].listener || arr[i];
}
return ret;
}

13
test/parallel/test-event-emitter-listeners.js

@ -36,3 +36,16 @@ function listener2() {}
assert.deepStrictEqual(ee.listeners('foo'), [listener, listener2]); assert.deepStrictEqual(ee.listeners('foo'), [listener, listener2]);
assert.deepStrictEqual(eeListenersCopy, [listener]); assert.deepStrictEqual(eeListenersCopy, [listener]);
} }
{
const ee = new events.EventEmitter();
ee.once('foo', listener);
assert.deepStrictEqual(ee.listeners('foo'), [listener]);
}
{
const ee = new events.EventEmitter();
ee.on('foo', listener);
ee.once('foo', listener2);
assert.deepStrictEqual(ee.listeners('foo'), [listener, listener2]);
}

Loading…
Cancel
Save