*Change ready to init

*Add new ready method that listens for a ready event but returns if activeRelay is set
This commit is contained in:
Derrick Hammer 2023-02-01 12:07:46 -05:00
parent 4e443f4ccb
commit 236e445ff8
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 20 additions and 3 deletions

View File

@ -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();
});
}