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);