fix(client): do not propagate Conversation updates
This commit is contained in:
parent
c278599612
commit
8e07d7f552
2 changed files with 29 additions and 8 deletions
|
@ -58,6 +58,14 @@ interface TelegramClientOptions extends Omit<BaseTelegramClientOptions, 'storage
|
||||||
* you should manually add a handler using `client.network.setUpdateHandler`.
|
* you should manually add a handler using `client.network.setUpdateHandler`.
|
||||||
*/
|
*/
|
||||||
disableUpdatesManager?: boolean
|
disableUpdatesManager?: boolean
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If `true`, the updates that were handled by some {@link Conversation}
|
||||||
|
* will not be dispatched any further.
|
||||||
|
*
|
||||||
|
* @default true
|
||||||
|
*/
|
||||||
|
skipConversationUpdates?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
// @initialize=super
|
// @initialize=super
|
||||||
|
@ -79,6 +87,7 @@ function _initializeClientSuper(this: TelegramClient, opts: TelegramClientOption
|
||||||
/** @internal */
|
/** @internal */
|
||||||
function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) {
|
function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) {
|
||||||
this._disableUpdatesManager = opts.disableUpdatesManager ?? false
|
this._disableUpdatesManager = opts.disableUpdatesManager ?? false
|
||||||
|
const skipConversationUpdates = opts.skipConversationUpdates ?? true
|
||||||
|
|
||||||
if (!opts.disableUpdates && !opts.disableUpdatesManager) {
|
if (!opts.disableUpdates && !opts.disableUpdatesManager) {
|
||||||
const { messageGroupingInterval, ...managerParams } = opts.updates ?? {}
|
const { messageGroupingInterval, ...managerParams } = opts.updates ?? {}
|
||||||
|
@ -88,7 +97,8 @@ function _initializeClient(this: TelegramClient, opts: TelegramClientOptions) {
|
||||||
onUpdate: makeParsedUpdateHandler({
|
onUpdate: makeParsedUpdateHandler({
|
||||||
messageGroupingInterval,
|
messageGroupingInterval,
|
||||||
onUpdate: (update) => {
|
onUpdate: (update) => {
|
||||||
Conversation.handleUpdate(this, update)
|
if (Conversation.handleUpdate(this, update) && skipConversationUpdates) return
|
||||||
|
|
||||||
this.emit('update', update)
|
this.emit('update', update)
|
||||||
this.emit(update.name, update.data)
|
this.emit(update.name, update.data)
|
||||||
},
|
},
|
||||||
|
|
|
@ -71,9 +71,15 @@ export class Conversation {
|
||||||
return (client as any)[CONVERSATION_SYMBOL] as ConversationsState
|
return (client as any)[CONVERSATION_SYMBOL] as ConversationsState
|
||||||
}
|
}
|
||||||
|
|
||||||
static handleUpdate(client: BaseTelegramClient, update: ParsedUpdate): void {
|
/**
|
||||||
|
* Pass the update to the conversation manager and all registered
|
||||||
|
* conversations on this client.
|
||||||
|
*
|
||||||
|
* @returns `true` if the update was handled by some conversation
|
||||||
|
*/
|
||||||
|
static handleUpdate(client: BaseTelegramClient, update: ParsedUpdate): boolean {
|
||||||
const state = Conversation._getState(client)
|
const state = Conversation._getState(client)
|
||||||
if (!state?.hasConversations) return
|
if (!state?.hasConversations) return false
|
||||||
|
|
||||||
let chatId
|
let chatId
|
||||||
|
|
||||||
|
@ -86,25 +92,30 @@ export class Conversation {
|
||||||
chatId = update.data.chatId
|
chatId = update.data.chatId
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
const conv = state.pendingConversations.get(chatId)
|
const conv = state.pendingConversations.get(chatId)
|
||||||
if (!conv) return
|
if (!conv) return false
|
||||||
|
|
||||||
for (const c of conv) {
|
for (const c of conv) {
|
||||||
switch (update.name) {
|
switch (update.name) {
|
||||||
case 'new_message':
|
case 'new_message':
|
||||||
c._onNewMessage(update.data)
|
c._onNewMessage(update.data)
|
||||||
break
|
|
||||||
|
return true
|
||||||
case 'edit_message':
|
case 'edit_message':
|
||||||
c._onEditMessage(update.data)
|
c._onEditMessage(update.data)
|
||||||
break
|
|
||||||
|
return true
|
||||||
case 'history_read':
|
case 'history_read':
|
||||||
c._onHistoryRead(update.data)
|
c._onHistoryRead(update.data)
|
||||||
break
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue