Skip to content
Advertisement

On an EventEmitter, how can I know all the events I can listen to?

Supposing I have an object that inherited from EventEmitter, like a stream or any other, is there a good way to know all the events I can listen to, and all the attached event listeners ?

I think the second part of the question is easy, emitter.listeners(event) will tell me all the listeners to an event. But is there a way to know beforehand all the events I can listen to?

Advertisement

Answer

As far as I know, there is no public API or documentation to help you list all the events an EventEmitter can emit.

But if you look in the EventEmitter source code, you can see that all events are stored in the _events property, so your code can loop on the keys of the object and find all possible events. Here is an exemple on how to list the event names:

var ee = new SomeEventEmitter();
console.log(Object.keys(ee._events));

However, as this is undocumented, I’d suggest you to be careful with this.

Edit: Some modules provide a list of possible events, see for instance SAX (and the corresponding source).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement