Skip to content

Commit 60162b5

Browse files
authored
chore: prevent unecessary allocation (#49)
1 parent f3b06d9 commit 60162b5

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/index.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,7 @@ class PubsubBaseProtocol extends EventEmitter {
172172
_onIncomingStream ({ protocol, stream, connection }) {
173173
const peerId = connection.remotePeer
174174
const idB58Str = peerId.toB58String()
175-
176-
const peer = this._addPeer(new Peer({
177-
id: peerId,
178-
protocols: [protocol]
179-
}))
175+
const peer = this._addPeer(peerId, [protocol])
180176

181177
this._processMessages(idB58Str, stream, peer)
182178
}
@@ -191,10 +187,7 @@ class PubsubBaseProtocol extends EventEmitter {
191187
const idB58Str = peerId.toB58String()
192188
this.log('connected', idB58Str)
193189

194-
const peer = this._addPeer(new Peer({
195-
id: peerId,
196-
protocols: this.multicodecs
197-
}))
190+
const peer = this._addPeer(peerId, this.multicodecs)
198191

199192
if (peer.isConnected) {
200193
return
@@ -225,15 +218,22 @@ class PubsubBaseProtocol extends EventEmitter {
225218
/**
226219
* Add a new connected peer to the peers map.
227220
* @private
228-
* @param {Peer} peer internal peer
221+
* @param {PeerId} peerId
222+
* @param {Array<string>} protocols
229223
* @returns {Peer}
230224
*/
231-
_addPeer (peer) {
232-
const id = peer.id.toB58String()
225+
_addPeer (peerId, protocols) {
226+
const id = peerId.toB58String()
233227
let existing = this.peers.get(id)
234228

235229
if (!existing) {
236230
this.log('new peer', id)
231+
232+
const peer = new Peer({
233+
id: peerId,
234+
protocols
235+
})
236+
237237
this.peers.set(id, peer)
238238
existing = peer
239239

0 commit comments

Comments
 (0)