Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why empty?

Copy link
Contributor Author

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).

}

/**
* Normalizes the arguments passed to `foregroundChild`.
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

@demurgos demurgos Sep 19, 2018

Choose a reason for hiding this comment

The 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