简体   繁体   English

在EventEmitter上,我如何知道我可以收听的所有事件?

[英]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 ? 假设我有一个从EventEmitter继承的对象,例如流或其他任何对象,是否有很好的方法知道我可以收听的所有事件以及所有附加的事件侦听器?

I think the second part of the question is easy, emitter.listeners(event) will tell me all the listeners to an event. 我认为问题的第二部分很简单, emitter.listeners(event)会告诉我emitter.listeners(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. 据我所知,没有公共的API或文档可以帮助您列出EventEmitter可以发出的所有事件。

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. 但是,如果查看EventEmitter源代码 ,则可以看到所有事件都存储在_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 ). 编辑:某些模块提供了可能的事件列表,例如,参见SAX (和相应的source )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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