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

Commit

Permalink
fix: improve close handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 8, 2016
1 parent 57c7318 commit cd89354
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@
"dependencies": {
"mafmt": "^1.0.1",
"multiaddr": "^1.1.1",
"run-parallel": "^1.1.6",
"simple-websocket": "github:diasdavid/simple-websocket#ec31437"
},
"devDependencies": {
"chai": "^3.5.0",
"aegir": "^2.0.0",
"aegir": "^3.0.1",
"gulp": "^3.9.1",
"interface-connection": "0.0.3",
"interface-transport": "^0.1.1",
Expand All @@ -49,4 +50,4 @@
"Francisco Baio Dias <xicombd@gmail.com>",
"dignifiedquire <dignifiedquire@gmail.com>"
]
}
}
13 changes: 5 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const debug = require('debug')
const log = debug('libp2p:websockets')
const SWS = require('simple-websocket')
const mafmt = require('mafmt')
const parallel = require('run-parallel')

exports = module.exports = WebSockets

Expand Down Expand Up @@ -64,14 +65,10 @@ function WebSockets () {
log('Called close with no active listeners')
return callback()
}
var count = 0
listeners.forEach((listener) => {
listener.close(() => {
if (++count === listeners.length) {
callback()
}
})
})

parallel(listeners.map((listener) => {
return (cb) => listener.close(cb)
}), callback)
}

this.filter = (multiaddrs) => {
Expand Down
16 changes: 4 additions & 12 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ describe('libp2p-websockets', function () {
ws.createListener(mh, (socket) => {
expect(socket).to.exist
socket.end()
ws.close(() => {
done()
})
ws.close(done)
}, () => {
const conn = ws.dial(mh)
conn.end()
Expand All @@ -45,9 +43,7 @@ describe('libp2p-websockets', function () {
expect(socket).to.exist
socket.end()
expect(socket.getObservedAddrs()).to.deep.equal([])
ws.close(() => {
done()
})
ws.close(done)
}, () => {
const conn = ws.dial(mh)
conn.end()
Expand Down Expand Up @@ -76,9 +72,7 @@ describe('libp2p-websockets', function () {
conn.on('data', (data) => {
expect(data.toString()).to.equal(message)
conn.end()
ws.close(() => {
done()
})
ws.close(done)
})
})
})
Expand All @@ -99,9 +93,7 @@ describe('libp2p-websockets', function () {
conn.on('data', (data) => {
expect(data.toString()).to.equal(message)
conn.end()
ws.close(() => {
done()
})
ws.close(done)
})
})
})
Expand Down

0 comments on commit cd89354

Please sign in to comment.