Files
DndGamePlayer/app/main/ipc/router.ts
T

20 lines
715 B
TypeScript

import { ipcMain } from 'electron';
import type { IpcInvokeMap } from '../../shared/ipc/contracts';
type Handler<K extends keyof IpcInvokeMap> = (
payload: IpcInvokeMap[K]['req'],
) => Promise<IpcInvokeMap[K]['res']> | IpcInvokeMap[K]['res'];
const handlers = new Map<string, (payload: unknown) => Promise<unknown>>();
export function registerHandler<K extends keyof IpcInvokeMap>(channel: K, handler: Handler<K>) {
handlers.set(channel as string, async (payload: unknown) => handler(payload as IpcInvokeMap[K]['req']));
}
export function installIpcRouter() {
for (const [channel, handler] of handlers.entries()) {
ipcMain.handle(channel, async (_event, payload: unknown) => handler(payload));
}
}