@@ -240,7 +240,8 @@ class NTTCP {
240
240
}
241
241
this . stream = null ;
242
242
this . connected = false ;
243
- this . waiter = null ;
243
+ this . drainWaiter = null ;
244
+ this . readWaiter = null ;
244
245
}
245
246
246
247
/**
@@ -323,10 +324,7 @@ class NTTCP {
323
324
if ( err ) {
324
325
this . savedErr = err ;
325
326
this . err = true ;
326
- if ( this . waiter ) {
327
- this . waiter ( ) ;
328
- this . waiter = null ;
329
- }
327
+ this . _notifyWaiters ( ) ;
330
328
}
331
329
} ) ;
332
330
if ( ! result ) {
@@ -352,7 +350,9 @@ class NTTCP {
352
350
async pauseWrite ( ) {
353
351
this . checkErr ( ) ;
354
352
if ( this . needsDrain ) {
355
- await new Promise ( ( resolve ) => this . stream . once ( 'drain' , resolve ) ) ;
353
+ await new Promise ( ( resolve ) => {
354
+ this . drainWaiter = resolve ;
355
+ } ) ;
356
356
this . checkErr ( ) ;
357
357
} else {
358
358
await new Promise ( ( resolve ) => Timers . setImmediate ( resolve ) ) ;
@@ -397,9 +397,9 @@ class NTTCP {
397
397
flags : tempBuf [ 5 ]
398
398
} ;
399
399
this . packets . push ( packet ) ;
400
- if ( this . waiter ) {
401
- this . waiter ( ) ;
402
- this . waiter = null ;
400
+ if ( this . readWaiter ) {
401
+ this . readWaiter ( ) ;
402
+ this . readWaiter = null ;
403
403
}
404
404
if ( process . env . NODE_ORACLEDB_DEBUG_PACKETS )
405
405
this . printPacket ( "Receiving packet" , packet . buf ) ;
@@ -436,7 +436,7 @@ class NTTCP {
436
436
if ( this . packets . length === 0 ) {
437
437
this . checkErr ( ) ;
438
438
await new Promise ( ( resolve ) => {
439
- this . waiter = resolve ;
439
+ this . readWaiter = resolve ;
440
440
this . numPacketsSinceLastWait = 0 ;
441
441
} ) ;
442
442
this . checkErr ( ) ;
@@ -472,26 +472,25 @@ class NTTCP {
472
472
this . stream . on ( 'error' , ( err ) => {
473
473
this . savedErr = err ;
474
474
this . err = true ;
475
- if ( this . waiter ) {
476
- this . waiter ( ) ;
477
- this . waiter = null ;
478
- }
475
+ this . _notifyWaiters ( ) ;
479
476
} ) ;
480
477
481
478
this . stream . on ( 'end' , ( ) => {
482
479
this . err = true ;
483
- if ( this . waiter ) {
484
- this . waiter ( ) ;
485
- this . waiter = null ;
486
- }
480
+ this . _notifyWaiters ( ) ;
487
481
} ) ;
488
482
489
483
this . stream . on ( 'close' , ( ) => {
490
484
this . connected = false ;
485
+ this . _notifyWaiters ( ) ;
491
486
} ) ;
492
487
493
488
this . stream . on ( 'drain' , ( ) => {
494
489
this . needsDrain = false ;
490
+ if ( this . drainWaiter ) {
491
+ this . drainWaiter ( ) ;
492
+ this . drainWaiter = null ;
493
+ }
495
494
} ) ;
496
495
497
496
}
@@ -516,6 +515,20 @@ class NTTCP {
516
515
}
517
516
}
518
517
518
+ /**
519
+ * Notify the waiters (drain and read) and reset them, if applicable.
520
+ */
521
+ _notifyWaiters ( ) {
522
+ if ( this . drainWaiter ) {
523
+ this . drainWaiter ( ) ;
524
+ this . drainWaiter = null ;
525
+ }
526
+ if ( this . readWaiter ) {
527
+ this . readWaiter ( ) ;
528
+ this . readWaiter = null ;
529
+ }
530
+ }
531
+
519
532
}
520
533
521
534
module . exports = NTTCP ;
0 commit comments