Home > @xoram/core > onEvent
onEvent() function
Listen to a specific event of a source and cleanly stop to listen when the plugin is disposed of.
Signature:
typescript
export declare function onEvent<notifications extends Notifications, event extends keyof notifications>(target: EventTarget<notifications>, on: event, handler: Handler<notifications[event]>): EventCleanup;
1
Parameters
Parameter | Type | Description |
---|---|---|
target | EventTarget<notifications> | target something to listen events on |
on | event | the name of the event to listen to |
handler | Handler<notifications[event]> | callback to invoke when the event is emitted by |
Returns:
Example
ts
// target getter
onEvent(app => app.services.myService, 'an-event', console.log);
onEvent(({services}) => services.myService, 'an-event', console.log);
// direct target
onEvent(myCustomEmitter, 'an-event', console.log);
// service id
onEvent('myService', 'an-event', console.log);
1
2
3
4
5
6
7
2
3
4
5
6
7