fix(core): correctly handle disableUpdates
for highlevel client
This commit is contained in:
parent
1c0bbcdb0c
commit
7194132557
3 changed files with 65 additions and 42 deletions
|
@ -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<ParsedUpdateHandlerParams, 'onUpdate'>
|
||||
/**
|
||||
* 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)
|
||||
|
|
|
@ -13,17 +13,28 @@ import { Conversation } from '../types/conversation.js'
|
|||
import { makeParsedUpdateHandler, ParsedUpdateHandlerParams } from '../updates/parsed.js'
|
||||
|
||||
// @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
|
||||
* 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<ParsedUpdateHandlerParams, 'onUpdate'>
|
||||
/**
|
||||
* 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)
|
||||
},
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue