blob: 03aa3b8a56ad1f6cb86525f06c9df50a325c67f6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
import { Injectable, NgZone, OnDestroy } from '@angular/core';
import { BehaviorSubject, interval, Subscription } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class RefreshIntervalService implements OnDestroy {
private intervalTime: number;
// Observable sources
private intervalDataSource = new BehaviorSubject(null);
private intervalSubscription: Subscription;
// Observable streams
intervalData$ = this.intervalDataSource.asObservable();
constructor(private ngZone: NgZone) {
const initialInterval = parseInt(sessionStorage.getItem('dashboard_interval'), 10) || 5000;
this.setRefreshInterval(initialInterval);
}
setRefreshInterval(newInterval: number) {
this.intervalTime = newInterval;
sessionStorage.setItem('dashboard_interval', newInterval.toString());
if (this.intervalSubscription) {
this.intervalSubscription.unsubscribe();
}
this.ngZone.runOutsideAngular(() => {
this.intervalSubscription = interval(this.intervalTime).subscribe(() =>
this.ngZone.run(() => {
this.intervalDataSource.next(this.intervalTime);
})
);
});
}
getRefreshInterval() {
return this.intervalTime;
}
ngOnDestroy() {
if (this.intervalSubscription) {
this.intervalSubscription.unsubscribe();
}
}
}
|