All files / src/app/shared/services refresh-interval.service.ts

100% Statements 23/23
83.33% Branches 5/6
100% Functions 6/6
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 4398x   98x         98x     25x     25x     25x 25x     98x 27x 27x   27x 2x   27x 63x       98x 4x     98x 1x 1x     98x  
import { Injectable, 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() {
    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.intervalSubscription = interval(this.intervalTime).subscribe(() =>
      this.intervalDataSource.next(this.intervalTime)
    );
  }
 
  getRefreshInterval() {
    return this.intervalTime;
  }
 
  ngOnDestroy() {
    Eif (this.intervalSubscription) {
      this.intervalSubscription.unsubscribe();
    }
  }
}