@@ -33,6 +33,7 @@ class ConnectionManager {
33
33
// Only add it if it's not there
34
34
if ( ! this . get ( connection ) ) {
35
35
this . connections [ connection . theirB58Id ] . push ( connection )
36
+ this . switch . emit ( 'peer-mux-established' , connection . theirPeerInfo )
36
37
}
37
38
}
38
39
@@ -78,14 +79,26 @@ class ConnectionManager {
78
79
* @returns {void }
79
80
*/
80
81
remove ( connection ) {
81
- if ( ! this . connections [ connection . theirB58Id ] ) return
82
+ // No record of the peer, disconnect it
83
+ if ( ! this . connections [ connection . theirB58Id ] ) {
84
+ connection . theirPeerInfo . disconnect ( )
85
+ this . switch . emit ( 'peer-mux-closed' , connection . theirPeerInfo )
86
+ return
87
+ }
82
88
83
89
for ( let i = 0 ; i < this . connections [ connection . theirB58Id ] . length ; i ++ ) {
84
90
if ( this . connections [ connection . theirB58Id ] [ i ] === connection ) {
85
91
this . connections [ connection . theirB58Id ] . splice ( i , 1 )
86
- return
92
+ break
87
93
}
88
94
}
95
+
96
+ // The peer is fully disconnected
97
+ if ( this . connections [ connection . theirB58Id ] . length === 0 ) {
98
+ delete this . connections [ connection . theirB58Id ]
99
+ connection . theirPeerInfo . disconnect ( )
100
+ this . switch . emit ( 'peer-mux-closed' , connection . theirPeerInfo )
101
+ }
89
102
}
90
103
91
104
/**
@@ -175,6 +188,7 @@ class ConnectionManager {
175
188
return log ( 'identify not successful' )
176
189
}
177
190
const b58Str = peerInfo . id . toB58String ( )
191
+ peerInfo = this . switch . _peerBook . put ( peerInfo )
178
192
179
193
const connection = new ConnectionFSM ( {
180
194
_switch : this . switch ,
@@ -185,24 +199,24 @@ class ConnectionManager {
185
199
} )
186
200
this . switch . connection . add ( connection )
187
201
188
- if ( peerInfo . multiaddrs . size > 0 ) {
189
- // with incomming conn and through identify, going to pick one
190
- // of the available multiaddrs from the other peer as the one
191
- // I'm connected to as we really can't be sure at the moment
192
- // TODO add this consideration to the connection abstraction!
193
- peerInfo . connect ( peerInfo . multiaddrs . toArray ( ) [ 0 ] )
194
- } else {
195
- // for the case of websockets in the browser, where peers have
196
- // no addr, use just their IPFS id
197
- peerInfo . connect ( `/ipfs/${ b58Str } ` )
202
+ // Only update if it's not already connected
203
+ if ( ! peerInfo . isConnected ( ) ) {
204
+ if ( peerInfo . multiaddrs . size > 0 ) {
205
+ // with incomming conn and through identify, going to pick one
206
+ // of the available multiaddrs from the other peer as the one
207
+ // I'm connected to as we really can't be sure at the moment
208
+ // TODO add this consideration to the connection abstraction!
209
+ peerInfo . connect ( peerInfo . multiaddrs . toArray ( ) [ 0 ] )
210
+ } else {
211
+ // for the case of websockets in the browser, where peers have
212
+ // no addr, use just their IPFS id
213
+ peerInfo . connect ( `/ipfs/${ b58Str } ` )
214
+ }
198
215
}
199
- peerInfo = this . switch . _peerBook . put ( peerInfo )
200
216
201
217
muxedConn . once ( 'close' , ( ) => {
202
218
connection . close ( )
203
219
} )
204
-
205
- this . switch . emit ( 'peer-mux-established' , peerInfo )
206
220
} )
207
221
} )
208
222
}
0 commit comments