From 236e445ff859762836c1f8a3a94307890505e306 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 1 Feb 2023 12:07:46 -0500 Subject: [PATCH] *Change ready to init *Add new ready method that listens for a ready event but returns if activeRelay is set --- src/index.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 25b2cdd..4baab18 100644 --- a/src/index.ts +++ b/src/index.ts @@ -49,6 +49,7 @@ addHandler("addRelay", handleAddRelay); addHandler("removeRelay", handleRemoveRelay); addHandler("clearRelays", handleClearRelays); addHandler("getRelays", handleGetRelays); +addHandler("init", handleInit); addHandler("ready", handleReady); async function handlePresentSeed(aq: ActiveQuery) { @@ -282,9 +283,25 @@ async function handleGetPeerByPubkey(aq: ActiveQuery) { ); } -async function handleReady(aq: ActiveQuery) { +async function handleInit(aq: ActiveQuery) { const swarm = await getSwarm(aq); - // @ts-ignore - await swarm.ready(); + try { + await swarm.init(); + } catch (e) { + aq.reject((e as Error).message); + return; + } + aq.respond(); } +async function handleReady(aq: ActiveQuery) { + const swarm = await getSwarm(aq); + + if (swarm.activeRelay) { + aq.respond(); + return; + } + swarm.once("ready", () => { + aq.respond(); + }); +}