Skip to content

Commit

Permalink
perform nextTick work only if work needs to be done (#931)
Browse files Browse the repository at this point in the history
* perform nextTick work only if work needs to be done, call done() immediately if not.

* check if done() is valid
  • Loading branch information
svogl authored and mcollina committed Mar 25, 2019
1 parent 524699d commit 93046a5
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/client.js
Expand Up @@ -256,18 +256,24 @@ MqttClient.prototype._setupStream = function () {
})

function nextTickWork () {
process.nextTick(work)
if (packets.length) {
process.nextTick(work)
} else {
var done = completeParse
completeParse = null
done()
}
}

function work () {
var packet = packets.shift()
var done = completeParse

if (packet) {
that._handlePacket(packet, nextTickWork)
} else {
var done = completeParse
completeParse = null
done()
if (done) done()
}
}

Expand Down

0 comments on commit 93046a5

Please sign in to comment.