Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update to new events, yay for simpler APIs #133

Merged
merged 2 commits into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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