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: #46523
PR-URL: #48592
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
  • Loading branch information
yuzheng14 authored and ruyadorno committed Sep 12, 2023
1 parent 616ecf0 commit caccc61
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 @@ -850,7 +850,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 caccc61

Please sign in to comment.