Browse Source

feat: use event emitter for webhook(WIP)

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5597/head
Pranav C 2 years ago
parent
commit
d5235a2361
  1. 2
      packages/nocodb/src/Noco.ts
  2. 5
      packages/nocodb/src/app.module.ts
  3. 6
      packages/nocodb/src/modules/event-emitter/event-emitter.interface.ts
  4. 16
      packages/nocodb/src/modules/event-emitter/event-emitter.module.ts
  5. 27
      packages/nocodb/src/modules/event-emitter/fallback-event-emitter.ts
  6. 23
      packages/nocodb/src/modules/event-emitter/nestjs-event-emitter.ts

2
packages/nocodb/src/Noco.ts

@ -9,6 +9,7 @@ import { NC_LICENSE_KEY } from './constants';
import Store from './models/Store';
import type { Express } from 'express';
import type * as http from 'http';
import { IEventEmitter } from './modules/event-emitter/event-emitter.interface'
export default class Noco {
private static _this: Noco;
@ -30,6 +31,7 @@ export default class Noco {
}
public static config: any;
public static eventEmitter: IEventEmitter;
public readonly router: express.Router;
public readonly projectRouter: express.Router;
public static _ncMeta: any;

5
packages/nocodb/src/app.module.ts

@ -1,4 +1,4 @@
import { Module, RequestMethod } from '@nestjs/common';
import { Inject, Module, RequestMethod } from '@nestjs/common'
import { APP_FILTER } from '@nestjs/core';
import { Connection } from './connection/connection';
import { GlobalExceptionFilter } from './filters/global-exception/global-exception.filter';
@ -7,6 +7,7 @@ import { GlobalMiddleware } from './middlewares/global/global.middleware';
import { GuiMiddleware } from './middlewares/gui/gui.middleware';
import { PublicMiddleware } from './middlewares/public/public.middleware';
import { DatasModule } from './modules/datas/datas.module';
import { IEventEmitter } from './modules/event-emitter/event-emitter.interface'
import { AuthService } from './services/auth.service';
import { UsersModule } from './modules/users/users.module';
import { MetaService } from './meta/meta.service';
@ -51,6 +52,7 @@ export class AppModule implements OnApplicationBootstrap {
constructor(
private readonly connection: Connection,
private readonly metaService: MetaService,
@Inject('IEventEmitter') private readonly eventEmitter: IEventEmitter,
) {}
// Global Middleware
@ -80,6 +82,7 @@ export class AppModule implements OnApplicationBootstrap {
// temporary hack
Noco._ncMeta = this.metaService;
Noco.config = this.connection.config;
Noco.eventEmitter = this.eventEmitter;
// init plugin manager
await NcPluginMgrv2.init(Noco.ncMeta);

6
packages/nocodb/src/modules/event-emitter/event-emitter.interface.ts

@ -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;
}

16
packages/nocodb/src/modules/event-emitter/event-emitter.module.ts

@ -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 {}

27
packages/nocodb/src/modules/event-emitter/fallback-event-emitter.ts

@ -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);
}
}

23
packages/nocodb/src/modules/event-emitter/nestjs-event-emitter.ts

@ -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…
Cancel
Save