Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: add listener argument to listenerCount #46523

Merged
merged 5 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,16 +646,23 @@ Returns the current max listener value for the `EventEmitter` which is either
set by [`emitter.setMaxListeners(n)`][] or defaults to
[`events.defaultMaxListeners`][].

### `emitter.listenerCount(eventName)`
### `emitter.listenerCount(eventName, [listener])`
ShogunPanda marked this conversation as resolved.
Show resolved Hide resolved

<!-- YAML
added: v3.2.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/46523
description: Added the listener argument.
ShogunPanda marked this conversation as resolved.
Show resolved Hide resolved
-->

* `eventName` {string|symbol} The name of the event being listened for
* `listener` {Function} The name of the listener
ShogunPanda marked this conversation as resolved.
Show resolved Hide resolved
* Returns: {integer}

Returns the number of listeners listening to the event named `eventName`.
If `listener` is provided, it will return `1` if the listener is found
ShogunPanda marked this conversation as resolved.
Show resolved Hide resolved
in the list of the listeners of the event, `0` otherwise.
ShogunPanda marked this conversation as resolved.
Show resolved Hide resolved

### `emitter.listeners(eventName)`

Expand Down Expand Up @@ -2468,7 +2475,7 @@ to the `EventTarget`.
[`EventTarget` error handling]: #eventtarget-error-handling
[`Event` Web API]: https://dom.spec.whatwg.org/#event
[`domain`]: domain.md
[`emitter.listenerCount()`]: #emitterlistenercounteventname
[`emitter.listenerCount()`]: #emitterlistenercounteventname-listener
[`emitter.removeListener()`]: #emitterremovelistenereventname-listener
[`emitter.setMaxListeners(n)`]: #emittersetmaxlistenersn
[`event.defaultPrevented`]: #eventdefaultprevented
Expand Down
18 changes: 17 additions & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const {
ErrorCaptureStackTrace,
FunctionPrototypeBind,
FunctionPrototypeCall,
Number,
NumberMAX_SAFE_INTEGER,
ObjectDefineProperty,
ObjectDefineProperties,
Expand Down Expand Up @@ -832,17 +833,32 @@ EventEmitter.prototype.listenerCount = listenerCount;
* Returns the number of listeners listening to event name
* specified as `type`.
* @param {string | symbol} type
* @param {Function} listener
* @returns {number}
*/
function listenerCount(type) {
function listenerCount(type, listener) {
const events = this._events;

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

if (typeof evlistener === 'function') {
if (listener) {
ShogunPanda marked this conversation as resolved.
Show resolved Hide resolved
return Number(listener === evlistener);
ShogunPanda marked this conversation as resolved.
Show resolved Hide resolved
}

return 1;
} else if (evlistener !== undefined) {
if (listener) {
ShogunPanda marked this conversation as resolved.
Show resolved Hide resolved
for (let i = 0, l = evlistener.length; i < l; i++) {
if (evlistener[i] === listener || evlistener[i].listener === listener) {
return 1;
}
}

return 0;
}

return evlistener.length;
}
}
Expand Down
47 changes: 47 additions & 0 deletions test/parallel/test-events-listener-count-with-listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const common = require('../common');
const EventEmitter = require('events');
const assert = require('assert');

const EE = new EventEmitter();
const handler = common.mustCall(undefined, 2);
const anotherHandler = common.mustCall();

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);
assert.strictEqual(EE.listenerCount('event', handler), 1);
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);

EE.once('event', anotherHandler);

assert.strictEqual(EE.listenerCount('event'), 2);
assert.strictEqual(EE.listenerCount('event', handler), 1);
assert.strictEqual(EE.listenerCount('event', anotherHandler), 1);

assert.strictEqual(EE.listenerCount('another-event'), 0);
assert.strictEqual(EE.listenerCount('another-event', handler), 0);
assert.strictEqual(EE.listenerCount('another-event', anotherHandler), 0);

EE.emit('event');

assert.strictEqual(EE.listenerCount('event'), 1);
assert.strictEqual(EE.listenerCount('event', handler), 1);
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);

EE.emit('event');

assert.strictEqual(EE.listenerCount('event'), 1);
assert.strictEqual(EE.listenerCount('event', handler), 1);
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);

EE.off('event', handler);

assert.strictEqual(EE.listenerCount('event'), 0);
assert.strictEqual(EE.listenerCount('event', handler), 0);
assert.strictEqual(EE.listenerCount('event', anotherHandler), 0);