event-emitter icon indicating copy to clipboard operation
event-emitter copied to clipboard

Listen to all events on instance

Open ahmadsholehin opened this issue 7 years ago • 7 comments

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 avatar Sep 05 '17 13:09 ahmadsholehin

@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?

medikoo avatar Sep 05 '17 13:09 medikoo

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 avatar Sep 06 '17 06:09 ahmadsholehin

@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?

medikoo avatar Sep 06 '17 06:09 medikoo

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 avatar Sep 07 '17 06:09 ahmadsholehin

@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

medikoo avatar Sep 07 '17 07:09 medikoo

I'm going to keep that open, as adding sort of reflection functionality makes sense

medikoo avatar Sep 11 '17 07:09 medikoo

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

image

pedrotorchio avatar Jun 11 '22 11:06 pedrotorchio