feat: create inject all bots decorator

This commit is contained in:
atassis 2022-07-09 18:15:13 +08:00
parent 9935b87d45
commit 92ff5164a6
No known key found for this signature in database
GPG Key ID: 263A448CB2BE27A4
7 changed files with 46 additions and 5 deletions

View File

@ -2,3 +2,4 @@ export * from './update.decorator';
export * from './scene.decorator';
export * from './wizard.decorator';
export * from './inject-bot.decorator';
export * from './inject-all-bots.decorator';

View File

@ -0,0 +1,9 @@
import { Inject } from '@nestjs/common';
import { Telegraf } from 'telegraf';
import { getAllBotsToken } from '../../utils/get-all-bots-token.util';
export type AllBotsMap = Map<string, Telegraf<any>>;
export const InjectAllBots = (): ParameterDecorator =>
Inject(getAllBotsToken());

View File

@ -10,8 +10,7 @@ export class BaseExplorerService {
if (!include || isEmpty(include)) {
return [...modulesContainer.values()];
}
const whitelisted = this.includeWhitelisted(modulesContainer, include);
return whitelisted;
return this.includeWhitelisted(modulesContainer, include);
}
includeWhitelisted(

View File

@ -0,0 +1,12 @@
import { Provider } from '@nestjs/common';
import { Telegraf } from 'telegraf';
import { TELEGRAF_ALL_BOTS } from './telegraf.constants';
export const allBotsMap = new Map<string, Telegraf<any>>();
setTimeout(() => console.log(allBotsMap), 2000);
export const telegrafAllBotsProvider: Provider = {
provide: TELEGRAF_ALL_BOTS,
useValue: allBotsMap,
};

View File

@ -19,6 +19,10 @@ import {
} from './telegraf.constants';
import { ListenersExplorerService, MetadataAccessorService } from './services';
import { telegrafStageProvider } from './stage.provider';
import {
allBotsMap,
telegrafAllBotsProvider,
} from './telegraf-all-bots.provider';
import { createBotFactory, getBotToken } from './utils';
@Global()
@ -43,7 +47,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
const telegrafBotProvider: Provider = {
provide: telegrafBotName,
useFactory: async () => await createBotFactory(options),
useFactory: async () => {
const bot = await createBotFactory(options);
allBotsMap.set(telegrafBotName, bot);
return bot;
},
};
return {
@ -56,11 +64,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
telegrafStageProvider,
telegrafBotNameProvider,
telegrafBotProvider,
telegrafAllBotsProvider,
],
exports: [
telegrafStageProvider,
telegrafBotNameProvider,
telegrafBotProvider,
telegrafAllBotsProvider,
],
};
}
@ -77,8 +87,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
const telegrafBotProvider: Provider = {
provide: telegrafBotName,
useFactory: async (options: TelegrafModuleOptions) =>
await createBotFactory(options),
useFactory: async (options: TelegrafModuleOptions) => {
const bot = await createBotFactory(options);
allBotsMap.set(telegrafBotName, bot);
return bot;
},
inject: [TELEGRAF_MODULE_OPTIONS],
};
@ -91,11 +104,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
telegrafStageProvider,
telegrafBotNameProvider,
telegrafBotProvider,
telegrafAllBotsProvider,
],
exports: [
telegrafStageProvider,
telegrafBotNameProvider,
telegrafBotProvider,
telegrafAllBotsProvider,
],
};
}

View File

@ -12,3 +12,5 @@ export const WIZARD_STEP_METADATA = 'WIZARD_STEP_METADATA';
export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA;
export const TELEGRAF_STAGE = 'TelegrafStage';
export const TELEGRAF_ALL_BOTS = 'TELEGRAF_ALL_BOTS';

View File

@ -0,0 +1,3 @@
import { TELEGRAF_ALL_BOTS } from '../telegraf.constants';
export const getAllBotsToken = (): string => TELEGRAF_ALL_BOTS;