Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPC周りの呼び出しをdot記法で書けるように #2240

Merged
merged 8 commits into from
Aug 29, 2024
34 changes: 19 additions & 15 deletions src/backend/browser/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "./fileImpl";
import { getConfigManager } from "./browserConfig";

import { IpcSOData } from "@/type/ipc";
import { IpcRendererOn, IpcSOData } from "@/type/ipc";
import {
defaultHotkeySettings,
defaultToolbarButtonSetting,
Expand All @@ -33,6 +33,23 @@ import {
// TODO: base pathを設定できるようにするか、ビルド時埋め込みにする
const toStaticPath = (fileName: string) => `/${fileName}`;

function onReceivedIPCMsg<T extends IpcRendererOn>(listeners: T): void;
function onReceivedIPCMsg(listeners: {
[key: string]: (event: unknown, ...args: unknown[]) => unknown;
}) {
window.addEventListener(
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
"message",
({
data,
}: MessageEvent<{
channel: keyof IpcSOData;
args: IpcSOData[keyof IpcSOData]["args"];
}>) => {
listeners[data.channel]?.({}, ...data.args);
},
);
}

/**
* Browser版のSandBox実装
* src/type/preload.tsのSandboxを変更した場合は、interfaceに追従した変更が必要
Expand Down Expand Up @@ -204,20 +221,7 @@ export const api: Sandbox = {
// NOTE: UIの表示状態の制御のためだけなので固定値を返している
return Promise.resolve(true);
},
onReceivedIPCMsg<T extends keyof IpcSOData>(
channel: T,
listener: (_: unknown, ...args: IpcSOData[T]["args"]) => void,
) {
window.addEventListener("message", (event) => {
const data = event.data as {
channel: keyof IpcSOData;
args: IpcSOData[keyof IpcSOData];
};
if (data.channel == channel) {
listener(data.args);
}
});
},
onReceivedIPCMsg,
closeWindow() {
throw new Error(`Not supported on Browser version: closeWindow`);
},
Expand Down
60 changes: 24 additions & 36 deletions src/backend/electron/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,32 @@
import { ipcMain, IpcMainInvokeEvent, BrowserWindow } from "electron";
import { BrowserWindow, ipcMain, IpcMainInvokeEvent } from "electron";
import log from "electron-log/main";
import { IpcIHData, IpcSOData } from "@/type/ipc";
import { IpcMainHandle, IpcMainSend } from "@/type/ipc";

export function ipcMainHandle<T extends keyof IpcIHData>(
channel: T,
listener: (
event: IpcMainInvokeEvent,
...args: IpcIHData[T]["args"]
) => IpcIHData[T]["return"] | Promise<IpcIHData[T]["return"]>,
export function registerIpcMainHandle<T extends IpcMainHandle>(
listeners: T,
): void;
export function ipcMainHandle(
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
channel: string,
listener: (event: IpcMainInvokeEvent, ...args: unknown[]) => unknown,
): void {
const errorHandledListener = (
event: IpcMainInvokeEvent,
...args: unknown[]
) => {
try {
validateIpcSender(event);
return listener(event, ...args);
} catch (e) {
log.error(e);
}
};
ipcMain.handle(channel, errorHandledListener);
export function registerIpcMainHandle(listeners: {
[key: string]: (event: IpcMainInvokeEvent, ...args: unknown[]) => unknown;
}) {
Object.entries(listeners).forEach(([channel, listener]) => {
const errorHandledListener: typeof listener = (event, ...args) => {
try {
validateIpcSender(event);
return listener(event, ...args);
} catch (e) {
log.error(e);
}
};
ipcMain.handle(channel, errorHandledListener);
});
}

export function ipcMainSend<T extends keyof IpcSOData>(
win: BrowserWindow,
channel: T,
...args: IpcSOData[T]["args"]
): void;
export function ipcMainSend(
win: BrowserWindow,
channel: string,
...args: unknown[]
): void {
win.webContents.send(channel, ...args);
}
export const ipcMainSend = new Proxy({} as IpcMainSend, {
get:
(_, channel: string) =>
(win: BrowserWindow, ...args: unknown[]) =>
win.webContents.send(channel, ...args),
});

MT224244 marked this conversation as resolved.
Show resolved Hide resolved
/** IPCメッセージの送信元を確認する */
const validateIpcSender = (event: IpcMainInvokeEvent) => {
Expand Down
Loading
Loading