@@ -283,20 +283,25 @@ function fixBufferList(list) {
283
283
function enqueue ( self , toEnqueue ) {
284
284
// If the send queue hasn't been initialized yet, do it, and install an
285
285
// event handler that flushes the send queue after binding is done.
286
- if ( ! self . _sendQueue ) {
287
- self . _sendQueue = [ ] ;
288
- self . once ( 'listening' , function ( ) {
289
- // Flush the send queue.
290
- for ( var i = 0 ; i < this . _sendQueue . length ; i ++ )
291
- this . send . apply ( self , this . _sendQueue [ i ] ) ;
292
- this . _sendQueue = undefined ;
293
- } ) ;
286
+ if ( ! self . _queue ) {
287
+ self . _queue = [ ] ;
288
+ self . once ( 'listening' , clearQueue ) ;
294
289
}
295
- self . _sendQueue . push ( toEnqueue ) ;
290
+ self . _queue . push ( toEnqueue ) ;
296
291
return ;
297
292
}
298
293
299
294
295
+ function clearQueue ( ) {
296
+ const queue = this . _queue ;
297
+ this . _queue = undefined ;
298
+
299
+ // Flush the send queue.
300
+ for ( var i = 0 ; i < queue . length ; i ++ )
301
+ queue [ i ] ( ) ;
302
+ }
303
+
304
+
300
305
// valid combinations
301
306
// send(buffer, offset, length, port, address, callback)
302
307
// send(buffer, offset, length, port, address)
@@ -353,7 +358,7 @@ Socket.prototype.send = function(buffer,
353
358
// If the socket hasn't been bound yet, push the outbound packet onto the
354
359
// send queue and send after binding is complete.
355
360
if ( self . _bindState != BIND_STATE_BOUND ) {
356
- enqueue ( self , [ list , port , address , callback ] ) ;
361
+ enqueue ( self , self . send . bind ( self , list , port , address , callback ) ) ;
357
362
return ;
358
363
}
359
364
@@ -407,10 +412,15 @@ function afterSend(err, sent) {
407
412
this . callback ( err , sent ) ;
408
413
}
409
414
410
-
411
415
Socket . prototype . close = function ( callback ) {
412
416
if ( typeof callback === 'function' )
413
417
this . on ( 'close' , callback ) ;
418
+
419
+ if ( this . _queue ) {
420
+ this . _queue . push ( this . close . bind ( this ) ) ;
421
+ return this ;
422
+ }
423
+
414
424
this . _healthCheck ( ) ;
415
425
this . _stopReceiving ( ) ;
416
426
this . _handle . close ( ) ;
0 commit comments