Delay Client error event

Right now the caller won't receive the event, because it's triggered
before the function returns.
This commit is contained in:
Simon Ser 2021-01-22 17:50:52 +01:00
parent 2f70256037
commit 0261bc11e7

View file

@ -36,8 +36,10 @@ export default class Client extends EventTarget {
try {
this.ws = new WebSocket(params.url);
} catch (err) {
this.dispatchEvent(new CustomEvent("error", { detail: "Failed to create connection: " + err }));
setTimeout(() => this.dispatchEvent(new CustomEvent("close")), 0);
setTimeout(() => {
this.dispatchEvent(new CustomEvent("error", { detail: "Failed to create connection: " + err }));
this.dispatchEvent(new CustomEvent("close"));
}, 0);
return;
}
this.ws.addEventListener("open", this.handleOpen.bind(this));