Implement full IPC API on render process#73
Open
rdickert wants to merge 2 commits intoelectron-webapps:masterfrom
Open
Implement full IPC API on render process#73rdickert wants to merge 2 commits intoelectron-webapps:masterfrom
rdickert wants to merge 2 commits intoelectron-webapps:masterfrom
Conversation
Collaborator
|
Thanks @rdickert! We actually ended up making changes very similar to this in our fork of the preload script, so this is a great contribution to the main script. |
app/preload.js
Outdated
| * @param {String} event - The name of an event. | ||
| * @param {...*} arg - additional arguments to pass to event handler. | ||
| */ | ||
| sendIpcEvent: function(/* event , ...args */) { |
Collaborator
There was a problem hiding this comment.
Would you mind naming this method send? I think that's actually most consistent with the IPC APIs—I should have named the other one just on. I think I just named it onEvent because I was using it more for listening to events than full two-way message passing at that time.
Contributor
Author
|
@wearhere Good call. Do you want me to also change |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For the app I'm working on, I'd like to be able to communicate arbitrary messages between the main and render processes. It requires a tray, etc., so I'm using a fork of the
app/directory inprivate/as recommended in the readme. I think the best way for the main and render processes to communicate is via messages, in the direction suggested in #58, rather than to expose functions directly.IPC is present in Electron for this purpose, but the full API is currently limited on the render process by
meson:electron.Electron.onEvent()allows the render process to receive an IPC message from main, but it does not pass through the event object or any additional parameters you may want to send. Also, there is no way to signal up to the main process.The code in this PR proposes changing
Electron.onEvent()to provide the full IPC API as documented here and addingElectron.sendIpcEvent()for communication from render process to main. I'm not attached to the exact method names. In fact, it might make sense to name them closer to the Electron API:Electron.ipcRendererOn()andElectron.ipcRendererSend()or similar so that new users can relate it to the Electron docs more readily.These changes make it possible to safely provide any functionality needed. It might make sense to add a method/RPC type of abstraction on top of this where you could provide something like
Electron.call('setTrayIcon', 'alert')orElectron.call('promptUser', choices)which could return a promise or otherwise help with async responses. This would feel natural to Meteor devs and wouldn't add much code tomeson:electron.