*Check and wait on peers on ready method

*Check peer count on query method
This commit is contained in:
Derrick Hammer 2023-02-20 07:39:23 -05:00
parent 3cffb47069
commit eebc09bee5
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 14 additions and 5 deletions

View File

@ -19,6 +19,17 @@ addHandler("query", handleQuery);
let swarm; let swarm;
let proxy: HandshakeProxy; let proxy: HandshakeProxy;
function resolveWithPeers(resolve: Function) {
if (proxy.node.peers.list.size === 0) {
proxy.node.pool.once("peer", () => {
resolve(null);
});
return;
}
return resolve(null);
}
async function handlePresentSeed(aq: ActiveQuery) { async function handlePresentSeed(aq: ActiveQuery) {
swarm = createClient(); swarm = createClient();
proxy = new HandshakeProxy({ swarm, listen: true }); proxy = new HandshakeProxy({ swarm, listen: true });
@ -34,19 +45,17 @@ async function handleReady(aq: ActiveQuery) {
await new Promise((resolve): void => { await new Promise((resolve): void => {
if (proxy.node.chain.synced) { if (proxy.node.chain.synced) {
return resolve(null); return resolveWithPeers(resolve);
} }
proxy.node.pool.on("full", () => { proxy.node.pool.on("full", resolveWithPeers);
resolve(null);
});
}); });
aq.respond(); aq.respond();
} }
async function handleQuery(aq: ActiveQuery) { async function handleQuery(aq: ActiveQuery) {
if (!proxy.node.chain.synced) { if (!proxy.node.chain.synced || proxy.node.peers.list.size === 0) {
aq.reject("not ready"); aq.reject("not ready");
return; return;
} }