*If we have no peers, hook on when we have a peer and recurse

*If we have a peer, loop over the peer list, and see if we have any syncable peers. If not, loop again, and hook on the peers open event
This commit is contained in:
Derrick Hammer 2023-02-20 17:33:40 -05:00
parent f3edce42fa
commit 61d385897a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 22 additions and 2 deletions

View File

@ -21,12 +21,32 @@ let proxy: HandshakeProxy;
function resolveWithPeers(resolve: Function) {
if (!proxy.node.pool.peers.head()) {
proxy.node.pool.once("peer", () => {
resolve(null);
proxy.node.pool.on("peer", () => {
resolveWithPeers(resolve);
});
return;
}
let syncable = false;
for (let peer = proxy.node.pool.peers.head(); peer; peer = peer.next) {
if (proxy.node.pool.isSyncable(peer)) {
syncable = true;
break;
}
}
if (!syncable) {
for (let peer = proxy.node.pool.peers.head(); peer; peer = peer.next) {
const listener = () => {
peer.off("open", listener);
resolve();
};
peer.on("open", listener);
}
return;
}
return resolve(null);
}