feat(listeners): support for chaining method listeners

This commit is contained in:
xTCry [Vladislav Kh] 2021-08-01 07:31:09 +03:00
parent 0339fe15ae
commit a4cb8df434
No known key found for this signature in database
GPG Key ID: 79A813C6734F732D
4 changed files with 41 additions and 28 deletions

View File

@ -22,7 +22,8 @@ import { TelegrafModuleOptions } from '../interfaces';
@Injectable()
export class ListenersExplorerService
extends BaseExplorerService
implements OnModuleInit {
implements OnModuleInit
{
private readonly telegrafParamsFactory = new TelegrafParamsFactory();
private bot: Telegraf<any>;
@ -123,7 +124,7 @@ export class ListenersExplorerService
): void {
const methodRef = prototype[methodName];
const metadata = this.metadataAccessor.getListenerMetadata(methodRef);
if (!metadata) {
if (!metadata || metadata.length < 1) {
return undefined;
}
@ -133,8 +134,7 @@ export class ListenersExplorerService
methodName,
);
const { method, args } = metadata;
for (const { method, args } of metadata) {
/* Basic callback */
// composer[method](...args, listenerCallbackFn);
@ -150,6 +150,7 @@ export class ListenersExplorerService
},
);
}
}
createContextCallback<T extends Record<string, unknown>>(
instance: T,

View File

@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import {
SCENE_METADATA,
LISTENER_METADATA,
LISTENERS_METADATA,
UPDATE_METADATA,
} from '../telegraf.constants';
import { ListenerMetadata } from '../interfaces';
@ -21,8 +21,8 @@ export class MetadataAccessorService {
return !!this.reflector.get(SCENE_METADATA, target);
}
getListenerMetadata(target: Function): ListenerMetadata | undefined {
return this.reflector.get(LISTENER_METADATA, target);
getListenerMetadata(target: Function): ListenerMetadata[] | undefined {
return this.reflector.get(LISTENERS_METADATA, target);
}
getSceneMetadata(target: Function): string | undefined {

View File

@ -6,7 +6,7 @@ export const DEFAULT_BOT_NAME = 'DEFAULT_BOT_NAME';
export const UPDATE_METADATA = 'UPDATE_METADATA';
export const SCENE_METADATA = 'SCENE_METADATA';
export const LISTENER_METADATA = 'LISTENER_METADATA';
export const LISTENERS_METADATA = 'LISTENERS_METADATA';
export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA;

View File

@ -1,17 +1,29 @@
import { SetMetadata } from '@nestjs/common';
import { Composer } from 'telegraf';
import { ComposerMethodArgs, OnlyFunctionPropertyNames } from '../types';
import { LISTENER_METADATA } from '../telegraf.constants';
import { LISTENERS_METADATA } from '../telegraf.constants';
import { ListenerMetadata } from '../interfaces';
export function createListenerDecorator<
TComposer extends Composer<never>,
TMethod extends OnlyFunctionPropertyNames<TComposer> = OnlyFunctionPropertyNames<TComposer>
TMethod extends OnlyFunctionPropertyNames<TComposer> = OnlyFunctionPropertyNames<TComposer>,
>(method: TMethod) {
return (...args: ComposerMethodArgs<TComposer, TMethod>): MethodDecorator => {
return SetMetadata(LISTENER_METADATA, {
return (
_target: any,
_key?: string | symbol,
descriptor?: TypedPropertyDescriptor<any>,
) => {
const metadata = [
{
method,
args,
} as ListenerMetadata);
} as ListenerMetadata,
];
const previousValue = Reflect.getMetadata(LISTENERS_METADATA, descriptor.value) || [];
const value = [...previousValue, ...metadata];
Reflect.defineMetadata(LISTENERS_METADATA, value, descriptor.value);
return descriptor;
};
};
}