Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

fix: emit boot error only once #1472

Merged
merged 2 commits into from
Jul 27, 2018
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
21 changes: 17 additions & 4 deletions src/core/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ module.exports = (self) => {
(repoOpened, cb) => {
// Init with existing initialized, opened, repo
if (repoOpened) {
return self.init({ repo: self._repo }, (err) => cb(err))
return self.init({ repo: self._repo }, (err) => {
if (err) return cb(Object.assign(err, { emitted: true }))
cb()
})
}

if (doInit) {
const initOptions = Object.assign(
{ bits: 2048, pass: self._options.pass },
typeof options.init === 'object' ? options.init : {}
)
return self.init(initOptions, (err) => cb(err))
return self.init(initOptions, (err) => {
if (err) return cb(Object.assign(err, { emitted: true }))
cb()
})
}

cb()
Expand All @@ -48,11 +54,18 @@ module.exports = (self) => {
if (!doStart) {
return cb()
}
self.start(cb)

self.start((err) => {
if (err) return cb(Object.assign(err, { emitted: true }))
cb()
})
}
], (err) => {
if (err) {
return self.emit('error', err)
if (!err.emitted) {
self.emit('error', err)
}
return
}
self.log('booted')
self.emit('ready')
Expand Down
24 changes: 19 additions & 5 deletions test/core/create-node.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('create node', function () {
})
})

it('init: false errors (start default: true)', function (done) {
it('init: false errors (start default: true) and errors only once', function (done) {
this.timeout(80 * 1000)

const node = new IPFS({
Expand All @@ -135,10 +135,24 @@ describe('create node', function () {
}
}
})
node.once('error', (err) => {
expect(err).to.exist()
done()
})

const shouldHappenOnce = () => {
let timeoutId = null

return (err) => {
expect(err).to.exist()

// Bad news, this handler has been executed before
if (timeoutId) {
clearTimeout(timeoutId)
return done(new Error('error handler called multiple times'))
}

timeoutId = setTimeout(done, 100)
}
}

node.on('error', shouldHappenOnce())
})

it('init: false, start: false', function (done) {
Expand Down