| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
@ChALkeR ... currently this returns an Array... given the various use cases that I've seen, however, the listenerCount API is typically sufficient. I implemented this quickly given your discussion in #1817. Do you still feel this is necessary? |
Sorry, something went wrong.
|
-1 for the reason you stated: listenerCount() can already handle most needs for a method like this. |
Sorry, something went wrong.
|
@mscdex listenerCount() is not enough in some cases, see, for example, the ultron module from #1817. I will prepare a list of other modules that do something like that.
That usecase is covered by the listenerCount(type) method, iterating over all present event listeners isn't. |
Sorry, something went wrong.
|
FWIW if such a mechanism were to be added, I'd prefer something like just events() to match our existing listeners(), but the problem would be naming clashes if (many) modules use that same property name. |
Sorry, something went wrong.
|
@mscdex .. possible name clashes is exactly why I used listEvents instead of just events... it's a less obvious choice. @ChALkeR ... couldn't the iterating over all present listeners be handled by listening for the 'newListener' and 'removeListener' events? Sure, it requires a bit more work but it's not too difficult. |
Sorry, something went wrong.
|
@jasnell Not if something receives an eventemitter that already has some listeners after those listeners were attached. |
Sorry, something went wrong.
|
@ChALkeR ... good point. Can you do a scan of existing modules to see if using events() would cause any conflicts? |
Sorry, something went wrong.
|
or even something like eventNames(). IMHO listEvents() sounds like a ("action") function that will print the event names to the console or something. |
Sorry, something went wrong.
|
+1 to eventNames() |
Sorry, something went wrong.
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.
Sorry, something went wrong.
| if (events) { | ||
| return Object.getOwnPropertyNames(events).concat( | ||
| Object.getOwnPropertySymbols(events)); | ||
| } else return []; |
There was a problem hiding this comment.
Having the return [] on the next line would be more readable IMHO
Sorry, something went wrong.
| } | ||
|
|
||
| EventEmitter.prototype.eventNames = function() { | ||
| const events = this._events; |
There was a problem hiding this comment.
minor nit: this can go inside the if block now.
Sorry, something went wrong.
|
LGTM if CI is ok: https://ci.nodejs.org/job/node-test-pull-request/1910/ |
Sorry, something went wrong.
|
|
||
| Returns `true` if event had listeners, `false` otherwise. | ||
|
|
||
| ### emitter.eventNames() |
There was a problem hiding this comment.
Should this be Names? Why not just events?
Sorry, something went wrong.
There was a problem hiding this comment.
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.
Sorry, something went wrong.
There was a problem hiding this comment.
@ChALkeR I am really sorry. I straight away started with the code. It makes sense. Thanks for explaining.
Sorry, something went wrong.
|
@jasnell, @mscdex eventNames is used by some of the modules, but I can't atm give an estimate of how many of those (if any) are properties of eventemitter objects. Quick grep for \.eventNames[^a-zA-Z_]: https://gist.github.com/ChALkeR/8ff143bd3189b9026d7e. Sorry for the delay. |
Sorry, something went wrong.
|
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). |
Sorry, something went wrong.
|
LGTM |
Sorry, something went wrong.
|
Landing as a major SGTM. Always good to be safe. I'll take a look at the
|
Sorry, something went wrong.
|
Updated. New CI : https://ci.nodejs.org/job/node-test-pull-request/1920/ |
Sorry, something went wrong.
|
CI is green with on apparently unrelated failure. |
Sorry, something went wrong.
|
semver-minor but marking as don't land on v5 or v4 just to be extra cautious per #5617 (comment) and #5617 (comment) |
Sorry, something went wrong.
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>
| if (this._eventsCount > 0) { | ||
| const events = this._events; | ||
| return Object.keys(events).concat( | ||
| Object.getOwnPropertySymbols(events)); |
There was a problem hiding this comment.
Shouldn't we check if the symbol property is enumerable events.propertyIsEnumerable(symbol) and add it to the array only if it is?
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Pull Request check-list
Please make sure to review and check all of these items:
this 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 that
returns an array of the events with currently registered listeners.
/cc @ChALkeR