-
-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: IPC message forwarding #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ const signalExit = require('signal-exit') | |
/* istanbul ignore next */ | ||
const spawn = process.platform === 'win32' ? require('cross-spawn') : require('child_process').spawn | ||
|
||
function noop() { | ||
} | ||
|
||
/** | ||
* Normalizes the arguments passed to `foregroundChild`. | ||
* | ||
|
@@ -82,14 +85,7 @@ function foregroundChild (...fgArgs) { | |
|
||
if (process.send) { | ||
process.removeAllListeners('message') | ||
|
||
child.on('message', (message, sendHandle) => { | ||
process.send(message, sendHandle) | ||
}) | ||
|
||
process.on('message', (message, sendHandle) => { | ||
child.send(message, sendHandle) | ||
}) | ||
proxyMessages(process, child) | ||
} | ||
|
||
return child | ||
|
@@ -119,4 +115,36 @@ function proxySignals (parent, child) { | |
} | ||
} | ||
|
||
/** | ||
* Starts forwarding IPC messages to `child` through `parent`. | ||
* | ||
* @param parent Parent process. | ||
* @param child Child Process. | ||
* @return `unproxy` function to stop the forwarding. | ||
* @internal | ||
*/ | ||
function proxyMessages(parent, child) { | ||
if (parent.send === undefined) { | ||
return noop | ||
} | ||
|
||
function childListener(message, handle) { | ||
parent.send(message, handle) | ||
} | ||
|
||
function parentListener(message, handle) { | ||
child.send(message, handle) | ||
} | ||
|
||
child.on('message', childListener) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Closure would have been cleaner in IMO: child.on('message', function (message, handle) {
child.send(message, handle);
} Same for L122. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't unproxy it if you do not have a reference to the function. The current code does not completely cleans up the proxies, but the end goal is to have a general proxy function with proper clean up. |
||
parent.on('message', parentListener) | ||
|
||
return unproxySignals; | ||
|
||
function unproxySignals() { | ||
child.removeListener('message', childListener) | ||
parent.removeListener('message', parentListener) | ||
} | ||
} | ||
|
||
module.exports = foregroundChild |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it's a no-op function. It avoids to redefine an empty function every time I need one. (in this PR it's used only once, but there are other places in the file).