1 import { Injectable, NgZone, OnDestroy } from '@angular/core';
3 import { BehaviorSubject, interval, Subscription } from 'rxjs';
8 export class RefreshIntervalService implements OnDestroy {
9 private intervalTime: number;
11 private intervalDataSource = new BehaviorSubject(null);
12 private intervalSubscription: Subscription;
14 intervalData$ = this.intervalDataSource.asObservable();
16 constructor(private ngZone: NgZone) {
17 const initialInterval = parseInt(sessionStorage.getItem('dashboard_interval'), 10) || 5000;
18 this.setRefreshInterval(initialInterval);
21 setRefreshInterval(newInterval: number) {
22 this.intervalTime = newInterval;
23 sessionStorage.setItem('dashboard_interval', newInterval.toString());
25 if (this.intervalSubscription) {
26 this.intervalSubscription.unsubscribe();
28 this.ngZone.runOutsideAngular(() => {
29 this.intervalSubscription = interval(this.intervalTime).subscribe(() =>
30 this.ngZone.run(() => {
31 this.intervalDataSource.next(this.intervalTime);
37 getRefreshInterval() {
38 return this.intervalTime;
42 if (this.intervalSubscription) {
43 this.intervalSubscription.unsubscribe();