Skip to content

Commit 2345976

Browse files
committed
chore: address review
1 parent 6f72c08 commit 2345976

File tree

1 file changed

+19
-37
lines changed

1 file changed

+19
-37
lines changed

src/connection-manager/topology.js

+19-37
Original file line numberDiff line numberDiff line change
@@ -44,47 +44,30 @@ class Topology {
4444
this._registrar = registrar
4545
this._registrar.peerStore.on('change:protocols', this._onProtocolChange)
4646

47-
// Add connected peers to the topology
48-
this._addConnectedPeers()
49-
// TODO: remaining peers in the store
47+
// Update topology peers
48+
this._updatePeers(this._registrar.peerStore.peers.values())
5049
}
5150

5251
/**
53-
* Add connected peers to the topology.
52+
* Update topology.
53+
* @param {Array<PeerInfo>} peerInfoIterable
54+
* @returns {void}
5455
*/
55-
_addConnectedPeers () {
56-
const knownPeers = []
57-
58-
for (const [, peer] of this._registrar.peerStore.peers) {
59-
if (this.multicodecs.filter(multicodec => peer.protocols.has(multicodec))) {
60-
knownPeers.push(peer)
61-
}
62-
}
63-
64-
for (const [id, conn] of this._registrar.connections.entries()) {
65-
const targetPeer = knownPeers.find((peerInfo) => peerInfo.id.toB58String() === id)
66-
67-
if (targetPeer) {
68-
// TODO: what should we return
69-
this.tryToConnect(targetPeer, conn[0])
56+
_updatePeers (peerInfoIterable) {
57+
for (const peerInfo of peerInfoIterable) {
58+
if (this.multicodecs.filter(multicodec => peerInfo.protocols.has(multicodec))) {
59+
// Add the peer regardless of whether or not there is currently a connection
60+
this.peers.set(peerInfo.id.toB58String(), peerInfo)
61+
// If there is a connection, call _onConnect
62+
const connection = this._registrar.getConnection(peerInfo)
63+
connection && this._onConnect(peerInfo, connection)
64+
} else {
65+
// Remove any peers we might be tracking that are no longer of value to us
66+
this.peers.delete(peerInfo.id.toB58String())
7067
}
7168
}
7269
}
7370

74-
/**
75-
* Try to add a connected peer to the topology, if inside the thresholds.
76-
* @param {PeerInfo} peerInfo
77-
* @param {Connection} connection
78-
* @returns {void}
79-
*/
80-
tryToConnect (peerInfo, connection) {
81-
// TODO: conn manager should validate if it should try to connect
82-
83-
this._onConnect(peerInfo, connection)
84-
85-
this.peers.set(peerInfo.id.toB58String(), peerInfo)
86-
}
87-
8871
/**
8972
* Notify protocol of peer disconnected.
9073
* @param {PeerInfo} peerInfo
@@ -105,11 +88,10 @@ class Topology {
10588
*/
10689
_onProtocolChange ({ peerInfo, protocols }) {
10790
const existingPeer = this.peers.get(peerInfo.id.toB58String())
108-
109-
protocols.filter(protocol => this.multicodecs.includes(protocol))
91+
const hasProtocol = protocols.filter(protocol => this.multicodecs.includes(protocol))
11092

11193
// Not supporting the protocol anymore?
112-
if (existingPeer && protocols.filter(protocol => this.multicodecs.includes(protocol)).length === 0) {
94+
if (existingPeer && hasProtocol.length === 0) {
11395
this._onDisconnect({
11496
peerInfo
11597
})
@@ -118,7 +100,7 @@ class Topology {
118100
// New to protocol support
119101
for (const protocol of protocols) {
120102
if (this.multicodecs.includes(protocol)) {
121-
this.tryToConnect(peerInfo, this._registrar.getConnection(peerInfo))
103+
this._updatePeers([peerInfo])
122104
return
123105
}
124106
}

0 commit comments

Comments
 (0)