Skip to content

Commit 21b9850

Browse files
committed
fix: use once to call cb once
1 parent b7ef098 commit 21b9850

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@
100100
"stream-http": "^2.8.1",
101101
"subcomandante": "^1.0.5",
102102
"superagent": "^3.8.2",
103-
"truthy": "0.0.1"
103+
"truthy": "0.0.1",
104+
"once": "^1.4.0"
104105
},
105106
"devDependencies": {
106107
"aegir": "^13.0.6",
@@ -112,7 +113,6 @@
112113
"ipfs": "~0.28.2",
113114
"is-running": "1.0.5",
114115
"mkdirp": "^0.5.1",
115-
"once": "^1.4.0",
116116
"proxyquire": "^2.0.1",
117117
"sinon": "^4.5.0",
118118
"superagent-mocker": "~0.5.2",

src/factory-in-proc.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const clone = require('lodash.clone')
55
const series = require('async/series')
66
const path = require('path')
77
const tmpDir = require('./utils/tmp-dir')
8+
const once = require('once')
89

910
const Node = require('./ipfsd-in-proc')
1011
const defaultConfig = require('./defaults/config')
@@ -130,12 +131,13 @@ class FactoryInProc {
130131
}
131132

132133
const node = new Node(options)
133-
let called = false
134-
const errHandler = (err) => {
135-
called = true
136-
callback(err, node)
137-
}
138-
node.once('error', errHandler)
134+
const callbackOnce = once((err) => {
135+
if (err) {
136+
return callback(err)
137+
}
138+
callback(null, node)
139+
})
140+
node.once('error', callbackOnce)
139141

140142
series([
141143
(cb) => node.once('ready', cb),
@@ -151,10 +153,7 @@ class FactoryInProc {
151153
(cb) => options.start
152154
? node.start(options.args, cb)
153155
: cb()
154-
], (err) => {
155-
node.removeListener('error', errHandler)
156-
if (!called) { callback(err, node) }
157-
})
156+
], callbackOnce)
158157
}
159158
}
160159

test/spawn-options.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const chai = require('chai')
88
const dirtyChai = require('dirty-chai')
99
const expect = chai.expect
1010
chai.use(dirtyChai)
11+
1112
const fs = require('fs')
1213
const isNode = require('detect-node')
1314
const hat = require('hat')
@@ -442,7 +443,7 @@ describe('Spawn options', function () {
442443
})
443444

444445
describe(`don't callback twice on error`, () => {
445-
if (!isNode && fOpts.type !== 'proc') { return }
446+
if (fOpts.type !== 'proc') { return }
446447
it('spawn with error', (done) => {
447448
this.timeout(20 * 1000)
448449
// `once.strict` should throw if its called more than once

0 commit comments

Comments
 (0)