Skip to content

Commit

Permalink
events: fix bug listenerCount don't compare wrapped listener
Browse files Browse the repository at this point in the history
When add listener by once, it will be wrapped into another function.
And when pass listener and there is just one event listener added by
once, it will return 0 even if passed listener equal wrapped event
listener.

Refs: nodejs#46523
PR-URL: nodejs#48592
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
yuzheng14 authored and Ceres6 committed Aug 14, 2023
1 parent 95c7722 commit 4fbfd0f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ function listenerCount(type, listener) {

if (typeof evlistener === 'function') {
if (listener != null) {
return listener === evlistener ? 1 : 0;
return listener === evlistener || listener === evlistener.listener ? 1 : 0;
}

return 1;
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-events-listener-count-with-listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ assert.strictEqual(EE.listenerCount('event'), 0);
assert.strictEqual(EE.listenerCount('event', handler), 0);
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);

EE.once('event', handler);

assert.strictEqual(EE.listenerCount('event'), 1);
assert.strictEqual(EE.listenerCount('event', handler), 1);
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);

EE.removeAllListeners('event');

assert.strictEqual(EE.listenerCount('event'), 0);
assert.strictEqual(EE.listenerCount('event', handler), 0);
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);

EE.on('event', handler);

assert.strictEqual(EE.listenerCount('event'), 1);
Expand Down

0 comments on commit 4fbfd0f

Please sign in to comment.