diff --git a/src/app/api/client.service.ts b/src/app/api/client.service.ts index 8b414f0..4a6691d 100644 --- a/src/app/api/client.service.ts +++ b/src/app/api/client.service.ts @@ -9,7 +9,7 @@ import {ListWithData} from "./list-with-data"; import {User} from "./types/users/user"; import {ActivityPage} from "./types/activity/activity-page"; import {Photo} from "./types/photos/photo"; -import {Observable} from "rxjs"; +import {BehaviorSubject, Observable, tap} from "rxjs"; import {Params} from "@angular/router"; import {ApiImplementation} from "./api-implementation"; import {Contest} from "./types/contests/contest"; @@ -22,6 +22,8 @@ export const defaultPageSize: number = 40; export class ClientService extends ApiImplementation { private readonly instance: LazySubject; private readonly categories: LazySubject>; + + private usersCache: User[] = []; constructor(http: HttpClient) { super(http); @@ -50,13 +52,27 @@ export class ClientService extends ApiImplementation { getLevelById(id: number) { return this.http.get(`/levels/id/${id}`); } + + private getUser(route: string, cacheLookup: (value: User, index: number, obj: User[]) => boolean): Observable { + const existingUser: User | undefined = this.usersCache.find(cacheLookup); + if (existingUser) { + // console.log("found in cache", existingUser); + return new BehaviorSubject(existingUser); + } + + return this.http.get("/users/" + route) + .pipe(tap(d => { + // console.log("adding to cache", d) + this.usersCache.push(d); + })); + } - getUserByUuid(userId: string) { - return this.http.get(`/users/uuid/${userId}`); + getUserByUuid(userId: string): Observable { + return this.getUser("uuid/" + userId, u => u.userId == userId) } getUserByUsername(username: string) { - return this.http.get(`/users/name/${username}`); + return this.getUser("name/" + username, u => u.username == username) } getUserByEitherLookup(username: string | undefined, uuid: string | undefined): Observable {