Open rdickert opened 8 years ago
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.
@wearhere Good call. Do you want me to also change onEvent()
to on()
and add an alias for the original with a deprecation warning?
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
.