Skip to content

Commit b6e3fa7

Browse files
committed
events: add eventNames() method
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>
1 parent f380db2 commit b6e3fa7

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

doc/api/events.markdown

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,24 @@ they were registered, passing the supplied arguments to each.
295295

296296
Returns `true` if event had listeners, `false` otherwise.
297297

298+
### emitter.eventNames()
299+
300+
Returns an array listing the events for which the emitter has registered
301+
listeners. The values in the array will be strings or Symbols.
302+
303+
```js
304+
const EventEmitter = require('events');
305+
const myEE = new EventEmitter();
306+
myEE.on('foo', () => {});
307+
myEE.on('bar', () => {});
308+
309+
const sym = Symbol('symbol');
310+
myEE.on(sym, () => {});
311+
312+
console.log(myErr.eventNames());
313+
// Prints ['foo', 'bar', Symbol('symbol')]
314+
```
315+
298316
### emitter.getMaxListeners()
299317

300318
Returns the current max listener value for the `EventEmitter` which is either

lib/events.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,15 @@ function listenerCount(type) {
436436
return 0;
437437
}
438438

439+
EventEmitter.prototype.eventNames = function eventNames() {
440+
if (this._eventsCount > 0) {
441+
const events = this._events;
442+
return Object.keys(events).concat(
443+
Object.getOwnPropertySymbols(events));
444+
}
445+
return [];
446+
};
447+
439448
// About 1.5x faster than the two-arg version of Array#splice().
440449
function spliceOne(list, index) {
441450
for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)

test/parallel/test-events-list.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
require('../common');
4+
const EventEmitter = require('events');
5+
const assert = require('assert');
6+
7+
const EE = new EventEmitter();
8+
const m = () => {};
9+
EE.on('foo', () => {});
10+
assert.deepStrictEqual(['foo'], EE.eventNames());
11+
EE.on('bar', m);
12+
assert.deepStrictEqual(['foo', 'bar'], EE.eventNames());
13+
EE.removeListener('bar', m);
14+
assert.deepStrictEqual(['foo'], EE.eventNames());
15+
const s = Symbol('s');
16+
EE.on(s, m);
17+
assert.deepStrictEqual(['foo', s], EE.eventNames());
18+
EE.removeListener(s, m);
19+
assert.deepStrictEqual(['foo'], EE.eventNames());

0 commit comments

Comments
 (0)