Skip to content

Commit 6566d24

Browse files
committed
finish refactor
1 parent b5801e2 commit 6566d24

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

packages/core/src/applicationcomposer/composerWebview.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,43 @@
55

66
import { join } from 'path'
77
import * as nls from 'vscode-nls'
8-
const localize = nls.loadMessageBundle()
98
import * as path from 'path'
109
import * as vscode from 'vscode'
1110
import { handleMessage } from './handleMessage'
1211
import { FileWatchInfo } from './types'
1312

13+
const localize = nls.loadMessageBundle()
14+
1415
export 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(

packages/core/src/applicationcomposer/webviewManager.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class ApplicationComposerManager {
4343
}
4444
}
4545

46-
private getWebviewContent = () => {
46+
private getWebviewContent = async () => {
4747
if (!this.webviewHtml) {
4848
void this.fetchWebviewHtml()
4949
return ''
@@ -78,7 +78,11 @@ export class ApplicationComposerManager {
7878

7979
// Existing visualization does not exist, construct new visualization
8080
try {
81-
const newVisualization = new ApplicationComposer(document, this.extensionContext, this.getWebviewContent)
81+
const newVisualization = await ApplicationComposer.create(
82+
document,
83+
this.extensionContext,
84+
this.getWebviewContent
85+
)
8286
this.handleNewVisualization(document.uri.fsPath, newVisualization)
8387

8488
if (vscode.version === '1.91.0') {
@@ -101,7 +105,11 @@ export class ApplicationComposerManager {
101105
const document = await vscode.workspace.openTextDocument({
102106
language: 'yaml',
103107
})
104-
const newVisualization = new ApplicationComposer(document, this.extensionContext, this.getWebviewContent)
108+
const newVisualization = await ApplicationComposer.create(
109+
document,
110+
this.extensionContext,
111+
this.getWebviewContent
112+
)
105113
this.handleNewVisualization(document.uri.fsPath, newVisualization)
106114

107115
return newVisualization.getPanel()

0 commit comments

Comments
 (0)