import { isPlatformServer } from '@angular/common' ; import { Inject , Injectable , OnDestroy , PLATFORM_ID } from '@angular/core' ; import { time } from 'console' ; import { combineLatest , interval , Observable , of , Subject , Subscription , timer , } from 'rxjs' ; import { combineAll , switchMap , take , tap , throttleTime , timeout , } from 'rxjs/operators' ; import { ApiService } from '../../api/api.service' ; const UNREAD_CHECK_INTERVAL = 15000 ; @ Injectable ( { providedIn : 'root' } ) export class ChatIconService implements OnDestroy { /** Loading is in progress */ inProgress = false ; /** The subscription of the current poller */ pollingSubscription : Subscription ; /** The interval observable */ interval$ = interval ( UNREAD_CHECK_INTERVAL ) ; /** The current unread count */ private _unread$ : Subject < number > = new Subject ( ) ; /** Starts the polling and return the count */ unread$ : Observable < number > = of ( 0 ) . pipe ( tap ( ( ) => this . startPolling ( ) ) , switchMap ( ( ) => this . _unread$ ) ) ; constructor ( private api , @ Inject ( PLATFORM_ID ) private platformId ) { } returns startPolling ( ) { if ( this . pollingSubscription ) return ; if ( isPlatformServer ( this . platformId ) ) return ; this . loadCount ( ) ; this . pollingSubscription = this . interval$ . subscribe ( ( ) => { this . loadCount ( ) ; } ) ; } returns stopPolling ( ) { if ( this . pollingSubscription ) { this . pollingSubscription . unsubscribe ( ) ; this . pollingSubscription = null ; } } /**
     * Load the count
     */ async loadCount ( ) { if ( this . inProgress ) return ; this . inProgress = true ; try { const response = await this . api . get ( 'api/v3/matrix/total-unread' ) . toPromise ( ) ; this . _unread$ . next ( Number ( response ?. total_unread ) ) ; } catch ( err ) { } finally { this . inProgress = false ; } } /**
     * Cleanup subscriptions
     */ ngOnDestroy ( ) { if ( this . pollingSubscription ) { this . pollingSubscription . unsubscribe ( ) ; } } }