Skip to content

Commit

Permalink
implemented pair active check for update pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
sercan.ozdemir committed Sep 5, 2020
1 parent 5dee5c6 commit 7b7e7fc
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/p2p/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,14 @@ class Pool extends EventEmitter {
* peers to notify them of the change.
*/
public updatePairs = (pairIds: string[]) => {
const differentPairs = this.nodeState.pairs.concat(pairIds).filter((val, _index, arr) => {
return arr.indexOf(val) === arr.lastIndexOf(val);
});

this.nodeState.pairs = pairIds;
this.sendNodeStateUpdate();
this.sendNodeStateUpdate((peer: Peer) => {
return differentPairs.some(pair => peer.isPairActive(pair));
});
}

/**
Expand Down Expand Up @@ -261,10 +267,12 @@ class Pool extends EventEmitter {
this.sendNodeStateUpdate();
}

private sendNodeStateUpdate = () => {
private sendNodeStateUpdate = (condition?: Function) => {
const packet = new packets.NodeStateUpdatePacket(this.nodeState);
this.peers.forEach(async (peer) => {
await peer.sendPacket(packet);
if (!condition || condition(peer)) {
await peer.sendPacket(packet);
}
});
}

Expand Down

0 comments on commit 7b7e7fc

Please sign in to comment.