fix(WebSocketShard): handle initial connect being a resume (#9549)

* fix(WebSocketShard): handle initial connect being a resume

* chore: remove leftover

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
DD
2023-05-11 20:42:10 +03:00
committed by GitHub
parent a51c48e743
commit 4dcc9c50f8

View File

@@ -128,10 +128,26 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
}
public async connect() {
const promise = this.initialConnectResolved ? Promise.resolve() : once(this, WebSocketShardEvents.Ready);
const controller = new AbortController();
let promise;
if (!this.initialConnectResolved) {
// Sleep for the remaining time, but if the connection closes in the meantime, we shouldn't wait the remainder to avoid blocking the new conn
promise = Promise.race([
once(this, WebSocketShardEvents.Ready, { signal: controller.signal }),
once(this, WebSocketShardEvents.Resumed, { signal: controller.signal }),
]);
}
void this.internalConnect();
await promise;
try {
await promise;
} finally {
// cleanup hanging listeners
controller.abort();
}
this.initialConnectResolved = true;
}