Permalink
Browse files

events: unwrap #once listeners in #listeners

Fixes: #6873
PR-URL: #6881
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
omsmith authored and jasnell committed Mar 4, 2016
1 parent 1b99093 commit b7a8a691b4c6ff0f89b5d8dba3184eaef0efc898
Showing with 23 additions and 2 deletions.
  1. +10 −2 lib/events.js
  2. +13 −0 test/parallel/test-event-emitter-listeners.js
@@ -425,9 +425,9 @@ EventEmitter.prototype.listeners = function listeners(type) {
if (!evlistener)
ret = [];
else if (typeof evlistener === 'function')
ret = [evlistener];
ret = [evlistener.listener || evlistener];
else
ret = arrayClone(evlistener, evlistener.length);
ret = unwrapListeners(evlistener);
}
return ret;
@@ -475,3 +475,11 @@ function arrayClone(arr, i) {
copy[i] = arr[i];
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;
}
@@ -36,3 +36,16 @@ function listener2() {}
assert.deepStrictEqual(ee.listeners('foo'), [listener, listener2]);
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]);
}

0 comments on commit b7a8a69

Please sign in to comment.