55
66import { join } from 'path'
77import * as nls from 'vscode-nls'
8- const localize = nls . loadMessageBundle ( )
98import * as path from 'path'
109import * as vscode from 'vscode'
1110import { handleMessage } from './handleMessage'
1211import { FileWatchInfo } from './types'
1312
13+ const localize = nls . loadMessageBundle ( )
14+
1415export class ApplicationComposer {
1516 public readonly documentUri : vscode . Uri
16- public webviewPanel : vscode . WebviewPanel
17+ public webviewPanel : vscode . WebviewPanel = { } as vscode . WebviewPanel
1718 protected readonly disposables : vscode . Disposable [ ] = [ ]
1819 protected isPanelDisposed = false
1920 private readonly onVisualizationDisposeEmitter = new vscode . EventEmitter < void > ( )
2021 public workSpacePath : string
2122 public defaultTemplatePath : string
2223 public defaultTemplateName : string
23- // fileWatches is used to monitor template file changes and achieve bi-direction sync
24- public fileWatches : Record < string , FileWatchInfo >
25- private getWebviewContent : ( ) => string
2624
27- public constructor (
25+ private constructor (
2826 textDocument : vscode . TextDocument ,
29- context : vscode . ExtensionContext ,
30- getWebviewContent : ( ) => string
27+ private getWebviewContent : ( ) => Promise < string > ,
28+ public fileWatches : Record < string , FileWatchInfo > = { } // fileWatches is used to monitor template file changes and achieve bi-direction sync
3129 ) {
3230 this . getWebviewContent = getWebviewContent
3331 this . documentUri = textDocument . uri
34- this . webviewPanel = this . setupWebviewPanel ( textDocument , context )
3532 this . workSpacePath = path . dirname ( textDocument . uri . fsPath )
3633 this . defaultTemplatePath = textDocument . uri . fsPath
3734 this . defaultTemplateName = path . basename ( this . defaultTemplatePath )
38- this . fileWatches = { }
35+ }
36+
37+ public static async create (
38+ textDocument : vscode . TextDocument ,
39+ context : vscode . ExtensionContext ,
40+ getWebviewContent : ( ) => Promise < string >
41+ ) {
42+ const obj = new ApplicationComposer ( textDocument , getWebviewContent )
43+ obj . webviewPanel = await obj . setupWebviewPanel ( textDocument , context )
44+ return obj
3945 }
4046
4147 public get onVisualizationDisposeEvent ( ) : vscode . Event < void > {
@@ -56,25 +62,25 @@ export class ApplicationComposer {
5662 if ( ! this . isPanelDisposed ) {
5763 this . webviewPanel . dispose ( )
5864 const document = await vscode . workspace . openTextDocument ( this . documentUri )
59- this . webviewPanel = this . setupWebviewPanel ( document , context )
65+ this . webviewPanel = await this . setupWebviewPanel ( document , context )
6066 }
6167 }
6268
6369 protected getText ( textDocument : vscode . TextDocument ) : string {
6470 return textDocument . getText ( )
6571 }
6672
67- private setupWebviewPanel (
73+ private async setupWebviewPanel (
6874 textDocument : vscode . TextDocument ,
6975 context : vscode . ExtensionContext
70- ) : vscode . WebviewPanel {
76+ ) : Promise < vscode . WebviewPanel > {
7177 const documentUri = textDocument . uri
7278
7379 // Create and show panel
7480 const panel = this . createVisualizationWebviewPanel ( documentUri , context )
7581
7682 // Set the initial html for the webpage
77- panel . webview . html = this . getWebviewContent ( )
83+ panel . webview . html = await this . getWebviewContent ( )
7884
7985 // Handle messages from the webview
8086 this . disposables . push (
0 commit comments