From 7554ca8a183b8ec5c5ff9cc411b43e2a9ff966ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alb=C3=A9rico=20Dias=20Barreto=20Filho?= Date: Tue, 9 Jan 2024 14:29:12 -0300 Subject: [PATCH] fix: useWindow wrapper --- src/hooks/useWindow/useWindow.ts | 21 ++++++++------------- src/hooks/useWindow/wrappers.ts | 3 +-- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/hooks/useWindow/useWindow.ts b/src/hooks/useWindow/useWindow.ts index 74db95b..88b304b 100644 --- a/src/hooks/useWindow/useWindow.ts +++ b/src/hooks/useWindow/useWindow.ts @@ -14,11 +14,7 @@ export const useWindow = ( name: string, shouldDisplayLog = false, listenToWindowStateChanges = false -): [ - (overwolf.windows.WindowInfo & WindowBehavior) | undefined, - overwolf.windows.WindowStateChangedEvent | undefined, - () => Promise -] => { +) => { const [owWindow, setOwWindow] = useState< overwolf.windows.WindowInfo & WindowBehavior >(); @@ -43,15 +39,10 @@ export const useWindow = ( ); } - const { id, ...windowInfo } = await obtainWindow(name); - if (!id) { - throw new Error(`Failed to obtain window ${name}`); - } - const bindedWindow = standardWindowBehavior.bind(null, id); - const updatedWindowInfo = actions.reduce((currentAction, action) => { currentAction[action] = async () => { - const actionResult = await bindedWindow(action); + await obtainWindow(name); + const actionResult = await standardWindowBehavior(name, action); if (shouldDisplayLog) { log( JSON.stringify(actionResult, null, 2), @@ -64,11 +55,15 @@ export const useWindow = ( return currentAction; }, {} as WindowBehavior); + const windowInfo = await obtainWindow(name); + if (!windowInfo) { + throw new Error(`Failed to obtain window ${name}`); + } + setOwWindow((prev) => ({ ...(prev || {}), ...windowInfo, ...updatedWindowInfo, - id, })); } catch (e) { const errorMessage = error( diff --git a/src/hooks/useWindow/wrappers.ts b/src/hooks/useWindow/wrappers.ts index c5ec880..ec57748 100644 --- a/src/hooks/useWindow/wrappers.ts +++ b/src/hooks/useWindow/wrappers.ts @@ -11,8 +11,7 @@ function standardWindowBehavior( id: string, behavior: Behavior ): Promise { - return new Promise(async (resolve, reject) => { - await obtainWindow(id); + return new Promise((resolve, reject) => { overwolf.windows[behavior](id, (result) => { if (result.success) resolve(result); else reject(result);