Skip to content

Commit

Permalink
fix: useWindow wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbericoD committed Jan 9, 2024
1 parent 5bce3a6 commit 7554ca8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
21 changes: 8 additions & 13 deletions src/hooks/useWindow/useWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ export const useWindow = (
name: string,
shouldDisplayLog = false,
listenToWindowStateChanges = false
): [
(overwolf.windows.WindowInfo & WindowBehavior) | undefined,
overwolf.windows.WindowStateChangedEvent | undefined,
() => Promise<void>
] => {
) => {
const [owWindow, setOwWindow] = useState<
overwolf.windows.WindowInfo & WindowBehavior
>();
Expand All @@ -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),
Expand All @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/useWindow/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ function standardWindowBehavior(
id: string,
behavior: Behavior
): Promise<overwolf.windows.WindowIdResult> {
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);
Expand Down

0 comments on commit 7554ca8

Please sign in to comment.