Skip to content

Commit 29dbe66

Browse files
committed
feat: Expand caching to Libraries, PlayMode, Status and Application Path
1 parent e33c63b commit 29dbe66

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

src/back/DatabaseCache.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type TagCache = {
1414
};
1515

1616
const cacheKeyList: string[] = [];
17-
let gameSaved = false;
17+
let gameSaveCount = 0;
1818
let tagCache: TagCache | null = null;
1919

2020
async function loadTagCache(cachePath: string): Promise<void> {
@@ -124,7 +124,7 @@ async function loadStringCache(key: string, cachePath: string): Promise<GameStri
124124
}
125125

126126
export function markGameSave(fpPath: string) {
127-
gameSaved = true;
127+
gameSaveCount += 1;
128128
for (const key of cacheKeyList) {
129129
const cachePath = path.join(fpPath, 'Cache', `${key}.json`);
130130
try {
@@ -149,6 +149,7 @@ function getStringCachedDataFactory(
149149
doSearch: (search: GameSearch) => Promise<string[]>
150150
): (state: BackState, tagFilters: TagFilterGroup[]) => Promise<string[]> {
151151
let cache: GameStringCache | null = null;
152+
let gameSaveCountLocal = gameSaveCount;
152153
cacheKeyList.push(key);
153154

154155
return async (state, tagFilters) => {
@@ -164,10 +165,11 @@ function getStringCachedDataFactory(
164165
return databaseReady()
165166
.then(async (db) => {
166167
const gameCount = await db.countGames();
167-
if (!gameSaved && cache!.gameCount === gameCount && cache!.filterKey === flatKey) {
168-
// Same game count and filter key, pretty accurate cache
168+
if (gameSaveCountLocal === gameSaveCount && cache!.gameCount === gameCount && cache!.filterKey === flatKey) {
169+
// No new games saved, same game count and filter key, pretty accurate cache
169170
return cache!.data;
170171
}
172+
gameSaveCountLocal = gameSaveCount;
171173
const search = getTaggedSearch(tagFilters);
172174
const data = await doSearch(search);
173175
await saveStringCache(cachePath, gameCount, flatKey, data);
@@ -176,6 +178,43 @@ function getStringCachedDataFactory(
176178
};
177179
}
178180

181+
function getStringCachedDataTaglessFactory(
182+
key: string,
183+
doSearch: () => Promise<string[]>
184+
): (state: BackState) => Promise<string[]> {
185+
let cache: GameStringCache | null = null;
186+
let gameSaveCountLocal = gameSaveCount;
187+
cacheKeyList.push(key);
188+
189+
return async (state) => {
190+
console.log('Loading cache for ' + key);
191+
const cachePath = path.join(state.config.flashpointPath, 'Cache', `${key}.json`);
192+
193+
if (cache === null) {
194+
cache = await loadStringCache(key, cachePath);
195+
}
196+
197+
198+
return databaseReady()
199+
.then(async (db) => {
200+
const gameCount = await db.countGames();
201+
if (gameSaveCountLocal === gameSaveCount && cache!.gameCount === gameCount) {
202+
// No new games saved and same game count, pretty accurate cache
203+
return cache!.data;
204+
}
205+
gameSaveCountLocal = gameSaveCount;
206+
const data = await doSearch();
207+
await saveStringCache(cachePath, gameCount, '', data);
208+
return data;
209+
});
210+
};
211+
}
212+
179213
export const getAllDevelopers = getStringCachedDataFactory('developers', (search) => fpDatabase.findAllGameDevelopers(search));
180214
export const getAllPublishers = getStringCachedDataFactory('publishers', (search) => fpDatabase.findAllGamePublishers(search));
181215
export const getAllSeries = getStringCachedDataFactory('series', (search) => fpDatabase.findAllGameSeries(search));
216+
217+
export const getAllLibraries = getStringCachedDataTaglessFactory('libraries', () => fpDatabase.findAllGameLibraries());
218+
export const getAllStatuses = getStringCachedDataTaglessFactory('statuses', () => fpDatabase.findAllGameStatuses());
219+
export const getAllApplicationPaths = getStringCachedDataTaglessFactory('applicationPaths', () => fpDatabase.findAllGameApplicationPaths());
220+
export const getAllPlayModes = getStringCachedDataTaglessFactory('playModes', () => fpDatabase.findAllGamePlayModes());

src/back/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { saveCurationFpfssInfo } from './curate/fpfss';
4242
import { loadCurationIndexImage } from './curate/parse';
4343
import { readCurationMeta } from './curate/read';
4444
import { onFileServerRequestCurationFileFactory, onFileServerRequestPostCuration } from './curate/util';
45+
import { getAllApplicationPaths, getAllLibraries, getAllPlayModes, getAllStatuses } from './DatabaseCache';
4546
import { axios } from './dns';
4647
import { downloadGameData } from './download';
4748
import { Downloader } from './Downloader';
@@ -864,17 +865,18 @@ async function initialize() {
864865
state.socketServer.broadcast(BackOut.OPEN_ALERT, 'Failed to open database: ' + e);
865866
}
866867

868+
state.init[BackInit.DATABASE_READY] = true;
869+
867870
// Populate unique values
868871
state.suggestions = {
869872
tags: [],
870-
playMode: await fpDatabase.findAllGamePlayModes(),
873+
playMode: await getAllPlayModes(state),
871874
platforms: (await fpDatabase.findAllPlatforms()).map(p => p.name),
872-
status: await fpDatabase.findAllGameStatuses(),
873-
applicationPath: await fpDatabase.findAllGameApplicationPaths(),
874-
library: await fpDatabase.findAllGameLibraries(),
875+
status: await getAllStatuses(state),
876+
applicationPath: await getAllApplicationPaths(state),
877+
library: await getAllLibraries(state),
875878
};
876879

877-
state.init[BackInit.DATABASE_READY] = true;
878880
state.initEmitter.emit(BackInit.DATABASE_READY);
879881

880882
// Check for Flashpoint Manager Updates

src/back/responses.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import * as url from 'url';
6161
import * as util from 'util';
6262
import * as YAML from 'yaml';
6363
import { ConfigFile } from './ConfigFile';
64-
import { getAllDevelopers, getAllPublishers, getAllSeries, getTags, markGameSave } from './DatabaseCache';
64+
import { getAllApplicationPaths, getAllDevelopers, getAllLibraries, getAllPlayModes, getAllPublishers, getAllSeries, getAllStatuses, getTags, markGameSave } from './DatabaseCache';
6565
import { ExtConfigFile } from './ExtConfigFile';
6666
import { escapeArgsForShell, GameLauncher } from './GameLauncher';
6767
import { ManagedChildProcess } from './ManagedChildProcess';
@@ -242,7 +242,7 @@ export function registerRequestCallbacks(state: BackState, init: () => Promise<v
242242
});
243243

244244
state.socketServer.register(BackIn.GET_RENDERER_LOADED_DATA, async (event) => {
245-
const libraries = await fpDatabase.findAllGameLibraries();
245+
const libraries = await getAllLibraries(state);
246246

247247
// Fetch update feed in background
248248
if (state.preferences.updateFeedUrl) {
@@ -537,11 +537,11 @@ export function registerRequestCallbacks(state: BackState, init: () => Promise<v
537537
// Send out new suggestions and library lists
538538
state.suggestions = {
539539
tags: [],
540-
playMode: await fpDatabase.findAllGamePlayModes(),
540+
playMode: await getAllPlayModes(state),
541541
platforms: (await fpDatabase.findAllPlatforms()).map(p => p.name),
542-
status: await fpDatabase.findAllGameStatuses(),
543-
applicationPath: await fpDatabase.findAllGameApplicationPaths(),
544-
library: await fpDatabase.findAllGameLibraries(),
542+
status: await getAllStatuses(state),
543+
applicationPath: await getAllApplicationPaths(state),
544+
library: await getAllLibraries(state),
545545
};
546546
state.platformAppPaths = processPlatformAppPaths(await fpDatabase.findPlatformAppPaths()); // Update cache
547547
const total = await fpDatabase.countGames();
@@ -590,11 +590,11 @@ export function registerRequestCallbacks(state: BackState, init: () => Promise<v
590590
state.socketServer.register(BackIn.GET_SUGGESTIONS, async () => {
591591
const suggestions: GamePropSuggestions = {
592592
tags: [],
593-
playMode: await fpDatabase.findAllGamePlayModes(),
593+
playMode: await getAllPlayModes(state),
594594
platforms: (await fpDatabase.findAllPlatforms()).map(p => p.name),
595-
status: await fpDatabase.findAllGameStatuses(),
596-
applicationPath: await fpDatabase.findAllGameApplicationPaths(),
597-
library: await fpDatabase.findAllGameLibraries(),
595+
status: await getAllStatuses(state),
596+
applicationPath: await getAllApplicationPaths(state),
597+
library: await getAllLibraries(state),
598598
};
599599
state.platformAppPaths = processPlatformAppPaths(await fpDatabase.findPlatformAppPaths()); // Update cache
600600
return {

0 commit comments

Comments
 (0)