import { Injectable } from '@angular/core' ; import { StorageService } from './storage.service' ; import { Constants } from '../Common/Constants' ; import { Observable , interval , Subscription , timer , Subject } from 'rxjs' ; @ Injectable ( { providedIn : 'root' } ) export class RefreshService { public isRefreshing = false ; public refreshRate = '' ; private autoRefreshInterval : Observable < any > = null ; public refreshSubject : Subject < number > = new Subject ( ) ; private currentSync : Subscription ; private previousRefreshSetting = 0 ; public refreshTick = 0 ; constructor ( private storage ) { } public init ( ) { const defaultRefreshInterval = this . storage . getValueNumber ( Constants . AutoRefreshIntervalStorageKey , Constants . DefaultAutoRefreshInterval ) ; this . updateRefreshInterval ( defaultRefreshInterval . toString ( ) , true ) ; } public refreshAll ( ) { this . refreshSubject . next ( this . refreshTick ) ; this . refreshTick ++ ; this . isRefreshing = true ; try { timer ( 1000 ) . subscribe ( ( ) => this . isRefreshing = false ) ; } catch { this . isRefreshing = false ; } } public updateRefreshInterval ( newValue , noRefresh = false ) { this . storage . setValue ( Constants . AutoRefreshIntervalStorageKey , newValue ) ; if ( this . autoRefreshInterval ) { this . currentSync . unsubscribe ( ) ; this . autoRefreshInterval = null ; } const newInterval = parseInt ( newValue , 10 ) ; this . refreshRate = newValue ; if ( newInterval > 0 ) { console . log ( 'Auto refresh interval = ' + newInterval + ' seconds' ) ; this . autoRefreshInterval = interval ( newInterval * 1000 ) ; this . currentSync = this . autoRefreshInterval . subscribe ( ( ) => this . refreshAll ( ) ) ; if ( ! noRefresh && ( this . previousRefreshSetting === 0 || newInterval < this . previousRefreshSetting ) ) { this . refreshAll ( ) ; } } this . previousRefreshSetting = newInterval ; } }