@@ -13,13 +13,6 @@ const { concat, toArrayBuffer, unmask } = require('./buffer-util');
13
13
const { isValidStatusCode, isValidUTF8 } = require ( './validation' ) ;
14
14
15
15
const FastBuffer = Buffer [ Symbol . species ] ;
16
- const promise = Promise . resolve ( ) ;
17
-
18
- //
19
- // `queueMicrotask()` is not available in Node.js < 11.
20
- //
21
- const queueTask =
22
- typeof queueMicrotask === 'function' ? queueMicrotask : queueMicrotaskShim ;
23
16
24
17
const GET_INFO = 0 ;
25
18
const GET_PAYLOAD_LENGTH_16 = 1 ;
@@ -567,17 +560,12 @@ class Receiver extends Writable {
567
560
data = fragments ;
568
561
}
569
562
570
- //
571
- // If the state is `INFLATING`, it means that the frame data was
572
- // decompressed asynchronously, so there is no need to defer the event
573
- // as it will be emitted asynchronously anyway.
574
- //
575
- if ( this . _state === INFLATING || this . _allowSynchronousEvents ) {
563
+ if ( this . _allowSynchronousEvents ) {
576
564
this . emit ( 'message' , data , true ) ;
577
565
this . _state = GET_INFO ;
578
566
} else {
579
567
this . _state = DEFER_EVENT ;
580
- queueTask ( ( ) => {
568
+ setImmediate ( ( ) => {
581
569
this . emit ( 'message' , data , true ) ;
582
570
this . _state = GET_INFO ;
583
571
this . startLoop ( cb ) ;
@@ -604,7 +592,7 @@ class Receiver extends Writable {
604
592
this . _state = GET_INFO ;
605
593
} else {
606
594
this . _state = DEFER_EVENT ;
607
- queueTask ( ( ) => {
595
+ setImmediate ( ( ) => {
608
596
this . emit ( 'message' , buf , false ) ;
609
597
this . _state = GET_INFO ;
610
598
this . startLoop ( cb ) ;
@@ -675,7 +663,7 @@ class Receiver extends Writable {
675
663
this . _state = GET_INFO ;
676
664
} else {
677
665
this . _state = DEFER_EVENT ;
678
- queueTask ( ( ) => {
666
+ setImmediate ( ( ) => {
679
667
this . emit ( this . _opcode === 0x09 ? 'ping' : 'pong' , data ) ;
680
668
this . _state = GET_INFO ;
681
669
this . startLoop ( cb ) ;
@@ -711,32 +699,3 @@ class Receiver extends Writable {
711
699
}
712
700
713
701
module . exports = Receiver ;
714
-
715
- /**
716
- * A shim for `queueMicrotask()`.
717
- *
718
- * @param {Function } cb Callback
719
- */
720
- function queueMicrotaskShim ( cb ) {
721
- promise . then ( cb ) . catch ( throwErrorNextTick ) ;
722
- }
723
-
724
- /**
725
- * Throws an error.
726
- *
727
- * @param {Error } err The error to throw
728
- * @private
729
- */
730
- function throwError ( err ) {
731
- throw err ;
732
- }
733
-
734
- /**
735
- * Throws an error in the next tick.
736
- *
737
- * @param {Error } err The error to throw
738
- * @private
739
- */
740
- function throwErrorNextTick ( err ) {
741
- process . nextTick ( throwError , err ) ;
742
- }
0 commit comments