mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-01-12 07:01:26 +03:00
feat(listeners): support for chaining method listeners
This commit is contained in:
parent
0339fe15ae
commit
a4cb8df434
@ -22,7 +22,8 @@ import { TelegrafModuleOptions } from '../interfaces';
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class ListenersExplorerService
|
export class ListenersExplorerService
|
||||||
extends BaseExplorerService
|
extends BaseExplorerService
|
||||||
implements OnModuleInit {
|
implements OnModuleInit
|
||||||
|
{
|
||||||
private readonly telegrafParamsFactory = new TelegrafParamsFactory();
|
private readonly telegrafParamsFactory = new TelegrafParamsFactory();
|
||||||
private bot: Telegraf<any>;
|
private bot: Telegraf<any>;
|
||||||
|
|
||||||
@ -123,7 +124,7 @@ export class ListenersExplorerService
|
|||||||
): void {
|
): void {
|
||||||
const methodRef = prototype[methodName];
|
const methodRef = prototype[methodName];
|
||||||
const metadata = this.metadataAccessor.getListenerMetadata(methodRef);
|
const metadata = this.metadataAccessor.getListenerMetadata(methodRef);
|
||||||
if (!metadata) {
|
if (!metadata || metadata.length < 1) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,8 +134,7 @@ export class ListenersExplorerService
|
|||||||
methodName,
|
methodName,
|
||||||
);
|
);
|
||||||
|
|
||||||
const { method, args } = metadata;
|
for (const { method, args } of metadata) {
|
||||||
|
|
||||||
/* Basic callback */
|
/* Basic callback */
|
||||||
// composer[method](...args, listenerCallbackFn);
|
// composer[method](...args, listenerCallbackFn);
|
||||||
|
|
||||||
@ -150,6 +150,7 @@ export class ListenersExplorerService
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
createContextCallback<T extends Record<string, unknown>>(
|
createContextCallback<T extends Record<string, unknown>>(
|
||||||
instance: T,
|
instance: T,
|
||||||
|
@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { Reflector } from '@nestjs/core';
|
import { Reflector } from '@nestjs/core';
|
||||||
import {
|
import {
|
||||||
SCENE_METADATA,
|
SCENE_METADATA,
|
||||||
LISTENER_METADATA,
|
LISTENERS_METADATA,
|
||||||
UPDATE_METADATA,
|
UPDATE_METADATA,
|
||||||
} from '../telegraf.constants';
|
} from '../telegraf.constants';
|
||||||
import { ListenerMetadata } from '../interfaces';
|
import { ListenerMetadata } from '../interfaces';
|
||||||
@ -21,8 +21,8 @@ export class MetadataAccessorService {
|
|||||||
return !!this.reflector.get(SCENE_METADATA, target);
|
return !!this.reflector.get(SCENE_METADATA, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
getListenerMetadata(target: Function): ListenerMetadata | undefined {
|
getListenerMetadata(target: Function): ListenerMetadata[] | undefined {
|
||||||
return this.reflector.get(LISTENER_METADATA, target);
|
return this.reflector.get(LISTENERS_METADATA, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
getSceneMetadata(target: Function): string | undefined {
|
getSceneMetadata(target: Function): string | undefined {
|
||||||
|
@ -6,7 +6,7 @@ export const DEFAULT_BOT_NAME = 'DEFAULT_BOT_NAME';
|
|||||||
|
|
||||||
export const UPDATE_METADATA = 'UPDATE_METADATA';
|
export const UPDATE_METADATA = 'UPDATE_METADATA';
|
||||||
export const SCENE_METADATA = 'SCENE_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;
|
export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA;
|
||||||
|
|
||||||
|
@ -1,17 +1,29 @@
|
|||||||
import { SetMetadata } from '@nestjs/common';
|
|
||||||
import { Composer } from 'telegraf';
|
import { Composer } from 'telegraf';
|
||||||
import { ComposerMethodArgs, OnlyFunctionPropertyNames } from '../types';
|
import { ComposerMethodArgs, OnlyFunctionPropertyNames } from '../types';
|
||||||
import { LISTENER_METADATA } from '../telegraf.constants';
|
import { LISTENERS_METADATA } from '../telegraf.constants';
|
||||||
import { ListenerMetadata } from '../interfaces';
|
import { ListenerMetadata } from '../interfaces';
|
||||||
|
|
||||||
export function createListenerDecorator<
|
export function createListenerDecorator<
|
||||||
TComposer extends Composer<never>,
|
TComposer extends Composer<never>,
|
||||||
TMethod extends OnlyFunctionPropertyNames<TComposer> = OnlyFunctionPropertyNames<TComposer>
|
TMethod extends OnlyFunctionPropertyNames<TComposer> = OnlyFunctionPropertyNames<TComposer>,
|
||||||
>(method: TMethod) {
|
>(method: TMethod) {
|
||||||
return (...args: ComposerMethodArgs<TComposer, TMethod>): MethodDecorator => {
|
return (...args: ComposerMethodArgs<TComposer, TMethod>): MethodDecorator => {
|
||||||
return SetMetadata(LISTENER_METADATA, {
|
return (
|
||||||
|
_target: any,
|
||||||
|
_key?: string | symbol,
|
||||||
|
descriptor?: TypedPropertyDescriptor<any>,
|
||||||
|
) => {
|
||||||
|
const metadata = [
|
||||||
|
{
|
||||||
method,
|
method,
|
||||||
args,
|
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;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user