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