fix(): update decorator detection

This commit is contained in:
unknown 2020-12-26 16:24:38 +03:00
parent 0dc981dc95
commit 66309b9415
3 changed files with 15 additions and 16 deletions

View File

@ -3,7 +3,7 @@ import {
UPDATE_LISTENER_OPTIONS_METADATA, UPDATE_LISTENER_OPTIONS_METADATA,
UPDATE_LISTENER_TYPE_METADATA, UPDATE_LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums';
import { TelegrafHearsTriggers } from '../../telegraf.types'; import { TelegrafHearsTriggers } from '../../telegraf.types';
export interface HearsOptions { export interface HearsOptions {

View File

@ -24,33 +24,27 @@ import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
@Injectable() @Injectable()
export class TelegrafExplorer implements OnModuleInit { export class TelegrafExplorer implements OnModuleInit {
constructor( constructor(
private readonly moduleRef: ModuleRef, private readonly telegraf: TelegrafProvider,
private readonly discoveryService: DiscoveryService, private readonly discoveryService: DiscoveryService,
private readonly metadataAccessor: TelegrafMetadataAccessor, private readonly metadataAccessor: TelegrafMetadataAccessor,
private readonly metadataScanner: MetadataScanner, private readonly metadataScanner: MetadataScanner,
) {} ) {}
private telegraf: TelegrafProvider;
onModuleInit(): void { onModuleInit(): void {
this.telegraf = this.moduleRef.get<TelegrafProvider>(TelegrafProvider, {
strict: false,
});
this.explore(); this.explore();
} }
explore(): void { explore(): void {
const updateInstanceWrappers = this.filterUpdateClass(); const updateClasses = this.filterUpdateClass();
updateInstanceWrappers.forEach((wrapper) => { updateClasses.forEach((wrapper) => {
const { instance } = wrapper; const { instance } = wrapper;
const prototype = Object.getPrototypeOf(instance); const prototype = Object.getPrototypeOf(instance);
this.metadataScanner.scanFromPrototype( this.metadataScanner.scanFromPrototype(
instance, instance,
prototype, prototype,
(methodKey: string) => (methodKey: string) => this.registerIfListener(instance, methodKey),
this.registerIfUpdateListener(instance, methodKey),
); );
}); });
} }
@ -59,10 +53,12 @@ export class TelegrafExplorer implements OnModuleInit {
return this.discoveryService return this.discoveryService
.getProviders() .getProviders()
.filter((wrapper) => wrapper.instance) .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<string, Function>, instance: Record<string, Function>,
methodKey: string, methodKey: string,
): void { ): void {

View File

@ -2,8 +2,8 @@ import { Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core'; import { Reflector } from '@nestjs/core';
import { import {
UPDATE_LISTENER_TYPE_METADATA, UPDATE_LISTENER_TYPE_METADATA,
TELEGRAF_MODULE_OPTIONS,
UPDATE_METADATA, UPDATE_METADATA,
UPDATE_LISTENER_OPTIONS_METADATA,
} from './telegraf.constants'; } from './telegraf.constants';
import { ListenerType } from './enums'; import { ListenerType } from './enums';
@ -22,7 +22,10 @@ export class TelegrafMetadataAccessor {
); );
} }
getListenerOptions<T>(target: Function): unknown | undefined { getListenerOptions(target: Function): unknown | undefined {
return this.reflector.get<unknown>(TELEGRAF_MODULE_OPTIONS, target); return this.reflector.get<unknown>(
UPDATE_LISTENER_OPTIONS_METADATA,
target,
);
} }
} }