*hook close event before querying

This commit is contained in:
Derrick Hammer 2023-03-18 14:48:43 -04:00
parent d572e39a65
commit e9e25b2573
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 14 additions and 15 deletions

View File

@ -115,14 +115,26 @@ export default class RpcNetwork {
this.setupRelayPromise();
this._swarm.on("connection", async (relay: any) => {
const pubkey = b4a.from(relay.remotePublicKey).toString("hex");
relay.on("close", () => {
this._methods.forEach((item) => {
if (item.has(pubkey)) {
item.delete(pubkey);
}
});
this.relays.delete(pubkey);
if (!this._relays.size) {
this.setupRelayPromise();
}
});
const query = this._factory.simple({
relay,
query: { module: "core", method: "get_methods", data: null },
});
const resp = await query.result;
const pubkey = b4a.from(relay.remotePublicKey).toString("hex");
if (resp.data) {
this._relays.set(pubkey, relay);
@ -135,19 +147,6 @@ export default class RpcNetwork {
});
this._relaysAvailableResolve?.();
}
relay.on("close", () => {
this._methods.forEach((item) => {
if (item.has(pubkey)) {
item.delete(pubkey);
}
});
this.relays.delete(pubkey);
if (!this._relays.size) {
this.setupRelayPromise();
}
});
});
}