diff options
Diffstat (limited to 'client/src/standalone/videos/shared/live-manager.ts')
-rw-r--r-- | client/src/standalone/videos/shared/live-manager.ts | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/client/src/standalone/videos/shared/live-manager.ts b/client/src/standalone/videos/shared/live-manager.ts new file mode 100644 index 000000000..422d39793 --- /dev/null +++ b/client/src/standalone/videos/shared/live-manager.ts | |||
@@ -0,0 +1,69 @@ | |||
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 | |||
9 | constructor ( | ||
10 | private readonly playerHTML: PlayerHTML | ||
11 | ) { | ||
12 | |||
13 | } | ||
14 | |||
15 | async displayInfoAndListenForChanges (options: { | ||
16 | video: VideoDetails | ||
17 | translations: Translations | ||
18 | onPublishedVideo: () => any | ||
19 | }) { | ||
20 | const { video, onPublishedVideo } = options | ||
21 | |||
22 | this.displayAppropriateInfo(options) | ||
23 | |||
24 | if (!this.liveSocket) { | ||
25 | const io = (await import('socket.io-client')).io | ||
26 | this.liveSocket = io(window.location.origin + '/live-videos') | ||
27 | } | ||
28 | |||
29 | this.liveSocket.on('state-change', (payload: LiveVideoEventPayload) => { | ||
30 | if (payload.state === VideoState.PUBLISHED) { | ||
31 | this.playerHTML.removeInformation() | ||
32 | onPublishedVideo() | ||
33 | return | ||
34 | } | ||
35 | }) | ||
36 | |||
37 | this.liveSocket.emit('subscribe', { videoId: video.id }) | ||
38 | } | ||
39 | |||
40 | stopListeningForChanges (video: VideoDetails) { | ||
41 | this.liveSocket.emit('unsubscribe', { videoId: video.id }) | ||
42 | } | ||
43 | |||
44 | private displayAppropriateInfo (options: { | ||
45 | video: VideoDetails | ||
46 | translations: Translations | ||
47 | }) { | ||
48 | const { video, translations } = options | ||
49 | |||
50 | if (video.state.id === VideoState.WAITING_FOR_LIVE) { | ||
51 | this.displayWaitingForLiveInfo(translations) | ||
52 | return | ||
53 | } | ||
54 | |||
55 | if (video.state.id === VideoState.LIVE_ENDED) { | ||
56 | this.displayEndedLiveInfo(translations) | ||
57 | return | ||
58 | } | ||
59 | } | ||
60 | |||
61 | private displayWaitingForLiveInfo (translations: Translations) { | ||
62 | this.playerHTML.displayInformation('This live has not started yet.', translations) | ||
63 | } | ||
64 | |||
65 | private displayEndedLiveInfo (translations: Translations) { | ||
66 | this.playerHTML.displayInformation('This live has ended.', translations) | ||
67 | |||
68 | } | ||
69 | } | ||