refactor: improve logic of ready promise

This commit is contained in:
Derrick Hammer 2023-07-23 13:54:42 -04:00
parent f7ae821bef
commit b73edb9029
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 19 additions and 3 deletions

View File

@ -17,9 +17,25 @@ export default class RPC extends ProtomuxRPC {
...options, ...options,
}; };
super(stream, options); super(stream, options);
this._ready = new Promise((resolve) => {
// @ts-ignore this._ready = new Promise((resolve, reject) => {
this.on("open", resolve); stream.on("open", onopen);
stream.on("destroy", ondestroy);
function onopen(handshake) {
removeListener();
resolve(handshake);
}
function ondestroy() {
removeListener();
reject(new Error("Client could not connect"));
}
function removeListener() {
stream.off("open", onopen);
stream.off("destroy", ondestroy);
}
}); });
} }