Skip to content

Commit

Permalink
fix: switched how deep copies are made to speed up load times
Browse files Browse the repository at this point in the history
  • Loading branch information
Tormak9970 committed Oct 3, 2024
1 parent db0423f commit d5ac0e8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/components/modals/manual-games/ManualGamesModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
let canSave = false;
const originalManualGames = $manualSteamGames;
let tempManualGames: GameStruct[] = JSON.parse(JSON.stringify(originalManualGames));
let tempManualGames: GameStruct[] = structuredClone(originalManualGames);
let addMethods = [
{ label: "Manual", data: "manual" },
Expand All @@ -40,7 +40,7 @@
LogController.log(`Added manually added game ${game.name}.`);
tempManualGames.push(game);
tempManualGames = [ ...tempManualGames ];
canSave = JSON.parse(JSON.stringify(originalManualGames)) !== JSON.parse(JSON.stringify(tempManualGames));
canSave = structuredClone(originalManualGames) !== structuredClone(tempManualGames);
}
}
Expand All @@ -53,7 +53,7 @@
const index = tempManualGames.findIndex((g) => g.appid === game.appid);
tempManualGames.splice(index, 1);
tempManualGames = [ ...tempManualGames ];
canSave = JSON.parse(JSON.stringify(originalManualGames)) !== JSON.parse(JSON.stringify(tempManualGames));
canSave = structuredClone(originalManualGames) !== structuredClone(tempManualGames);
}
/**
Expand All @@ -70,8 +70,8 @@
if (!appLibCache[game.appid]) appLibCache[game.appid] = { "Capsule": "", "Wide Capsule": "", "Hero": "", "Logo": "", "Icon": "" };
}
originalAppLibraryCache.set(JSON.parse(JSON.stringify(originalAppLibCache)));
appLibraryCache.set(JSON.parse(JSON.stringify(appLibCache)));
originalAppLibraryCache.set(structuredClone(originalAppLibCache));
appLibraryCache.set(structuredClone(appLibCache));
LogController.log(`Saved ${tempManualGames.length} manually added games.`);
ToastController.showSuccessToast(`Saved ${tempManualGames.length} manually added games.`);
Expand Down
31 changes: 16 additions & 15 deletions src/lib/controllers/AppController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,20 @@ export class AppController {
shortcut.icon = changedPath.targetPath === "REMOVE" ? "" : changedPath.targetPath;
}
}
originalAppLibraryCache.set(JSON.parse(JSON.stringify(libraryCache)));
originalAppLibraryCache.set(structuredClone(libraryCache));
appLibraryCache.set(libraryCache);

originalSteamShortcuts.set(JSON.parse(JSON.stringify(shortcuts)));
originalSteamShortcuts.set(structuredClone(shortcuts));
steamShortcuts.set(shortcuts);

let logoPosEntries = Object.entries(steamLogoPos);
logoPosEntries = logoPosEntries.filter(([ appid, logoPos ]) => {
return logoPos.logoPosition && logoPos.logoPosition.pinnedPosition !== "REMOVE";
});

originalLogoPositions.set(JSON.parse(JSON.stringify(Object.fromEntries(logoPosEntries))));
steamLogoPositions.set(JSON.parse(JSON.stringify(Object.fromEntries(logoPosEntries))));
const logoPos = Object.fromEntries(logoPosEntries);
originalLogoPositions.set(structuredClone(logoPos));
steamLogoPositions.set(structuredClone(logoPos));
ToastController.showSuccessToast("Changes saved!");
LogController.log("Saved changes.");
}
Expand All @@ -159,13 +160,13 @@ export class AppController {
*/
static discardChanges(): void {
const originalCache = get(originalAppLibraryCache);
appLibraryCache.set(JSON.parse(JSON.stringify(originalCache)));
appLibraryCache.set(structuredClone(originalCache));

const originalShortcuts = get(originalSteamShortcuts);
steamShortcuts.set(JSON.parse(JSON.stringify(originalShortcuts)));
steamShortcuts.set(structuredClone(originalShortcuts));

const originalPositions = get(originalLogoPositions);
steamLogoPositions.set(JSON.parse(JSON.stringify(originalPositions)));
steamLogoPositions.set(structuredClone(originalPositions));

ToastController.showSuccessToast("Changes discarded!");
LogController.log("Discarded changes.");
Expand All @@ -191,14 +192,14 @@ export class AppController {
let shortcutToEdit = shortcuts.find((shortcut) => shortcut.appid.toString() === appId);
const targetShortcut = originalShortcuts.find((shortcut) => shortcut.appid.toString() === appId);
shortcutToEdit = targetShortcut;
steamShortcuts.set(JSON.parse(JSON.stringify(shortcuts)));
steamShortcuts.set(structuredClone(shortcuts));
}

appCache[appId] = originalCache[appId];
appLibraryCache.set(JSON.parse(JSON.stringify(appCache)));
appLibraryCache.set(structuredClone(appCache));

logoPositionCache[appId] = originalLogoCache[appId];
steamLogoPositions.set(JSON.parse(JSON.stringify(logoPositionCache)));
steamLogoPositions.set(structuredClone(logoPositionCache));

ToastController.showSuccessToast("Discarded!");
LogController.log(`Discarded changes for ${appId}.`);
Expand All @@ -218,7 +219,7 @@ export class AppController {
if (platform === Platforms.NON_STEAM) {
const shortcutToEdit = shortcuts.find((shortcut) => shortcut.appid.toString() === appId)!;
shortcutToEdit.icon = "";
steamShortcuts.set(JSON.parse(JSON.stringify(shortcuts)));
steamShortcuts.set(structuredClone(shortcuts));
}

appCache[appId] = {
Expand All @@ -228,7 +229,7 @@ export class AppController {
"Logo": "REMOVE",
"Icon": "REMOVE"
};
appLibraryCache.set(JSON.parse(JSON.stringify(appCache)));
appLibraryCache.set(structuredClone(appCache));

ToastController.showSuccessToast("Cleared!");
LogController.log(`Cleared grids for ${appId}.`);
Expand All @@ -255,7 +256,7 @@ export class AppController {
const logoPositionCache = get(steamLogoPositions);

logoPositionCache[appid].logoPosition.pinnedPosition = "REMOVE";
steamLogoPositions.set(JSON.parse(JSON.stringify(logoPositionCache)));
steamLogoPositions.set(structuredClone(logoPositionCache));

LogController.log(`Cleared logo position for ${appid}`);

Expand Down Expand Up @@ -290,8 +291,8 @@ export class AppController {
};
}

steamShortcuts.set(JSON.parse(JSON.stringify(shortcuts)));
appLibraryCache.set(JSON.parse(JSON.stringify(appCache)));
steamShortcuts.set(structuredClone(shortcuts));
appLibraryCache.set(structuredClone(appCache));

ToastController.showSuccessToast("Cleared all grids!");
LogController.log("Cleared all grids.");
Expand Down
6 changes: 3 additions & 3 deletions src/lib/controllers/CacheController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ export class CacheController {
const nonSteamGameNameEntries = nonSteamGamesList.map((game: GameStruct) => [ game.appid, game.name ]);
const nonSteamGameNameMap = Object.fromEntries(nonSteamGameNameEntries);

const gridsCopy = JSON.parse(JSON.stringify(get(appLibraryCache)));
const shortcutsCopy = JSON.parse(JSON.stringify(get(steamShortcuts)));
const gridsCopy = structuredClone(get(appLibraryCache));
const shortcutsCopy = structuredClone(get(steamShortcuts));
const selectedGridType = get(gridType);
const filters = get(dbFilters);

Expand Down Expand Up @@ -431,7 +431,7 @@ export class CacheController {
if (localPath) {
if (!isSteamGame && selectedGridType === GridTypes.ICON) {
shortcutsNeedUpdate = true;
const shortcut = shortcutsCopy.find((s: SteamShortcut) => s.appid === appidInt);
const shortcut = shortcutsCopy.find((s: SteamShortcut) => s.appid === appidInt)!;
shortcut.icon = localPath;
}

Expand Down
12 changes: 6 additions & 6 deletions src/lib/controllers/SteamController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export class SteamController {
}
}

originalLogoPositions.set(JSON.parse(JSON.stringify(configs)));
steamLogoPositions.set(JSON.parse(JSON.stringify(configs)));
originalLogoPositions.set(structuredClone(configs));
steamLogoPositions.set(structuredClone(configs));

LogController.log(`Cached logo positions for ${Object.entries(configs).length} games.`);
}
Expand Down Expand Up @@ -145,7 +145,7 @@ export class SteamController {
}

const entries = Object.entries(res);
unfilteredLibraryCache.set(JSON.parse(JSON.stringify(unfiltered)));
unfilteredLibraryCache.set(structuredClone(unfiltered));
const filtered = entries.filter(([ appId, entry ]) => Object.keys(entry).length >= 2 || shortcutIds.includes(appId)); //! Look into this because it seems like it aint ideal this because it caused issues with games with no grids
return Object.fromEntries(filtered);
}
Expand Down Expand Up @@ -346,7 +346,7 @@ export class SteamController {
});

if (manualGames.length !== originalManualGames.length) {
manualSteamGames.set(JSON.parse(JSON.stringify(manualGames)));
manualSteamGames.set(structuredClone(manualGames));
ToastController.showWarningToast(`Removed ${Math.abs(manualGames.length - originalManualGames.length)} duplicate manual games!`);
}

Expand All @@ -366,7 +366,7 @@ export class SteamController {

// LogController.log("Loading non-steam games...");
const shortcuts = await RustInterop.readShortcutsVdf(userId.toString());
originalSteamShortcuts.set(JSON.parse(JSON.stringify(Object.values(shortcuts))));
originalSteamShortcuts.set(structuredClone(Object.values(shortcuts)));
steamShortcuts.set(Object.values(shortcuts));

const structuredShortcuts = Object.values(shortcuts).map((shortcut: any) => {
Expand All @@ -382,7 +382,7 @@ export class SteamController {

const filteredCache = await SteamController.getCacheData(structuredShortcuts);

originalAppLibraryCache.set(JSON.parse(JSON.stringify(filteredCache)));
originalAppLibraryCache.set(structuredClone(filteredCache));
appLibraryCache.set(filteredCache);

await SteamController.loadSteamApps(Object.values(shortcuts), filteredCache);
Expand Down

0 comments on commit d5ac0e8

Please sign in to comment.