Skip to content

Commit

Permalink
fix copy nodes loop
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Nov 30, 2023
1 parent 86040aa commit c175308
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/peer-discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,9 @@ module.exports = class PeerDiscovery {

if (!nodes) nodes = this._closestNodes
else if (this._closestNodes !== nodes) {
const len = nodes.length
for (const newer of this._closestNodes) {
for (const older of nodes) {
if (!older.id || !newer.id || b4a.equals(older.id, newer.id)) continue
nodes.push(newer)
}
if (newer.id && !hasNode(nodes, len, newer)) nodes.push(newer)
}
}

Expand Down Expand Up @@ -288,4 +286,13 @@ class PeerDiscoverySession {
}
}

function hasNode (nodes, len, node) {
for (let i = 0; i < len; i++) {
const existing = nodes[i]
if (existing.id && b4a.equals(existing.id, node.id)) return true
}

return false
}

function noop () {}

0 comments on commit c175308

Please sign in to comment.