perf: WebView2 HostObject 프록시 캐싱으로 성능 개선

- communication.ts에서 machine 프록시를 모듈 로드 시 한 번만 초기화
- 10개 API 메서드에서 매번 hostObjects.machine 접근하던 오버헤드 제거

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
backuppc
2025-11-25 15:58:42 +09:00
parent 362263ab05
commit a40cecc846

View File

@@ -2,6 +2,9 @@
// Check if running in WebView2
const isWebView = typeof window !== 'undefined' && !!window.chrome?.webview;
// 비동기 프록시 캐싱 (한 번만 초기화) - 매번 접근 시 오버헤드 제거
const machine = isWebView ? window.chrome!.webview!.hostObjects.machine : null;
type MessageCallback = (data: any) => void;
class CommunicationLayer {
@@ -70,8 +73,8 @@ class CommunicationLayer {
// --- API Methods ---
public async getConfig(): Promise<string> {
if (isWebView) {
return await window.chrome!.webview!.hostObjects.machine.GetConfig();
if (isWebView && machine) {
return await machine.GetConfig();
} else {
// WebSocket Request/Response Pattern
return new Promise((resolve, reject) => {
@@ -103,8 +106,8 @@ class CommunicationLayer {
}
public async getIOList(): Promise<string> {
if (isWebView) {
return await window.chrome!.webview!.hostObjects.machine.GetIOList();
if (isWebView && machine) {
return await machine.GetIOList();
} else {
return new Promise((resolve, reject) => {
if (!this.isConnected) {
@@ -132,8 +135,8 @@ class CommunicationLayer {
}
public async getRecipeList(): Promise<string> {
if (isWebView) {
return await window.chrome!.webview!.hostObjects.machine.GetRecipeList();
if (isWebView && machine) {
return await machine.GetRecipeList();
} else {
return new Promise((resolve, reject) => {
if (!this.isConnected) {
@@ -161,40 +164,40 @@ class CommunicationLayer {
}
public async saveConfig(configJson: string): Promise<void> {
if (isWebView) {
await window.chrome!.webview!.hostObjects.machine.SaveConfig(configJson);
if (isWebView && machine) {
await machine.SaveConfig(configJson);
} else {
this.ws?.send(JSON.stringify({ type: 'SAVE_CONFIG', data: JSON.parse(configJson) }));
}
}
public async sendControl(command: string) {
if (isWebView) {
await window.chrome!.webview!.hostObjects.machine.SystemControl(command);
if (isWebView && machine) {
await machine.SystemControl(command);
} else {
this.ws?.send(JSON.stringify({ type: 'CONTROL', command }));
}
}
public async moveAxis(axis: string, value: number) {
if (isWebView) {
await window.chrome!.webview!.hostObjects.machine.MoveAxis(axis, value);
if (isWebView && machine) {
await machine.MoveAxis(axis, value);
} else {
this.ws?.send(JSON.stringify({ type: 'MOVE', axis, value }));
}
}
public async setIO(id: number, state: boolean) {
if (isWebView) {
await window.chrome!.webview!.hostObjects.machine.SetIO(id, false, state);
if (isWebView && machine) {
await machine.SetIO(id, false, state);
} else {
this.ws?.send(JSON.stringify({ type: 'SET_IO', id, state }));
}
}
public async selectRecipe(recipeId: string): Promise<{ success: boolean; message: string; recipeId?: string }> {
if (isWebView) {
const resultJson = await window.chrome!.webview!.hostObjects.machine.SelectRecipe(recipeId);
if (isWebView && machine) {
const resultJson = await machine.SelectRecipe(recipeId);
return JSON.parse(resultJson);
} else {
return new Promise((resolve, reject) => {
@@ -223,8 +226,8 @@ class CommunicationLayer {
}
public async copyRecipe(recipeId: string, newName: string): Promise<{ success: boolean; message: string; newRecipe?: any }> {
if (isWebView) {
const resultJson = await window.chrome!.webview!.hostObjects.machine.CopyRecipe(recipeId, newName);
if (isWebView && machine) {
const resultJson = await machine.CopyRecipe(recipeId, newName);
return JSON.parse(resultJson);
} else {
return new Promise((resolve, reject) => {
@@ -253,8 +256,8 @@ class CommunicationLayer {
}
public async deleteRecipe(recipeId: string): Promise<{ success: boolean; message: string; recipeId?: string }> {
if (isWebView) {
const resultJson = await window.chrome!.webview!.hostObjects.machine.DeleteRecipe(recipeId);
if (isWebView && machine) {
const resultJson = await machine.DeleteRecipe(recipeId);
return JSON.parse(resultJson);
} else {
return new Promise((resolve, reject) => {