*Check and wait on peers on ready method
*Check peer count on query method
This commit is contained in:
parent
3cffb47069
commit
eebc09bee5
19
src/index.ts
19
src/index.ts
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue