Skip to content

Commit

Permalink
Add typed ipc from ft
Browse files Browse the repository at this point in the history
  • Loading branch information
SRichner committed Feb 17, 2024
1 parent 9db1be3 commit c78b9c4
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/electron/types/TypedIpcMain.ts
Original file line number Diff line number Diff line change
@@ -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> = T | Promise<T>;
type InputMap = {
[key: string]: (...args: any) => any;
};

export interface TypedIpcMain<IpcEvents extends InputMap, IpcCommands extends InputMap>
extends IpcMain {
on<K extends keyof IpcEvents>(
channel: K,
listener: (event: IpcMainEvent, ...args: Parameters<IpcEvents[K]>) => void
): this;
once<K extends keyof IpcEvents>(
channel: K,
listener: (event: IpcMainEvent, ...args: Parameters<IpcEvents[K]>) => void
): this;
removeListener<K extends keyof IpcEvents>(
channel: K,
listener: (event: IpcMainEvent, ...args: Parameters<IpcEvents[K]>) => void
): this;
removeAllListeners<K extends keyof IpcEvents>(channel?: K): this;
handle<K extends keyof IpcCommands>(
channel: K,
listener: (
event: IpcMainInvokeEvent,
...args: Parameters<IpcCommands[K]>
) => OptionalPromise<ReturnType<IpcCommands[K]>>
): void;
handleOnce<K extends keyof IpcCommands>(
channel: K,
listener: (
event: IpcMainInvokeEvent,
...args: Parameters<IpcCommands[K]>
) => OptionalPromise<ReturnType<IpcCommands[K]>>
): void;
removeHandler<K extends keyof IpcCommands>(channel: K): void;
}

export interface TypedIpcRenderer<IpcEvents extends InputMap, IpcCommands extends InputMap>
extends IpcRenderer {
on<K extends keyof IpcEvents>(
channel: K,
listener: (event: IpcRendererEvent, ...args: Parameters<IpcEvents[K]>) => void
): this;
once<K extends keyof IpcEvents>(
channel: K,
listener: (event: IpcRendererEvent, ...args: Parameters<IpcEvents[K]>) => void
): this;
removeListener<K extends keyof IpcEvents>(
channel: K,
listener: (event: IpcRendererEvent, ...args: Parameters<IpcEvents[K]>) => void
): this;
removeAllListeners<K extends keyof IpcEvents>(channel: K): this;
send<K extends keyof IpcEvents>(channel: K, ...args: Parameters<IpcEvents[K]>): void;
sendSync<K extends keyof IpcEvents>(
channel: K,
...args: Parameters<IpcEvents[K]>
): ReturnType<IpcEvents[K]>;
sendTo<K extends keyof IpcEvents>(
webContentsId: number,
channel: K,
...args: Parameters<IpcEvents[K]>
): void;
sendToHost<K extends keyof IpcEvents>(channel: K, ...args: Parameters<IpcEvents[K]>): void;
invoke<K extends keyof IpcCommands>(
channel: K,
...args: Parameters<IpcCommands[K]>
): Promise<ReturnType<IpcCommands[K]>>;
}

export interface TypedWebContents<IpcEvents extends InputMap> extends WebContents {
send<K extends keyof IpcEvents>(channel: K, ...args: Parameters<IpcEvents[K]>): void;
}

0 comments on commit c78b9c4

Please sign in to comment.