refactor: improve logic of ready promise
This commit is contained in:
parent
f7ae821bef
commit
b73edb9029
22
src/index.ts
22
src/index.ts
|
@ -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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue