@@ -18,9 +18,10 @@ const xtend = require('xtend')
18
18
const debug = require ( 'debug' ) ( 'mqttjs:client' )
19
19
const nextTick = process ? process . nextTick : function ( callback ) { setTimeout ( callback , 0 ) }
20
20
const setImmediate = global . setImmediate || function ( callback ) {
21
- // works in node v0.8
22
- nextTick ( callback )
21
+ const args = arguments . slice ( 1 )
22
+ process . nextTick ( callback . bind ( null , ... args ) )
23
23
}
24
+
24
25
const defaultConnectOptions = {
25
26
keepalive : 60 ,
26
27
reschedulePings : true ,
@@ -89,11 +90,11 @@ const errors = {
89
90
162 : 'Wildcard Subscriptions not supported'
90
91
}
91
92
92
- function defaultId ( ) {
93
+ function defaultId ( ) {
93
94
return 'mqttjs_' + Math . random ( ) . toString ( 16 ) . substr ( 2 , 8 )
94
95
}
95
96
96
- function applyTopicAlias ( client , packet ) {
97
+ function applyTopicAlias ( client , packet ) {
97
98
if ( client . options . protocolVersion === 5 ) {
98
99
if ( packet . cmd === 'publish' ) {
99
100
let alias
@@ -143,7 +144,7 @@ function applyTopicAlias (client, packet) {
143
144
}
144
145
}
145
146
146
- function removeTopicAliasAndRecoverTopicName ( client , packet ) {
147
+ function removeTopicAliasAndRecoverTopicName ( client , packet ) {
147
148
let alias
148
149
if ( packet . properties ) {
149
150
alias = packet . properties . topicAlias
@@ -168,7 +169,7 @@ function removeTopicAliasAndRecoverTopicName (client, packet) {
168
169
}
169
170
}
170
171
171
- function sendPacket ( client , packet , cb ) {
172
+ function sendPacket ( client , packet , cb ) {
172
173
debug ( 'sendPacket :: packet: %O' , packet )
173
174
debug ( 'sendPacket :: emitting `packetsend`' )
174
175
@@ -186,7 +187,7 @@ function sendPacket (client, packet, cb) {
186
187
}
187
188
}
188
189
189
- function flush ( queue ) {
190
+ function flush ( queue ) {
190
191
if ( queue ) {
191
192
debug ( 'flush: queue exists? %b' , ! ! ( queue ) )
192
193
Object . keys ( queue ) . forEach ( function ( messageId ) {
@@ -200,7 +201,7 @@ function flush (queue) {
200
201
}
201
202
}
202
203
203
- function flushVolatile ( queue ) {
204
+ function flushVolatile ( queue ) {
204
205
if ( queue ) {
205
206
debug ( 'flushVolatile :: deleting volatile messages from the queue and setting their callbacks as error function' )
206
207
Object . keys ( queue ) . forEach ( function ( messageId ) {
@@ -212,7 +213,7 @@ function flushVolatile (queue) {
212
213
}
213
214
}
214
215
215
- function storeAndSend ( client , packet , cb , cbStorePut ) {
216
+ function storeAndSend ( client , packet , cb , cbStorePut ) {
216
217
debug ( 'storeAndSend :: store packet with cmd %s to outgoingStore' , packet . cmd )
217
218
let storePacket = packet
218
219
let err
@@ -226,7 +227,7 @@ function storeAndSend (client, packet, cb, cbStorePut) {
226
227
return cb && cb ( err )
227
228
}
228
229
}
229
- client . outgoingStore . put ( storePacket , function storedPacket ( err ) {
230
+ client . outgoingStore . put ( storePacket , function storedPacket ( err ) {
230
231
if ( err ) {
231
232
return cb && cb ( err )
232
233
}
@@ -235,7 +236,7 @@ function storeAndSend (client, packet, cb, cbStorePut) {
235
236
} )
236
237
}
237
238
238
- function nop ( error ) {
239
+ function nop ( error ) {
239
240
debug ( 'nop ::' , error )
240
241
}
241
242
@@ -246,7 +247,7 @@ function nop (error) {
246
247
* @param {Object } [options] - connection options
247
248
* (see Connection#connect)
248
249
*/
249
- function MqttClient ( streamBuilder , options ) {
250
+ function MqttClient ( streamBuilder , options ) {
250
251
let k
251
252
const that = this
252
253
@@ -338,7 +339,7 @@ function MqttClient (streamBuilder, options) {
338
339
this . on ( 'connect' , function ( ) {
339
340
const queue = that . queue
340
341
341
- function deliver ( ) {
342
+ function deliver ( ) {
342
343
const entry = queue . shift ( )
343
344
debug ( 'deliver :: entry %o' , entry )
344
345
let packet = null
@@ -426,7 +427,7 @@ MqttClient.prototype._setupStream = function () {
426
427
packets . push ( packet )
427
428
} )
428
429
429
- function nextTickWork ( ) {
430
+ function nextTickWork ( ) {
430
431
if ( packets . length ) {
431
432
nextTick ( work )
432
433
} else {
@@ -436,7 +437,7 @@ MqttClient.prototype._setupStream = function () {
436
437
}
437
438
}
438
439
439
- function work ( ) {
440
+ function work ( ) {
440
441
debug ( 'work :: getting next packet in queue' )
441
442
const packet = packets . shift ( )
442
443
@@ -459,7 +460,7 @@ MqttClient.prototype._setupStream = function () {
459
460
work ( )
460
461
}
461
462
462
- function streamErrorHandler ( error ) {
463
+ function streamErrorHandler ( error ) {
463
464
debug ( 'streamErrorHandler :: error' , error . message )
464
465
if ( socketErrors . includes ( error . code ) ) {
465
466
// handle error
@@ -747,7 +748,7 @@ MqttClient.prototype.subscribe = function () {
747
748
debug ( 'subscribe: array topic %s' , topic )
748
749
if ( ! Object . prototype . hasOwnProperty . call ( that . _resubscribeTopics , topic ) ||
749
750
that . _resubscribeTopics [ topic ] . qos < opts . qos ||
750
- resubscribe ) {
751
+ resubscribe ) {
751
752
const currentOpts = {
752
753
topic : topic ,
753
754
qos : opts . qos
@@ -769,7 +770,7 @@ MqttClient.prototype.subscribe = function () {
769
770
debug ( 'subscribe: object topic %s' , k )
770
771
if ( ! Object . prototype . hasOwnProperty . call ( that . _resubscribeTopics , k ) ||
771
772
that . _resubscribeTopics [ k ] . qos < obj [ k ] . qos ||
772
- resubscribe ) {
773
+ resubscribe ) {
773
774
const currentOpts = {
774
775
topic : k ,
775
776
qos : obj [ k ] . qos
@@ -988,7 +989,7 @@ MqttClient.prototype.end = function (force, opts, cb) {
988
989
debug ( 'end :: cb? %s' , ! ! cb )
989
990
cb = cb || nop
990
991
991
- function closeStores ( ) {
992
+ function closeStores ( ) {
992
993
debug ( 'end :: closeStores: closing incoming and outgoing stores' )
993
994
that . disconnected = true
994
995
that . incomingStore . close ( function ( e1 ) {
@@ -1007,7 +1008,7 @@ MqttClient.prototype.end = function (force, opts, cb) {
1007
1008
}
1008
1009
}
1009
1010
1010
- function finish ( ) {
1011
+ function finish ( ) {
1011
1012
// defer closesStores of an I/O cycle,
1012
1013
// just to make sure things are
1013
1014
// ok for websockets
@@ -1263,7 +1264,7 @@ MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut, noStore) {
1263
1264
* anyway it will result in -1 evaluation
1264
1265
*/
1265
1266
case 0 :
1266
- /* falls through */
1267
+ /* falls through */
1267
1268
default :
1268
1269
sendPacket ( this , packet , cb )
1269
1270
break
@@ -1622,7 +1623,7 @@ MqttClient.prototype._handleAck = function (packet) {
1622
1623
debug ( '_handleAck :: packet type' , type )
1623
1624
switch ( type ) {
1624
1625
case 'pubcomp' :
1625
- // same thing as puback for QoS 2
1626
+ // same thing as puback for QoS 2
1626
1627
case 'puback' : {
1627
1628
const pubackRC = packet . reasonCode
1628
1629
// Callback - we're done
@@ -1685,7 +1686,7 @@ MqttClient.prototype._handleAck = function (packet) {
1685
1686
}
1686
1687
1687
1688
if ( this . disconnecting &&
1688
- Object . keys ( this . outgoing ) . length === 0 ) {
1689
+ Object . keys ( this . outgoing ) . length === 0 ) {
1689
1690
this . emit ( 'outgoingEmpty' )
1690
1691
}
1691
1692
}
@@ -1754,8 +1755,8 @@ MqttClient.prototype._resubscribe = function () {
1754
1755
debug ( '_resubscribe' )
1755
1756
const _resubscribeTopicsKeys = Object . keys ( this . _resubscribeTopics )
1756
1757
if ( ! this . _firstConnection &&
1757
- ( this . options . clean || ( this . options . protocolVersion === 5 && ! this . connackPacket . sessionPresent ) ) &&
1758
- _resubscribeTopicsKeys . length > 0 ) {
1758
+ ( this . options . clean || ( this . options . protocolVersion === 5 && ! this . connackPacket . sessionPresent ) ) &&
1759
+ _resubscribeTopicsKeys . length > 0 ) {
1759
1760
if ( this . options . resubscribe ) {
1760
1761
if ( this . options . protocolVersion === 5 ) {
1761
1762
debug ( '_resubscribe: protocolVersion 5' )
@@ -1796,10 +1797,10 @@ MqttClient.prototype._onConnect = function (packet) {
1796
1797
1797
1798
this . connected = true
1798
1799
1799
- function startStreamProcess ( ) {
1800
+ function startStreamProcess ( ) {
1800
1801
let outStore = that . outgoingStore . createStream ( )
1801
1802
1802
- function clearStoreProcessing ( ) {
1803
+ function clearStoreProcessing ( ) {
1803
1804
that . _storeProcessing = false
1804
1805
that . _packetIdsDuringStoreProcessing = { }
1805
1806
}
@@ -1812,14 +1813,14 @@ MqttClient.prototype._onConnect = function (packet) {
1812
1813
that . emit ( 'error' , err )
1813
1814
} )
1814
1815
1815
- function remove ( ) {
1816
+ function remove ( ) {
1816
1817
outStore . destroy ( )
1817
1818
outStore = null
1818
1819
that . _flushStoreProcessingQueue ( )
1819
1820
clearStoreProcessing ( )
1820
1821
}
1821
1822
1822
- function storeDeliver ( ) {
1823
+ function storeDeliver ( ) {
1823
1824
// edge case, we wrapped this twice
1824
1825
if ( ! outStore ) {
1825
1826
return
0 commit comments