-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
Description
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:
if (!dest._events || !dest._events.error)else if (isArray(dest._events.error))dest._events.error.unshift(onerror);dest._events.error = [onerror, dest._events.error];
- dicer/lib/Dicer.js:
if (this._events.preamble)if ((start + i) < end && this._events.trailer)if (this._events[ev])
- busboy/lib/types/multipart.js:
if (!boy._events.file) {
- ultron/index.js:
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
EventEmmiterthat currently have listeners. This is whatultronmodule 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.listenerCountbut using a prototype, likeEventEmitter.prototype.listenersbut 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 usingthis._events.whateverto 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.jsand thereadable-streammodule do.Implemented by @jasnell in events: add ability to prepend event listeners #6032.