From e9e25b2573709ae574f6d5e1bf42ef3c25c262ea Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 18 Mar 2023 14:48:43 -0400 Subject: [PATCH] *hook close event before querying --- src/network.ts | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/network.ts b/src/network.ts index 834349b..349f2d9 100644 --- a/src/network.ts +++ b/src/network.ts @@ -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(); - } - }); }); }