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