fix: do not register new ipc event listener when main screen component is loaded (fixes #176)
This commit is contained in:
1 parent
52ffc48cde
commit
a71feb8a08
5 files changed
+46
-19
No files matched your search
+28
-7
@@ -1,22 +1,43 @@
|
||||
import { contextBridge, ipcRenderer } from 'electron'
|
||||
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron'
|
||||
|
||||
// Whitelist of valid channels used for IPC communication (Send message from Renderer to Main)
|
||||
const mainAvailChannels: string[] = ['msgRequestGetVersion', 'msgOpenExternalLink']
|
||||
const rendererAvailChannels: string[] = ['msgReceivedVersion']
|
||||
const rendererAvailChannels: string[] = []
|
||||
|
||||
contextBridge.exposeInMainWorld('mainApi', {
|
||||
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}`)
|
||||
throw new Error(`Unknown ipc channel name: ${channel}`)
|
||||
}
|
||||
},
|
||||
receive: (channel: string, cbFunc: Function): void => {
|
||||
sendSync: (channel: string, ...data: any[]): void => {
|
||||
if (mainAvailChannels.includes(channel)) {
|
||||
return ipcRenderer.sendSync.apply(null, [channel, ...data])
|
||||
}
|
||||
|
||||
throw new Error(`Unknown ipc channel name: ${channel}`)
|
||||
},
|
||||
on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => {
|
||||
if (rendererAvailChannels.includes(channel)) {
|
||||
ipcRenderer.on(channel, (event, ...args) => cbFunc(event, ...args))
|
||||
ipcRenderer.on(channel, listener)
|
||||
} else {
|
||||
throw new Error(`Receive failed: Unknown ipc channel name: ${channel}`)
|
||||
throw new Error(`Unknown ipc channel name: ${channel}`)
|
||||
}
|
||||
},
|
||||
once: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => {
|
||||
if (rendererAvailChannels.includes(channel)) {
|
||||
ipcRenderer.once(channel, listener)
|
||||
} else {
|
||||
throw new Error(`Unknown ipc channel name: ${channel}`)
|
||||
}
|
||||
},
|
||||
off: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => {
|
||||
if (rendererAvailChannels.includes(channel)) {
|
||||
ipcRenderer.off(channel, listener)
|
||||
} else {
|
||||
throw new Error(`Unknown ipc channel name: ${channel}`)
|
||||
}
|
||||
},
|
||||
invoke: async (channel: string, ...data: any[]): Promise<any> => {
|
||||
@@ -25,6 +46,6 @@ contextBridge.exposeInMainWorld('mainApi', {
|
||||
return result
|
||||
}
|
||||
|
||||
throw new Error(`Invoke failed: Unknown ipc channel name: ${channel}`)
|
||||
throw new Error(`Unknown ipc channel name: ${channel}`)
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user