Skip to content

Commit b31245a

Browse files
committed
Merge pull request libp2p#49 from diasdavid/fix/close-count
fix: call cb in close after all transport are closed
2 parents fb56cc3 + 85a0647 commit b31245a

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"stream-pair": "^1.0.3"
5555
},
5656
"dependencies": {
57+
"async": "^2.0.0-rc.4",
5758
"babel-runtime": "^6.6.1",
5859
"duplex-passthrough": "github:diasdavid/duplex-passthrough",
5960
"ip-address": "^5.8.0",
@@ -78,4 +79,4 @@
7879
"Richard Littauer <[email protected]>",
7980
"dignifiedquire <[email protected]>"
8081
]
81-
}
82+
}

src/index.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const async = require('async')
34
const multistream = require('multistream-select')
45
const identify = require('./identify')
56
const DuplexPassThrough = require('duplex-passthrough')
@@ -367,19 +368,18 @@ function Swarm (peerInfo) {
367368
}
368369

369370
this.close = (callback) => {
370-
var count = 0
371-
372371
Object.keys(this.muxedConns).forEach((key) => {
373372
this.muxedConns[key].muxer.end()
374373
})
375374

376-
Object.keys(this.transports).forEach((key) => {
377-
this.transports[key].close(() => {
378-
if (++count === Object.keys(this.transports).length) {
379-
callback()
380-
}
381-
})
382-
})
375+
async.each(
376+
Object.keys(this.transports),
377+
(key, cb) => this.transports[key].close(cb),
378+
() => {
379+
// Ignoring close errors
380+
callback()
381+
}
382+
)
383383
}
384384
}
385385

0 commit comments

Comments
 (0)