feat: dispatching event when remote games are fetched

This commit is contained in:
Chubby Granny Chaser 2024-06-19 02:35:57 +01:00
parent ca81281f1f
commit 944f3891bf
No known key found for this signature in database
8 changed files with 31 additions and 17 deletions

View File

@ -10,7 +10,7 @@ import { fetchDownloadSourcesAndUpdate } from "./helpers";
import { publishNewRepacksNotifications } from "./services/notifications"; import { publishNewRepacksNotifications } from "./services/notifications";
import { MoreThan } from "typeorm"; import { MoreThan } from "typeorm";
import { HydraApi } from "./services/hydra-api"; import { HydraApi } from "./services/hydra-api";
import { getRemoteGames, uploadBatchGames } from "./services/library-sync"; import { uploadGamesBatch } from "./services/library-sync";
startMainLoop(); startMainLoop();
@ -23,10 +23,7 @@ const loadState = async (userPreferences: UserPreferences | null) => {
RealDebridClient.authorize(userPreferences?.realDebridApiToken); RealDebridClient.authorize(userPreferences?.realDebridApiToken);
HydraApi.setupApi().then(async () => { HydraApi.setupApi().then(async () => {
if (HydraApi.isLoggedIn()) { if (HydraApi.isLoggedIn()) uploadGamesBatch();
await uploadBatchGames();
getRemoteGames();
}
}); });
const [nextQueueItem] = await downloadQueueRepository.find({ const [nextQueueItem] = await downloadQueueRepository.find({

View File

@ -2,7 +2,7 @@ import { userAuthRepository } from "@main/repository";
import axios, { AxiosError, AxiosInstance } from "axios"; import axios, { AxiosError, AxiosInstance } from "axios";
import { WindowManager } from "./window-manager"; import { WindowManager } from "./window-manager";
import url from "url"; import url from "url";
import { getRemoteGames, uploadBatchGames } from "./library-sync"; import { uploadGamesBatch } from "./library-sync";
export class HydraApi { export class HydraApi {
private static instance: AxiosInstance; private static instance: AxiosInstance;
@ -50,9 +50,7 @@ export class HydraApi {
if (WindowManager.mainWindow) { if (WindowManager.mainWindow) {
WindowManager.mainWindow.webContents.send("on-signin"); WindowManager.mainWindow.webContents.send("on-signin");
uploadGamesBatch();
await uploadBatchGames();
await getRemoteGames();
} }
} }

View File

@ -1,4 +1,4 @@
export * from "./get-remote-games"; export * from "./merge-with-remote-games";
export * from "./upload-batch-games"; export * from "./upload-games-batch";
export * from "./update-game-playtime"; export * from "./update-game-playtime";
export * from "./create-game"; export * from "./create-game";

View File

@ -5,7 +5,7 @@ import { getSteamAppAsset } from "@main/helpers";
import { logger } from "../logger"; import { logger } from "../logger";
import { AxiosError } from "axios"; import { AxiosError } from "axios";
export const getRemoteGames = async () => { export const mergeWithRemoteGames = async () => {
try { try {
const games = await HydraApi.get("/games"); const games = await HydraApi.get("/games");

View File

@ -5,13 +5,17 @@ import { HydraApi } from "../hydra-api";
import { logger } from "../logger"; import { logger } from "../logger";
import { AxiosError } from "axios"; import { AxiosError } from "axios";
export const uploadBatchGames = async () => { import { mergeWithRemoteGames } from "./merge-with-remote-games";
import { WindowManager } from "../window-manager";
export const uploadGamesBatch = async () => {
try { try {
const games = await gameRepository.find({ const games = await gameRepository.find({
where: { remoteId: IsNull(), isDeleted: false }, where: { remoteId: IsNull(), isDeleted: false },
}); });
const gamesChunks = chunk(games, 200); const gamesChunks = chunk(games, 200);
for (const chunk of gamesChunks) { for (const chunk of gamesChunks) {
await HydraApi.post( await HydraApi.post(
"/games/batch", "/games/batch",
@ -25,11 +29,16 @@ export const uploadBatchGames = async () => {
}) })
); );
} }
await mergeWithRemoteGames();
if (WindowManager.mainWindow)
WindowManager.mainWindow.webContents.send("on-library-batch-complete");
} catch (err) { } catch (err) {
if (err instanceof AxiosError) { if (err instanceof AxiosError) {
logger.error("uploadBatchGames", err.response, err.message); logger.error("uploadGamesBatch", err.response, err.message);
} else { } else {
logger.error("uploadBatchGames", err); logger.error("uploadGamesBatch", err);
} }
} }
}; };

View File

@ -96,6 +96,12 @@ contextBridge.exposeInMainWorld("electron", {
ipcRenderer.on("on-game-close", listener); ipcRenderer.on("on-game-close", listener);
return () => ipcRenderer.removeListener("on-game-close", listener); return () => ipcRenderer.removeListener("on-game-close", listener);
}, },
onLibraryBatchComplete: (cb: () => void) => {
const listener = (_event: Electron.IpcRendererEvent) => cb();
ipcRenderer.on("on-library-batch-complete", listener);
return () =>
ipcRenderer.removeListener("on-library-batch-complete", listener);
},
/* Hardware */ /* Hardware */
getDiskFreeSpace: (path: string) => getDiskFreeSpace: (path: string) =>

View File

@ -81,6 +81,9 @@ export function App() {
window.electron.onSignIn(() => { window.electron.onSignIn(() => {
updateUser(); updateUser();
}), }),
window.electron.onLibraryBatchComplete(() => {
updateLibrary();
}),
window.electron.onSignOut(() => { window.electron.onSignOut(() => {
clearUser(); clearUser();
}), }),
@ -89,7 +92,7 @@ export function App() {
return () => { return () => {
listeners.forEach((unsubscribe) => unsubscribe()); listeners.forEach((unsubscribe) => unsubscribe());
}; };
}, [updateUser, clearUser]); }, [updateUser, updateLibrary, clearUser]);
const handleSearch = useCallback( const handleSearch = useCallback(
(query: string) => { (query: string) => {

View File

@ -73,6 +73,7 @@ declare global {
getGameByObjectID: (objectID: string) => Promise<Game | null>; getGameByObjectID: (objectID: string) => Promise<Game | null>;
onPlaytime: (cb: (gameId: number) => void) => () => Electron.IpcRenderer; onPlaytime: (cb: (gameId: number) => void) => () => Electron.IpcRenderer;
onGameClose: (cb: (gameId: number) => void) => () => Electron.IpcRenderer; onGameClose: (cb: (gameId: number) => void) => () => Electron.IpcRenderer;
onLibraryBatchComplete: (cb: () => void) => () => Electron.IpcRenderer;
/* User preferences */ /* User preferences */
getUserPreferences: () => Promise<UserPreferences | null>; getUserPreferences: () => Promise<UserPreferences | null>;
@ -111,7 +112,7 @@ declare global {
checkForUpdates: () => Promise<boolean>; checkForUpdates: () => Promise<boolean>;
restartAndInstallUpdate: () => Promise<void>; restartAndInstallUpdate: () => Promise<void>;
/* Authg */ /* Auth */
signOut: () => Promise<void>; signOut: () => Promise<void>;
onSignIn: (cb: () => void) => () => Electron.IpcRenderer; onSignIn: (cb: () => void) => () => Electron.IpcRenderer;
onSignOut: (cb: () => void) => () => Electron.IpcRenderer; onSignOut: (cb: () => void) => () => Electron.IpcRenderer;