Skip to content

Commit

Permalink
fix: Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Jan 23, 2020
1 parent f974d4f commit d6bb864
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/client.js
Expand Up @@ -51,11 +51,16 @@ function Client (broker, conn, req) {
// queue packets received before client fires 'connect' event. Prevents memory leaks on 'connect' event
this.parser._queue = []

this.on('connected', () => {
while (this.parser._queue.length > 0) {
this.on('connected', dequeue)

function dequeue () {
var q = this.parser._queue
for (var i = 0, len = q.length; i < len; i++) {
handle(this, this.parser._queue.shift(), this._nextBatch)
}
})

this.parser._queue = null
}

this.parser.on('packet', enqueue)

Expand Down
4 changes: 2 additions & 2 deletions test/connect.js
Expand Up @@ -364,8 +364,8 @@ test('After first CONNECT Packet, others are queued until \'connect\' event', fu
broker.on('client', function (client) {
t.equal(client.parser._queue.length, 10, 'Packets have been queued')

client.on('connected', () => {
t.equal(client.parser._queue.length, 0, 'Queue is empty')
client.once('connected', () => {
t.equal(client.parser._queue, null, 'Queue is empty')
s.conn.destroy()
broker.close(t.end)
})
Expand Down

0 comments on commit d6bb864

Please sign in to comment.