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

Commit

Permalink
Merge fb8cb95 into a7bb72b
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jan 11, 2017
2 parents a7bb72b + fb8cb95 commit d6103da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 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
4 changes: 3 additions & 1 deletion src/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ class Peer {
// no connection to close
}
// end the pushable pull-stream
this.stream.end()
if (this.stream) {
this.stream.end()
}
setImmediate(() => {
this.conn = null
this.stream = null
Expand Down

0 comments on commit d6103da

Please sign in to comment.