import Logger from '../tools/env.logger' ; import ServiceElectron from './service.electron' ; import ServiceProduction from './service.production' ; import ServiceEnv from './service.env' ; import { IService } from '../interfaces/interface.service' ; import { IPCMessages , Subscription } from './service.electron' ; class description class ServiceLogs implements IService { private _logger : Logger = new Logger ( 'ServiceLogs' ) ; private _subscriptions : { [ key ] : Subscription ; } = { } ; returns public init ( ) { return new Promise ( ( resolve , reject ) => { Promise . all ( [ ServiceElectron . IPC . subscribe ( IPCMessages . ChipmunkDevModeRequest , this . _ipc_onChipmunkDevModeRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . ChipmunkDevModeRequest = subscription ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe to render event "ChipmunkDevModeRequest" due error: ${ error . message }. This is not blocked error, loading will be continued.` ) ; } ) , ServiceElectron . IPC . subscribe ( IPCMessages . ChipmunkLogLevelRequest , this . _ipc_onChipmunkLogLevelRequest . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . ChipmunkLogLevelRequest = subscription ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe to render event "ChipmunkLogLevelRequest" due error: ${ error . message }. This is not blocked error, loading will be continued.` ) ; } ) , ServiceElectron . IPC . subscribe ( IPCMessages . ChipmunkClientLog , this . _ipc_onChipmunkClientLog . bind ( this ) ) . then ( ( subscription ) => { this . _subscriptions . ChipmunkClientLog = subscription ; } ) . catch ( ( error ) => { this . _logger . warn ( `Fail to subscribe to render event "ChipmunkClientLog" due error: ${ error . message }. This is not blocked error, loading will be continued.` ) ; } ) , ] ) . then ( ( ) => { resolve ( ) ; } ) ; } ) ; } public destroy ( ) { return new Promise ( ( resolve ) => { Object . keys ( this . _subscriptions ) . forEach ( ( key ) => { this . _subscriptions [ key ] . destroy ( ) ; } ) ; resolve ( ) ; } ) ; } public getName ( ) { return 'ServiceLogs' ; } private _ipc_onChipmunkDevModeRequest ( req , response ) { response ( new IPCMessages . ChipmunkDevModeResponse ( { production : ServiceProduction . isProduction ( ) } ) ) . catch ( ( err ) => { this . _logger . warn ( `Fail to send ChipmunkDevModeResponse due error: ${ err . message }` ) ; } ) ; } private _ipc_onChipmunkLogLevelRequest ( req , response ) { response ( new IPCMessages . ChipmunkLogLevelResponse ( { level : ServiceProduction . getLogLevel ( ) as IPCMessages . ELogLevels } ) ) . catch ( ( err ) => { this . _logger . warn ( `Fail to send ChipmunkLogLevelResponse due error: ${ err . message }` ) ; } ) ; } private _ipc_onChipmunkClientLog ( req , response ) { const request = req as IPCMessages . ChipmunkClientLog ; if ( ! ServiceEnv . get ( ) . CHIPMUNK_NO_RENDER_LOGS && typeof request . msg === 'string' && request . msg . trim ( ) !== '' ) { this . _logger . publish ( 'RENDER: ' + request . msg , request . level ) ; } } } export default ( new ServiceLogs ( ) ) ;