Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2eb8875

Browse filesBrowse files
ShogunPandatargos
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 19b5d07 commit 2eb8875
Copy full SHA for 2eb8875

File tree

Expand file treeCollapse file tree

4 files changed

+82
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+82
-5
lines changed
Open diff view settings
Collapse file

‎doc/api/deprecations.md‎

Copy file name to clipboardExpand all lines: doc/api/deprecations.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -3373,7 +3373,7 @@ In a future version of Node.js, [`message.headers`][],
33733373
[`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options
33743374
[`domain`]: domain.md
33753375
[`ecdh.setPublicKey()`]: crypto.md#ecdhsetpublickeypublickey-encoding
3376-
[`emitter.listenerCount(eventName)`]: events.md#emitterlistenercounteventname
3376+
[`emitter.listenerCount(eventName)`]: events.md#emitterlistenercounteventname-listener
33773377
[`events.listenerCount(emitter, eventName)`]: events.md#eventslistenercountemitter-eventname
33783378
[`fs.FileHandle`]: fs.md#class-filehandle
33793379
[`fs.access()`]: fs.md#fsaccesspath-mode-callback
Collapse file

‎doc/api/events.md‎

Copy file name to clipboardExpand all lines: doc/api/events.md
+10-3Lines changed: 10 additions & 3 deletions
  • Display the source diff
  • Display the rich diff
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

@@ -2482,7 +2489,7 @@ to the `EventTarget`.
24822489
[`EventTarget` error handling]: #eventtarget-error-handling
24832490
[`Event` Web API]: https://dom.spec.whatwg.org/#event
24842491
[`domain`]: domain.md
2485-
[`emitter.listenerCount()`]: #emitterlistenercounteventname
2492+
[`emitter.listenerCount()`]: #emitterlistenercounteventname-listener
24862493
[`emitter.removeListener()`]: #emitterremovelistenereventname-listener
24872494
[`emitter.setMaxListeners(n)`]: #emittersetmaxlistenersn
24882495
[`event.defaultPrevented`]: #eventdefaultprevented
Collapse file

‎lib/events.js‎

Copy file name to clipboardExpand all lines: lib/events.js
+18-1Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,17 +834,34 @@ EventEmitter.prototype.listenerCount = listenerCount;
834834
* Returns the number of listeners listening to event name
835835
* specified as `type`.
836836
* @param {string | symbol} type
837+
* @param {Function} listener
837838
* @returns {number}
838839
*/
839-
function listenerCount(type) {
840+
function listenerCount(type, listener) {
840841
const events = this._events;
841842

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

845846
if (typeof evlistener === 'function') {
847+
if (listener != null) {
848+
return listener === evlistener ? 1 : 0;
849+
}
850+
846851
return 1;
847852
} else if (evlistener !== undefined) {
853+
if (listener != null) {
854+
let matching = 0;
855+
856+
for (let i = 0, l = evlistener.length; i < l; i++) {
857+
if (evlistener[i] === listener || evlistener[i].listener === listener) {
858+
matching++;
859+
}
860+
}
861+
862+
return matching;
863+
}
864+
848865
return evlistener.length;
849866
}
850867
}
Collapse file
+53Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.