From 85a064765a15fe3ab64f8b03fcb6de5fe72097b1 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Fri, 6 May 2016 13:53:39 +0200 Subject: [PATCH] fix: call cb in close after all transport are closed --- package.json | 3 ++- src/index.js | 18 +++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 49faa22..9f3cc0f 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "stream-pair": "^1.0.3" }, "dependencies": { + "async": "^2.0.0-rc.4", "babel-runtime": "^6.6.1", "duplex-passthrough": "github:diasdavid/duplex-passthrough", "ip-address": "^5.8.0", @@ -78,4 +79,4 @@ "Richard Littauer ", "dignifiedquire " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 2416382..676575d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ 'use strict' +const async = require('async') const multistream = require('multistream-select') const identify = require('./identify') const DuplexPassThrough = require('duplex-passthrough') @@ -367,19 +368,18 @@ function Swarm (peerInfo) { } this.close = (callback) => { - var count = 0 - Object.keys(this.muxedConns).forEach((key) => { this.muxedConns[key].muxer.end() }) - Object.keys(this.transports).forEach((key) => { - this.transports[key].close(() => { - if (++count === Object.keys(this.transports).length) { - callback() - } - }) - }) + async.each( + Object.keys(this.transports), + (key, cb) => this.transports[key].close(cb), + () => { + // Ignoring close errors + callback() + } + ) } }