mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
6 changed files with 78 additions and 1 deletions
@ -0,0 +1,6 @@
|
||||
export interface IEventEmitter { |
||||
emit(event: string, arg: any): void; |
||||
on(event: string, listener: (arg: any) => void): () => void; |
||||
removeListener(event: string, listener: (arg: any) => void): void; |
||||
removeAllListeners(event?: string): void; |
||||
} |
@ -0,0 +1,16 @@
|
||||
import { Global, Module } from '@nestjs/common'; |
||||
import { FallbackEventEmitter } from './fallback-event-emitter'; |
||||
|
||||
@Global() |
||||
@Module({ |
||||
providers: [ |
||||
{ |
||||
provide: 'IEventEmitter', |
||||
useFactory: () => { |
||||
return new FallbackEventEmitter(); |
||||
}, |
||||
}, |
||||
], |
||||
exports: ['IEventEmitter'], |
||||
}) |
||||
export class EventEmitterModule {} |
@ -0,0 +1,27 @@
|
||||
import Emittery from 'emittery'; |
||||
import { IEventEmitter } from './event-emitter.interface'; |
||||
|
||||
export class FallbackEventEmitter implements IEventEmitter { |
||||
private readonly emitter: Emittery; |
||||
|
||||
constructor() { |
||||
this.emitter = new Emittery(); |
||||
} |
||||
|
||||
emit(event: string, data: any): void { |
||||
this.emitter.emit(event, data); |
||||
} |
||||
|
||||
on(event: string, listener: (...args: any[]) => void) { |
||||
this.emitter.on(event, listener); |
||||
return () => this.emitter.off(event, listener); |
||||
} |
||||
|
||||
removeListener(event: string, listener: (...args: any[]) => void): void { |
||||
this.emitter.off(event, listener); |
||||
} |
||||
|
||||
removeAllListeners(event?: string): void { |
||||
this.emitter.clearListeners(event); |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter'; |
||||
import { IEventEmitter } from './event-emitter.interface'; |
||||
|
||||
export class NestjsEventEmitter implements IEventEmitter { |
||||
constructor(private readonly eventEmitter: EventEmitter2) {} |
||||
|
||||
emit(event: string, data: any): void { |
||||
this.eventEmitter.emit(event, data); |
||||
} |
||||
|
||||
on(event: string, listener: (...args: any[]) => void) { |
||||
this.eventEmitter.on(event, listener); |
||||
return () => this.eventEmitter.removeListener(event, listener); |
||||
} |
||||
|
||||
removeListener(event: string, listener: (...args: any[]) => void): void { |
||||
this.eventEmitter.removeListener(event, listener); |
||||
} |
||||
|
||||
removeAllListeners(event?: string): void { |
||||
this.eventEmitter.removeAllListeners(event); |
||||
} |
||||
} |
Loading…
Reference in new issue