Created
March 28, 2022 16:15
-
-
Save sillyslux/57cb381cbea06c5fde4e2c8b57e7d13d to your computer and use it in GitHub Desktop.
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
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