chrisdavies / eev

A tiny, fast, zero-dependency event emitter
522 stars 32 forks source link

Support wildcard event name (e.g. '*', or empty string '')? #8

Closed barryels closed 6 years ago

barryels commented 6 years ago

Hi, is it possible with the current implementation to subscribe to all events with a single 'on()' call? An example use case would be to have a (user-land) debugger that can listen to all events (without explicitly subscribing to all of them).

Thanks

chrisdavies commented 6 years ago

There isn't currently a built in wildcard support, but you can fairly easily mimic it by overriding the emit method. Something like this:

const e = new Eev();
const superEmit = e.emit;
e.emit = function() {
   console.log(arguments);
   superEmit.apply(e, arguments);
};
barryels commented 6 years ago

Thanks @chrisdavies, that'll work :)

Here's a trivial example showcasing the desired structure (the key thing here, is that I need to facade Eev behind an eventBus so as to limit 3rd party library usage across multiple user-land "components") https://jsbin.com/nesiliw/edit?html,console

chrisdavies commented 6 years ago

Ah. I see. So, it sounds like you want to use this wildcard in production? The solution I gave will definitely be slow, relative to Eev's normal performance. Though it probably is fast enough for most applications.

Adding in wildcards as a first-class citizen will grow the library a good bit, I think, as I'd probably want to support partial wildcards: eev.on('foo/*', () => console.log('Some foo event was emitted!')).

I think that would be best done via a plugin, possibly one using a router like rlite, so that the core of Eev remains small and light.