Skip to content

Commit

Permalink
Merge pull request #133 from ipfs/feat/new-libp2p-events
Browse files Browse the repository at this point in the history
feat: update to new events, yay for simpler APIs
  • Loading branch information
daviddias authored Mar 29, 2017
2 parents b25f8b4 + 2050185 commit 12b8dd0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
"homepage": "https://github.com/ipfs/js-ipfs-bitswap#readme",
"devDependencies": {
"aegir": "^11.0.1",
"benchmark": "^2.1.3",
"benchmark": "^2.1.4",
"chai": "^3.5.0",
"dirty-chai": "^1.2.2",
"ipfs-repo": "~0.13.0",
"libp2p-ipfs-nodejs": "~0.21.0",
"libp2p-ipfs-nodejs": "~0.22.0",
"lodash": "^4.17.4",
"multiaddr": "^2.2.3",
"multiaddr": "^2.3.0",
"ncp": "^2.0.0",
"peer-book": "~0.3.2",
"peer-id": "~0.8.5",
Expand Down
20 changes: 9 additions & 11 deletions src/components/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,21 @@ class Network {
start () {
this._running = true
// bind event listeners
this._onPeerMux = this._onPeerMux.bind(this)
this._onPeerMuxClosed = this._onPeerMuxClosed.bind(this)
this._onPeerConnect = this._onPeerConnect.bind(this)
this._onPeerDisconnect = this._onPeerDisconnect.bind(this)

this._onConnection = this._onConnection.bind(this)
this.libp2p.handle(BITSWAP100, this._onConnection)
if (!this.b100Only) {
this.libp2p.handle(BITSWAP110, this._onConnection)
}

this.libp2p.swarm.on('peer-mux-established', this._onPeerMux)
this.libp2p.swarm.on('peer-mux-closed', this._onPeerMuxClosed)
this.libp2p.on('peer:connect', this._onPeerConnect)
this.libp2p.on('peer:disconnect', this._onPeerDisconnect)

// All existing connections are like new ones for us
const pKeys = Object.keys(this.peerBook.getAll())
pKeys.forEach((k) => {
this._onPeerMux(this.peerBook.getByB58String(k))
})
pKeys.forEach((k) => this._onPeerConnect(this.peerBook.getByB58String(k)))
}

stop () {
Expand All @@ -55,8 +53,8 @@ class Network {
this.libp2p.unhandle(BITSWAP110)
}

this.libp2p.swarm.removeListener('peer-mux-established', this._onPeerMux)
this.libp2p.swarm.removeListener('peer-mux-closed', this._onPeerMuxClosed)
this.libp2p.removeListener('peer:connect', this._onPeerConnect)
this.libp2p.removeListener('peer:disconnect', this._onPeerDisconnect)
}

// Handles both types of bitswap messgages
Expand Down Expand Up @@ -87,14 +85,14 @@ class Network {
)
}

_onPeerMux (peerInfo) {
_onPeerConnect (peerInfo) {
if (!this._running) {
return
}
this.bitswap._onPeerConnected(peerInfo.id)
}

_onPeerMuxClosed (peerInfo) {
_onPeerDisconnect (peerInfo) {
if (!this._running) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ const hasBlocks = (msg, store, cb) => {
module.exports = (repo) => {
const libp2pMock = {
handle: function () {},
on () {},
swarm: {
muxedConns: {},
on () {},
setMaxListeners () {}
}
}
Expand Down

0 comments on commit 12b8dd0

Please sign in to comment.