Skip to content

Commit e33c63b

Browse files
committed
fix: Incorrect game save marker logic
fix: Make sure to clear on disk cache immediately when game saved
1 parent b9a1f84 commit e33c63b

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/back/DatabaseCache.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type TagCache = {
1313
tagCount: number;
1414
};
1515

16+
const cacheKeyList: string[] = [];
1617
let gameSaved = false;
1718
let tagCache: TagCache | null = null;
1819

@@ -91,7 +92,6 @@ type GameStringCache = {
9192
};
9293

9394
async function loadStringCache(key: string, cachePath: string): Promise<GameStringCache> {
94-
log.info('Cache', `Loading ${key} string cache from ${cachePath}`);
9595
try {
9696
const cache = await readJsonFile(cachePath);
9797
if (typeof cache?.gameCount !== 'number' ||
@@ -123,8 +123,16 @@ async function loadStringCache(key: string, cachePath: string): Promise<GameStri
123123
}
124124
}
125125

126-
export function markGameSave() {
126+
export function markGameSave(fpPath: string) {
127127
gameSaved = true;
128+
for (const key of cacheKeyList) {
129+
const cachePath = path.join(fpPath, 'Cache', `${key}.json`);
130+
try {
131+
fs.unlinkSync(cachePath);
132+
} catch {
133+
// Doesn't exist or being edited, ignore
134+
}
135+
}
128136
}
129137

130138
async function saveStringCache(cachePath: string, gameCount: number, filterKey: string, data: string[]) {
@@ -141,6 +149,7 @@ function getStringCachedDataFactory(
141149
doSearch: (search: GameSearch) => Promise<string[]>
142150
): (state: BackState, tagFilters: TagFilterGroup[]) => Promise<string[]> {
143151
let cache: GameStringCache | null = null;
152+
cacheKeyList.push(key);
144153

145154
return async (state, tagFilters) => {
146155
console.log('Loading cache for ' + key);
@@ -152,20 +161,15 @@ function getStringCachedDataFactory(
152161
cache = await loadStringCache(key, cachePath);
153162
}
154163

155-
console.log('Checked existing cache for ' + key);
156-
157164
return databaseReady()
158165
.then(async (db) => {
159166
const gameCount = await db.countGames();
160-
if (gameSaved || cache!.gameCount === gameCount && cache!.filterKey === flatKey) {
167+
if (!gameSaved && cache!.gameCount === gameCount && cache!.filterKey === flatKey) {
161168
// Same game count and filter key, pretty accurate cache
162-
console.log('Returning existing cache for ' + key);
163169
return cache!.data;
164170
}
165-
console.log('Rebuilding cache for ' + key);
166171
const search = getTaggedSearch(tagFilters);
167172
const data = await doSearch(search);
168-
console.log('Saving cache for ' + key);
169173
await saveStringCache(cachePath, gameCount, flatKey, data);
170174
return data;
171175
});

src/back/responses.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ export function registerRequestCallbacks(state: BackState, init: () => Promise<v
546546
state.platformAppPaths = processPlatformAppPaths(await fpDatabase.findPlatformAppPaths()); // Update cache
547547
const total = await fpDatabase.countGames();
548548
const cats = await fpDatabase.findAllTagCategories();
549-
markGameSave();
549+
markGameSave(state.config.flashpointPath);
550550
state.socketServer.broadcast(BackOut.POST_SYNC_CHANGES, state.suggestions.library, state.suggestions, state.platformAppPaths, cats, total, state.preferences.gameMetadataSources[sourceIdx]);
551551
state.socketServer.broadcast(BackOut.TOAST, 'sync', 'Update Complete', {
552552
type: 'success',
@@ -835,7 +835,7 @@ export function registerRequestCallbacks(state: BackState, init: () => Promise<v
835835

836836
state.socketServer.register(BackIn.SAVE_GAMES, async (event, data) => {
837837
await fpDatabase.saveGames(data);
838-
markGameSave();
838+
markGameSave(state.config.flashpointPath);
839839
});
840840

841841
state.socketServer.register(BackIn.SAVE_GAME, async (event, game) => {
@@ -855,7 +855,7 @@ export function registerRequestCallbacks(state: BackState, init: () => Promise<v
855855
// info.game.activeGameConfigOwner = undefined;
856856
// }
857857
const savedGame = await fpDatabase.saveGame(game);
858-
markGameSave();
858+
markGameSave(state.config.flashpointPath);
859859
broadcastGameUpdate(state, game.id);
860860
return savedGame;
861861
} catch (err) {

0 commit comments

Comments
 (0)