Add AdvancedAsyncSubject in order to avoid duplicate code to be executed while userInfo has been called

This commit is contained in:
Konstantinos Triantafyllou 2021-11-05 17:22:41 +02:00
parent 0aa44646b1
commit 1488f8ef00
2 changed files with 55 additions and 5 deletions

View File

@ -1,19 +1,20 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {BehaviorSubject, from, Observable, of} from "rxjs"; import {from, Observable} from "rxjs";
import {COOKIE, Session, User} from "../login/utils/helper.class"; import {COOKIE, Session, User} from "../login/utils/helper.class";
import {map} from "rxjs/operators"; import {map} from "rxjs/operators";
import {NavigationEnd, Router} from "@angular/router"; import {NavigationEnd, Router} from "@angular/router";
import {properties} from "../../../environments/environment"; import {properties} from "../../../environments/environment";
import {StringUtils} from "../utils/string-utils.class"; import {StringUtils} from "../utils/string-utils.class";
import {CustomOptions} from "./servicesUtils/customOptions.class"; import {CustomOptions} from "./servicesUtils/customOptions.class";
import {AdvancedAsyncSubject} from "../utils/AdvancedAsyncSubject";
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class UserManagementService { export class UserManagementService {
private getUserInfoSubject: BehaviorSubject<User> = new BehaviorSubject<User>(null); private getUserInfoSubject: AdvancedAsyncSubject<User> = new AdvancedAsyncSubject<User>();
public fixRedirectURL: string = null; public fixRedirectURL: string = null;
private redirectUrl: string = null; private redirectUrl: string = null;
private readonly promise: Promise<User>; private readonly promise: Promise<User>;
@ -53,9 +54,8 @@ export class UserManagementService {
} }
public updateUserInfo(resolve: Function = null) { public updateUserInfo(resolve: Function = null) {
/** @deprecated **/
const token = COOKIE.getCookie('AccessToken'); const token = COOKIE.getCookie('AccessToken');
if (!token && properties.userInfoUrl.includes("accessToken")) { if (!token) {
this.getUserInfoSubject.next(null); this.getUserInfoSubject.next(null);
if (resolve) { if (resolve) {
resolve(); resolve();
@ -70,7 +70,7 @@ export class UserManagementService {
resolve(); resolve();
} }
}, error => { }, error => {
if(this.getUserInfoSubject.value) { if(this.getUserInfoSubject.getValue()) {
this.getUserInfoSubject.next(null); this.getUserInfoSubject.next(null);
} }
if (resolve) { if (resolve) {

View File

@ -0,0 +1,50 @@
import {Subject, Subscriber, Subscription} from "rxjs";
export class AdvancedAsyncSubject<T> extends Subject<T>{
private _value: T = null;
private hasNext: boolean = false;
private hasFinished: boolean = false;
/** @deprecated This is an internal implementation detail, do not use. */
_subscribe(subscriber: Subscriber<any>): Subscription {
if (this.hasError) {
subscriber.error(this.thrownError);
return Subscription.EMPTY;
} else if (this.hasFinished && this.hasNext) {
subscriber.next(this._value);
}
return super._subscribe(subscriber);
}
next(value: T): void {
if (!this.hasFinished) {
this._value = value;
this.hasNext = true;
this.finish();
} else {
this._value = value;
super.next(value);
}
}
get value() {
return this.getValue();
}
getValue() {
return this._value;
}
error(error: any): void {
if (!this.hasFinished) {
super.error(error);
}
}
finish(): void {
this.hasFinished = true;
if (this.hasNext) {
super.next(this._value);
}
}
}