feat: coalescing dial support (#518)

* docs: fix spelling in api

* fix: dont create peerstore twice

* feat: add support for dial coalescing

* doc(fix): add setPeerValue to API TOC

* docs: add more jsdocs to dialer

* chore: remove old comment

* fix: ensure connections are closed

* fix: registrar.getConnections returns first open conn

* fix: directly set the closed status

* chore: remove unneeded log

* refactor: peerStore.put takes an options object
This commit is contained in:
Jacob Heun
2019-12-15 17:33:16 +01:00
parent 4384d139d2
commit 15f7c2a974
14 changed files with 325 additions and 172 deletions

View File

@ -36,11 +36,16 @@ class PeerStore extends EventEmitter {
/**
* Stores the peerInfo of a new peer.
* If already exist, its info is updated.
* If already exist, its info is updated. If `silent` is set to
* true, no 'peer' event will be emitted. This can be useful if you
* are already in the process of dialing the peer. The peer is technically
* known, but may not have been added to the PeerStore yet.
* @param {PeerInfo} peerInfo
* @param {object} [options]
* @param {boolean} [options.silent] (Default=false)
* @return {PeerInfo}
*/
put (peerInfo) {
put (peerInfo, options = { silent: false }) {
assert(PeerInfo.isPeerInfo(peerInfo), 'peerInfo must be an instance of peer-info')
let peer
@ -50,8 +55,8 @@ class PeerStore extends EventEmitter {
} else {
peer = this.add(peerInfo)
// Emit the new peer found
this.emit('peer', peerInfo)
// Emit the peer if silent = false
!options.silent && this.emit('peer', peerInfo)
}
return peer
}
@ -219,13 +224,12 @@ class PeerStore extends EventEmitter {
}
/**
* Returns the known multiaddrs for a given `PeerId`
* @param {PeerId} peerId
* Returns the known multiaddrs for a given `PeerInfo`
* @param {PeerInfo} peer
* @returns {Array<Multiaddr>}
*/
multiaddrsForPeer (peerId) {
const peerInfo = this.get(peerId.toB58String())
return peerInfo.multiaddrs.toArray()
multiaddrsForPeer (peer) {
return this.put(peer, true).multiaddrs.toArray()
}
}