Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create useLauncherEventProvider hook #22

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "overwolf-hooks",
"version": "4.1.5",
"version": "4.2.0",
"description": "custom hooks to help use overwolf api with the new react hooks technology.",
"author": "AlbericoD",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useLauncherEventProvider/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./useLauncherEventProvider";
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useLauncherEventProvider } from "./useLauncherEventProvider";
import { renderHook } from "@testing-library/react";

describe("useGameEventProvider values", () => {
it("should return false when there are no required features", () => {
const { result } = renderHook(() =>
useLauncherEventProvider(
-1,
{ onInfoUpdates: () => {}, onNewEvents: () => {} },
[]
)
);
const { started } = result.current;
expect(started).toBeFalsy();
});
});
136 changes: 136 additions & 0 deletions src/hooks/useLauncherEventProvider/useLauncherEventProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { useCallback, useState } from "react";
import { sleep } from "../../lib/utils";
import { log } from "../../lib/log";

interface UseLauncherEventProviderEventsDelegate {
onInfoUpdates(
info: any
): void;
onNewEvents(events: any): void;
Comment on lines +7 to +9
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love using any here but it's what overwolf sends

https://github.com/overwolf/types/blob/master/overwolf.d.ts#L2772-L2782

}
const getInfo = (launcherClassId: number): Promise<overwolf.games.launchers.events.GetInfoResult<any>> => {
return new Promise((resolve) => {
overwolf.games.launchers.events.getInfo(launcherClassId, (info) => resolve(info));
});
};
export function useLauncherEventProvider(
launcherClassId: number,
delegate: UseLauncherEventProviderEventsDelegate,
requiredFeatures: Array<string>,
featureRetries = 10,
displayLog = false
) {
const [started, setStarted] = useState(false);

const onInfoUpdates = useCallback(
(
info: UseLauncherEventProviderEventsDelegate["onInfoUpdates"]["arguments"]
): void => {
delegate.onInfoUpdates(info);
if (displayLog) {
log(
JSON.stringify(info, null, 2),
"useLauncherEventProvider.ts",
"onInfoUpdates() -> delegate"
);
}
},
[delegate, displayLog]
);

const onNewEvents = useCallback(
(
events: UseLauncherEventProviderEventsDelegate["onNewEvents"]["arguments"]
): void => {
delegate.onNewEvents(events);
if (displayLog) {
log(
JSON.stringify(events, null, 2),
"useLauncherEventProvider.ts",
"onNewEvents() -> delegate"
);
}
},
[delegate, displayLog]
);

const unRegisterEvents = (): void => {
overwolf.games.launchers.events.onInfoUpdates.removeListener(onInfoUpdates);
overwolf.games.launchers.events.onNewEvents.removeListener(onNewEvents);
};
const registerEvents = (): void => {
unRegisterEvents();
overwolf.games.launchers.events.onInfoUpdates.addListener(onInfoUpdates);
overwolf.games.launchers.events.onNewEvents.addListener(onNewEvents);
};

const setRequiredFeatures = useCallback(async () => {
if (!requiredFeatures.length) return setStarted(false);
let tries: number = 1;
let result: overwolf.games.launchers.events.SetRequiredFeaturesResult = {
success: false,
supportedFeatures: [],
};

while (tries <= featureRetries) {
log(
`try ${tries} of ${featureRetries}`,
"useLauncherEventProvider.ts",
"setRequiredFeatures() -> callback -> try"
);
result = await new Promise((resolve) => {
overwolf.games.launchers.events.setRequiredFeatures(
launcherClassId,
requiredFeatures,
(requiredResult) => resolve(requiredResult)
);
});

if (result.success) {
log(
JSON.stringify(result, null, 2),
"useLauncherEventProvider.ts",
"setRequiredFeatures() -> callback -> success"
);
const isSupported =
Array.isArray(result.supportedFeatures) &&
result.supportedFeatures.length > 0;

setStarted(isSupported);
return void 0;
}

await sleep(3000);
tries++;
}
log(
JSON.stringify(result, null, 2),
"useLauncherEventProvider.ts",
"setRequiredFeatures() -> callback -> failure"
);

setStarted(false);
return void 0;
}, [requiredFeatures]);

const start = useCallback(async (): Promise<void> => {
if (started) return;

registerEvents();

await setRequiredFeatures();

const { res, success } = await getInfo(launcherClassId);

if (res && success) {
onInfoUpdates(res);
}
}, [setRequiredFeatures, onInfoUpdates, started, registerEvents]);

const stop = useCallback((): void => {
setStarted(false);
unRegisterEvents();
}, [unRegisterEvents]);

return { started, start, stop } as const;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./hooks/useGameEventProvider/useGameEventProvider";
export * from "./hooks/useLauncherEventProvider/useLauncherEventProvider";
export * from "./hooks/useRunningGame/useRunningGame";
export * from "./hooks/useWindow";
export * from "./hooks/useDrag/useDrag";