import { ipcMain , app , dialog } from "electron" ; import { CurrentWindow } from ".." ; import { registerRemoteEventNames } from "../../types" ; import { Logger } from "../logger" ; import { ScriptRemote } from "./scripts" ; const logger = Logger . of ( "remote" ) ; param name 事件前缀名称，需要传递到  registerRemoteEventNames param target export function registerRemoteEvent ( name , target ) { const events = registerRemoteEventNames ( name ) ; logger . info ( "remote事件注册" , name ) ; ipcMain . on ( events . get , ( event , arg ) => { const property = arg [ 0 ] ; event . returnValue = target [ property ] ; } ) . on ( events . set , ( event , arg ) => { const [ property , value ] = [ arg [ 0 ] , arg [ 1 ] ] ; event . returnValue = target [ property ] = value ; } ) . on ( events . call , async ( event , arg ) => { const [ property , ... value ] = [ arg . shift ( ) , ... arg ] ; event . returnValue = await target [ property ] ( ... value ) ; } ) . on ( events . on , ( event , eventName ) => { target . on ( eventName . split ( "-" ) [ 0 ] , ( ) => event . reply ( eventName ) ) ; } ) . on ( events . once , ( event , eventName ) => { target . once ( eventName . split ( "-" ) [ 0 ] , ( ) => event . reply ( eventName ) ) ; } ) ; } /**
 *  注册远程事件，渲染进程通过同样的方式注册，然后进行 ipc 通信，只需要相应的事件名称一致即可
 * ***
 * 具体实现参考： registerRemoteEventNames 和  registerRemoteEvent example export function RemoteRouter ( ) { const win = CurrentWindow ; const script = ScriptRemote ; registerRemoteEvent ( "script" , script ) ; registerRemoteEvent ( "app" , app ) ; registerRemoteEvent ( "win" , win ) ; registerRemoteEvent ( "webContents" , CurrentWindow ?. webContents ) ; registerRemoteEvent ( "dialog" , dialog ) ; }