Skip to content

Commit

Permalink
Refactored, in performance-wise
Browse files Browse the repository at this point in the history
  • Loading branch information
gnought committed Aug 11, 2019
1 parent 16d6825 commit 4888b72
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/client.js
Expand Up @@ -51,7 +51,6 @@ function Client (broker, conn) {
return that.emit('error', err)
}

var buf = empty
var client = that

if (client._paused) {
Expand All @@ -61,6 +60,7 @@ function Client (broker, conn) {
that._parsingBatch--
if (that._parsingBatch <= 0) {
that._parsingBatch = 0
var buf = empty
buf = client.conn.read(null)

if (buf) {
Expand Down Expand Up @@ -158,11 +158,7 @@ function onError (err) {
this.errored = true
this.conn.removeAllListeners('error')
this.conn.on('error', nop)
if (this.id) {
this.broker.emit('clientError', this, err)
} else {
this.broker.emit('connectionError', this, err)
}
this.broker.emit(this.id ? 'clientError' : 'connectionError', this, err)
this.close()
}

Expand All @@ -175,8 +171,9 @@ Client.prototype.publish = function (message, done) {
var that = this
if (packet.qos === 0) {
// skip offline and send it as it is
this.deliver0(packet, done || nop)
} else if (!this.clean && this.id) {
return this.deliver0(packet, done)
}
if (!this.clean && this.id) {
this.broker.persistence.outgoingEnqueue({
clientId: this.id
}, packet, function deliver (err) {
Expand Down Expand Up @@ -308,13 +305,14 @@ Client.prototype.resume = function () {
}

function enqueue (packet) {
this.client._parsingBatch++
var client = this.client
client._parsingBatch++
// already connected or it's the first packet
if (this.client.connackSent || this.client._parsingBatch === 1) {
handle(this.client, packet, this.client._nextBatch)
if (client.connackSent || client._parsingBatch === 1) {
handle(client, packet, client._nextBatch)
} else {
this.client.on('connected', () => {
handle(this.client, packet, this.client._nextBatch)
client.on('connected', () => {
handle(client, packet, client._nextBatch)
})
}
}
Expand Down

0 comments on commit 4888b72

Please sign in to comment.