From 43ce2a572eb8977b6994680171ac0c5f9bda1703 Mon Sep 17 00:00:00 2001 From: Qjuh <76154676+Qjuh@users.noreply.github.com> Date: Sun, 25 Dec 2022 20:05:19 +0100 Subject: [PATCH] fix(WebSocketShard): either start close timeout or emit destroyed but never both (#8956) * fix(WebSocketShard): only close timeout or destroy * Remove optional chaining Co-authored-by: Voxelli <69213593+legendhimself@users.noreply.github.com> Co-authored-by: Voxelli <69213593+legendhimself@users.noreply.github.com> --- .../src/client/websocket/WebSocketShard.js | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/discord.js/src/client/websocket/WebSocketShard.js b/packages/discord.js/src/client/websocket/WebSocketShard.js index c18c82aea..1c5d6ec4d 100644 --- a/packages/discord.js/src/client/websocket/WebSocketShard.js +++ b/packages/discord.js/src/client/websocket/WebSocketShard.js @@ -835,23 +835,27 @@ class WebSocketShard extends EventEmitter { ); this.connection.terminate(); } + // Emit the destroyed event if needed - if (emit) this._emitDestroyed(); + if (emit) { + this._emitDestroyed(); + } else if ( + this.connection.readyState === WebSocket.CLOSING || + this.connection.readyState === WebSocket.CLOSED + ) { + this.closeEmitted = false; + this.debug( + `[WebSocket] Adding a WebSocket close timeout to ensure a correct WS reconnect. + Timeout: ${this.manager.client.options.closeTimeout}ms`, + ); + this.setWsCloseTimeout(this.manager.client.options.closeTimeout); + } } } else if (emit) { // We requested a destroy, but we had no connection. Emit destroyed this._emitDestroyed(); } - if (this.connection?.readyState === WebSocket.CLOSING || this.connection?.readyState === WebSocket.CLOSED) { - this.closeEmitted = false; - this.debug( - `[WebSocket] Adding a WebSocket close timeout to ensure a correct WS reconnect. - Timeout: ${this.manager.client.options.closeTimeout}ms`, - ); - this.setWsCloseTimeout(this.manager.client.options.closeTimeout); - } - // Step 2: Null the connection object this.connection = null;