简体   繁体   中英

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 ?

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM