import * as vscode from 'vscode' ; import { ResultView } from './resultView' ; import { disposeAll } from './common' ; import { QueryResults } from '../common/database' ; export class ResultsManager implements vscode . WebviewPanelSerializer { private static readonly pgsqlResultContextKey = 'vscodePostgresResultFocus' ; private readonly _results : ResultView [ ] = [ ] ; private _activeResults : ResultView | undefined = undefined ; private readonly _disposables : vscode . Disposable [ ] = [ ] ; public constructor ( ) { this . _disposables . push ( vscode . window . registerWebviewPanelSerializer ( ResultView . viewType , this ) ) ; } public dispose ( ) { disposeAll ( this . _disposables ) ; disposeAll ( this . _results ) ; } public refresh ( ) { for ( const view of this . _results ) { view . refresh ( ) ; } } public showResults ( resource , viewColumn , res ) { let view = this . getExistingView ( resource ) ; if ( view ) { view . reveal ( viewColumn ) ; } else { view = this . createNewView ( resource , viewColumn ) ; } view . update ( resource , res ) ; } public get activeWinResults ( ) { if ( ! this . _activeResults ) return null ; return this . _activeResults . currentResults ; } public async deserializeWebviewPanel ( webview , state ) { const view = await ResultView . revive ( webview , state ) ; this . registerView ( view ) ; } private getExistingView ( resource ) { return this . _results . find ( view => { return view . matchesResource ( resource ) ; } ) ; } private createNewView ( resource , viewColumn ) { const view = ResultView . create ( resource , viewColumn ) ; this . _activeResults = view ; return this . registerView ( view ) ; } private registerView ( view ) { this . _results . push ( view ) ; view . onDispose ( ( ) => { const existing = this . _results . indexOf ( view ) ; if ( existing === - 1 ) return ; this . _results . splice ( existing , 1 ) ; if ( this . _activeResults === view ) { this . _activeResults = undefined ; } } ) ; view . onDidChangeViewState ( ( { webviewPanel } ) => { disposeAll ( this . _results . filter ( otherView => view !== otherView && view ! . matches ( otherView ) ) ) ; vscode . commands . executeCommand ( 'setContext' , ResultsManager . pgsqlResultContextKey , webviewPanel . visible && webviewPanel . active ) ; this . _activeResults = webviewPanel . active ? view : undefined ; } ) ; return view ; } }