From c78b9c44fbc1d07f655d78162d9b31c7f37c1d6c Mon Sep 17 00:00:00 2001 From: Sebastian Richner Date: Sat, 17 Feb 2024 14:26:01 +0100 Subject: [PATCH] Add typed ipc from ft --- src/electron/types/TypedIpcMain.ts | 86 ++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/electron/types/TypedIpcMain.ts diff --git a/src/electron/types/TypedIpcMain.ts b/src/electron/types/TypedIpcMain.ts new file mode 100644 index 00000000..c2783755 --- /dev/null +++ b/src/electron/types/TypedIpcMain.ts @@ -0,0 +1,86 @@ +/* To following types are part of electron-typed-ipc, written by Andrei Canta (deiucanta) + * The package is no longer maintained which causes dependency issues. Therefore, we copied the type declaration to this file + * The original package can be found here: https://github.com/deiucanta/electron-typed-ipc + */ + +import { + IpcMain, + IpcMainEvent, + IpcMainInvokeEvent, + IpcRenderer, + IpcRendererEvent, + WebContents +} from 'electron'; + +type OptionalPromise = T | Promise; +type InputMap = { + [key: string]: (...args: any) => any; +}; + +export interface TypedIpcMain + extends IpcMain { + on( + channel: K, + listener: (event: IpcMainEvent, ...args: Parameters) => void + ): this; + once( + channel: K, + listener: (event: IpcMainEvent, ...args: Parameters) => void + ): this; + removeListener( + channel: K, + listener: (event: IpcMainEvent, ...args: Parameters) => void + ): this; + removeAllListeners(channel?: K): this; + handle( + channel: K, + listener: ( + event: IpcMainInvokeEvent, + ...args: Parameters + ) => OptionalPromise> + ): void; + handleOnce( + channel: K, + listener: ( + event: IpcMainInvokeEvent, + ...args: Parameters + ) => OptionalPromise> + ): void; + removeHandler(channel: K): void; +} + +export interface TypedIpcRenderer + extends IpcRenderer { + on( + channel: K, + listener: (event: IpcRendererEvent, ...args: Parameters) => void + ): this; + once( + channel: K, + listener: (event: IpcRendererEvent, ...args: Parameters) => void + ): this; + removeListener( + channel: K, + listener: (event: IpcRendererEvent, ...args: Parameters) => void + ): this; + removeAllListeners(channel: K): this; + send(channel: K, ...args: Parameters): void; + sendSync( + channel: K, + ...args: Parameters + ): ReturnType; + sendTo( + webContentsId: number, + channel: K, + ...args: Parameters + ): void; + sendToHost(channel: K, ...args: Parameters): void; + invoke( + channel: K, + ...args: Parameters + ): Promise>; +} + +export interface TypedWebContents extends WebContents { + send(channel: K, ...args: Parameters): void; +}