Various styling and small QoL improvements

This commit is contained in:
grimsi
2022-08-05 10:26:01 +02:00
parent 7f2e77c8bc
commit 8ee217bbe8
12 changed files with 160 additions and 85 deletions
+27 -3
View File
@@ -12,14 +12,33 @@ export class GamesService implements GamesApi {
private readonly apiPath = '/games';
private cache: Map<string, DetectedGameDto> = new Map<string, DetectedGameDto>();
constructor(private http: HttpClient) {
}
getAllGames(): Observable<DetectedGameDto[]> {
return this.http.get<DetectedGameDto[]>(this.apiPath).pipe(map(games => games.sort((g1, g2) => g1.title.localeCompare(g2.title))));
getAllGames(forceReloadFromServer: boolean = false): Observable<DetectedGameDto[]> {
if (this.cache.size === 0 || forceReloadFromServer) {
let gamesObservable: Observable<DetectedGameDto[]> = this.http.get<DetectedGameDto[]>(this.apiPath).pipe(map(games => games.sort((g1, g2) => g1.title.localeCompare(g2.title))));
gamesObservable.subscribe(g => this.cacheGames(g));
return gamesObservable;
}
return new Observable<DetectedGameDto[]>(subscriber => {
subscriber.next(Array.from(this.cache.values()));
subscriber.complete();
});
}
getGame(slug: String): Observable<DetectedGameDto> {
getGame(slug: string): Observable<DetectedGameDto> {
if (this.cache.has(slug)) {
return new Observable<DetectedGameDto>(subscriber => {
subscriber.next(this.cache.get(slug));
subscriber.complete();
});
}
return this.http.get<DetectedGameDto>(`${this.apiPath}/game/${slug}`);
}
@@ -34,4 +53,9 @@ export class GamesService implements GamesApi {
getAllGameMappings(): Observable<Map<string, string>> {
return this.http.get<Map<string, string>>(`${this.apiPath}/game-mappings`);
}
private cacheGames(gameList: DetectedGameDto[]): void {
this.cache.clear();
gameList.forEach(game => this.cache.set(game.slug, game));
}
}