Skip to content

Commit

Permalink
Expose the error only inside the close callback
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor committed Jul 13, 2017
1 parent 8a68584 commit c3e35b4
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ function Boot (server, opts, done) {
this._readyQ.drain = () => {
this.emit('start')
}
this._closeQ = fastq(this, callWithCbOrNextTick, 1)

this._closeQ = fastq(this, closeWithCbOrNextTick, 1)
this._closeQ.pause()
this._closeQ.drain = () => {
this.emit('close')
}
this._thereIsCloseCb = false

// we init, because we need to emit "start" if no use is called
this._init()
Expand Down Expand Up @@ -191,6 +193,7 @@ Boot.prototype.close = function (cb) {
this._error = null
if (cb) {
this._closeQ.push(cb)
this._thereIsCloseCb = true
}
process.nextTick(this._closeQ.resume.bind(this._closeQ))
}
Expand Down Expand Up @@ -219,6 +222,27 @@ function callWithCbOrNextTick (func, cb, context) {
}
}

function closeWithCbOrNextTick (func, cb, context) {
context = this._server
if (this._closeQ.length() === 0 && this._thereIsCloseCb) {
if (func.length === 0 || func.length === 1) {
func(this._error)
process.nextTick(cb)
} else if (func.length === 2) {
func(this._error, cb)
} else {
func(this._error, context, cb)
}
} else {
if (func.length === 0 || func.length === 1) {
func(context)
process.nextTick(cb)
} else {
func(context, cb)
}
}
}

module.exports = Boot
module.exports.express = function (app) {
return Boot(app, {
Expand Down

0 comments on commit c3e35b4

Please sign in to comment.