mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2026-07-24 17:03:14 +10:00
refactor: Converting useGame to DownloadableMetadata
Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
+52
-41
@@ -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<GameStatus> } = {};
|
||||
const downloadStatusRegistry: Map<DownloadableMetadata, Ref<DownloadStatus>> = 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 };
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user