Skip to content

EventEmitter API #1817

Closed
Closed
@ChALkeR

Description

@ChALkeR

Moved from #1785 (comment)

Some modules use the internal _events object. It's not a good thing, and that probably means that EventEmitter is missing some API methods.

Also lib/_stream_readable.js uses the internal _events object of lib/events.js, which is ok, but not very nice. What makes that a bit worse is that lib/_stream_readable.js is also packaged as an external readable-stream module.

Samples of _events usage:

  • readable-stream/lib/_stream_readable.js:
    1. if (!dest._events || !dest._events.error)
    2. else if (isArray(dest._events.error))
    3. dest._events.error.unshift(onerror);
    4. dest._events.error = [onerror, dest._events.error];
  • dicer/lib/Dicer.js:
    1. if (this._events.preamble)
    2. if ((start + i) < end && this._events.trailer)
    3. if (this._events[ev])
  • busboy/lib/types/multipart.js:
    1. if (!boy._events.file) {
  • ultron/index.js:
    1. for (event in this.ee._events) { if (this.ee._events.hasOwnProperty(event)) {

It looks to me that there should be methods in EventEmitter to:

  • Get a list of all events from an EventEmmiter that currently have listeners. This is what ultron module does, and I do not see an API for that. Getting a list of events could also be usable for debugging.

    Implemented by @jasnell in events: add eventNames() method #5617, will be available in 6.0.

  • (optional) Check if there are any listeners for a specific event. Like EventEmitter.listenerCount but using a prototype, like EventEmitter.prototype.listeners but returning just the count. This is what seems to be most commonly used.

    It has a valid API EventEmitter.listenerCount, but why isn't it inside the prototype? That makes it a bit harder to find and that could be the reason behind modules using this._events.whatever to count (or check the presense of) the event listeners.

    That's since 75305f3. @trevnorris

    Solved by events: deprecate static listenerCount function #2349.

  • (optional) To prepend an event. Does the documentation specify the order in which the events are executed, btw?

    This is what the internal lib/_stream_readable.js and the readable-stream module do.

    Implemented by @jasnell in events: add ability to prepend event listeners #6032.

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussIssues opened for discussions and feedbacks.eventsIssues and PRs related to the events subsystem / EventEmitter.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions