From 92c15b477b1f24654fef9163dbf194995c99f505 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Thu, 29 Oct 2020 17:29:38 +0530 Subject: [PATCH] EventEmitter fix --- package.json | 2 +- src/WAConnection/0.Base.ts | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index bc5a398..28e6602 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@adiwajshing/baileys", - "version": "3.2.2", + "version": "3.2.3", "description": "WhatsApp Web API", "homepage": "https://github.com/adiwajshing/Baileys", "main": "lib/WAConnection/WAConnection.js", diff --git a/src/WAConnection/0.Base.ts b/src/WAConnection/0.Base.ts index b5bfc73..d642605 100644 --- a/src/WAConnection/0.Base.ts +++ b/src/WAConnection/0.Base.ts @@ -308,22 +308,27 @@ export class WAConnection extends EventEmitter { protected async waitForConnection () { if (this.state === 'open') return - await Utils.promiseTimeout ( - this.pendingRequestTimeoutMs, - (resolve, reject) => { - const onClose = ({ reason }) => { - if (reason === DisconnectReason.invalidSession || reason === DisconnectReason.intentional) { - reject (new Error(reason)) + let onOpen: () => void + let onClose: ({ reason }) => void + + await ( + Utils.promiseTimeout ( + this.pendingRequestTimeoutMs, + (resolve, reject) => { + onClose = ({ reason }) => { + if (reason === DisconnectReason.invalidSession || reason === DisconnectReason.intentional) { + reject (new Error(reason)) + } } - this.off ('open', onOpen) + onOpen = resolve + this.once ('close', onClose) + this.once ('open', onOpen) } - const onOpen = () => { - resolve () - this.off ('close', onClose) - } - this.once ('close', onClose) - this.once ('open', onOpen) - } + ) + .finally(() => { + this.off ('open', onOpen) + this.off ('close', onClose) + }) ) } /**