@@ -59,6 +59,7 @@ const { convertToValidSignal, deprecate } = require('internal/util');
59
59
const { isArrayBufferView } = require ( 'internal/util/types' ) ;
60
60
const spawn_sync = internalBinding ( 'spawn_sync' ) ;
61
61
const { kStateSymbol } = require ( 'internal/dgram' ) ;
62
+ const console = require ( 'console' ) ;
62
63
63
64
const {
64
65
UV_EACCES ,
@@ -526,6 +527,7 @@ class Control extends EventEmitter {
526
527
constructor ( channel ) {
527
528
super ( ) ;
528
529
this . #channel = channel ;
530
+ this . pendingMessages = new Set ( ) ;
529
531
}
530
532
531
533
// The methods keeping track of the counter are being used to track the
@@ -699,6 +701,19 @@ function setupChannel(target, channel, serializationMode) {
699
701
} ) ;
700
702
} ) ;
701
703
704
+ target . on ( 'newListener' , function ( ) {
705
+ if ( ! target . channel ) return ;
706
+
707
+ const messages = target . channel . pendingMessages ;
708
+ if ( ! messages . size ) return ;
709
+
710
+ for ( const messageParams of messages ) {
711
+ process . nextTick ( target . emit . bind ( target ) , ...messageParams ) ;
712
+ }
713
+
714
+ messages . clear ( ) ;
715
+ } ) ;
716
+
702
717
target . send = function ( message , handle , options , callback ) {
703
718
if ( typeof handle === 'function' ) {
704
719
callback = handle ;
@@ -912,7 +927,15 @@ function setupChannel(target, channel, serializationMode) {
912
927
} ;
913
928
914
929
function emit ( event , message , handle ) {
915
- target . emit ( event , message , handle ) ;
930
+ const args = [ event , message , handle ] ;
931
+ const isInternalMessage = "internalMessage" === event ;
932
+ const hasListenersInstalled = target . listenerCount ( 'message' ) ;
933
+ if ( hasListenersInstalled || isInternalMessage ) {
934
+ target . emit ( ...args ) ;
935
+ return ;
936
+ }
937
+
938
+ target . channel . pendingMessages . add ( args ) ;
916
939
}
917
940
918
941
function handleMessage ( message , handle , internal ) {
0 commit comments