mirror of
https://git.sr.ht/~emersion/gamja
synced 2024-11-14 11:15:13 -05:00
lib/client: reject Client.roundtrip promise on disconnect
This commit is contained in:
parent
edbf294380
commit
c187eac3c8
1 changed files with 17 additions and 2 deletions
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue