The handlery function
The handlery
function takes an Emitter
and returns a bunch of decorators (on
, subscribe
, ...) alongside an EventHandler
base class.
An emitter can be any object that has functions like on
, emit
, off
and once
. You can find the exact typing of an Emitter
here, but chances are you are using an adapter
alongside a pre-made emitter library like the emittery
or the nodejs EventEmitter
.
If you want to code your own emitter
, go for it! For example, build one that uses WebSockets to send typed events. handlery
will tell you if your implementation does not match the requirements.
But, in short:
TIP
You will probably pass an adapter into handlery
, which itself takes a third-party emitter library.
The important part:
The important part is what handlery
returns:
const { EventHandler, on, subscribe, register } = handlery(emitter);
Here's a quick explanation:
EventHandler
is the base class you need to use for your classes. It manages your listeners, allowing for automatic unsubscribes, easily subscribing all events etc.on
is used for methods within theEventHandler
. It takes an event it (likeuser.add
) and passes the event data (and acontext
) to the method.register
is a decorator called on the class, used to create an instance of your handler and register it to the globalEventHandler
. That way you can manage all your listeners from a single place.subscribe
is also a class decorator, used to automatically 'enable' all event listeners of the class. This way you don't have to callMyHandler.subscribe()
afterwards.
INFO
Note that register
and subscribe
are totally optional and only for convenience. It's perfectly valid (and sometimes even necessary) to call MyHandler.register()
and MyHandler.subscribe()
afterwards.
INFO
Also, note that subscribe
calls register
under the hood. That's because internally, register
creates a singleton instance of your handler class, and subscribe
operates on that singleton. Thus a class must be registered before it can subscribe to events.