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

View File

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

View File

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

View File

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

View File

@ -5,13 +5,17 @@ import { HydraApi } from "../hydra-api";
import { logger } from "../logger";
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 {
const games = await gameRepository.find({
where: { remoteId: IsNull(), isDeleted: false },
});
const gamesChunks = chunk(games, 200);
for (const chunk of gamesChunks) {
await HydraApi.post(
"/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) {
if (err instanceof AxiosError) {
logger.error("uploadBatchGames", err.response, err.message);
logger.error("uploadGamesBatch", err.response, err.message);
} else {
logger.error("uploadBatchGames", err);
logger.error("uploadGamesBatch", err);
}
}
};

View File

@ -96,6 +96,12 @@ contextBridge.exposeInMainWorld("electron", {
ipcRenderer.on("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 */
getDiskFreeSpace: (path: string) =>

View File

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

View File

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