From 66309b9415f377fecf1deedd700f3443b2b2eaf0 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 26 Dec 2020 16:24:38 +0300 Subject: [PATCH] fix(): update decorator detection --- lib/decorators/listeners/hears.decorator.ts | 2 +- lib/telegraf.explorer.ts | 20 ++++++++------------ lib/telegraf.metadata-accessor.ts | 9 ++++++--- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/decorators/listeners/hears.decorator.ts b/lib/decorators/listeners/hears.decorator.ts index 1f2f93e..760409a 100644 --- a/lib/decorators/listeners/hears.decorator.ts +++ b/lib/decorators/listeners/hears.decorator.ts @@ -3,7 +3,7 @@ import { UPDATE_LISTENER_OPTIONS_METADATA, UPDATE_LISTENER_TYPE_METADATA, } from '../../telegraf.constants'; -import { ListenerType } from '../../enums/listener-type.enum'; +import { ListenerType } from '../../enums'; import { TelegrafHearsTriggers } from '../../telegraf.types'; export interface HearsOptions { diff --git a/lib/telegraf.explorer.ts b/lib/telegraf.explorer.ts index 4df95e5..0d2751b 100644 --- a/lib/telegraf.explorer.ts +++ b/lib/telegraf.explorer.ts @@ -24,33 +24,27 @@ import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper'; @Injectable() export class TelegrafExplorer implements OnModuleInit { constructor( - private readonly moduleRef: ModuleRef, + private readonly telegraf: TelegrafProvider, private readonly discoveryService: DiscoveryService, private readonly metadataAccessor: TelegrafMetadataAccessor, private readonly metadataScanner: MetadataScanner, ) {} - private telegraf: TelegrafProvider; - onModuleInit(): void { - this.telegraf = this.moduleRef.get(TelegrafProvider, { - strict: false, - }); this.explore(); } explore(): void { - const updateInstanceWrappers = this.filterUpdateClass(); + const updateClasses = this.filterUpdateClass(); - updateInstanceWrappers.forEach((wrapper) => { + updateClasses.forEach((wrapper) => { const { instance } = wrapper; const prototype = Object.getPrototypeOf(instance); this.metadataScanner.scanFromPrototype( instance, prototype, - (methodKey: string) => - this.registerIfUpdateListener(instance, methodKey), + (methodKey: string) => this.registerIfListener(instance, methodKey), ); }); } @@ -59,10 +53,12 @@ export class TelegrafExplorer implements OnModuleInit { return this.discoveryService .getProviders() .filter((wrapper) => wrapper.instance) - .filter((wrapper) => this.metadataAccessor.isUpdate(wrapper.instance)); + .filter((wrapper) => + this.metadataAccessor.isUpdate(wrapper.instance.constructor), + ); } - private registerIfUpdateListener( + private registerIfListener( instance: Record, methodKey: string, ): void { diff --git a/lib/telegraf.metadata-accessor.ts b/lib/telegraf.metadata-accessor.ts index 29b5c68..e79e9e1 100644 --- a/lib/telegraf.metadata-accessor.ts +++ b/lib/telegraf.metadata-accessor.ts @@ -2,8 +2,8 @@ import { Injectable } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import { UPDATE_LISTENER_TYPE_METADATA, - TELEGRAF_MODULE_OPTIONS, UPDATE_METADATA, + UPDATE_LISTENER_OPTIONS_METADATA, } from './telegraf.constants'; import { ListenerType } from './enums'; @@ -22,7 +22,10 @@ export class TelegrafMetadataAccessor { ); } - getListenerOptions(target: Function): unknown | undefined { - return this.reflector.get(TELEGRAF_MODULE_OPTIONS, target); + getListenerOptions(target: Function): unknown | undefined { + return this.reflector.get( + UPDATE_LISTENER_OPTIONS_METADATA, + target, + ); } }