import * as vscode from 'vscode' ; import * as fs from 'fs' ; import * as path from 'path' ; class Session extends vscode . TreeItem { public label : string ; public state : string ; public name : string ; public terminal : vscode . Terminal ; public creds ; public environmentId ; constructor ( public region , public collapsibleState , public command ? ) { super ( "<new session>" , collapsibleState ) ; this . region = region ; this . label = region + " - <new session> (connecting)" ; this . state = "CONNECTING" ; } setSessionName ( name ) { this . name = name ; this . label = this . region + " - " + this . name + " (connecting)" ; } setConnected ( ) { this . state = "CONNECTED" ; this . contextValue = "connectedSession" ; this . label = this . region + " - " + this . name + " (connected)" ; } setTerminal ( terminal ) { this . terminal = terminal ; vscode . window . activeColorTheme . kind ; } setCreds ( creds ) { this . creds = creds ; } setEnvironmentId ( environmentId ) { this . environmentId = environmentId ; } iconPath = path . join ( __filename , '..' , '..' , 'resources' , 'icons' , ( vscode . window . activeColorTheme . kind == vscode . ColorThemeKind . Light ? 'session-light.png' : 'session-dark.png' ) ) ; contextValue = 'connectingSession' ; } export class SessionProvider implements vscode . TreeDataProvider < Session > { private _onDidChangeTreeData : vscode . EventEmitter < Session | undefined > = new vscode . EventEmitter < Session | undefined > ( ) ; readonly onDidChangeTreeData : vscode . Event < Session | undefined > = this . _onDidChangeTreeData . event ; public sessions : Session [ ] ; private uploadCmdDisposable ; constructor ( ) { this . sessions = [ ] ; } addSession ( region ) { let session = new Session ( region , vscode . TreeItemCollapsibleState . None ) ; this . sessions . push ( session ) ; this . refresh ( ) ; return session ; } clearAll ( ) { this . sessions = [ ] ; this . refresh ( ) ; } refresh ( ) { this . _onDidChangeTreeData . fire ( null ) ; } getTreeItem ( element ) { return element ; } getChildren ( element ? ) { return new Promise ( resolve => { resolve ( this . sessions ) ; } ) ; } getLastSession ( ) { return this . sessions [ this . sessions . length - 1 ] ; } onError ( ) { setTimeout ( ( that ) => { for ( let i = 0 ; i < that . sessions . length ; i ++ ) { if ( ! that . sessions [ i ] . terminal || ( that . sessions [ i ] . terminal . exitStatus && that . sessions [ i ] . terminal . exitStatus . code ) ) { delete that . sessions [ i ] ; } } that . refresh ( ) ; } , 1000 , this ) ; } onTerminalDisposed ( terminal ) { let found = false ; for ( let i = 0 ; i < this . sessions . length ; i ++ ) { if ( this . sessions [ i ] . terminal && this . sessions [ i ] . terminal . processId == terminal . processId ) { delete this . sessions [ i ] ; found = true ; } } this . refresh ( ) ; if ( ! found ) { this . onError ( ) ; } } }