@@ -13,13 +13,6 @@ const { concat, toArrayBuffer, unmask } = require('./buffer-util');
1313const { isValidStatusCode, isValidUTF8 } = require ( './validation' ) ;
1414
1515const 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 ;
2316
2417const GET_INFO = 0 ;
2518const GET_PAYLOAD_LENGTH_16 = 1 ;
@@ -567,17 +560,12 @@ class Receiver extends Writable {
567560 data = fragments ;
568561 }
569562
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 ) {
576564 this . emit ( 'message' , data , true ) ;
577565 this . _state = GET_INFO ;
578566 } else {
579567 this . _state = DEFER_EVENT ;
580- queueTask ( ( ) => {
568+ setImmediate ( ( ) => {
581569 this . emit ( 'message' , data , true ) ;
582570 this . _state = GET_INFO ;
583571 this . startLoop ( cb ) ;
@@ -604,7 +592,7 @@ class Receiver extends Writable {
604592 this . _state = GET_INFO ;
605593 } else {
606594 this . _state = DEFER_EVENT ;
607- queueTask ( ( ) => {
595+ setImmediate ( ( ) => {
608596 this . emit ( 'message' , buf , false ) ;
609597 this . _state = GET_INFO ;
610598 this . startLoop ( cb ) ;
@@ -675,7 +663,7 @@ class Receiver extends Writable {
675663 this . _state = GET_INFO ;
676664 } else {
677665 this . _state = DEFER_EVENT ;
678- queueTask ( ( ) => {
666+ setImmediate ( ( ) => {
679667 this . emit ( this . _opcode === 0x09 ? 'ping' : 'pong' , data ) ;
680668 this . _state = GET_INFO ;
681669 this . startLoop ( cb ) ;
@@ -711,32 +699,3 @@ class Receiver extends Writable {
711699}
712700
713701module . 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