Switch to more specific IPC helpers (#2821)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-06-10 10:34:22 -04:00
1 parent a12cb0a16e
commit 280af6391e
18 files changed
+191 -178

No files matched your search

+15 -30
View File
@@ -26,22 +26,21 @@
import { ipcMain, ipcRenderer, remote, webContents } from "electron";
import { toJS } from "../utils/toJS";
import logger from "../../main/logger";
import { ClusterFrameInfo, clusterFrameMap } from "../cluster-frames";
import { ClusterFrameInfo, clusterFrameMap } from "../cluster-frames";
import type { Disposer } from "../utils";
const subFramesChannel = "ipc:get-sub-frames";
export function handleRequest(channel: string, listener: (event: Electron.IpcMainInvokeEvent, ...args: any[]) => any) {
ipcMain.handle(channel, async (event, ...args) => {
const payload = await listener(event, ...args);
return sanitizePayload(payload);
});
}
export async function requestMain(channel: string, ...args: any[]) {
return ipcRenderer.invoke(channel, ...args.map(sanitizePayload));
}
export function ipcMainHandle(channel: string, listener: (event: Electron.IpcMainInvokeEvent, ...args: any[]) => any) {
ipcMain.handle(channel, async (event, ...args) => {
return sanitizePayload(await listener(event, ...args));
});
}
function getSubFrames(): ClusterFrameInfo[] {
return Array.from(clusterFrameMap.values());
}
@@ -76,34 +75,20 @@ export function broadcastMessage(channel: string, ...args: any[]) {
});
}
export function subscribeToBroadcast(channel: string, listener: (...args: any[]) => any) {
if (ipcRenderer) {
ipcRenderer.on(channel, listener);
} else if (ipcMain) {
ipcMain.on(channel, listener);
}
export function ipcMainOn(channel: string, listener: (event: Electron.IpcMainEvent, ...args: any[]) => any): Disposer {
ipcMain.on(channel, listener);
return listener;
return () => ipcMain.off(channel, listener);
}
export function unsubscribeFromBroadcast(channel: string, listener: (...args: any[]) => any) {
if (ipcRenderer) {
ipcRenderer.off(channel, listener);
} else if (ipcMain) {
ipcMain.off(channel, listener);
}
}
export function ipcRendererOn(channel: string, listener: (event: Electron.IpcRendererEvent, ...args: any[]) => any): Disposer {
ipcRenderer.on(channel, listener);
export function unsubscribeAllFromBroadcast(channel: string) {
if (ipcRenderer) {
ipcRenderer.removeAllListeners(channel);
} else if (ipcMain) {
ipcMain.removeAllListeners(channel);
}
return () => ipcRenderer.off(channel, listener);
}
export function bindBroadcastHandlers() {
handleRequest(subFramesChannel, () => getSubFrames());
ipcMainHandle(subFramesChannel, () => getSubFrames());
}
/**
+2 -1
View File
@@ -23,6 +23,7 @@ import type { EventEmitter } from "events";
import { ipcMain } from "electron";
import logger from "../../main/logger";
import type { Disposer } from "../utils";
import { ipcMainHandle } from "./ipc";
export type ListenerEvent<EM extends EventEmitter> = Parameters<Parameters<EM["on"]>[1]>[0];
export type ListVerifier<T extends any[]> = (args: unknown[]) => args is T;
@@ -116,7 +117,7 @@ export function handleCorrect<
throw new TypeError(`Invalid args for invoke on channel: ${channel}`);
}
ipcMain.handle(channel, wrappedHandler);
ipcMainHandle(channel, wrappedHandler);
return () => ipcMain.removeHandler(channel);
}