import { Injectable , InternalServerErrorException } from '@nestjs/common' ; import { NestEvent } from './nest-event' ; import { InjectNestEvent } from './decorators' ; import { EventEmitter } from 'events' ; import StrictEventEmitter from 'strict-event-emitter-types/types/src' ; Injectable ( ) ; export class NestEventEmitter { constructor ( @ InjectNestEvent ( ) private readonly nestEvent ) { } public emit ( event , ... args ) { const eventEmitter = this . emitter ( ) ; if ( eventEmitter ) { eventEmitter . emit ( event , ... args ) ; } } public on ( event , listener ) { const eventEmitter = this . emitter ( ) ; if ( eventEmitter ) { eventEmitter . on ( event , listener ) ; } } public off ( event , listener ) { const eventEmitter = this . emitter ( ) ; if ( eventEmitter ) { eventEmitter . off ( event , listener ) ; } } public emitter ( emitter = 'default' ) { if ( ! this . nestEvent . getEmitters ( ) . has ( emitter ) ) { throw new InternalServerErrorException ( `Emitter with name: '${ emitter }' not found` ) ; } return this . nestEvent . getEmitters ( ) . get ( emitter ) ; } public strictEmitter < T > ( emitter = 'default' ) { return this . emitter ( emitter ) ; } }