| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 75c565b commit d9465ae
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -81,6 +81,7 @@ let HTTPParser; | |||
| 81 | 81 | const MAX_HANDLE_RETRANSMISSIONS = 3; | |
| 82 | 82 | const kChannelHandle = Symbol('kChannelHandle'); | |
| 83 | 83 | const kIsUsedAsStdio = Symbol('kIsUsedAsStdio'); | |
| 84 | + const kPendingMessages = Symbol('kPendingMessages'); | ||
| 84 | 85 | ||
| 85 | 86 | // This object contain function to convert TCP objects to native handle objects | |
| 86 | 87 | // and back again. | |
@@ -526,6 +527,7 @@ class Control extends EventEmitter { | |||
| 526 | 527 | constructor(channel) { | |
| 527 | 528 | super(); | |
| 528 | 529 | this.#channel = channel; | |
| 530 | + this[kPendingMessages] = []; | ||
| 529 | 531 | } | |
| 530 | 532 | ||
| 531 | 533 | // The methods keeping track of the counter are being used to track the | |
@@ -699,6 +701,24 @@ function setupChannel(target, channel, serializationMode) { | |||
| 699 | 701 | }); | |
| 700 | 702 | }); | |
| 701 | 703 | ||
| 704 | + target.on('newListener', function() { | ||
| 705 | + | ||
| 706 | + process.nextTick(() => { | ||
| 707 | + if (!target.channel || !target.listenerCount('message')) | ||
| 708 | + return; | ||
| 709 | + | ||
| 710 | + const messages = target.channel[kPendingMessages]; | ||
| 711 | + const { length } = messages; | ||
| 712 | + if (!length) return; | ||
| 713 | + | ||
| 714 | + for (let i = 0; i < length; i++) { | ||
| 715 | + ReflectApply(target.emit, target, messages[i]); | ||
| 716 | + } | ||
| 717 | + | ||
| 718 | + target.channel[kPendingMessages] = []; | ||
| 719 | + }); | ||
| 720 | + }); | ||
| 721 | + | ||
| 702 | 722 | target.send = function(message, handle, options, callback) { | |
| 703 | 723 | if (typeof handle === 'function') { | |
| 704 | 724 | callback = handle; | |
@@ -912,7 +932,15 @@ function setupChannel(target, channel, serializationMode) { | |||
| 912 | 932 | }; | |
| 913 | 933 | ||
| 914 | 934 | function emit(event, message, handle) { | |
| 915 | - target.emit(event, message, handle); | ||
| 935 | + if ('internalMessage' === event || target.listenerCount('message')) { | ||
| 936 | + target.emit(event, message, handle); | ||
| 937 | + return; | ||
| 938 | + } | ||
| 939 | + | ||
| 940 | + ArrayPrototypePush( | ||
| 941 | + target.channel[kPendingMessages], | ||
| 942 | + [event, message, handle] | ||
| 943 | + ); | ||
| 916 | 944 | } | |
| 917 | 945 | ||
| 918 | 946 | function handleMessage(message, handle, internal) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,20 @@ | |||
| 1 | + import '../common/index.mjs'; | ||
| 2 | + import assert from 'assert'; | ||
| 3 | + import { fork } from 'child_process'; | ||
| 4 | + import { once } from 'events'; | ||
| 5 | + import { fileURLToPath } from 'url'; | ||
| 6 | + | ||
| 7 | + if (process.argv[2] !== 'child') { | ||
| 8 | + const filename = fileURLToPath(import.meta.url); | ||
| 9 | + const cp = fork(filename, ['child']); | ||
| 10 | + const message = 'Hello World'; | ||
| 11 | + cp.send(message); | ||
| 12 | + | ||
| 13 | + const [received] = await once(cp, 'message'); | ||
| 14 | + assert.deepStrictEqual(received, message); | ||
| 15 | + | ||
| 16 | + cp.disconnect(); | ||
| 17 | + await once(cp, 'exit'); | ||
| 18 | + } else { | ||
| 19 | + process.on('message', (msg) => process.send(msg)); | ||
| 20 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments