import { getLogger } from '../shared/logger' ; /**
 * Encapsulate frontend <-> backend communication
 * to be able to switch this layer later on...
 */ const { ipcRenderer } = window . electron_functions ; const log = getLogger ( 'renderer/ipc' ) ; export const ipcBackend = ipcRenderer ; export function onDCEvent ( event , cb ) { const wrapperCb = ( _ , [ data1 , data2 ] ) => { cb ( data1 , data2 ) ; } ; if ( Array . isArray ( event ) ) { event . forEach ( e => ipcBackend . on ( e , wrapperCb ) ) ; return ( ) => event . forEach ( e => ipcBackend . removeListener ( e , wrapperCb ) ) ; } else { ipcBackend . on ( event , wrapperCb ) ; return ( ) => ipcBackend . removeListener ( event , wrapperCb ) ; } } let backendLoggingStarted = false ; export function startBackendLogging ( ) { if ( backendLoggingStarted === true ) return log . error ( 'Backend logging is already started!' ) ; backendLoggingStarted = true ; ipcBackend . on ( 'ALL' , ( _e , eName , [ data1 , data2 ] ) => console . debug ( `%c📡 ${ eName }%c ${ data1 } ${ data2 }` , 'background:rgba(125,125,125,0.15);border-radius:2px;padding:2px 4px;' , 'color:grey' ) ) ; } export function sendToBackend ( event , ... args ) { log . debug ( `sendToBackend: ${ event } ${ args . join ( ' ' ) }` ) ; ipcRenderer . send ( 'ALL' , event , ... args ) ; ipcRenderer . send ( event , ... args ) ; } let callDcMethodIdentifier = 0 ; function callDcMethod ( methodName , args , cb ? ) { const identifier = callDcMethodIdentifier ++ ; if ( identifier >= Number . MAX_SAFE_INTEGER - 1 ) callDcMethodIdentifier = 0 ; const ignoreReturn = typeof cb !== 'function' ; const eventName = ignoreReturn ? 'EVENT_DC_DISPATCH' : 'EVENT_DC_DISPATCH_CB' ; sendToBackend ( eventName , identifier , methodName , args ) ; if ( ignoreReturn ) return ; const onError = ( _ev , err ) => { log . debug ( `EVENT_DD_DISPATCH_RETURN_ERR_${ identifier }_${ methodName }` , 'Got back return: ' , err ) ; removeListeners ( ) ; if ( cb ) { cb ( err , null ) ; } } ; const onSuccess = ( _ev , returnValue ) => { log . debug ( `EVENT_DD_DISPATCH_RETURN_${ identifier }_${ methodName }` , 'Got back return: [truncated]' ) ; removeListeners ( ) ; if ( cb ) { cb ( null , returnValue ) ; } } ; const removeListeners = ( ) => { ipcRenderer . removeAllListeners ( `EVENT_DD_DISPATCH_RETURN_${ identifier }_${ methodName }` ) ; ipcRenderer . removeAllListeners ( `EVENT_DD_DISPATCH_RETURN_ERR_${ identifier }_${ methodName }` ) ; } ; ipcRenderer . once ( `EVENT_DD_DISPATCH_RETURN_${ identifier }_${ methodName }` , onSuccess ) ; ipcRenderer . once ( `EVENT_DD_DISPATCH_RETURN_ERR_${ identifier }_${ methodName }` , onError ) ; } export function _callDcMethodAsync ( fnName , ... args ) { return new Promise ( ( resolve , reject ) => callDcMethod ( fnName , args , ( err , returnValue ) => { if ( err ) return reject ( err ) ; resolve ( returnValue ) ; } ) ) ; } export function saveLastChatId ( chatId ) { ipcRenderer . send ( 'saveLastChatId' , chatId ) ; } /**
 * get the last selected chats id from previous session
 */ export function getLastSelectedChatId ( ) { return ipcRenderer . sendSync ( 'getLastSelectedChatId' ) ; }