Files
pqs-9100_client/frontend/src/utils/ipcRenderer.ts

119 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-10-15 14:12:24 +08:00
/**
* Electron IPC Renderer
*/
interface IpcRenderer {
/**
* invoke/handle
* @param channel
* @param param
* @returns Promise<T>
*/
invoke<T = any>(channel: string, param?: any): Promise<T>;
/**
* send/on
* @param channel
* @param param
* @returns
*/
sendSync(channel: string, param?: any): any;
/**
* channel listener
* @param channel
* @param listener
*/
on(channel: string, listener: (event: any, ...args: any[]) => void): void;
/**
* listener
* @param channel
* @param listener
*/
once(channel: string, listener: (event: any, ...args: any[]) => void): void;
/**
* channel listener
* @param channel
* @param listener
*/
removeListener(channel: string, listener: (event: any, ...args: any[]) => void): void;
/**
* channel
* @param channel
*/
removeAllListeners(channel?: string): void;
/**
* channel
* @param channel
* @param args
*/
send(channel: string, ...args: any[]): void;
/**
*
* @param channel
* @param message
* @param transfer
*/
postMessage(channel: string, message: any, transfer?: MessagePort[]): void;
/**
* channel webContentsId
* @param webContentsId ID
* @param channel
* @param args
*/
sendTo(webContentsId: number, channel: string, ...args: any[]): void;
/**
* host <webview>
* @param channel
* @param args
*/
sendToHost(channel: string, ...args: any[]): void;
}
interface Electron {
ipcRenderer?: IpcRenderer;
}
const Renderer = (window.require && window.require('electron')) || (window as any).electron || {};
/**
* ipc
* api说明https://www.electronjs.org/zh/docs/latest/api/ipc-renderer
*
* /
* ipc.invoke(channel, param) - invoke/handle
* ipc.sendSync(channel, param) - send/on
* ipc.on(channel, listener) - channel, listener
* ipc.once(channel, listener) - listener
* ipc.removeListener(channel, listener) - channel listener
* ipc.removeAllListeners(channel) - channel
* ipc.send(channel, ...args) - channel向主进程发送异步消息
* ipc.postMessage(channel, message, [transfer]) -
* ipc.sendTo(webContentsId, channel, ...args) - channel webContentsId
* ipc.sendToHost(channel, ...args) - host <webview>
*/
/**
* ipc
*/
const ipc: IpcRenderer | undefined = Renderer.ipcRenderer;
/**
* EE环境
*/
const isEE: boolean = ipc ? true : false;
export {
type IpcRenderer,
type Electron,
Renderer,
ipc,
isEE
};