import Logger from '../tools/env.logger' ; import guid from '../tools/tools.guid' ; import { IService } from '../interfaces/interface.service' ; import ServiceElectron from './service.electron' ; import { IPCMessages , Subscription } from './service.electron' ; export type TTaskCloser = ( ) => void ; class description class ServiceElectronState implements IService { private _logger : Logger = new Logger ( 'ServiceElectronState' ) ; private _history : string [ ] = [ ] ; private _tasks : Map < string , string > = new Map ( ) ; private _isReadyState : boolean = false ; private _subscriptions : { [ key ] : Subscription | undefined ; } = { history : undefined , state : undefined , } ; constructor ( ) { this . onHistoryOfHostStateRequested = this . onHistoryOfHostStateRequested . bind ( this ) ; this . onHostStateRequested = this . onHostStateRequested . bind ( this ) ; } returns public init ( ) { return new Promise ( ( resolve ) => { Promise . all ( [ ServiceElectron . IPC . subscribe ( IPCMessages . HostStateHistory , this . onHistoryOfHostStateRequested ) . then ( ( subscription ) => { this . _subscriptions . history = subscription ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe to render event "HostStateHistory" due error: ${ error . message }. This is not blocked error, loading will be continued.` ) ; } ) , ServiceElectron . IPC . subscribe ( IPCMessages . HostState , this . onHostStateRequested ) . then ( ( subscription ) => { this . _subscriptions . state = subscription ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe to render event "HostState" due error: ${ error . message }. This is not blocked error, loading will be continued.` ) ; } ) , ] ) . then ( ( ) => { resolve ( ) ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe due error: ${ error . message }. This is not blocked error, loading will be continued.` ) ; resolve ( ) ; } ) ; } ) ; } public destroy ( ) { return new Promise ( ( resolve ) => { resolve ( ) ; } ) ; } public getName ( ) { return 'ServiceElectronState' ; } public openTask ( name ) { const taskId = guid ( ) ; this . _tasks . set ( taskId , name ) ; return this . _closeTask . bind ( this , taskId ) ; } param { string } message returns public logStateToRender ( message ) { if ( typeof message === 'string' && message . trim ( ) !== '' ) { this . _history . push ( message ) ; } if ( ! ServiceElectron . IPC . available ( ) ) { this . _logger . debug ( message ) ; } else { ServiceElectron . IPC . send ( new ServiceElectron . IPCMessages . HostState ( { message : message , state : this . _isReadyState ? ServiceElectron . IPCMessages . HostState . States . ready : ServiceElectron . IPCMessages . HostState . States . working , } ) ) . catch ( ( error ) => { this . _logger . warn ( `Fait to send IPC message "HostState" to render due: ${ error . message }` ) ; } ) ; } } returns public setStateAsBusy ( ) { this . _isReadyState = false ; this . _setState ( ) ; } returns public setStateAsReady ( ) { this . _isReadyState = true ; this . _setState ( ) ; } private _setState ( ) { ServiceElectron . IPC . send ( new ServiceElectron . IPCMessages . HostState ( { state : this . _isReadyState ? ServiceElectron . IPCMessages . HostState . States . ready : ServiceElectron . IPCMessages . HostState . States . working , } ) ) . catch ( ( error ) => { this . _logger . warn ( `Fait to send IPC message "HostState" to render due: ${ error . message }` ) ; } ) ; } private _closeTask ( taskId ) { this . _tasks . delete ( taskId ) ; } param { IPCMessages . HostStateHistory } message returns private onHistoryOfHostStateRequested ( message ) { ServiceElectron . IPC . send ( new ServiceElectron . IPCMessages . HostStateHistory ( { history : this . _history , } ) ) . catch ( ( error ) => { this . _logger . warn ( `Fait to send IPC message "HostStateHistory" to render due: ${ error . message }` ) ; } ) ; } private onHostStateRequested ( message , response ) { response ( new ServiceElectron . IPCMessages . HostState ( { state : this . _isReadyState ? ServiceElectron . IPCMessages . HostState . States . ready : ServiceElectron . IPCMessages . HostState . States . working , } ) ) . catch ( ( error ) => { this . _logger . warn ( `Fait to send IPC message "HostState" to render due: ${ error . message }` ) ; } ) ; } } export default ( new ServiceElectronState ( ) ) ;