Skip to content

Commit

Permalink
events: unwrap #once listeners in #listeners
Browse files Browse the repository at this point in the history
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 Aug 9, 2016
1 parent 1b99093 commit b7a8a69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
13 changes: 13 additions & 0 deletions test/parallel/test-event-emitter-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.