Skip to content

Commit

Permalink
Cache whether the active game has a package list in IndexedDB
Browse files Browse the repository at this point in the history
SettingRow items update their value for on one second interval. While
reading the status from IndexedDB isn't a noticeable performance hit
(at least on my PC), it becomes an issue if checking the status throws
an error. In those cases TSMM's Sentry gets bombarded with an error
every second users stays on the settings screen.

Cache invalidation obviously makes the implementation more complex. The
cache can still show incorrect values if user manually deletes the
entries from IndexedDB via dev tools but that's acceptable.
  • Loading branch information
anttimaki committed Jan 16, 2025
1 parent e211c04 commit b44ae70
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/store/modules/TsModsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface CachedMod {
}

interface State {
activeGameCacheStatus: string|undefined;
cache: Map<string, CachedMod>;
deprecated: Map<string, boolean>;
exclusions: string[];
Expand Down Expand Up @@ -55,6 +56,8 @@ export const TsModsModule = {
namespaced: true,

state: (): State => ({
/*** Does the active game have a mod list stored in IndexedDB? */
activeGameCacheStatus: undefined,
cache: new Map<string, CachedMod>(),
deprecated: new Map<string, boolean>(),
/*** Packages available through API that should be ignored by the manager */
Expand Down Expand Up @@ -127,6 +130,7 @@ export const TsModsModule = {

mutations: <MutationTree<State>>{
reset(state: State) {
state.activeGameCacheStatus = undefined;
state.cache = new Map<string, CachedMod>();
state.deprecated = new Map<string, boolean>();
state.mods = [];
Expand All @@ -141,6 +145,9 @@ export const TsModsModule = {
state.isThunderstoreModListUpdateInProgress = false;
state.thunderstoreModListUpdateStatus = '';
},
setActiveGameCacheStatus(state, status: string|undefined) {
state.activeGameCacheStatus = status;
},
setMods(state, payload: ThunderstoreMod[]) {
state.mods = payload;
},
Expand Down Expand Up @@ -222,6 +229,7 @@ export const TsModsModule = {
} catch (e) {
commit('setThunderstoreModListUpdateError', e);
} finally {
commit('setActiveGameCacheStatus', undefined);
commit('finishThunderstoreModListUpdate');
}
},
Expand Down Expand Up @@ -293,14 +301,28 @@ export const TsModsModule = {
return updated !== undefined;
},

async getActiveGameCacheStatus({state, rootState}) {
async getActiveGameCacheStatus({commit, state, rootState}): Promise<string> {
if (state.isThunderstoreModListUpdateInProgress) {
return "Online mod list is currently updating, please wait for the operation to complete";
}

return (await PackageDb.hasEntries(rootState.activeGame.internalFolderName))
? `${rootState.activeGame.displayName} has a local copy of online mod list`
: `${rootState.activeGame.displayName} has no local copy stored`;
// Only check the status once, as this is used in the settings
// where the value is polled on one second intervals.
if (state.activeGameCacheStatus === undefined) {
let status = '';
try {
status = (await PackageDb.hasEntries(rootState.activeGame.internalFolderName))
? `${rootState.activeGame.displayName} has a local copy of online mod list`
: `${rootState.activeGame.displayName} has no local copy stored`;
} catch (e) {
console.error(e);
status = 'Error occurred while checking mod list status';
}

commit('setActiveGameCacheStatus', status);
}

return state.activeGameCacheStatus || 'Unknown status';
},

async prewarmCache({getters, rootGetters}) {
Expand All @@ -326,6 +348,7 @@ export const TsModsModule = {
await PackageDb.resetCommunity(community);
commit('setModsLastUpdated', undefined);
} finally {
commit('setActiveGameCacheStatus', undefined);
commit('finishThunderstoreModListUpdate');
}
},
Expand Down

0 comments on commit b44ae70

Please sign in to comment.