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