Class: EventListenService
Event Listen Service
This service coordinates the registering of listeners on dispatched events to the NestJS EventEmitter module.
Constructors
new EventListenService()
new EventListenService(
eventEmitter
):EventListenService
Constructor
Parameters
Parameter | Type | Description |
---|---|---|
eventEmitter | EventEmitter2 | Injected event emitter instance |
Returns
Defined in
Methods
createListenWrapper()
protected
createListenWrapper<E
>(listener
): (e
) =>EventReturnType
<E
>
Internal
Type Parameters
Type Parameter |
---|
E |
Parameters
Parameter | Type |
---|---|
listener | EventListenOnInterface <E > |
Returns
Function
Parameters
Parameter | Type |
---|---|
e | EventInstance <E > |
Returns
EventReturnType
<E
>
Defined in
on()
on<
E
>(eventClass
,listener
,options
):void
Adds a listener to the end of the listeners list for the specified event.
See the EventEmitter2.on (opens in a new tab) documentation for more details about the underlying emitter API.
Type Parameters
Type Parameter |
---|
E |
Parameters
Parameter | Type | Description |
---|---|---|
eventClass | EventClassInterface <E > | The event class to subscribe to. This is the class, NOT an instance. |
listener | EventListenOnInterface <E > | Instance of the event listener class to attach to the event. |
options | EventListenOnOptionsInterface | Overriding options. |
Returns
void
Example
import { Injectable, OnModuleInit } from '@nestjs/common';
import { EventListenService, EventListenerOn } from '@concepta/nestjs-events';
import { TargetEvent } from 'target-module';
class MyListener extends EventListenerOn<TargetEvent> {
listen(event: TargetEvent) {
console.log(event.payload);
}
}
@Injectable()
class MyClass implements OnModuleInit {
constructor(private eventListenService: EventListenService) {}
onModuleInit() {
// listener instance
const listener = new MyListener();
// register the listener
this.eventListenService.on(TargetEvent, listener);
}
}