mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-24 15:04:38 +03:00
feat(explorer): add custom context callback creation
This commit is contained in:
parent
84f90495ea
commit
89d7304eba
@ -18,11 +18,6 @@ import { TelegrafParamsFactory } from '../factories/telegraf-params-factory';
|
|||||||
import { TelegrafContextType } from '../execution-context/telegraf-execution-context';
|
import { TelegrafContextType } from '../execution-context/telegraf-execution-context';
|
||||||
import { ParamMetadata } from '@nestjs/core/helpers/interfaces';
|
import { ParamMetadata } from '@nestjs/core/helpers/interfaces';
|
||||||
|
|
||||||
interface ListenerCallbackMetadata {
|
|
||||||
methodName: string;
|
|
||||||
metadata: ListenerMetadata;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ListenersExplorerService
|
export class ListenersExplorerService
|
||||||
extends BaseExplorerService
|
extends BaseExplorerService
|
||||||
@ -66,24 +61,21 @@ export class ListenersExplorerService
|
|||||||
const updates = this.flatMap<InstanceWrapper>(modules, (instance) =>
|
const updates = this.flatMap<InstanceWrapper>(modules, (instance) =>
|
||||||
this.filterUpdates(instance),
|
this.filterUpdates(instance),
|
||||||
);
|
);
|
||||||
updates.forEach(({ instance }) =>
|
updates.forEach((wrapper) => this.registerListeners(this.bot, wrapper));
|
||||||
this.registerInstanceMethodListeners(this.bot, instance),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerScenes(modules: Module[]): void {
|
private registerScenes(modules: Module[]): void {
|
||||||
const scenes = this.flatMap<InstanceWrapper>(
|
const scenes = this.flatMap<InstanceWrapper>(modules, (wrapper) =>
|
||||||
modules,
|
this.filterScenes(wrapper),
|
||||||
(instance, moduleRef) => this.filterScenes(instance),
|
|
||||||
);
|
);
|
||||||
scenes.forEach((wrapper) => {
|
scenes.forEach((wrapper) => {
|
||||||
const sceneId = this.metadataAccessor.getSceneMetadata(
|
const sceneId = this.metadataAccessor.getSceneMetadata(
|
||||||
instance.constructor,
|
wrapper.instance.constructor,
|
||||||
);
|
);
|
||||||
const scene = new BaseScene(sceneId);
|
const scene = new BaseScene(sceneId);
|
||||||
this.stage.register(scene);
|
this.stage.register(scene);
|
||||||
|
|
||||||
this.registerInstanceMethodListeners(scene, wrapper);
|
this.registerListeners(scene, wrapper);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,49 +99,42 @@ export class ListenersExplorerService
|
|||||||
return wrapper;
|
return wrapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerInstanceMethodListeners(
|
private registerListeners(
|
||||||
composer: Composer<never>,
|
composer: Composer<any>,
|
||||||
wrapper: InstanceWrapper<unknown>,
|
wrapper: InstanceWrapper<unknown>,
|
||||||
): void {
|
): void {
|
||||||
const { instance } = wrapper;
|
const { instance } = wrapper;
|
||||||
const prototype = Object.getPrototypeOf(instance);
|
const prototype = Object.getPrototypeOf(instance);
|
||||||
const listenersMetadata = this.metadataScanner.scanFromPrototype(
|
this.metadataScanner.scanFromPrototype(instance, prototype, (name) =>
|
||||||
instance,
|
this.registerIfListener(composer, instance, prototype, name),
|
||||||
prototype,
|
|
||||||
(name): ListenerCallbackMetadata =>
|
|
||||||
this.extractListenerCallbackMetadata(prototype, name),
|
|
||||||
);
|
|
||||||
|
|
||||||
const contextCallbackFn = this.createContextCallback(
|
|
||||||
instance,
|
|
||||||
prototype,
|
|
||||||
wrapper,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private extractListenerCallbackMetadata(
|
private registerIfListener(
|
||||||
|
composer: Composer<any>,
|
||||||
|
instance: any,
|
||||||
prototype: any,
|
prototype: any,
|
||||||
methodName: string,
|
methodName: string,
|
||||||
): ListenerCallbackMetadata {
|
): void {
|
||||||
const callback = prototype[methodName];
|
const methodRef = prototype[methodName];
|
||||||
const metadata = this.metadataAccessor.getListenerMetadata(callback);
|
const metadata = this.metadataAccessor.getListenerMetadata(methodRef);
|
||||||
|
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
const listenerCallbackFn = this.createContextCallback(
|
||||||
|
instance,
|
||||||
|
prototype,
|
||||||
methodName,
|
methodName,
|
||||||
metadata: metadata,
|
);
|
||||||
};
|
|
||||||
|
// TODO: Add callback to middleware and handle return data
|
||||||
}
|
}
|
||||||
|
|
||||||
createContextCallback<T extends Record<string, unknown>>(
|
createContextCallback<T extends Record<string, unknown>>(
|
||||||
instance: T,
|
instance: T,
|
||||||
prototype: unknown,
|
prototype: unknown,
|
||||||
wrapper: InstanceWrapper,
|
methodName: string,
|
||||||
moduleRef: Module,
|
|
||||||
listener: ListenerMetadata,
|
|
||||||
) {
|
) {
|
||||||
const paramsFactory = this.telegrafParamsFactory;
|
const paramsFactory = this.telegrafParamsFactory;
|
||||||
const resolverCallback = this.externalContextCreator.create<
|
const resolverCallback = this.externalContextCreator.create<
|
||||||
@ -157,8 +142,8 @@ export class ListenersExplorerService
|
|||||||
TelegrafContextType
|
TelegrafContextType
|
||||||
>(
|
>(
|
||||||
instance,
|
instance,
|
||||||
prototype[listener.methodName],
|
prototype[methodName],
|
||||||
listener.methodName,
|
methodName,
|
||||||
PARAM_ARGS_METADATA,
|
PARAM_ARGS_METADATA,
|
||||||
paramsFactory,
|
paramsFactory,
|
||||||
undefined,
|
undefined,
|
||||||
|
Loading…
Reference in New Issue
Block a user