mirror of
https://git.sr.ht/~emersion/gamja
synced 2024-12-05 13:51:05 -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) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
let removeEventListeners;
|
||||||
|
|
||||||
let handleMessage = (event) => {
|
let handleMessage = (event) => {
|
||||||
let msg = event.detail.message;
|
let msg = event.detail.message;
|
||||||
|
|
||||||
|
@ -531,18 +533,31 @@ export default class Client extends EventTarget {
|
||||||
try {
|
try {
|
||||||
result = done(msg);
|
result = done(msg);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.removeEventListener("message", handleMessage);
|
removeEventListeners();
|
||||||
reject(err);
|
reject(err);
|
||||||
}
|
}
|
||||||
if (result) {
|
if (result) {
|
||||||
this.removeEventListener("message", handleMessage);
|
removeEventListeners();
|
||||||
resolve(result);
|
resolve(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: handle end of labeled response somehow
|
// 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("message", handleMessage);
|
||||||
|
this.addEventListener("status", handleStatus);
|
||||||
this.send(msg);
|
this.send(msg);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue