Skip to content

Commit b7a8a69

Browse files
omsmithjasnell
authored andcommittedAug 9, 2016
events: unwrap #once listeners in #listeners
Fixes: #6873 PR-URL: #6881 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 1b99093 commit b7a8a69

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
 

‎lib/events.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,9 @@ EventEmitter.prototype.listeners = function listeners(type) {
425425
if (!evlistener)
426426
ret = [];
427427
else if (typeof evlistener === 'function')
428-
ret = [evlistener];
428+
ret = [evlistener.listener || evlistener];
429429
else
430-
ret = arrayClone(evlistener, evlistener.length);
430+
ret = unwrapListeners(evlistener);
431431
}
432432

433433
return ret;
@@ -475,3 +475,11 @@ function arrayClone(arr, i) {
475475
copy[i] = arr[i];
476476
return copy;
477477
}
478+
479+
function unwrapListeners(arr) {
480+
const ret = new Array(arr.length);
481+
for (var i = 0; i < ret.length; ++i) {
482+
ret[i] = arr[i].listener || arr[i];
483+
}
484+
return ret;
485+
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@ function listener2() {}
3636
assert.deepStrictEqual(ee.listeners('foo'), [listener, listener2]);
3737
assert.deepStrictEqual(eeListenersCopy, [listener]);
3838
}
39+
40+
{
41+
const ee = new events.EventEmitter();
42+
ee.once('foo', listener);
43+
assert.deepStrictEqual(ee.listeners('foo'), [listener]);
44+
}
45+
46+
{
47+
const ee = new events.EventEmitter();
48+
ee.on('foo', listener);
49+
ee.once('foo', listener2);
50+
assert.deepStrictEqual(ee.listeners('foo'), [listener, listener2]);
51+
}

0 commit comments

Comments
 (0)
Please sign in to comment.