diff --git a/frontend/communication.ts b/frontend/communication.ts index 54ad236..d4e5363 100644 --- a/frontend/communication.ts +++ b/frontend/communication.ts @@ -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 { - 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 { - 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 { - 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 { - 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) => {