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>
This commit is contained in:
Qjuh
2022-12-25 20:05:19 +01:00
committed by GitHub
parent 22e2bbb0d2
commit 43ce2a572e

View File

@@ -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;