lib/client: reject Client.roundtrip promise on disconnect

This commit is contained in:
Simon Ser 2021-06-21 00:18:17 +02:00
parent edbf294380
commit c187eac3c8

View file

@ -519,6 +519,8 @@ export default class Client extends EventTarget {
}
return new Promise((resolve, reject) => {
let removeEventListeners;
let handleMessage = (event) => {
let msg = event.detail.message;
@ -531,18 +533,31 @@ export default class Client extends EventTarget {
try {
result = done(msg);
} catch (err) {
this.removeEventListener("message", handleMessage);
removeEventListeners();
reject(err);
}
if (result) {
this.removeEventListener("message", handleMessage);
removeEventListeners();
resolve(result);
}
// TODO: handle end of labeled response somehow
};
let handleStatus = () => {
if (this.status === Client.Status.DISCONNECTED) {
removeEventListeners();
reject(new Error("Connection closed"));
}
};
removeEventListeners = () => {
this.removeEventListener("message", handleMessage);
this.removeEventListener("status", handleStatus);
};
this.addEventListener("message", handleMessage);
this.addEventListener("status", handleStatus);
this.send(msg);
});
}