Skip to content

Instantly share code, notes, and snippets.

@sillyslux
Created March 28, 2022 16:15
Show Gist options
  • Save sillyslux/57cb381cbea06c5fde4e2c8b57e7d13d to your computer and use it in GitHub Desktop.
Save sillyslux/57cb381cbea06c5fde4e2c8b57e7d13d to your computer and use it in GitHub Desktop.
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('ipc', {
submit: (channel, ...data) => {
console.log('api send');
const validChannels = ['toMain'];
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, ...data);
}
},
subscribe: (channel, fn) => {
// debugger;
console.log('api receive', channel, fn);
const validChannels = ['fromMain'];
if (!validChannels.includes(channel)) return () => { };
const handler = (event, ...args) => fn(...args);
ipcRenderer.on(channel, handler);
return () => { ipcRenderer.removeListener(channel, handler); };
},
request: async (key, ...args) => ipcRenderer.invoke(key, ...args),
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment