unsubscribe an existing subscription before creating a new one (avoids jumps in session timer in case of multiple query on one page)

This commit is contained in:
Wolfgang Hottgenroth 2021-09-07 14:44:48 +02:00
parent fd25f1fcf5
commit 0afef9f3cd
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F

View File

@ -1,10 +1,10 @@
import { Injectable } from '@angular/core';
import { MessageService } from './message.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { UserCreds } from './userCreds'
import jwt_decode from 'jwt-decode'
import { Observable, interval, Subject, Subscription } from 'rxjs'
import { map, takeUntil, takeWhile } from 'rxjs/operators'
import { map, takeWhile } from 'rxjs/operators'
import { authserviceBaseUrl } from './config'
@ -22,6 +22,7 @@ export class TokenService {
public static Id_RefreshToken_Key : string = "id_refreshtoken";
private _expiryTime = new Subject<number>()
private subscription: Subscription
constructor(private http: HttpClient, private messageService: MessageService) {
}
@ -56,7 +57,10 @@ export class TokenService {
let exp = jwt_decode(token)["exp"]
let iat = jwt_decode(token)["iat"]
let start = exp - iat
interval(1000).pipe(map(v => start - v)).pipe(takeWhile(v => v != 0)).subscribe(v => this._expiryTime.next(v))
if (this.subscription && !this.subscription.closed) {
this.subscription.unsubscribe()
}
this.subscription = interval(1000).pipe(map(v => start - v)).pipe(takeWhile(v => v != 0)).subscribe(v => this._expiryTime.next(v))
}
async login(login: string, password: string) : Promise<void> {