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
  • Loading branch information
yuzheng14 committed Jun 29, 2023
1 parent 0ffad8a commit bec4fff
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 @@ -845,7 +845,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 bec4fff

Please sign in to comment.