[Library | Trunk]: Community Service: fix getCommunity method to avoid multiple requests

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@60735 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2021-03-29 07:39:48 +00:00
parent 1f643b347c
commit 4214b08f87
1 changed files with 10 additions and 12 deletions

View File

@ -12,16 +12,12 @@ import {StringUtils} from "../../utils/string-utils.class";
@Injectable({providedIn: 'root'})
export class CommunityService {
public community: BehaviorSubject<CommunityInfo> = null;
public community: BehaviorSubject<CommunityInfo> = new BehaviorSubject(null);
public communityId: string = null;
private promise: Promise<boolean> = null;
private subs = [];
constructor(private http: HttpClient) {
this.community = new BehaviorSubject(null);
}
ngOnDestroy() {
this.clearSubscriptions();
}
constructor(private http: HttpClient) {}
clearSubscriptions() {
this.subs.forEach(subscription => {
@ -40,7 +36,8 @@ export class CommunityService {
}
getCommunity(communityId: string, refresh = false) {
if(!this.community.value || this.community.value.communityId !== communityId || refresh) {
if (this.communityId !== communityId || !this.community.value || refresh) {
this.communityId = communityId;
this.promise = new Promise<any>((resolve, reject) => {
this.subs.push(this.http.get<CommunityInfo>(properties.communityAPI + communityId)
.pipe(map(community => this.parseCommunity(community))).subscribe(community => {
@ -58,6 +55,7 @@ export class CommunityService {
async getCommunityAsync() {
await this.promise;
this.clearSubscriptions();
return this.community.getValue();
}