From 218b0874ede2f0d35fdbcfb64dbe19083b0a2b04 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 6 Jan 2020 16:13:18 +0100 Subject: Add get user cache for comments --- client/src/app/shared/users/user.service.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'client/src/app/shared/users') diff --git a/client/src/app/shared/users/user.service.ts b/client/src/app/shared/users/user.service.ts index 051e0bfa4..e24d91df3 100644 --- a/client/src/app/shared/users/user.service.ts +++ b/client/src/app/shared/users/user.service.ts @@ -1,5 +1,5 @@ -import { from, Observable } from 'rxjs' -import { catchError, concatMap, map, toArray } from 'rxjs/operators' +import { from, Observable, of } from 'rxjs' +import { catchError, concatMap, map, share, shareReplay, tap, toArray } from 'rxjs/operators' import { HttpClient, HttpParams } from '@angular/common/http' import { Injectable } from '@angular/core' import { ResultList, User, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared' @@ -17,6 +17,8 @@ export class UserService { private bytesPipe = new BytesPipe() + private userCache: { [ id: number ]: Observable } = {} + constructor ( private authHttp: HttpClient, private restExtractor: RestExtractor, @@ -194,6 +196,14 @@ export class UserService { ) } + getUserWithCache (userId: number) { + if (!this.userCache[userId]) { + this.userCache[ userId ] = this.getUser(userId).pipe(shareReplay()) + } + + return this.userCache[userId] + } + getUser (userId: number) { return this.authHttp.get(UserService.BASE_USERS_URL + userId) .pipe(catchError(err => this.restExtractor.handleError(err))) -- cgit v1.2.3