diff --git a/composables/game.ts b/composables/game.ts index 28a26fb..0e83b75 100644 --- a/composables/game.ts +++ b/composables/game.ts @@ -1,19 +1,19 @@ import { invoke } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; -import type { Game, GameStatus, GameStatusEnum, GameVersion } from "~/types"; +import { type Game, type GameStatus as DownloadStatus, type GameStatusEnum as DownloadStatusEnum, type GameVersion, type DownloadableMetadata, DownloadableType } from "~/types"; const gameRegistry: { [key: string]: { game: Game; version?: GameVersion } } = {}; -const gameStatusRegistry: { [key: string]: Ref } = {}; +const downloadStatusRegistry: Map> = new Map(); -type OptionGameStatus = { [key in GameStatusEnum]: { version_name?: string } }; -export type SerializedGameStatus = [ - { type: GameStatusEnum }, - OptionGameStatus | null +type OptionDownloadStatus = { [key in DownloadStatusEnum]: { version_name?: string } }; +export type SerializedDownloadStatus = [ + { type: DownloadStatusEnum }, + OptionDownloadStatus | null ]; -export const parseStatus = (status: SerializedGameStatus): GameStatus => { +export const parseStatus = (status: SerializedDownloadStatus): DownloadStatus => { console.log(status); if (status[0]) { return { @@ -22,7 +22,7 @@ export const parseStatus = (status: SerializedGameStatus): GameStatus => { } else if (status[1]) { const [[gameStatus, options]] = Object.entries(status[1]); return { - type: gameStatus as GameStatusEnum, + type: gameStatus as DownloadStatusEnum, ...options, }; } else { @@ -30,43 +30,54 @@ export const parseStatus = (status: SerializedGameStatus): GameStatus => { } }; -export const useGame = async (gameId: string) => { - if (!gameRegistry[gameId]) { - const data: { - game: Game; - status: SerializedGameStatus; - version?: GameVersion; - } = await invoke("fetch_game", { - gameId, - }); - gameRegistry[gameId] = { game: data.game, version: data.version }; - if (!gameStatusRegistry[gameId]) { - gameStatusRegistry[gameId] = ref(parseStatus(data.status)); +export const useStatus = (meta: DownloadableMetadata) => { + return downloadStatusRegistry.get(meta) +} - listen(`update_game/${gameId}`, (event) => { - const payload: { - status: SerializedGameStatus; - version?: GameVersion; - } = event.payload as any; - console.log(payload.status); - gameStatusRegistry[gameId].value = parseStatus(payload.status); - - /** - * I am not super happy about this. - * - * This will mean that we will still have a version assigned if we have a game installed then uninstall it. - * It is necessary because a flag to check if we should overwrite seems excessive, and this function gets called - * on transient state updates. - */ - if (payload.version) { - gameRegistry[gameId].version = payload.version; - } - }); - } +export const useGame = async (gameId: string) => { + const data: { + game: Game; + status: SerializedDownloadStatus; + version?: GameVersion; + } = await invoke("fetch_game", { + gameId, + }); + const meta = { + id: gameId, + version: data.version?.versionName, + downloadType: DownloadableType.Game + } satisfies DownloadableMetadata; + if (!gameRegistry[gameId]) { + + gameRegistry[gameId] = { game: data.game, version: data.version }; + } + if (!downloadStatusRegistry.has(meta)) { + downloadStatusRegistry.set(meta, ref(parseStatus(data.status))); + + listen(`update_game/${gameId}`, (event) => { + const payload: { + status: SerializedDownloadStatus; + version?: GameVersion; + } = event.payload as any; + + downloadStatusRegistry.get(meta)!.value = parseStatus(payload.status); + + /** + * I am not super happy about this. + * + * This will mean that we will still have a version assigned if we have a game installed then uninstall it. + * It is necessary because a flag to check if we should overwrite seems excessive, and this function gets called + * on transient state updates. + */ + if (payload.version) { + gameRegistry[gameId].version = payload.version; + } + }); } + const game = gameRegistry[gameId]; - const status = gameStatusRegistry[gameId]; + const status = downloadStatusRegistry.get(meta)!; return { ...game, status }; }; diff --git a/pages/library/[id]/index.vue b/pages/library/[id]/index.vue index 95f1304..9e30ad9 100644 --- a/pages/library/[id]/index.vue +++ b/pages/library/[id]/index.vue @@ -471,6 +471,7 @@ const router = useRouter(); const id = route.params.id.toString(); const { game: rawGame, status } = await useGame(id); +console.log("status: ", status); const game = ref(rawGame); const remoteUrl: string = await invoke("gen_drop_url", { diff --git a/pages/queue.vue b/pages/queue.vue index cee74b6..482a3e7 100644 --- a/pages/queue.vue +++ b/pages/queue.vue @@ -1,46 +1,32 @@