Skip to content
Advertisement

Allow only certain events

Is there a way to only allow certain events in a custom eventEmitter?

For example, if I have something like this everything works as expected:

JavaScript

I get a callback every second on the first event and every 2 seconds on the 2nd event. For the 3rd nothing happens, because I don’t emit this event. Is there a way that this class tells me that this event is not defined and I cannot add a listener to it, or that it is only possible to add listeners to event 1 and 2?

Advertisement

Answer

There is no built-in functionality, but you can create some kind of wrapper to do this.

JavaScript
Advertisement