event-emitter
event-emitter copied to clipboard
Listen to all events on instance
Is there a way to listen to all events emitted from an instance, e.g. using a wildcard "*"
or have an on all function .onAll()
?
Not sure if this is nice, but something like so:
obj.onAll( ( eventName, param1, param2, ... ) => {
...
} );
@ahmadsholehin no there is not.
Such functionalities were widely discussed in other implementations (and e.g. node.js one), implementing such thing internally will have significant impact on performance (while event emitter can be part of really hot paths).
Such things are best if configured as if you emit *
events together with events of any other type, and then you can listen for *
event.
What is the use case? Debugging?
I agree with your points. Event-emitter should be kept simple and fast.
Nonetheless, it'll be very painful to comb a project with close to a hundred events and ensure each emits a *
event in addition as well. Unique case in my situation.
You're right about the use case. I'm using event-emitter to double up as an action logger. Other projects would use a real logging library to do the job.
@ahmadsholehin One more question, do you have in hands an object on which you want to observe all events, or you want to be able to listen to all events emitted on any of the emitter?
I do have the object in hand. Need to observe all events the particular emitter instance emits. That way, a parent can safely monitor the object's status/performance.
@ahmadsholehin I think it can be somewhat solved by adding some reflection layer. e.g. public API that will:
- provide a function that returns all event names for which there are registered listeners
- provide a mean to listen for events where listener for not observed yet event was added.
Having that you'd be able to replicate functionality you're after, and it shouldn't add any performance overhead as it doesn't affect events emitting logic.
Let me know if that would be a solution for you
I'm going to keep that open, as adding sort of reflection functionality makes sense
Hey everyone. Super old issue here, but since it's still open I just want to ask how bad of an idea you think this is