import { EventEmitter , Injectable } from '@angular/core' ; import { Client } from '../../services/api/client' ; import { Session } from '../../services/session' ; import { BehaviorSubject , ReplaySubject , interval } from 'rxjs' ; import { startWith } from 'rxjs/operators' ; export type JitsiConfig = { roomName : string ; username : string ; } ; @ Injectable ( ) export class VideoChatService { static heartBeatIntervalSeconds = 5 ; isActive : boolean ; activate$ : EventEmitter < JitsiConfig | false > = new EventEmitter < JitsiConfig | false > ( ) ; heartBeatSubscription ; constructor ( private client , private session ) { } async activate ( entity ) { if ( this . isActive ) { return ; } try { const roomName = await this . getRoomName ( entity ) ; this . isActive = true ; this . activate$ . emit ( { username : this . session . getLoggedInUser ( ) . username , roomName : roomName , } ) ; if ( this . heartBeatSubscription ) this . heartBeatSubscription . unsubscribe ( ) ; this . heartBeatSubscription = interval ( VideoChatService . heartBeatIntervalSeconds * 1000 ) . pipe ( startWith ( 0 ) ) . subscribe ( ( ) => this . heartBeat ( entity . guid ) ) ; } catch ( e ) { console . error ( 'Error trying to open video chat.' ) ; console . error ( e ) ; } } deactivate ( ) { this . isActive = false ; this . activate$ . emit ( false ) ; if ( this . heartBeatSubscription ) this . heartBeatSubscription . unsubscribe ( ) ; } public async heartBeat ( key ) { await this . client . put ( `api/v2/notifications/markers/heartbeat/${ key }` ) ; } private async getRoomName ( entity ) { const response = await this . client . get ( `api/v2/video/room/${ entity . guid }` ) ; return response . room ; } }