fix(dispatcher): reuse parsed updates in child dispatchers

This commit is contained in:
teidesu 2021-05-23 14:16:01 +03:00
parent 8e81ce21f2
commit 2d335af78e

View file

@ -220,15 +220,30 @@ export class Dispatcher {
async dispatchUpdateNow( async dispatchUpdateNow(
update: tl.TypeUpdate | tl.TypeMessage, update: tl.TypeUpdate | tl.TypeMessage,
users: UsersIndex, users: UsersIndex,
chats: ChatsIndex chats: ChatsIndex,
): Promise<void> {
return this._dispatchUpdateNowImpl(update, users, chats)
}
private async _dispatchUpdateNowImpl(
update: tl.TypeUpdate | tl.TypeMessage,
users: UsersIndex,
chats: ChatsIndex,
parsed?: any,
parsedType?: Exclude<UpdateHandler['type'], 'raw'> | null
): Promise<void> { ): Promise<void> {
if (!this._client) return if (!this._client) return
const isRawMessage = tl.isAnyMessage(update) const isRawMessage = tl.isAnyMessage(update)
if (parsed === undefined) {
const pair = PARSERS[update._] const pair = PARSERS[update._]
const parsed = pair if (pair) {
? pair[1](this._client, update, users, chats) parsed = pair[1](this._client, update, users, chats)
: undefined parsedType = pair[0]
} else {
parsed = parsedType = null
}
}
outer: for (const grp of this._groupsOrder) { outer: for (const grp of this._groupsOrder) {
for (const handler of this._groups[grp]) { for (const handler of this._groups[grp]) {
@ -252,8 +267,8 @@ export class Dispatcher {
chats chats
) )
} else if ( } else if (
pair && parsedType &&
handler.type === pair[0] && handler.type === parsedType &&
(!handler.check || (!handler.check ||
(await handler.check(parsed, this._client))) (await handler.check(parsed, this._client)))
) { ) {
@ -269,7 +284,7 @@ export class Dispatcher {
} }
for (const child of this._children) { for (const child of this._children) {
await child.dispatchUpdateNow(update, users, chats) await child._dispatchUpdateNowImpl(update, users, chats, parsed, parsedType)
} }
} }