]>
Commit | Line | Data |
---|---|---|
d3f4689b C |
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' | |
5 | ||
6 | export class LiveManager { | |
7 | private liveSocket: Socket | |
8 | ||
b1934b7e C |
9 | private listeners = new Map<string, (payload: LiveVideoEventPayload) => void>() |
10 | ||
d3f4689b C |
11 | constructor ( |
12 | private readonly playerHTML: PlayerHTML | |
13 | ) { | |
14 | ||
15 | } | |
16 | ||
c2419476 | 17 | async listenForChanges (options: { |
d3f4689b | 18 | video: VideoDetails |
d3f4689b C |
19 | onPublishedVideo: () => any |
20 | }) { | |
21 | const { video, onPublishedVideo } = options | |
22 | ||
d3f4689b C |
23 | if (!this.liveSocket) { |
24 | const io = (await import('socket.io-client')).io | |
25 | this.liveSocket = io(window.location.origin + '/live-videos') | |
26 | } | |
27 | ||
b1934b7e | 28 | const listener = (payload: LiveVideoEventPayload) => { |
d3f4689b C |
29 | if (payload.state === VideoState.PUBLISHED) { |
30 | this.playerHTML.removeInformation() | |
31 | onPublishedVideo() | |
32 | return | |
33 | } | |
b1934b7e C |
34 | } |
35 | ||
36 | this.liveSocket.on('state-change', listener) | |
37 | this.listeners.set(video.uuid, listener) | |
d3f4689b C |
38 | |
39 | this.liveSocket.emit('subscribe', { videoId: video.id }) | |
40 | } | |
41 | ||
42 | stopListeningForChanges (video: VideoDetails) { | |
b1934b7e C |
43 | const listener = this.listeners.get(video.uuid) |
44 | if (listener) { | |
45 | this.liveSocket.off('state-change', listener) | |
46 | } | |
47 | ||
d3f4689b C |
48 | this.liveSocket.emit('unsubscribe', { videoId: video.id }) |
49 | } | |
50 | ||
c2419476 C |
51 | displayInfo (options: { |
52 | state: VideoState | |
d3f4689b C |
53 | translations: Translations |
54 | }) { | |
c2419476 | 55 | const { state, translations } = options |
d3f4689b | 56 | |
c2419476 | 57 | if (state === VideoState.WAITING_FOR_LIVE) { |
d3f4689b C |
58 | this.displayWaitingForLiveInfo(translations) |
59 | return | |
60 | } | |
61 | ||
c2419476 | 62 | if (state === VideoState.LIVE_ENDED) { |
d3f4689b C |
63 | this.displayEndedLiveInfo(translations) |
64 | return | |
65 | } | |
66 | } | |
67 | ||
68 | private displayWaitingForLiveInfo (translations: Translations) { | |
69 | this.playerHTML.displayInformation('This live has not started yet.', translations) | |
70 | } | |
71 | ||
72 | private displayEndedLiveInfo (translations: Translations) { | |
73 | this.playerHTML.displayInformation('This live has ended.', translations) | |
74 | ||
75 | } | |
76 | } |