Created
February 22, 2018 17:10
-
-
Save sallar/b1e2163934a3e933ea6458f33f20e2fe to your computer and use it in GitHub Desktop.
Electron IPC Enqueue
This file contains 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
ipc.on('channel:enqueue', (e, payload) => { | |
const { channelName, stack, id } = payload; | |
getQueue() | |
.channel(channelName) | |
.enqueue( | |
() => { | |
return new Promise(resolve => { | |
ipc.once(`channel:resolve:${id}`, resolve); | |
e.sender.webContents.send(`channel:execute:${id}`); | |
}); | |
}, | |
undefined, | |
stack | |
); | |
}); |
This file contains 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
export function enqueueInMain(channelName, fn, stack) { | |
const id = uuid.v4(); | |
ipc.once(`channel:execute:${id}`, async () => { | |
const output = fn(); | |
if (output instanceof Promise) { | |
await output; | |
} | |
ipc.send(`channel:resolve:${id}`); | |
}); | |
ipc.send('channel:enqueue', { | |
channelName, | |
stack, | |
id | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment