refactor: move common logic to handleRemoteCommand

This commit is contained in:
Milan Burda 2018-08-24 01:27:52 +02:00
parent f1fe485768
commit 28e4fcea3b
2 changed files with 108 additions and 138 deletions

View file

@ -275,21 +275,21 @@ function metaToException (meta) {
return error
}
function handleMessage (channel, handler) {
ipcRenderer.on(channel, (event, passedContextId, ...args) => {
if (passedContextId === contextId) {
handler(...args)
}
})
}
// Browser calls a callback in renderer.
ipcRenderer.on('ELECTRON_RENDERER_CALLBACK', (event, passedContextId, id, args) => {
if (passedContextId !== contextId) {
// The invoked callback belongs to an old page in this renderer.
return
}
handleMessage('ELECTRON_RENDERER_CALLBACK', (id, args) => {
callbacksRegistry.apply(id, metaToValue(args))
})
// A callback in browser is released.
ipcRenderer.on('ELECTRON_RENDERER_RELEASE_CALLBACK', (event, passedContextId, id) => {
if (passedContextId !== contextId) {
// The freed callback belongs to an old page in this renderer.
return
}
handleMessage('ELECTRON_RENDERER_RELEASE_CALLBACK', (id) => {
callbacksRegistry.remove(id)
})