feat(client): allow disabling updates manager

This commit is contained in:
alina 🌸 2023-12-19 00:08:10 +03:00
parent 2815567330
commit c278599612
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
3 changed files with 52 additions and 5 deletions

View file

@ -346,6 +346,25 @@ interface TelegramClientOptions extends Omit<BaseTelegramClientOptions, 'storage
* Parameters for updates manager.
*/
updates?: Omit<ParsedUpdateHandlerParams & UpdatesManagerParams, 'onUpdate' | 'onRawUpdate'>
/**
* **ADVANCED**
*
* If set to `true`, updates manager will not be created,
* and only raw TL Updates will be emitted.
*
* Unlike {@link TelegramClientOptions.disableUpdates}, this
* does not prevent the updates from being sent by the server,
* but disables proper handling of them (see [Working with Updates](https://core.telegram.org/api/updates))
*
* This may be useful in some cases when you require more control over
* the updates or to minimize additional overhead from properly handling them
* for some very particular use cases.
*
* The updates **will not** be dispatched the normal way, instead
* you should manually add a handler using `client.network.setUpdateHandler`.
*/
disableUpdatesManager?: boolean
}
export interface TelegramClient extends BaseTelegramClient {
@ -5206,6 +5225,7 @@ export interface TelegramClient extends BaseTelegramClient {
export type { TelegramClientOptions }
export class TelegramClient extends BaseTelegramClient {
protected _disableUpdatesManager: boolean
constructor(opts: TelegramClientOptions) {
if (typeof opts.storage === 'string') {
opts.storage = _defaultStorageFactory(opts.storage)
@ -5216,9 +5236,10 @@ export class TelegramClient extends BaseTelegramClient {
/* eslint-disable @typescript-eslint/no-unsafe-call */
// @ts-expect-error codegen
super(opts)
/* eslint-enable @typescript-eslint/no-unsafe-call */
if (!opts.disableUpdates) {
this._disableUpdatesManager = opts.disableUpdatesManager ?? false
if (!opts.disableUpdates && !opts.disableUpdatesManager) {
const { messageGroupingInterval, ...managerParams } = opts.updates ?? {}
enableUpdatesProcessing(this, {
@ -6239,7 +6260,7 @@ TelegramClient.prototype.start =
async function _start(this: TelegramClient, params: Parameters<typeof start>[1]) {
const user = await start(this, params)
if (!this.network.params.disableUpdates) {
if (!this.network.params.disableUpdates && !this._disableUpdatesManager) {
await this.startUpdatesLoop()
}

View file

@ -17,6 +17,11 @@ import {
UpdatesManagerParams,
} from './updates/index.js'
// @extension
interface TelegramClientExt {
_disableUpdatesManager: boolean
}
// @copy
interface TelegramClientOptions extends Omit<BaseTelegramClientOptions, 'storage'> {
/**
@ -34,6 +39,25 @@ interface TelegramClientOptions extends Omit<BaseTelegramClientOptions, 'storage
* Parameters for updates manager.
*/
updates?: Omit<ParsedUpdateHandlerParams & UpdatesManagerParams, 'onUpdate' | 'onRawUpdate'>
/**
* **ADVANCED**
*
* If set to `true`, updates manager will not be created,
* and only raw TL Updates will be emitted.
*
* Unlike {@link TelegramClientOptions.disableUpdates}, this
* does not prevent the updates from being sent by the server,
* but disables proper handling of them (see [Working with Updates](https://core.telegram.org/api/updates))
*
* This may be useful in some cases when you require more control over
* the updates or to minimize additional overhead from properly handling them
* for some very particular use cases.
*
* The updates **will not** be dispatched the normal way, instead
* you should manually add a handler using `client.network.setUpdateHandler`.
*/
disableUpdatesManager?: boolean
}
// @initialize=super
@ -54,7 +78,9 @@ function _initializeClientSuper(this: TelegramClient, opts: TelegramClientOption
// @initialize
/** @internal */
function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) {
if (!opts.disableUpdates) {
this._disableUpdatesManager = opts.disableUpdatesManager ?? false
if (!opts.disableUpdates && !opts.disableUpdatesManager) {
const { messageGroupingInterval, ...managerParams } = opts.updates ?? {}
enableUpdatesProcessing(this, {

View file

@ -217,7 +217,7 @@ export async function start(
async function _start(this: TelegramClient, params: Parameters<typeof start>[1]) {
const user = await start(this, params)
if (!this.network.params.disableUpdates) {
if (!this.network.params.disableUpdates && !this._disableUpdatesManager) {
await this.startUpdatesLoop()
}