feat: add ConnectionManager#getAll

This commit is contained in:
Cayman 2020-06-08 15:33:42 -05:00 committed by Jacob Heun
parent afafd08943
commit 8f680e20e9

View File

@ -229,6 +229,26 @@ class ConnectionManager extends EventEmitter {
return null
}
/**
* Get all open connections with a peer.
* @param {PeerId} peerId
* @returns {Array<Connection>}
*/
getAll (peerId) {
if (!PeerId.isPeerId(peerId)) {
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
}
const id = peerId.toB58String()
const connections = this.connections.get(id)
// Return all open connections
if (connections) {
return connections.filter(connection => connection.stat.status === 'open')
}
return []
}
/**
* If the event loop is slow, maybe close a connection
* @private