'use strict' ; import * as vscode from 'vscode' ; import * as path from 'path' ; import { BlockchainTreeItem } from './BlockchainTreeItem' ; import { BlockchainExplorerProvider } from '../BlockchainExplorerProvider' ; import { VSCodeBlockchainOutputAdapter } from '../../logging/VSCodeBlockchainOutputAdapter' ; import { ExtensionCommands } from '../../../ExtensionCommands' ; import { LogType , FabricGatewayRegistryEntry , FabricEnvironmentRegistryEntry , FabricEnvironmentRegistry } from 'ibm-blockchain-platform-common' ; import { LocalMicroEnvironment } from '../../fabric/environments/LocalMicroEnvironment' ; import { LocalMicroEnvironmentManager } from '../../fabric/environments/LocalMicroEnvironmentManager' ; export class LocalGatewayTreeItem extends BlockchainTreeItem { static async newLocalGatewayTreeItem ( provider , label , gateway , collapsableState , _runtime , command ? ) { const environmentEntry = await FabricEnvironmentRegistry . instance ( ) . get ( gateway . fromEnvironment ) ; const treeItem = new LocalGatewayTreeItem ( provider , label , gateway , collapsableState , environmentEntry , _runtime , command ) ; await treeItem . updateProperties ( ) ; return treeItem ; } iconPath : { light : string ; dark : string ; } = { light : path . join ( __filename , '..' , '..' , '..' , '..' , '..' , 'resources' , 'light' , 'organization.svg' ) , dark : path . join ( __filename , '..' , '..' , '..' , '..' , '..' , 'resources' , 'dark' , 'organization.svg' ) } ; contextValue : string = 'blockchain-local-gateway-item' ; public readonly name : string ; private runtime : LocalMicroEnvironment ; private busyTicker : NodeJS . Timer ; private busyTicks : number = 0 ; constructor ( provider , public readonly label , public gateway , public readonly collapsableState , public environmentEntry , public _runtime , public readonly command ? ) { super ( provider , label , collapsableState ) ; 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 + '  ' ; let newTooltip ; let newCommand = this . command ; if ( busy ) { this . enableBusyTicker ( ) ; const busyStates = [ '◐' , '◓' , '◑' , '◒' ] ; const currentBusyState = busyStates [ this . busyTicks % 4 ] ; newLabel += currentBusyState ; newTooltip = `${ this . name }  ${ currentBusyState }
ⓘ Associated wallet:
${ this . gateway . fromEnvironment } - ${ this . gateway . associatedWallet } Wallet` ; newCommand = null ; } else if ( running ) { this . disableBusyTicker ( ) ; newLabel += '●' ; newTooltip = `${ this . name } is running
ⓘ Associated wallet:
${ this . gateway . fromEnvironment } - ${ this . gateway . associatedWallet } Wallet` ; newCommand = { command : ExtensionCommands . CONNECT_TO_GATEWAY , title : '' , arguments : [ this . gateway ] } ; } else { this . disableBusyTicker ( ) ; newLabel += '○' ; newTooltip = `${ this . name } is not running
ⓘ Associated wallet:
${ this . gateway . fromEnvironment } - ${ this . gateway . associatedWallet } Wallet` ; newCommand = { command : ExtensionCommands . CONNECT_TO_GATEWAY , title : '' , arguments : [ this . gateway ] } ; } this . setLabel ( newLabel ) ; this . setTooltip ( newTooltip ) ; this . setCommand ( newCommand ) ; const runtimeName = this . runtime . getName ( ) ; LocalMicroEnvironmentManager . instance ( ) . updateRuntime ( runtimeName , this . runtime ) ; this . refresh ( ) ; } private setLabel ( label ) { ( this as any ) . label = label ; } private setTooltip ( tooltip ) { ( this as any ) . tooltip = tooltip ; } private setCommand ( command ) { ( this as any ) . command = command ; } 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 ; } } }