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 <luigipinca@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
yuzheng14 authored and ruyadorno committed Sep 16, 2023
1 parent 1aa798d commit 3b8ec34
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
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
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 3b8ec34

Please sign in to comment.