refactor(): extract bot instace factory

This commit is contained in:
Morb0 2021-01-03 13:04:53 +03:00
parent 34070fcf68
commit d48123ea38
2 changed files with 22 additions and 37 deletions

View File

@ -7,7 +7,6 @@ import {
Global,
Inject,
OnApplicationShutdown,
Logger,
} from '@nestjs/common';
import {
TelegrafModuleOptions,
@ -24,8 +23,7 @@ import {
UpdatesExplorerService,
} from './services';
import { getBotToken } from './utils';
import { Telegraf } from 'telegraf';
import { defer } from 'rxjs';
import { createBotFactory } from './utils/create-bot-factory.util';
@Global()
@Module({
@ -37,8 +35,6 @@ import { defer } from 'rxjs';
],
})
export class TelegrafCoreModule implements OnApplicationShutdown {
private static logger = new Logger(TelegrafCoreModule.name);
constructor(
@Inject(TELEGRAF_BOT_NAME) private readonly botName: string,
private readonly moduleRef: ModuleRef,
@ -47,15 +43,9 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
public static forRoot(options: TelegrafModuleOptions): DynamicModule {
const telegrafBotName = getBotToken(options.botName);
const telegrafBotProvider = {
const telegrafBotProvider: Provider = {
provide: telegrafBotName,
useFactory: async (): Promise<any> =>
await defer(async () => {
const bot = new Telegraf<any>(options.token);
this.applyBotMiddlewares(bot, options.middlewares);
await bot.launch(options.launchOptions);
return bot;
}).toPromise(),
useFactory: async () => await createBotFactory(options),
};
return {
@ -80,20 +70,10 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
): DynamicModule {
const telegrafBotName = getBotToken(options.botName);
const telegrafBotProvider = {
const telegrafBotProvider: Provider = {
provide: telegrafBotName,
useFactory: async (
telegrafModuleOptions: TelegrafModuleOptions,
): Promise<any> => {
const { botName, ...telegrafOptions } = telegrafModuleOptions;
return await defer(async () => {
const bot = new Telegraf<any>(telegrafOptions.token);
this.applyBotMiddlewares(bot, telegrafOptions.middlewares);
await bot.launch(telegrafOptions.launchOptions);
return bot;
}).toPromise();
},
useFactory: async (options: TelegrafModuleOptions) =>
await createBotFactory(options),
inject: [TELEGRAF_MODULE_OPTIONS],
};
@ -113,12 +93,9 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
};
}
private static applyBotMiddlewares(bot, middlewares) {
if (middlewares) {
middlewares.forEach((middleware) => {
bot.use(middleware);
});
}
async onApplicationShutdown(): Promise<void> {
const bot = this.moduleRef.get<any>(this.botName);
bot && (await bot.stop());
}
private static createAsyncProviders(
@ -158,9 +135,4 @@ export class TelegrafCoreModule implements OnApplicationShutdown {
inject,
};
}
async onApplicationShutdown(): Promise<void> {
const bot = this.moduleRef.get<any>(this.botName);
bot && (await bot.stop());
}
}

View File

@ -0,0 +1,13 @@
import { Telegraf } from 'telegraf';
import { TelegrafModuleOptions } from '../interfaces';
export async function createBotFactory(
options: TelegrafModuleOptions,
): Promise<Telegraf<never>> {
const bot = new Telegraf<never>(options.token, options.options);
bot.use(...(options.middlewares ?? []));
await bot.launch(options.launchOptions);
return bot;
}