@botol/tg-threads
TypeScript icon, indicating that this package has built-in type declarations

0.0.5 • Public • Published

Threads Middleware from BotolTG

Options

Name Type Default Description
scriptPath string - Path to script
workerCount number 3 (Optional) Workers count
execTimeout number 0 (Optional) Max allowed time to process update (Set 0 to unlimit). Throw error on limit

Example

main.ts

import { BotolTg } from '@botol/tg-bot';
import { BotThreads } from '@botol/tg-threads';
import { isMainThread } from 'worker_threads';

let bot = new BotolTg('<token>');
bot.use(async (ctx, next) => {
    if (isMainThread) {
        // Some in main thread before pass update to workers
    } else {
        // Some in worker thread before executing
    }
    await next();
    if (isMainThread) {
        // Some in main thread after pass update to workers
    } else {
        // Some in worker thread after executing
    }
});
bot.middleware(
    BotThreads(bot, {
        scriptPath: __filename,
    }),
);
bot.use((ctx) => {
    ctx.reply('hi');
});
if (isMainThread) {
    bot.startPolling();
}

With ts-node

index.js

const path = require('path');

require('ts-node').register({ transpileOnly: true });
require(path.resolve(__dirname, './main.ts'));

main.ts

import { BotolTg } from '@botol/tg-bot';
import { BotThreads } from '@botol/tg-threads';
import { isMainThread } from 'worker_threads';
import path from 'path';

let bot = new BotolTg('<token>');
bot.use(async (ctx, next) => {
    if (isMainThread) {
        // Some in main thread before pass update to workers
    } else {
        // Some in worker thread before executing
    }
    await next();
    if (isMainThread) {
        // Some in main thread after pass update to workers
    } else {
        // Some in worker thread after executing
    }
});
bot.middleware(
    BotThreads(bot, {
        scriptPath: path.resolve(__dirname, './index.js'),
    }),
);
bot.use((ctx) => {
    ctx.reply('hi');
});
if (isMainThread) {
    bot.startPolling();
}

> ts-node index.js

Package Sidebar

Install

npm i @botol/tg-threads

Weekly Downloads

1

Version

0.0.5

License

MIT

Unpacked Size

4.53 kB

Total Files

7

Last publish

Collaborators

  • botol