Skip to content

Instantly share code, notes, and snippets.

@alielmajdaoui
Created October 30, 2022 02:16
Show Gist options
  • Save alielmajdaoui/67a9f0c8357225ccfe15148cff87f64b to your computer and use it in GitHub Desktop.
Save alielmajdaoui/67a9f0c8357225ccfe15148cff87f64b to your computer and use it in GitHub Desktop.
./src/main/preload.ts
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';
export type Channels = 'ipc-example';
contextBridge.exposeInMainWorld('electron', {
ipcRenderer: {
openSettingsWindow() {
ipcRenderer.send('open-settings-window');
},
sendMessage(channel: Channels, args: unknown[]) {
ipcRenderer.send(channel, args);
},
on(channel: Channels, func: (...args: unknown[]) => void) {
const subscription = (_event: IpcRendererEvent, ...args: unknown[]) =>
func(...args);
ipcRenderer.on(channel, subscription);
return () => {
ipcRenderer.removeListener(channel, subscription);
};
},
once(channel: Channels, func: (...args: unknown[]) => void) {
ipcRenderer.once(channel, (_event, ...args) => func(...args));
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment