Skip to content

Commit

Permalink
fix ipc communication
Browse files Browse the repository at this point in the history
  • Loading branch information
jooy2 committed Jan 11, 2024
1 parent 81fbbb8 commit c2e7cc3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/main/IPCs.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { BrowserWindow, ipcMain, shell } from 'electron';
import { IpcMainEvent, ipcMain, shell } from 'electron';
import { version } from '../../package.json';

/*
* IPC Communications
* */
export default class IPCs {
static initialize(window: BrowserWindow): void {
static initialize(): void {
// Get application version
ipcMain.on('msgRequestGetVersion', () => {
window.webContents.send('msgReceivedVersion', version);
ipcMain.on('msgRequestGetVersion', (event: IpcMainEvent) => {
event.returnValue = version;
});

// Open url via web browser
ipcMain.on('msgOpenExternalLink', async (event, url) => {
ipcMain.on('msgOpenExternalLink', async (event: IpcMainEvent, url: string) => {
await shell.openExternal(url);
});
}
Expand Down
38 changes: 31 additions & 7 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
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}`);
},
// eslint-disable-next-line no-unused-vars
on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => {
if (rendererAvailChannels.includes(channel)) {
ipcRenderer.on(channel, listener);
} else {
throw new Error(`Unknown ipc channel name: ${channel}`);
}
},
// eslint-disable-next-line no-unused-vars
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}`);
}
},
// eslint-disable-next-line no-unused-vars
off: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => {
if (rendererAvailChannels.includes(channel)) {
ipcRenderer.on(channel, (event, ...args) => cbFunc(event, ...args));
ipcRenderer.off(channel, listener);
} else {
throw new Error(`Receive failed: Unknown ipc channel name: ${channel}`);
throw new Error(`Unknown ipc channel name: ${channel}`);
}
},
invoke: async (channel: string, ...data: any[]): Promise<any> => {
if (mainAvailChannels.includes(channel)) {
const result = await ipcRenderer.invoke.apply(null, [channel, ...data]);
return result;
}
throw new Error(`Invoke failed: Unknown ipc channel name: ${channel}`);
throw new Error(`Unknown ipc channel name: ${channel}`);
},
});
6 changes: 1 addition & 5 deletions src/renderer/screens/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ const Main = () => {

useEffect(() => {
// Get application version from package.json version string (Using IPC communication)
window.mainApi.receive('msgReceivedVersion', (event, version: string) => {
dispatch(setVersion(version));
});

window.mainApi.send('msgRequestGetVersion');
dispatch(setVersion(window.mainApi.sendSync('msgRequestGetVersion')));
}, []);

return (
Expand Down

0 comments on commit c2e7cc3

Please sign in to comment.