-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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 eventNames() method #5617
Conversation
-1 for the reason you stated: |
@mscdex
That usecase is covered by the |
FWIW if such a mechanism were to be added, I'd prefer something like just |
@mscdex .. possible name clashes is exactly why I used @ChALkeR ... couldn't the iterating over all present listeners be handled by listening for the |
@jasnell Not if something receives an eventemitter that already has some listeners after those listeners were attached. |
@ChALkeR ... good point. Can you do a scan of existing modules to see if using |
or even something like |
+1 to |
Per nodejs#1817, there are many modules that currently abuse the private `_events` property on EventEmitter. One of the ways it is used is to determine if a particular event is being listened for. This adds a simple `listEvents()` method that returns an array of the events with currently registered listeners.
if (events) { | ||
return Object.getOwnPropertyNames(events).concat( | ||
Object.getOwnPropertySymbols(events)); | ||
} else return []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having the return []
on the next line would be more readable IMHO
@mscdex ... updated! |
@@ -436,6 +436,15 @@ function listenerCount(type) { | |||
return 0; | |||
} | |||
|
|||
EventEmitter.prototype.eventNames = function() { | |||
const events = this._events; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nit: this can go inside the if
block now.
@mscdex ... fixed! |
LGTM if CI is ok: https://ci.nodejs.org/job/node-test-pull-request/1910/ |
@@ -295,6 +295,24 @@ they were registered, passing the supplied arguments to each. | |||
|
|||
Returns `true` if event had listeners, `false` otherwise. | |||
|
|||
### emitter.eventNames() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be Names
? Why not just events
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed above. Two reasons:
-
This is less likely to cause conflicts with modules extending
EventEmitter
. -
This is more reasonable name because it correctly describes what this method actually does — it returns the list of event names for the registered event listeners.
_events
, for an example, is an object that stores all listeners (grouped by event name).It would be easier to understand what the code that uses this method does without navigating to the docs this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ChALkeR I am really sorry. I straight away started with the code. It makes sense. Thanks for explaining.
@jasnell, @mscdex Quick grep for Sorry for the delay. |
Actually, the usage looks low. This is technically a semver-minor, but I would prefer this to be merged in a major release (i.e. 6.0). |
LGTM |
Landing as a major SGTM. Always good to be safe. I'll take a look at the
|
Updated. New CI : https://ci.nodejs.org/job/node-test-pull-request/1920/ |
CI is green with on apparently unrelated failure. |
semver-minor but marking as don't land on v5 or v4 just to be extra cautious per #5617 (comment) and #5617 (comment) |
@jasnell Thanks! |
Per #1817, there are many modules that currently abuse the private `_events` property on EventEmitter. One of the ways it is used is to determine if a particular event is being listened for. This adds a simple `eventNames()` method that returns an array of the events with currently registered listeners. PR-URL: #5617 Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Landed in b6e3fa7 |
if (this._eventsCount > 0) { | ||
const events = this._events; | ||
return Object.keys(events).concat( | ||
Object.getOwnPropertySymbols(events)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we check if the symbol property is enumerable events.propertyIsEnumerable(symbol)
and add it to the array only if it is?
Pull Request check-list
Please make sure to review and check all of these items:
make -j8 test
(UNIX) orvcbuild test nosign
(Windows) pass withthis change (including linting)?
test (or a benchmark) included?
existing APIs, or introduces new ones)?
NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.
Affected core subsystem(s)
events
Description of change
Per #1817, there are many modules
that currently abuse the private
_events
property on EventEmitter.One of the ways it is used is to determine if a particular event is
being listened for. This adds a simple
eventNames()
method thatreturns an array of the events with currently registered listeners.
/cc @ChALkeR