From 4e422bdf91c1e8b9dfa283da99df9625c499ec79 Mon Sep 17 00:00:00 2001 From: Chubby Granny Chaser Date: Thu, 4 Jul 2024 18:35:47 +0100 Subject: [PATCH] feat: migrating download source validation to worker thread --- .../validate-download-source.ts | 21 ++++++---------- src/main/main.ts | 2 +- src/main/workers/download-source.worker.ts | 24 ++++++++++++++++++- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/main/events/download-sources/validate-download-source.ts b/src/main/events/download-sources/validate-download-source.ts index 122eb49c..fdb67961 100644 --- a/src/main/events/download-sources/validate-download-source.ts +++ b/src/main/events/download-sources/validate-download-source.ts @@ -1,17 +1,12 @@ import { registerEvent } from "../register-event"; -import axios from "axios"; import { downloadSourceRepository } from "@main/repository"; -import { downloadSourceSchema } from "../helpers/validators"; import { RepacksManager } from "@main/services"; +import { downloadSourceWorker } from "@main/workers"; const validateDownloadSource = async ( _event: Electron.IpcMainInvokeEvent, url: string ) => { - const response = await axios.get(url); - - const source = downloadSourceSchema.parse(response.data); - const existingSource = await downloadSourceRepository.findOne({ where: { url }, }); @@ -21,14 +16,12 @@ const validateDownloadSource = async ( const repacks = RepacksManager.repacks; - const existingUris = source.downloads - .flatMap((download) => download.uris) - .filter((uri) => repacks.some((repack) => repack.magnet === uri)); - - return { - name: source.name, - downloadCount: source.downloads.length - existingUris.length, - }; + return downloadSourceWorker.run( + { url, repacks }, + { + name: "validateDownloadSource", + } + ); }; registerEvent("validateDownloadSource", validateDownloadSource); diff --git a/src/main/main.ts b/src/main/main.ts index 1abd8c2f..4a48e6a7 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -15,7 +15,7 @@ import { uploadGamesBatch } from "./services/library-sync"; startMainLoop(); const loadState = async (userPreferences: UserPreferences | null) => { - await RepacksManager.updateRepacks(); + RepacksManager.updateRepacks(); import("./events"); diff --git a/src/main/workers/download-source.worker.ts b/src/main/workers/download-source.worker.ts index cc33dd38..066aa6f5 100644 --- a/src/main/workers/download-source.worker.ts +++ b/src/main/workers/download-source.worker.ts @@ -1,6 +1,7 @@ +import { Repack } from "@main/entity"; import { downloadSourceSchema } from "@main/events/helpers/validators"; import { DownloadSourceStatus } from "@shared"; -import type { DownloadSource } from "@types"; +import type { DownloadSource, GameRepack } from "@types"; import axios, { AxiosError, AxiosHeaders } from "axios"; import { z } from "zod"; @@ -48,3 +49,24 @@ export const getUpdatedRepacks = async (downloadSources: DownloadSource[]) => { return results; }; + +export const validateDownloadSource = async ({ + url, + repacks, +}: { + url: string; + repacks: GameRepack[]; +}) => { + const response = await axios.get(url); + + const source = downloadSourceSchema.parse(response.data); + + const existingUris = source.downloads + .flatMap((download) => download.uris) + .filter((uri) => repacks.some((repack) => repack.magnet === uri)); + + return { + name: source.name, + downloadCount: source.downloads.length - existingUris.length, + }; +};