webnaplo-gulbaba-frontend/src/app/app.component.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-11-11 12:00:45 +01:00
import { Component, OnDestroy, OnInit } from '@angular/core';
import { AuthService } from "./auth/auth.service";
import { FaultManagerService } from "./fault-manager/fault-manager.service";
2018-11-12 07:08:57 +01:00
import { Subscription, timer } from "rxjs";
2018-11-11 12:00:45 +01:00
// timers are in milliseconds
const RENEW_TIMER_INITIAL = 300000; // 5min
const RENEW_TIMER_PERIOD = 300000; // 5min
const FAULT_RELOAD_PERIOD = 5000;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy {
private authRenewTimer: Subscription;
private faultReloadTimer: Subscription;
constructor(private authService: AuthService,
private faultManager: FaultManagerService) {}
ngOnInit(): void {
2018-11-12 07:08:57 +01:00
const authRenewObservable = timer(RENEW_TIMER_INITIAL, RENEW_TIMER_PERIOD);
2018-11-11 12:00:45 +01:00
this.authRenewTimer = authRenewObservable.subscribe(() => {
if (this.authService.isLoggedIn) {
this.authService.renew();
}
});
2018-11-12 07:08:57 +01:00
const faultReloadObservable = timer(FAULT_RELOAD_PERIOD, FAULT_RELOAD_PERIOD);
2018-11-11 12:00:45 +01:00
this.faultReloadTimer = faultReloadObservable.subscribe(() => {
if (this.authService.isLoggedIn) {
this.faultManager.list().subscribe(faults => this.faultManager.faults = faults);
}
});
}
ngOnDestroy(): void {
this.authRenewTimer.unsubscribe();
this.faultReloadTimer.unsubscribe();
}
get isLoggedIn(): boolean {
return this.authService.isLoggedIn;
}
}