Class: abstract
EventListener<E>
Abstract event listener class.
To create a custom event listener, extend the EventListener class and implement the [listen]EventListener#listen method. The [listen]EventListener#listen method will receive the payload dispatched by EventDispatchService.
You will also need to implement one of the interfaces that is enforced by the EventListenService method you intend to use. For example [EventListenService.on]EventListenService#on requires the EventListenOnInterface interface.
There are additional abstract classes available which have implemented the basic types for you. So far we have EventListenerOn... more to come!
Example
// event payload type
type MyEventPayload = {id: number, active: boolean};
// example event class
class MyEvent extends EventSync<MyEventPayload> {}
// example listener class
class MyListenOn extends EventListener<MyEvent>
implements EventListenOnInterface
{
// default options
options = {};
// custom handler
listen(event: MyEvent): void {
console.log(event.payload);
}
}
// new listener
const myListener = new MyListener();
// subscribe to the event
EventListenService.on(MyEvent, myListener);
// and you can remove the listener easily
listener.remove();
Extended by
Type Parameters
Type Parameter |
---|
E |
Implements
Constructors
new EventListener()
new EventListener<
E
>():EventListener
<E
>
Returns
Methods
listen()
abstract
listen(event
):EventReturnType
<E
>
Listen to an event.
Parameters
Parameter | Type |
---|---|
event | EventInstance <E > |
Returns
EventReturnType
<E
>
Implementation of
Defined in
.tmp/repos/rockets/packages/nestjs-event/src/listeners/event-listener.ts:60 (opens in a new tab)
remove()
remove():
void
Remove the subscription.
Returns
void
Implementation of
Defined in
.tmp/repos/rockets/packages/nestjs-event/src/listeners/event-listener.ts:85 (opens in a new tab)
subscription()
subscription(
emitterListener
):void
Internal
Called after successful subscription.
Parameters
Parameter | Type | Description |
---|---|---|
emitterListener | Listener | The Listener object returned by EventEmitter2 |
Returns
void
Implementation of
EventListenerInterface
.subscription
Defined in
.tmp/repos/rockets/packages/nestjs-event/src/listeners/event-listener.ts:68 (opens in a new tab)