'use strict' ; import * as vscode from 'vscode' ; import * as path from 'path' ; import { BlockchainExplorerProvider } from '../../BlockchainExplorerProvider' ; import { VSCodeBlockchainOutputAdapter } from '../../../logging/VSCodeBlockchainOutputAdapter' ; import { FabricEnvironmentTreeItem } from './FabricEnvironmentTreeItem' ; import { FabricEnvironmentRegistryEntry , LogType } from 'ibm-blockchain-platform-common' ; import { LocalMicroEnvironment } from '../../../fabric/environments/LocalMicroEnvironment' ; import { LocalMicroEnvironmentManager } from '../../../fabric/environments/LocalMicroEnvironmentManager' ; export class RuntimeTreeItem extends FabricEnvironmentTreeItem { static async newRuntimeTreeItem ( provider , label , environmentRegistryEntry , command , _runtime ) { const treeItem = new RuntimeTreeItem ( provider , label , environmentRegistryEntry , command , _runtime ) ; await treeItem . updateProperties ( ) ; return treeItem ; } contextValue : string = 'blockchain-runtime-item' ; iconPath : { light : string ; dark : string ; } = { light : path . join ( __filename , '..' , '..' , '..' , '..' , '..' , '..' , 'resources' , 'light' , 'network--3.svg' ) , dark : path . join ( __filename , '..' , '..' , '..' , '..' , '..' , '..' , 'resources' , 'dark' , 'network--3.svg' ) } ; private name : string ; private runtime : LocalMicroEnvironment ; private busyTicker : NodeJS . Timer ; private busyTicks : number = 0 ; private constructor ( provider , public readonly label , environmentRegistryEntry , public readonly command , public _runtime ) { super ( provider , label , environmentRegistryEntry , command ) ; this . name = label ; this . runtime = _runtime ; this . runtime . on ( 'busy' , ( ) => { this . safelyUpdateProperties ( ) ; } ) ; } private safelyUpdateProperties ( ) { const outputAdapter = VSCodeBlockchainOutputAdapter . instance ( ) ; this . updateProperties ( ) . catch ( ( error ) => { outputAdapter . log ( LogType . ERROR , error . message , error . toString ( ) ) ; } ) ; } private async updateProperties ( ) { const busy = this . runtime . isBusy ( ) ; const running = await this . runtime . isRunning ( ) ; let newLabel = this . name + '  ' ; if ( busy ) { this . enableBusyTicker ( ) ; const busyStates = [ '◐' , '◓' , '◑' , '◒' ] ; newLabel = `${ this . name } runtime is ${ this . runtime . getState ( ) }... ` ; newLabel += busyStates [ this . busyTicks % 4 ] ; this . tooltip = `The local development runtime is ${ this . runtime . getState ( ) }...` ; } else if ( running ) { this . disableBusyTicker ( ) ; newLabel = `${ this . name }  ●` ; this . tooltip = 'The local development runtime is running' ; } else { this . disableBusyTicker ( ) ; newLabel = `${ this . name }  ○ (click to start)` ; this . tooltip = 'Creates a local development runtime using Hyperledger Fabric Docker images' ; } this . setLabel ( newLabel ) ; LocalMicroEnvironmentManager . instance ( ) . updateRuntime ( this . name , this . runtime ) ; this . refresh ( ) ; } private setLabel ( label ) { ( this as any ) . label = label ; } private enableBusyTicker ( ) { if ( ! this . busyTicker ) { this . busyTicker = setInterval ( ( ) => { this . busyTicks ++ ; this . safelyUpdateProperties ( ) ; } , 500 ) ; this . busyTicks = 0 ; } } private disableBusyTicker ( ) { if ( this . busyTicker ) { clearInterval ( this . busyTicker ) ; this . busyTicker = null ; this . busyTicks = 0 ; } } }