mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-03 08:43:48 +03:00
feat: refactor achievement listeners
This commit is contained in:
parent
f5445b00f4
commit
63aee44982
@ -38,8 +38,7 @@ const getAchievements = async (
|
|||||||
return { achievementsData, unlockedAchievements };
|
return { achievementsData, unlockedAchievements };
|
||||||
};
|
};
|
||||||
|
|
||||||
const getGameAchievements = async (
|
export const getGameAchievements = async (
|
||||||
_event: Electron.IpcMainInvokeEvent,
|
|
||||||
objectId: string,
|
objectId: string,
|
||||||
shop: GameShop,
|
shop: GameShop,
|
||||||
userId?: string
|
userId?: string
|
||||||
@ -78,4 +77,13 @@ const getGameAchievements = async (
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
registerEvent("getGameAchievements", getGameAchievements);
|
const getGameAchievementsEvent = async (
|
||||||
|
_event: Electron.IpcMainInvokeEvent,
|
||||||
|
objectId: string,
|
||||||
|
shop: GameShop,
|
||||||
|
userId?: string
|
||||||
|
): Promise<GameAchievement[]> => {
|
||||||
|
return getGameAchievements(objectId, shop, userId);
|
||||||
|
};
|
||||||
|
|
||||||
|
registerEvent("getGameAchievements", getGameAchievementsEvent);
|
||||||
|
@ -2,6 +2,7 @@ import { gameAchievementRepository, gameRepository } from "@main/repository";
|
|||||||
import type { GameShop, UnlockedAchievement } from "@types";
|
import type { GameShop, UnlockedAchievement } from "@types";
|
||||||
import { WindowManager } from "../window-manager";
|
import { WindowManager } from "../window-manager";
|
||||||
import { HydraApi } from "../hydra-api";
|
import { HydraApi } from "../hydra-api";
|
||||||
|
import { getGameAchievements } from "@main/events/catalogue/get-game-achievements";
|
||||||
|
|
||||||
const saveAchievementsOnLocal = async (
|
const saveAchievementsOnLocal = async (
|
||||||
objectId: string,
|
objectId: string,
|
||||||
@ -17,11 +18,10 @@ const saveAchievementsOnLocal = async (
|
|||||||
},
|
},
|
||||||
["objectId", "shop"]
|
["objectId", "shop"]
|
||||||
)
|
)
|
||||||
.then(() => {
|
.then(async () => {
|
||||||
WindowManager.mainWindow?.webContents.send(
|
WindowManager.mainWindow?.webContents.send(
|
||||||
"on-achievement-unlocked",
|
`on-update-achievements-${objectId}-${shop}`,
|
||||||
objectId,
|
await getGameAchievements(objectId, shop as GameShop)
|
||||||
shop
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -14,6 +14,7 @@ import type {
|
|||||||
} from "@types";
|
} from "@types";
|
||||||
import type { CatalogueCategory } from "@shared";
|
import type { CatalogueCategory } from "@shared";
|
||||||
import type { AxiosProgressEvent } from "axios";
|
import type { AxiosProgressEvent } from "axios";
|
||||||
|
import { GameAchievement } from "@main/entity";
|
||||||
|
|
||||||
contextBridge.exposeInMainWorld("electron", {
|
contextBridge.exposeInMainWorld("electron", {
|
||||||
/* Torrenting */
|
/* Torrenting */
|
||||||
@ -69,6 +70,22 @@ contextBridge.exposeInMainWorld("electron", {
|
|||||||
return () =>
|
return () =>
|
||||||
ipcRenderer.removeListener("on-achievement-unlocked", listener);
|
ipcRenderer.removeListener("on-achievement-unlocked", listener);
|
||||||
},
|
},
|
||||||
|
onUpdateAchievements: (
|
||||||
|
objectId: string,
|
||||||
|
shop: GameShop,
|
||||||
|
cb: (achievements: GameAchievement[]) => void
|
||||||
|
) => {
|
||||||
|
const listener = (
|
||||||
|
_event: Electron.IpcRendererEvent,
|
||||||
|
achievements: GameAchievement[]
|
||||||
|
) => cb(achievements);
|
||||||
|
ipcRenderer.on(`on-update-achievements-${objectId}-${shop}`, listener);
|
||||||
|
return () =>
|
||||||
|
ipcRenderer.removeListener(
|
||||||
|
`on-update-achievements-${objectId}-${shop}`,
|
||||||
|
listener
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
/* User preferences */
|
/* User preferences */
|
||||||
getUserPreferences: () => ipcRenderer.invoke("getUserPreferences"),
|
getUserPreferences: () => ipcRenderer.invoke("getUserPreferences"),
|
||||||
|
@ -132,13 +132,14 @@ export function GameDetailsContextProvider({
|
|||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
window.electron.getGameStats(objectId!, shop as GameShop).then((result) => {
|
window.electron.getGameStats(objectId, shop as GameShop).then((result) => {
|
||||||
setStats(result);
|
setStats(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
window.electron
|
window.electron
|
||||||
.getGameAchievements(objectId!, shop as GameShop)
|
.getGameAchievements(objectId, shop as GameShop)
|
||||||
.then((achievements) => {
|
.then((achievements) => {
|
||||||
|
// TODO: race condition
|
||||||
setAchievements(achievements);
|
setAchievements(achievements);
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
@ -175,14 +176,11 @@ export function GameDetailsContextProvider({
|
|||||||
}, [game?.id, isGameRunning, updateGame]);
|
}, [game?.id, isGameRunning, updateGame]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsubscribe = window.electron.onAchievementUnlocked(
|
const unsubscribe = window.electron.onUpdateAchievements(
|
||||||
(objectId, shop) => {
|
objectId,
|
||||||
if (objectId !== objectId || shop !== shop) return;
|
shop,
|
||||||
|
(achievements) => {
|
||||||
window.electron
|
setAchievements(achievements);
|
||||||
.getGameAchievements(objectId!, shop as GameShop)
|
|
||||||
.then(setAchievements)
|
|
||||||
.catch(() => {});
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
5
src/renderer/src/declaration.d.ts
vendored
5
src/renderer/src/declaration.d.ts
vendored
@ -76,6 +76,11 @@ declare global {
|
|||||||
achievements?: { displayName: string; iconUrl: string }[]
|
achievements?: { displayName: string; iconUrl: string }[]
|
||||||
) => void
|
) => void
|
||||||
) => () => Electron.IpcRenderer;
|
) => () => Electron.IpcRenderer;
|
||||||
|
onUpdateAchievements: (
|
||||||
|
objectId: string,
|
||||||
|
shop: GameShop,
|
||||||
|
cb: (achievements: GameAchievement[]) => void
|
||||||
|
) => () => Electron.IpcRenderer;
|
||||||
|
|
||||||
/* Library */
|
/* Library */
|
||||||
addGameToLibrary: (
|
addGameToLibrary: (
|
||||||
|
@ -82,7 +82,6 @@ export function Sidebar() {
|
|||||||
>
|
>
|
||||||
<span>
|
<span>
|
||||||
<Link to={buildGameAchievementPath()}>Ver todas</Link>
|
<Link to={buildGameAchievementPath()}>Ver todas</Link>
|
||||||
<a></a>
|
|
||||||
</span>
|
</span>
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
|
Loading…
Reference in New Issue
Block a user