@@ -44,47 +44,30 @@ class Topology {
44
44
this . _registrar = registrar
45
45
this . _registrar . peerStore . on ( 'change:protocols' , this . _onProtocolChange )
46
46
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 ( ) )
50
49
}
51
50
52
51
/**
53
- * Add connected peers to the topology.
52
+ * Update topology.
53
+ * @param {Array<PeerInfo> } peerInfoIterable
54
+ * @returns {void }
54
55
*/
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 ( ) )
70
67
}
71
68
}
72
69
}
73
70
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
-
88
71
/**
89
72
* Notify protocol of peer disconnected.
90
73
* @param {PeerInfo } peerInfo
@@ -105,11 +88,10 @@ class Topology {
105
88
*/
106
89
_onProtocolChange ( { peerInfo, protocols } ) {
107
90
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 ) )
110
92
111
93
// Not supporting the protocol anymore?
112
- if ( existingPeer && protocols . filter ( protocol => this . multicodecs . includes ( protocol ) ) . length === 0 ) {
94
+ if ( existingPeer && hasProtocol . length === 0 ) {
113
95
this . _onDisconnect ( {
114
96
peerInfo
115
97
} )
@@ -118,7 +100,7 @@ class Topology {
118
100
// New to protocol support
119
101
for ( const protocol of protocols ) {
120
102
if ( this . multicodecs . includes ( protocol ) ) {
121
- this . tryToConnect ( peerInfo , this . _registrar . getConnection ( peerInfo ) )
103
+ this . _updatePeers ( [ peerInfo ] )
122
104
return
123
105
}
124
106
}
0 commit comments