Skip to content

Suggestion: Re-purpose events.listenerCount() as agnostic function over EventEmitters/EventTargets #60212

Description

@Renegade334

There are many top-level functions in node:events that mirror their equivalents on the EventEmitter prototype, but also accept EventTarget arguments. (getEventListeners(), getMaxListeners(), etc.)

events.listenerCount() was previously deprecated on the basis of duplicating the functionality of EventEmitter.prototype.listenerCount(). However, there's no equivalent function on the EventTarget prototype, and it seems like this would be a good candidate to follow the others and become an agnostic utility function.

const { listenerCount } = require('events')

const et = new EventTarget()
et.addEventListener('event', () => {})

listenerCount(et, 'event') // 1

An alternative for this functionality exists via getEventListeners(...).length, but since listenerCount() already exists (and has an analogue on the EventEmitter prototype that isn't going anywhere), it seems like this would be a low-effort improvement.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    eventsIssues and PRs related to EventEmitter and the events module.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions