Skip to content

Commit

Permalink
fix(WebSocketShard): either start close timeout or emit destroyed but…
Browse files Browse the repository at this point in the history
… 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>
  • Loading branch information
Qjuh and legendhimself committed Dec 25, 2022
1 parent 22e2bbb commit 43ce2a5
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/discord.js/src/client/websocket/WebSocketShard.js
Expand Up @@ -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;

Expand Down

0 comments on commit 43ce2a5

Please sign in to comment.