import { Component, OnDestroy, OnInit } from '@angular/core'; import { AuthService } from "./auth/auth.service"; import { FaultManagerService } from "./fault-manager/fault-manager.service"; import { Subscription, timer } from "rxjs"; // 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 { const authRenewObservable = timer(RENEW_TIMER_INITIAL, RENEW_TIMER_PERIOD); this.authRenewTimer = authRenewObservable.subscribe(() => { if (this.authService.isLoggedIn) { this.authService.renew(); } }); const faultReloadObservable = timer(FAULT_RELOAD_PERIOD, FAULT_RELOAD_PERIOD); 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; } }