From 8f680e20e93cdf52296f91ebdf49c5af01b67135 Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 8 Jun 2020 15:33:42 -0500 Subject: [PATCH] feat: add ConnectionManager#getAll --- src/connection-manager/index.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/connection-manager/index.js b/src/connection-manager/index.js index 24927dcd..47cd8730 100644 --- a/src/connection-manager/index.js +++ b/src/connection-manager/index.js @@ -229,6 +229,26 @@ class ConnectionManager extends EventEmitter { return null } + /** + * Get all open connections with a peer. + * @param {PeerId} peerId + * @returns {Array} + */ + 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