Skip to content

Commit cc6deea

Browse files
ShogunPandadanielleadams
authored andcommitted
events: add listener argument to listenerCount
PR-URL: #46523 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 45fccc9 commit cc6deea

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

doc/api/deprecations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3364,7 +3364,7 @@ In a future version of Node.js, [`message.headers`][],
33643364
[`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options
33653365
[`domain`]: domain.md
33663366
[`ecdh.setPublicKey()`]: crypto.md#ecdhsetpublickeypublickey-encoding
3367-
[`emitter.listenerCount(eventName)`]: events.md#emitterlistenercounteventname
3367+
[`emitter.listenerCount(eventName)`]: events.md#emitterlistenercounteventname-listener
33683368
[`events.listenerCount(emitter, eventName)`]: events.md#eventslistenercountemitter-eventname
33693369
[`fs.FileHandle`]: fs.md#class-filehandle
33703370
[`fs.access()`]: fs.md#fsaccesspath-mode-callback

doc/api/events.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,16 +646,23 @@ Returns the current max listener value for the `EventEmitter` which is either
646646
set by [`emitter.setMaxListeners(n)`][] or defaults to
647647
[`events.defaultMaxListeners`][].
648648

649-
### `emitter.listenerCount(eventName)`
649+
### `emitter.listenerCount(eventName[, listener])`
650650

651651
<!-- YAML
652652
added: v3.2.0
653+
changes:
654+
- version: REPLACEME
655+
pr-url: https://github.com/nodejs/node/pull/46523
656+
description: Added the `listener` argument.
653657
-->
654658

655659
* `eventName` {string|symbol} The name of the event being listened for
660+
* `listener` {Function} The event handler function
656661
* Returns: {integer}
657662

658-
Returns the number of listeners listening to the event named `eventName`.
663+
Returns the number of listeners listening for the event named `eventName`.
664+
If `listener` is provided, it will return how many times the listener is found
665+
in the list of the listeners of the event.
659666

660667
### `emitter.listeners(eventName)`
661668

@@ -2462,7 +2469,7 @@ to the `EventTarget`.
24622469
[`EventTarget` error handling]: #eventtarget-error-handling
24632470
[`Event` Web API]: https://dom.spec.whatwg.org/#event
24642471
[`domain`]: domain.md
2465-
[`emitter.listenerCount()`]: #emitterlistenercounteventname
2472+
[`emitter.listenerCount()`]: #emitterlistenercounteventname-listener
24662473
[`emitter.removeListener()`]: #emitterremovelistenereventname-listener
24672474
[`emitter.setMaxListeners(n)`]: #emittersetmaxlistenersn
24682475
[`event.defaultPrevented`]: #eventdefaultprevented

lib/events.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,17 +835,34 @@ EventEmitter.prototype.listenerCount = listenerCount;
835835
* Returns the number of listeners listening to event name
836836
* specified as `type`.
837837
* @param {string | symbol} type
838+
* @param {Function} listener
838839
* @returns {number}
839840
*/
840-
function listenerCount(type) {
841+
function listenerCount(type, listener) {
841842
const events = this._events;
842843

843844
if (events !== undefined) {
844845
const evlistener = events[type];
845846

846847
if (typeof evlistener === 'function') {
848+
if (listener != null) {
849+
return listener === evlistener ? 1 : 0;
850+
}
851+
847852
return 1;
848853
} else if (evlistener !== undefined) {
854+
if (listener != null) {
855+
let matching = 0;
856+
857+
for (let i = 0, l = evlistener.length; i < l; i++) {
858+
if (evlistener[i] === listener || evlistener[i].listener === listener) {
859+
matching++;
860+
}
861+
}
862+
863+
return matching;
864+
}
865+
849866
return evlistener.length;
850867
}
851868
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const EventEmitter = require('events');
5+
const assert = require('assert');
6+
7+
const EE = new EventEmitter();
8+
const handler = common.mustCall(undefined, 3);
9+
const anotherHandler = common.mustCall();
10+
11+
assert.strictEqual(EE.listenerCount('event'), 0);
12+
assert.strictEqual(EE.listenerCount('event', handler), 0);
13+
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
14+
15+
EE.on('event', handler);
16+
17+
assert.strictEqual(EE.listenerCount('event'), 1);
18+
assert.strictEqual(EE.listenerCount('event', handler), 1);
19+
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
20+
21+
EE.once('event', anotherHandler);
22+
23+
assert.strictEqual(EE.listenerCount('event'), 2);
24+
assert.strictEqual(EE.listenerCount('event', handler), 1);
25+
assert.strictEqual(EE.listenerCount('event', anotherHandler), 1);
26+
27+
assert.strictEqual(EE.listenerCount('another-event'), 0);
28+
assert.strictEqual(EE.listenerCount('another-event', handler), 0);
29+
assert.strictEqual(EE.listenerCount('another-event', anotherHandler), 0);
30+
31+
EE.once('event', handler);
32+
33+
assert.strictEqual(EE.listenerCount('event'), 3);
34+
assert.strictEqual(EE.listenerCount('event', handler), 2);
35+
assert.strictEqual(EE.listenerCount('event', anotherHandler), 1);
36+
37+
EE.emit('event');
38+
39+
assert.strictEqual(EE.listenerCount('event'), 1);
40+
assert.strictEqual(EE.listenerCount('event', handler), 1);
41+
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
42+
43+
EE.emit('event');
44+
45+
assert.strictEqual(EE.listenerCount('event'), 1);
46+
assert.strictEqual(EE.listenerCount('event', handler), 1);
47+
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);
48+
49+
EE.off('event', handler);
50+
51+
assert.strictEqual(EE.listenerCount('event'), 0);
52+
assert.strictEqual(EE.listenerCount('event', handler), 0);
53+
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy