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
8 changed files with 31 additions and 17 deletions

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);
}
}
};