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

100% Statements 22/22
100% Branches 2/2
100% Functions 5/5
100% Lines 19/19

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
    43
    4492x

    92x

    92x

    92x

    23x

    23x

    23x
    23x

    92x
    25x
    25x

    2x

    25x
    35x

    92x
    4x

    92x

    1x

    92x
     import { Injectable, OnDestroy } from '@angular/core';

    import { BehaviorSubject, interval, Subscription } from 'rxjs';

    import { ServicesModule } from './services.module';
    @Injectable({
      providedIn: ServicesModule
    })
    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() {
        if (this.intervalSubscription) {
          this.intervalSubscription.unsubscribe();
        }
      }
    }