|
2 | 2 |
|
3 | 3 | const {
|
4 | 4 | ArrayIsArray,
|
| 5 | + ArrayPrototypePush, |
5 | 6 | ObjectDefineProperty,
|
6 | 7 | ObjectSetPrototypeOf,
|
| 8 | + ReflectApply, |
7 | 9 | Symbol,
|
8 | 10 | Uint8Array,
|
9 | 11 | } = primordials;
|
@@ -73,6 +75,7 @@ let HTTPParser;
|
73 | 75 | const MAX_HANDLE_RETRANSMISSIONS = 3;
|
74 | 76 | const kChannelHandle = Symbol('kChannelHandle');
|
75 | 77 | const kIsUsedAsStdio = Symbol('kIsUsedAsStdio');
|
| 78 | +const kPendingMessages = Symbol('kPendingMessages'); |
76 | 79 |
|
77 | 80 | // This object contain function to convert TCP objects to native handle objects
|
78 | 81 | // and back again.
|
@@ -520,6 +523,7 @@ class Control extends EventEmitter {
|
520 | 523 | constructor(channel) {
|
521 | 524 | super();
|
522 | 525 | this.#channel = channel;
|
| 526 | + this[kPendingMessages] = []; |
523 | 527 | }
|
524 | 528 |
|
525 | 529 | // The methods keeping track of the counter are being used to track the
|
@@ -693,6 +697,24 @@ function setupChannel(target, channel, serializationMode) {
|
693 | 697 | });
|
694 | 698 | });
|
695 | 699 |
|
| 700 | + target.on('newListener', function() { |
| 701 | + |
| 702 | + process.nextTick(() => { |
| 703 | + if (!target.channel || !target.listenerCount('message')) |
| 704 | + return; |
| 705 | + |
| 706 | + const messages = target.channel[kPendingMessages]; |
| 707 | + const { length } = messages; |
| 708 | + if (!length) return; |
| 709 | + |
| 710 | + for (let i = 0; i < length; i++) { |
| 711 | + ReflectApply(target.emit, target, messages[i]); |
| 712 | + } |
| 713 | + |
| 714 | + target.channel[kPendingMessages] = []; |
| 715 | + }); |
| 716 | + }); |
| 717 | + |
696 | 718 | target.send = function(message, handle, options, callback) {
|
697 | 719 | if (typeof handle === 'function') {
|
698 | 720 | callback = handle;
|
@@ -909,7 +931,15 @@ function setupChannel(target, channel, serializationMode) {
|
909 | 931 | };
|
910 | 932 |
|
911 | 933 | function emit(event, message, handle) {
|
912 |
| - target.emit(event, message, handle); |
| 934 | + if ('internalMessage' === event || target.listenerCount('message')) { |
| 935 | + target.emit(event, message, handle); |
| 936 | + return; |
| 937 | + } |
| 938 | + |
| 939 | + ArrayPrototypePush( |
| 940 | + target.channel[kPendingMessages], |
| 941 | + [event, message, handle] |
| 942 | + ); |
913 | 943 | }
|
914 | 944 |
|
915 | 945 | function handleMessage(message, handle, internal) {
|
|
0 commit comments