From c33646943c37a23b99af2e1edd7286e351d734f9 Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Fri, 9 Jul 2021 01:49:53 +0300 Subject: [PATCH] fix(dispatcher): middleware and event emitter related fixes --- packages/dispatcher/src/dispatcher.ts | 50 ++++++++++++++++----------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/packages/dispatcher/src/dispatcher.ts b/packages/dispatcher/src/dispatcher.ts index 59647c88..5391f38f 100644 --- a/packages/dispatcher/src/dispatcher.ts +++ b/packages/dispatcher/src/dispatcher.ts @@ -437,11 +437,16 @@ export class Dispatcher< const isRawMessage = update && tl.isAnyMessage(update) - if (parsed === undefined && this._handlersCount[update!._]) { + if (parsed === undefined) { const pair = PARSERS[update!._] if (pair) { - parsed = pair[1](this._client, update!, users!, chats!) - parsedType = pair[0] + if ( + this._handlersCount[update!._] || + this.listenerCount(pair[0]) + ) { + parsed = pair[1](this._client, update!, users!, chats!) + parsedType = pair[0] + } } else { parsed = parsedType = null } @@ -524,19 +529,22 @@ export class Dispatcher< let shouldDispatch = true let shouldDispatchChildren = true let wasHandled = false + let updateInfo: any = null - const updateInfo = { type: parsedType, data: parsed } - switch ( - await this._preUpdateHandler?.( - updateInfo as any, - parsedState as any - ) - ) { - case 'stop': - shouldDispatch = false - break - case 'stop-children': - return false + if (parsed) { + updateInfo = { type: parsedType, data: parsed } + switch ( + await this._preUpdateHandler?.( + updateInfo as any, + parsedState as any + ) + ) { + case 'stop': + shouldDispatch = false + break + case 'stop-children': + return false + } } if (shouldDispatch) { @@ -678,11 +686,13 @@ export class Dispatcher< } } - this._postUpdateHandler?.( - wasHandled, - updateInfo as any, - parsedState as any - ) + if (updateInfo) { + this._postUpdateHandler?.( + wasHandled, + updateInfo, + parsedState as any + ) + } return wasHandled }