preload: add data types
This commit is contained in:
1 parent
00ae01dc3f
commit
0398810622
1 file changed
+5
-5
@@ -1,25 +1,25 @@
|
||||
import { contextBridge, ipcRenderer } from 'electron'
|
||||
|
||||
// Whitelist of valid channels used for IPC communication (Send message from Renderer to Main)
|
||||
const mainAvailChannels = ['msgRequestGetVersion', 'msgOpenExternalLink']
|
||||
const rendererAvailChannels = ['msgReceivedVersion']
|
||||
const mainAvailChannels: string[] = ['msgRequestGetVersion', 'msgOpenExternalLink']
|
||||
const rendererAvailChannels: string[] = ['msgReceivedVersion']
|
||||
|
||||
contextBridge.exposeInMainWorld('mainApi', {
|
||||
send: (channel, ...data) => {
|
||||
send: (channel: string, ...data: any[]): void => {
|
||||
if (mainAvailChannels.includes(channel)) {
|
||||
ipcRenderer.send.apply(null, [channel, ...data])
|
||||
} else {
|
||||
throw new Error(`Send failed: Unknown ipc channel name: ${channel}`)
|
||||
}
|
||||
},
|
||||
receive: (channel, cbFunc) => {
|
||||
receive: (channel: string, cbFunc: Function): void => {
|
||||
if (rendererAvailChannels.includes(channel)) {
|
||||
ipcRenderer.on(channel, (event, ...args) => cbFunc(event, ...args))
|
||||
} else {
|
||||
throw new Error(`Receive failed: Unknown ipc channel name: ${channel}`)
|
||||
}
|
||||
},
|
||||
invoke: async (channel, ...data) => {
|
||||
invoke: async (channel: string, ...data: any[]): Promise<any> => {
|
||||
if (mainAvailChannels.includes(channel)) {
|
||||
const result = await ipcRenderer.invoke.apply(null, [channel, ...data])
|
||||
return result
|
||||
|
||||
Reference in new issue
Block a user