1 import { Socket } from 'socket.io-client'
2 import { LiveVideoEventPayload, VideoDetails, VideoState } from '../../../../../shared/models'
3 import { PlayerHTML } from './player-html'
4 import { Translations } from './translations'
6 export class LiveManager {
7 private liveSocket: Socket
9 private listeners = new Map<string, (payload: LiveVideoEventPayload) => void>()
12 private readonly playerHTML: PlayerHTML
17 async displayInfoAndListenForChanges (options: {
19 translations: Translations
20 onPublishedVideo: () => any
22 const { video, onPublishedVideo } = options
24 this.displayAppropriateInfo(options)
26 if (!this.liveSocket) {
27 const io = (await import('socket.io-client')).io
28 this.liveSocket = io(window.location.origin + '/live-videos')
31 const listener = (payload: LiveVideoEventPayload) => {
32 if (payload.state === VideoState.PUBLISHED) {
33 this.playerHTML.removeInformation()
39 this.liveSocket.on('state-change', listener)
40 this.listeners.set(video.uuid, listener)
42 this.liveSocket.emit('subscribe', { videoId: video.id })
45 stopListeningForChanges (video: VideoDetails) {
46 const listener = this.listeners.get(video.uuid)
48 this.liveSocket.off('state-change', listener)
51 this.liveSocket.emit('unsubscribe', { videoId: video.id })
54 private displayAppropriateInfo (options: {
56 translations: Translations
58 const { video, translations } = options
60 if (video.state.id === VideoState.WAITING_FOR_LIVE) {
61 this.displayWaitingForLiveInfo(translations)
65 if (video.state.id === VideoState.LIVE_ENDED) {
66 this.displayEndedLiveInfo(translations)
71 private displayWaitingForLiveInfo (translations: Translations) {
72 this.playerHTML.displayInformation('This live has not started yet.', translations)
75 private displayEndedLiveInfo (translations: Translations) {
76 this.playerHTML.displayInformation('This live has ended.', translations)