'use strict' ; import { ProcessRepresentation , ServiceRepresentation } from '../domain' ; import { ServiceReconciler } from '../service/ServiceReconciler' ; import { EnvironmentUtil , Environment } from 'pandora-env' ; import { ProcessContextAccessor } from './ProcessContextAccessor' ; import { Facade as HubFacade } from 'pandora-hub' ; import { consoleLogger } from '../universal/LoggerBroker' ; /**
 * Class ProcessContext
 *  1. Inject the service
 *  2. manage the process's lifecycle, provide start() and stop()
 *  3. drive the lifecycle of the service
 */ export class ProcessContext { public processRepresentation : ProcessRepresentation ; public serviceReconciler : ServiceReconciler ; public processContextAccessor : ProcessContextAccessor ; private ipcHub : HubFacade ; constructor ( processRepresentation ) { this . processRepresentation = processRepresentation ; this . serviceReconciler = new ServiceReconciler ( processRepresentation , this ) ; this . processContextAccessor = new ProcessContextAccessor ( this ) ; } returns { Environment } getEnvironment ( ) { return EnvironmentUtil . getInstance ( ) . getCurrentEnvironment ( ) ; } getIPCHub ( ) { if ( ! this . ipcHub ) { this . ipcHub = new HubFacade ( ) ; this . ipcHub . setup ( { location : { appName : this . processRepresentation . appName , processName : this . processRepresentation . processName , pid : process . pid . toString ( ) } , logger : consoleLogger } ) ; } return this . ipcHub ; } param { ServiceRepresentation | ServiceRepresentation [ ] } services bindService ( services ) { if ( Array . isArray ( services ) ) { for ( const service of services ) { this . serviceReconciler . receiveServiceRepresentation ( service ) ; } return ; } this . serviceReconciler . receiveServiceRepresentation ( services ) ; } returns { Promise < void > } async start ( ) { if ( ! process . env . SKIP_IPC_HUB ) { await this . getIPCHub ( ) . start ( ) ; } await this . serviceReconciler . start ( ) ; } returns { Promise < void > } async stop ( ) { if ( ! process . env . SKIP_IPC_HUB ) { await this . getIPCHub ( ) . stop ( ) ; } await this . serviceReconciler . stop ( ) ; } }