Event Decorator
Decorates EventEmitter with handy methods when working with multiple events
Install
$ npm install --save events-decorator
API
const onceAny onceAll decorate = ;
onceAny(emitter, eventNames, handler)
Will subscribe to all events is evantNames
from emitter
and will execute handler
once for
the first event that occurs. When handler is executed, it will immediately unsubscribe from all
events.
Returns a function to unsubscribe from the events.
const onceAny = ; const unsubscribe = ;
Parameters
Name | Type | Description |
---|---|---|
emitter | EventEmitter |
|
eventNames | Array |
defaults to empty array [] |
handler | Function |
user provider handler |
Returns
unsubscriber: function()
calling this function before the handler is executed will unsubscribe
from all the events. Calling it after the handler is executed has no effect.
onceAll(emitter, eventNames, handler)
Will subscribe to all events is evantNames
from emitter
and will execute handler
once for
the last event that occurs. When handler is executed, it will immediately unsubscribe from all
events.
Returns a function to unsubscribe from the events.
const onceAll = ; const unsubscribe = ;
Parameters
Name | Type | Description |
---|---|---|
emitter | EventEmitter |
|
eventNames | Array |
defaults to empty array [] |
handler | Function |
user provider handler |
Returns
unsubscriber: function()
calling this function before the handler is executed will unsubscribe
from all the events. Calling it after the handler is executed has no effect.
decorate(emitter)
Decorates the EventEmitter
with the onceAny
and onceAll
methods. It won't modify the emitter
.
The new methods will have similar API to the ones described above, but omitting the first parameter.
Returns a Proxy
for the emitter
.
const decorate = ; const decorated = ; const unsub1 = decorated; const unsub2 = decorated;
Parameters
Name | Type | Description |
---|---|---|
emitter | EventEmitter |
Returns
decorated: Proxy<EventEmitter>
Examples
To count the in-flight requests on a express server:
const onceAny = ; let inflightRequests = 0;app;
To log the response time for each request:
const onceAny = ; app;
MIT © Guilherme Hermeto