import { contextBridge , ipcRenderer } from 'electron' ; import { WindowMetadata } from '../common/types' ; import { Events , Invokables , InternalInvokables , WindowApi , } from '../common/ipc' ; import { NeovimState } from '../main/neovim/state' ; let nvimState = undefined ; let homeDir = '' ; const onReady = new Promise ( ( resolve , _ ) => ipcRenderer . on ( Events . invokeHandlersReady , ( ) => resolve ( null ) ) ) ; ipcRenderer . on ( Events . nvimState , ( _event , state ) => ( nvimState = state ) ) ; ipcRenderer . on ( Events . homeDir , ( _event , dir ) => ( homeDir = dir ) ) ; const api = { onRedrawEvent : ( evt , fn ) => { ipcRenderer . on ( evt , ( _evt , ... args ) => fn ( ... args ) ) ; } , isMacos : process . platform === 'darwin' , homeDir , setWinTitle : ( newTitle ) => ipcRenderer . invoke ( InternalInvokables . setWinTitle , newTitle ) , luaeval : async ( ... args ) => { await onReady ; return ipcRenderer . invoke ( InternalInvokables . luaeval , ... args ) ; } , on : ( event , func ) => { if ( Object . values ( Events ) . indexOf ( event ) > - 1 ) { ipcRenderer . on ( event , ( _event , ... args ) => { func ( ... args ) ; } ) ; } else { const message = `Tried to handle event ${ event } that isn't a valid event: this should NOT happen` ; throw new Error ( message ) ; } } , stealInput : ( fn ) => { ipcRenderer . invoke ( InternalInvokables . stealInput ) . then ( ( [ inputKeys , inputType ] ) => fn ( inputKeys , inputType ) ) ; } , restoreInput : ( ) => ipcRenderer . invoke ( InternalInvokables . restoreInput ) , gitOnBranch : ( fn ) => ipcRenderer . on ( Events . gitOnBranch , ( _ , b ) => fn ( b ) ) , gitOnStatus : ( fn ) => ipcRenderer . on ( Events . gitOnStatus , ( _ , s ) => fn ( s ) ) , nvimWatchState : ( key , fn ) => ipcRenderer . on ( Events . nvimState , ( _event , newState ) => fn ( newState [ key ] ) ) , nvimState : ( ) => { if ( ! nvimState ) { throw new Error ( 'Umm . . . yeah this should be defined, nvimState, preload' ) ; } return nvimState ; } , getWindowMetadata : async ( ) : Promise < WindowMetadata [ ] > => { await onReady ; return await ipcRenderer . invoke ( Invokables . getWindowMetadata ) ; } , invoke : async ( invokable , ... args ) => { await onReady ; if ( Object . values ( Invokables ) . indexOf ( invokable ) > - 1 ) { return await ipcRenderer . invoke ( invokable , ... args ) ; } else { const message = `Tried to call invokable ${ invokable } that isn't valid: this should NOT happen` ; throw new Error ( message ) ; } } , } ; contextBridge . exposeInMainWorld ( 'api' , api ) ;