diff --git a/packages/core/src/highlevel/client.ts b/packages/core/src/highlevel/client.ts index eb8cfea3..ca16ef77 100644 --- a/packages/core/src/highlevel/client.ts +++ b/packages/core/src/highlevel/client.ts @@ -350,6 +350,14 @@ type TelegramClientOptions = ( }) | { client: ITelegramClient } ) & { + /** + * If true, all API calls will be wrapped with `tl.invokeWithoutUpdates`, + * effectively disabling the server-sent events for the clients. + * May be useful in some cases. + * + * @default false + */ + disableUpdates?: boolean updates?: Omit /** * If `true`, the updates that were handled by some {@link Conversation} @@ -5410,23 +5418,25 @@ export class TelegramClient extends EventEmitter implements ITelegramClient { // @ts-expect-error codegen this.storage = this._client.storage - const skipConversationUpdates = opts.skipConversationUpdates ?? true - const { messageGroupingInterval } = opts.updates ?? {} + if (!opts.disableUpdates) { + const skipConversationUpdates = opts.skipConversationUpdates ?? true + const { messageGroupingInterval } = opts.updates ?? {} - this._client.onUpdate( - makeParsedUpdateHandler({ - messageGroupingInterval, - onUpdate: (update) => { - if (Conversation.handleUpdate(this._client, update) && skipConversationUpdates) return + this._client.onUpdate( + makeParsedUpdateHandler({ + messageGroupingInterval, + onUpdate: (update) => { + if (Conversation.handleUpdate(this._client, update) && skipConversationUpdates) return - this.emit('update', update) - this.emit(update.name, update.data) - }, - onRawUpdate: (update, peers) => { - this.emit('raw_update', update, peers) - }, - }), - ) + this.emit('update', update) + this.emit(update.name, update.data) + }, + onRawUpdate: (update, peers) => { + this.emit('raw_update', update, peers) + }, + }), + ) + } } withParams(params: RpcCallOptions): this { return withParams(this, params) diff --git a/packages/core/src/highlevel/methods/_init.ts b/packages/core/src/highlevel/methods/_init.ts index c555b1b2..36d8df55 100644 --- a/packages/core/src/highlevel/methods/_init.ts +++ b/packages/core/src/highlevel/methods/_init.ts @@ -13,17 +13,28 @@ import { Conversation } from '../types/conversation.js' import { makeParsedUpdateHandler, ParsedUpdateHandlerParams } from '../updates/parsed.js' // @copy -type TelegramClientOptions = ((PartialOnly, 'transport' | 'crypto'> & { +type TelegramClientOptions = ( + | (PartialOnly, 'transport' | 'crypto'> & { + /** + * Storage to use for this client. + * + * If a string is passed, it will be used as + * a name for the default platform-specific storage provider to use. + * + * @default `"client.session"` + */ + storage?: string | ITelegramStorageProvider + }) + | { client: ITelegramClient } +) & { /** - * Storage to use for this client. + * If true, all API calls will be wrapped with `tl.invokeWithoutUpdates`, + * effectively disabling the server-sent events for the clients. + * May be useful in some cases. * - * If a string is passed, it will be used as - * a name for the default platform-specific storage provider to use. - * - * @default `"client.session"` + * @default false */ - storage?: string | ITelegramStorageProvider -}) | ({ client: ITelegramClient })) & { + disableUpdates?: boolean updates?: Omit /** * If `true`, the updates that were handled by some {@link Conversation} @@ -41,7 +52,9 @@ function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) { this._client = opts.client } else { if (!opts.storage || typeof opts.storage === 'string' || !opts.transport || !opts.crypto) { - throw new MtUnsupportedError('You need to explicitly provide storage, transport and crypto for @mtcute/core') + throw new MtUnsupportedError( + 'You need to explicitly provide storage, transport and crypto for @mtcute/core', + ) } this._client = new BaseTelegramClient(opts as BaseTelegramClientOptions) @@ -52,19 +65,23 @@ function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) { // @ts-expect-error codegen this.storage = this._client.storage - const skipConversationUpdates = opts.skipConversationUpdates ?? true - const { messageGroupingInterval } = opts.updates ?? {} + if (!opts.disableUpdates) { + const skipConversationUpdates = opts.skipConversationUpdates ?? true + const { messageGroupingInterval } = opts.updates ?? {} - this._client.onUpdate(makeParsedUpdateHandler({ - messageGroupingInterval, - onUpdate: (update) => { - if (Conversation.handleUpdate(this._client, update) && skipConversationUpdates) return + this._client.onUpdate( + makeParsedUpdateHandler({ + messageGroupingInterval, + onUpdate: (update) => { + if (Conversation.handleUpdate(this._client, update) && skipConversationUpdates) return - this.emit('update', update) - this.emit(update.name, update.data) - }, - onRawUpdate: (update, peers) => { - this.emit('raw_update', update, peers) - }, - })) + this.emit('update', update) + this.emit(update.name, update.data) + }, + onRawUpdate: (update, peers) => { + this.emit('raw_update', update, peers) + }, + }), + ) + } } diff --git a/packages/core/src/network/client.ts b/packages/core/src/network/client.ts index 6e793e7d..4295edda 100644 --- a/packages/core/src/network/client.ts +++ b/packages/core/src/network/client.ts @@ -122,14 +122,10 @@ export interface MtClientOptions { maxRetryCount?: number /** - * If true, every single API call will be wrapped with `tl.invokeWithoutUpdates`, + * If true, all API calls will be wrapped with `tl.invokeWithoutUpdates`, * effectively disabling the server-sent events for the clients. * May be useful in some cases. * - * Note that this only wraps calls made with `.call()` within the primary - * connection. Additional connections and direct `.sendForResult()` calls - * must be wrapped manually. - * * @default false */ disableUpdates?: boolean