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

Commit

Permalink
feat: match expectation on start (that dials are done)
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jan 10, 2017
1 parent a7bb72b commit b802af9
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class FloodSub extends EventEmitter {
this._dialPeer = this._dialPeer.bind(this)
}

_dialPeer (peerInfo) {
_dialPeer (peerInfo, callback) {
callback = callback || function noop () {}
const idB58Str = peerInfo.id.toB58String()
log('dialing %s', idB58Str)

Expand All @@ -72,11 +73,11 @@ class FloodSub extends EventEmitter {
return log.err(err)
}

this._onDial(peerInfo, conn)
this._onDial(peerInfo, conn, callback)
})
}

_onDial (peerInfo, conn) {
_onDial (peerInfo, conn, callback) {
const idB58Str = peerInfo.id.toB58String()

// If already had a dial to me, just add the conn
Expand All @@ -89,6 +90,7 @@ class FloodSub extends EventEmitter {

// Immediately send my own subscriptions to the newly established conn
peer.sendSubscriptions(this.subscriptions)
setImmediate(() => callback())
}

_onConnection (protocol, conn) {
Expand Down Expand Up @@ -212,13 +214,13 @@ class FloodSub extends EventEmitter {
// Dial already connected peers
const peerInfos = values(this.libp2p.peerBook.getAll())

peerInfos.forEach((peerInfo) => {
this._dialPeer(peerInfo)
})

setImmediate(() => {
this.started = true
callback()
asyncEach(peerInfos, (peerInfo, cb) => {
this.dialPeer(peerInfo, cb)
}, (err) => {
setImmediate(() => {
this.started = true
callback(err)
})
})
}

Expand Down

0 comments on commit b802af9

Please sign in to comment.