From 60162b567d44708c53c261a78648b4db3146b05f Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Mon, 15 Jun 2020 16:30:06 +0200 Subject: [PATCH] chore: prevent unecessary allocation (#49) --- src/index.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index 9daa573e28..b7835899a8 100644 --- a/src/index.js +++ b/src/index.js @@ -172,11 +172,7 @@ class PubsubBaseProtocol extends EventEmitter { _onIncomingStream ({ protocol, stream, connection }) { const peerId = connection.remotePeer const idB58Str = peerId.toB58String() - - const peer = this._addPeer(new Peer({ - id: peerId, - protocols: [protocol] - })) + const peer = this._addPeer(peerId, [protocol]) this._processMessages(idB58Str, stream, peer) } @@ -191,10 +187,7 @@ class PubsubBaseProtocol extends EventEmitter { const idB58Str = peerId.toB58String() this.log('connected', idB58Str) - const peer = this._addPeer(new Peer({ - id: peerId, - protocols: this.multicodecs - })) + const peer = this._addPeer(peerId, this.multicodecs) if (peer.isConnected) { return @@ -225,15 +218,22 @@ class PubsubBaseProtocol extends EventEmitter { /** * Add a new connected peer to the peers map. * @private - * @param {Peer} peer internal peer + * @param {PeerId} peerId + * @param {Array} protocols * @returns {Peer} */ - _addPeer (peer) { - const id = peer.id.toB58String() + _addPeer (peerId, protocols) { + const id = peerId.toB58String() let existing = this.peers.get(id) if (!existing) { this.log('new peer', id) + + const peer = new Peer({ + id: peerId, + protocols + }) + this.peers.set(id, peer) existing = peer