-
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
Changes from all commits
e9b1ea9
1d173c6
fe7ffc9
673c837
fabfa63
e70f2d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -295,6 +295,24 @@ they were registered, passing the supplied arguments to each. | |
|
||
Returns `true` if event had listeners, `false` otherwise. | ||
|
||
### emitter.eventNames() | ||
|
||
Returns an array listing the events for which the emitter has registered | ||
listeners. The values in the array will be strings or Symbols. | ||
|
||
```js | ||
const EventEmitter = require('events'); | ||
const myEE = new EventEmitter(); | ||
myEE.on('foo', () => {}); | ||
myEE.on('bar', () => {}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe throw in a symbol example too? |
||
|
||
const sym = Symbol('symbol'); | ||
myEE.on(sym, () => {}); | ||
|
||
console.log(myErr.eventNames()); | ||
// Prints ['foo', 'bar', Symbol('symbol')] | ||
``` | ||
|
||
### emitter.getMaxListeners() | ||
|
||
Returns the current max listener value for the `EventEmitter` which is either | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -436,6 +436,15 @@ function listenerCount(type) { | |
return 0; | ||
} | ||
|
||
EventEmitter.prototype.eventNames = function eventNames() { | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we check if the symbol property is enumerable |
||
} | ||
return []; | ||
}; | ||
|
||
// About 1.5x faster than the two-arg version of Array#splice(). | ||
function spliceOne(list, index) { | ||
for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const EventEmitter = require('events'); | ||
const assert = require('assert'); | ||
|
||
const EE = new EventEmitter(); | ||
const m = () => {}; | ||
EE.on('foo', () => {}); | ||
assert.deepStrictEqual(['foo'], EE.eventNames()); | ||
EE.on('bar', m); | ||
assert.deepStrictEqual(['foo', 'bar'], EE.eventNames()); | ||
EE.removeListener('bar', m); | ||
assert.deepStrictEqual(['foo'], EE.eventNames()); | ||
const s = Symbol('s'); | ||
EE.on(s, m); | ||
assert.deepStrictEqual(['foo', s], EE.eventNames()); | ||
EE.removeListener(s, m); | ||
assert.deepStrictEqual(['foo'], EE.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 justevents
?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.