import { EventEmitter } from 'events' ; import { FabricEnvironmentRegistryEntry , IFabricEnvironmentConnection , EnvironmentType } from 'ibm-blockchain-platform-common' ; import { TimerUtil } from '../../util/TimerUtil' ; import { ExtensionCommands } from '../../../ExtensionCommands' ; export enum ConnectedState { CONNECTED , SETUP , DISCONNECTED , CONNECTING } export class FabricEnvironmentManager extends EventEmitter { public static instance ( ) { return FabricEnvironmentManager . _instance ; } private static _instance : FabricEnvironmentManager = new FabricEnvironmentManager ( ) ; private connection : IFabricEnvironmentConnection ; private environmentRegistryEntry : FabricEnvironmentRegistryEntry ; private state : ConnectedState = ConnectedState . DISCONNECTED ; private timeoutObject : NodeJS . Timeout ; private constructor ( ) { super ( ) ; } public getConnection ( ) { return this . connection ; } public getEnvironmentRegistryEntry ( ) { return this . environmentRegistryEntry ; } public getState ( ) { return this . state ; } public setState ( newState ) { this . state = newState ; } public connect ( connection , environmentRegistryEntry , state , startRefresh = true ) { this . connection = connection ; this . environmentRegistryEntry = environmentRegistryEntry ; this . state = state ; if ( startRefresh ) { this . startEnvironmentRefresh ( ) ; } this . emit ( 'connected' ) ; } public stopEnvironmentRefresh ( ) { if ( this . timeoutObject ) { TimerUtil . cancelInterval ( this . timeoutObject ) ; } } public startEnvironmentRefresh ( ) { if ( this . timeoutObject ) { TimerUtil . cancelInterval ( this . timeoutObject ) ; } if ( ( this . state === ConnectedState . CONNECTING || this . state === ConnectedState . CONNECTED ) && ( this . environmentRegistryEntry . environmentType === EnvironmentType . OPS_TOOLS_ENVIRONMENT || this . environmentRegistryEntry . environmentType === EnvironmentType . SAAS_OPS_TOOLS_ENVIRONMENT ) ) { this . timeoutObject = TimerUtil . setInterval ( ExtensionCommands . CONNECT_TO_ENVIRONMENT , [ this . environmentRegistryEntry , false ] , 10000 ) ; } } public disconnect ( ) { this . stopEnvironmentRefresh ( ) ; this . state = ConnectedState . DISCONNECTED ; if ( this . connection ) { this . connection . disconnect ( ) ; this . connection = null ; } if ( this . environmentRegistryEntry ) { this . environmentRegistryEntry = null ; } this . emit ( 'disconnected' ) ; } }