fix(core): correctly handle disableUpdates for highlevel client

This commit is contained in:
alina 🌸 2024-05-25 14:58:40 +03:00
parent 1c0bbcdb0c
commit 7194132557
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
3 changed files with 65 additions and 42 deletions

View file

@ -350,6 +350,14 @@ type TelegramClientOptions = (
}) })
| { client: ITelegramClient } | { 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<ParsedUpdateHandlerParams, 'onUpdate'> updates?: Omit<ParsedUpdateHandlerParams, 'onUpdate'>
/** /**
* If `true`, the updates that were handled by some {@link Conversation} * 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 // @ts-expect-error codegen
this.storage = this._client.storage this.storage = this._client.storage
const skipConversationUpdates = opts.skipConversationUpdates ?? true if (!opts.disableUpdates) {
const { messageGroupingInterval } = opts.updates ?? {} const skipConversationUpdates = opts.skipConversationUpdates ?? true
const { messageGroupingInterval } = opts.updates ?? {}
this._client.onUpdate( this._client.onUpdate(
makeParsedUpdateHandler({ makeParsedUpdateHandler({
messageGroupingInterval, messageGroupingInterval,
onUpdate: (update) => { onUpdate: (update) => {
if (Conversation.handleUpdate(this._client, update) && skipConversationUpdates) return if (Conversation.handleUpdate(this._client, update) && skipConversationUpdates) return
this.emit('update', update) this.emit('update', update)
this.emit(update.name, update.data) this.emit(update.name, update.data)
}, },
onRawUpdate: (update, peers) => { onRawUpdate: (update, peers) => {
this.emit('raw_update', update, peers) this.emit('raw_update', update, peers)
}, },
}), }),
) )
}
} }
withParams(params: RpcCallOptions): this { withParams(params: RpcCallOptions): this {
return withParams(this, params) return withParams(this, params)

View file

@ -13,17 +13,28 @@ import { Conversation } from '../types/conversation.js'
import { makeParsedUpdateHandler, ParsedUpdateHandlerParams } from '../updates/parsed.js' import { makeParsedUpdateHandler, ParsedUpdateHandlerParams } from '../updates/parsed.js'
// @copy // @copy
type TelegramClientOptions = ((PartialOnly<Omit<BaseTelegramClientOptions, 'storage'>, 'transport' | 'crypto'> & { type TelegramClientOptions = (
| (PartialOnly<Omit<BaseTelegramClientOptions, 'storage'>, '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 * @default false
* a name for the default platform-specific storage provider to use.
*
* @default `"client.session"`
*/ */
storage?: string | ITelegramStorageProvider disableUpdates?: boolean
}) | ({ client: ITelegramClient })) & {
updates?: Omit<ParsedUpdateHandlerParams, 'onUpdate'> updates?: Omit<ParsedUpdateHandlerParams, 'onUpdate'>
/** /**
* If `true`, the updates that were handled by some {@link Conversation} * 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 this._client = opts.client
} else { } else {
if (!opts.storage || typeof opts.storage === 'string' || !opts.transport || !opts.crypto) { 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) this._client = new BaseTelegramClient(opts as BaseTelegramClientOptions)
@ -52,19 +65,23 @@ function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) {
// @ts-expect-error codegen // @ts-expect-error codegen
this.storage = this._client.storage this.storage = this._client.storage
const skipConversationUpdates = opts.skipConversationUpdates ?? true if (!opts.disableUpdates) {
const { messageGroupingInterval } = opts.updates ?? {} const skipConversationUpdates = opts.skipConversationUpdates ?? true
const { messageGroupingInterval } = opts.updates ?? {}
this._client.onUpdate(makeParsedUpdateHandler({ this._client.onUpdate(
messageGroupingInterval, makeParsedUpdateHandler({
onUpdate: (update) => { messageGroupingInterval,
if (Conversation.handleUpdate(this._client, update) && skipConversationUpdates) return onUpdate: (update) => {
if (Conversation.handleUpdate(this._client, update) && skipConversationUpdates) return
this.emit('update', update) this.emit('update', update)
this.emit(update.name, update.data) this.emit(update.name, update.data)
}, },
onRawUpdate: (update, peers) => { onRawUpdate: (update, peers) => {
this.emit('raw_update', update, peers) this.emit('raw_update', update, peers)
}, },
})) }),
)
}
} }

View file

@ -122,14 +122,10 @@ export interface MtClientOptions {
maxRetryCount?: number 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. * effectively disabling the server-sent events for the clients.
* May be useful in some cases. * 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 * @default false
*/ */
disableUpdates?: boolean disableUpdates?: boolean