import * as vscode from 'vscode' ; import * as fs from 'fs-extra' ; import * as path from 'path' ; import { EventEmitter } from 'events' ; import { FabricWalletRegistryEntry , IFabricGatewayConnection , FabricGatewayRegistryEntry } from 'ibm-blockchain-platform-common' ; import { TransactionView } from '../webview/TransactionView' ; export class FabricGatewayConnectionManager extends EventEmitter { public static instance ( ) { return FabricGatewayConnectionManager . _instance ; } private static _instance : FabricGatewayConnectionManager = new FabricGatewayConnectionManager ( ) ; private connection : IFabricGatewayConnection ; private gatewayRegistryEntry : FabricGatewayRegistryEntry ; private walletRegistryEntry : FabricWalletRegistryEntry ; private constructor ( ) { super ( ) ; } public getConnection ( ) { return this . connection ; } public async getGatewayRegistryEntry ( ) { if ( ! this . gatewayRegistryEntry ) { return this . gatewayRegistryEntry ; } else { if ( this . gatewayRegistryEntry . fromEnvironment ) { const connectionProfilePath = this . gatewayRegistryEntry . connectionProfilePath ; const gatewayFolderPath = connectionProfilePath . substr ( 0 , connectionProfilePath . lastIndexOf ( '/' ) ) ; const configPath = path . join ( gatewayFolderPath , '.config.json' ) ; try { const gatewayConfigRegistryEntry = await fs . readJSON ( configPath ) ; return gatewayConfigRegistryEntry ; } catch ( error ) { return this . gatewayRegistryEntry ; } } else { return this . gatewayRegistryEntry ; } } } public connect ( connection , gatewayRegistryEntry , walletRegistryEntry ) { this . connection = connection ; this . gatewayRegistryEntry = gatewayRegistryEntry ; this . walletRegistryEntry = walletRegistryEntry ; this . emit ( 'connected' , connection ) ; } public disconnect ( ) { if ( this . connection ) { this . connection . disconnect ( ) ; this . connection = null ; } if ( this . gatewayRegistryEntry ) { this . gatewayRegistryEntry = null ; } this . emit ( 'disconnected' ) ; const panel = TransactionView . panel ; if ( panel ) { TransactionView . closeView ( ) ; } } public getConnectionIdentity ( ) { return this . connection . identityName ; } public getConnectionWallet ( ) { return this . walletRegistryEntry ; } }