Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
chore: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-Authored-By: Jacob Heun <[email protected]>
  • Loading branch information
vasco-santos and jacobheun committed Apr 16, 2020
1 parent 3daa016 commit e4d4016
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/topology/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Add a peer to the topology.
- `topology.disconnect(peerId)`

**Parameters**
- `peerId` is the [PeerIid][peer-id] of the peer disconnected.
- `peerId` is the [PeerId][peer-id] of the peer disconnected.

### Multicodec Topology

Expand Down Expand Up @@ -139,3 +139,5 @@ const toplogy = new MulticodecTopology({
- `handlers` is an optional `Object` containing the handler called when a peer is connected or disconnected.
- `onConnect` is a `function` called everytime a peer is connected in the topology context.
- `onDisconnect` is a `function` called everytime a peer is disconnected in the topology context.

[peer-id]: https://github.com/libp2p/js-peer-id
7 changes: 5 additions & 2 deletions src/topology/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ class Topology {
this._onConnect = handlers.onConnect || noop
this._onDisconnect = handlers.onDisconnect || noop

// TODO: can this be a set?
this.peers = new Map()
/**
* Set of peers that support the protocol.
* @type {Set<string>}
*/
this.peers = new Set()
}

set registrar (registrar) {
Expand Down
8 changes: 4 additions & 4 deletions src/topology/multicodec-topology.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ class MulticodecTopology extends Topology {

/**
* Update topology.
* @param {Array<PeerData>} peerDataIterable
* @param {Array<{id: PeerId, multiaddrs: Array<Multiaddr>}>} peerDataIterable
* @returns {void}
*/
_updatePeers (peerDataIterable) {
for (const peerData of peerDataIterable) {
if (this.multicodecs.filter(multicodec => peerData.protocols.includes(multicodec)).length) {
// Add the peer regardless of whether or not there is currently a connection
this.peers.set(peerData.id.toB58String(), peerData.id)
this.peers.add(peerData.id.toB58String())
// If there is a connection, call _onConnect
const connection = this._registrar.getConnection(peerData.id)
connection && this._onConnect(peerData.id, connection)
Expand All @@ -80,11 +80,11 @@ class MulticodecTopology extends Topology {
* @param {Array<string>} props.protocols
*/
_onProtocolChange ({ peerId, protocols }) {
const existingPeer = this.peers.get(peerId.toB58String())
const hadPeer = this.peers.has(peerId.toB58String())
const hasProtocol = protocols.filter(protocol => this.multicodecs.includes(protocol))

// Not supporting the protocol anymore?
if (existingPeer && hasProtocol.length === 0) {
if (hadPeer && hasProtocol.length === 0) {
this._onDisconnect(peerId)
}

Expand Down

0 comments on commit e4d4016

Please sign in to comment.