Skip to content

Commit

Permalink
refactor: separation of concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
DIY0R committed Jun 18, 2024
1 parent bbf8f67 commit a3884b6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
37 changes: 20 additions & 17 deletions lib/common/meta-teg.discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,47 @@ export class MetaTegsScannerService {
private readonly metadataScanner: MetadataScanner,
private readonly reflector: Reflector,
private readonly modulesContainer: ModulesContainer,
@Inject(TARGET_MODULE) private readonly targetModuleName: string,
@Inject(TARGET_MODULE) private readonly targetModuleName: string
) {}

public scan(metaTeg: string) {
const rmqMessagesMap = new Map();
const modules = [...this.modulesContainer.values()];

const currentModule = modules.find(
(module) => module.metatype?.name === this.targetModuleName,
);
const currentModule = this.getCurrentModule();
if (!currentModule) return rmqMessagesMap;
const providers = [...currentModule.providers.values()];
const controllers = [...currentModule.controllers.values()];
[...providers, ...controllers].forEach((provider: InstanceWrapper) => {

const providersAndControllers =
this.getProvidersAndControllers(currentModule);

providersAndControllers.forEach((provider: InstanceWrapper) => {
const { instance } = provider;
const prototype = Object.getPrototypeOf(instance);
this.metadataScanner
.getAllMethodNames(prototype)
.forEach((name: string) =>
this.lookupMethods(
metaTeg,
rmqMessagesMap,
instance,
prototype,
name,
),
this.lookupMethods(metaTeg, rmqMessagesMap, instance, prototype, name)
);
});

return rmqMessagesMap;
}

private getCurrentModule() {
const modules = [...this.modulesContainer.values()];
return (
modules.find(
(module) => module.metatype?.name === this.targetModuleName
) || null
);
}
private getProvidersAndControllers(module) {
return [...module.providers.values(), ...module.controllers.values()];
}
private lookupMethods(
metaTeg: string,
rmqMessagesMap: IMetaTegsMap,
instance: object,
prototype: object,
methodName: string,
methodName: string
) {
const method = prototype[methodName];
const event = this.reflector.get<string>(metaTeg, method);
Expand Down
8 changes: 2 additions & 6 deletions lib/rmq-connect.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class RmqNestjsConnectService implements OnModuleInit, OnModuleDestroy {
private connection: Connection = null;
private baseChannel: Channel = null;
private replyToChannel: Channel = null;

private declared = false;

constructor(
@Inject(RMQ_CONNECT_OPTIONS) private readonly options: IRabbitMQConfig
) {}
Expand Down Expand Up @@ -157,11 +157,7 @@ export class RmqNestjsConnectService implements OnModuleInit, OnModuleDestroy {
}

private async createChannel() {
try {
return await this.connection.createChannel();
} catch (error) {
throw error;
}
return await this.connection.createChannel();
}
async onModuleDestroy(): Promise<void> {
await this.baseChannel.close();
Expand Down
4 changes: 2 additions & 2 deletions lib/rmq-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IAppOptions } from './interfaces/app-options.interface';
export class RmqNestjsCoreModule {
static forRoot(
options: IRabbitMQConfig,
appOptions?: IAppOptions
appOptions?: IAppOptions,
): DynamicModule {
return {
module: RmqNestjsCoreModule,
Expand All @@ -23,7 +23,7 @@ export class RmqNestjsCoreModule {
}
static forRootAsync(
options: IRMQSRootAsyncOptions,
appOptions?: IAppOptions
appOptions?: IAppOptions,
): DynamicModule {
return {
module: RmqNestjsCoreModule,
Expand Down

0 comments on commit a3884b6

Please sign in to comment.