diff options
Diffstat (limited to 'client/src/standalone/player')
-rw-r--r-- | client/src/standalone/player/definitions.ts | 2 | ||||
-rw-r--r-- | client/src/standalone/player/events.ts | 14 | ||||
-rw-r--r-- | client/src/standalone/player/player.ts | 31 |
3 files changed, 26 insertions, 21 deletions
diff --git a/client/src/standalone/player/definitions.ts b/client/src/standalone/player/definitions.ts index cc5203ed5..495f1a98c 100644 --- a/client/src/standalone/player/definitions.ts +++ b/client/src/standalone/player/definitions.ts | |||
@@ -21,5 +21,5 @@ export type PeerTubeTextTrack = { | |||
21 | id: string | 21 | id: string |
22 | label: string | 22 | label: string |
23 | src: string | 23 | src: string |
24 | mode: 'showing' | 'disabled' | 24 | mode: TextTrackMode |
25 | } | 25 | } |
diff --git a/client/src/standalone/player/events.ts b/client/src/standalone/player/events.ts index 28a13c727..7a8e9dbec 100644 --- a/client/src/standalone/player/events.ts +++ b/client/src/standalone/player/events.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import { EventHandler } from './definitions' | 1 | import { EventHandler } from './definitions' |
2 | 2 | ||
3 | interface PlayerEventRegistrar { | 3 | interface PlayerEventRegistrar { |
4 | registrations: Function[] | 4 | registrations: EventHandler<any>[] |
5 | } | 5 | } |
6 | 6 | ||
7 | interface PlayerEventRegistrationMap { | 7 | interface PlayerEventRegistrationMap { |
@@ -20,28 +20,28 @@ export class EventRegistrar { | |||
20 | 20 | ||
21 | public registerTypes (names: string[]) { | 21 | public registerTypes (names: string[]) { |
22 | for (const name of names) { | 22 | for (const name of names) { |
23 | this.eventRegistrations[ name ] = { registrations: [] } | 23 | this.eventRegistrations[name] = { registrations: [] } |
24 | } | 24 | } |
25 | } | 25 | } |
26 | 26 | ||
27 | public fire<T> (name: string, event: T) { | 27 | public fire<T> (name: string, event: T) { |
28 | this.eventRegistrations[ name ].registrations.forEach(x => x(event)) | 28 | this.eventRegistrations[name].registrations.forEach(x => x(event)) |
29 | } | 29 | } |
30 | 30 | ||
31 | public addListener<T> (name: string, handler: EventHandler<T>) { | 31 | public addListener<T> (name: string, handler: EventHandler<T>) { |
32 | if (!this.eventRegistrations[ name ]) { | 32 | if (!this.eventRegistrations[name]) { |
33 | console.warn(`PeerTube: addEventListener(): The event '${name}' is not supported`) | 33 | console.warn(`PeerTube: addEventListener(): The event '${name}' is not supported`) |
34 | return false | 34 | return false |
35 | } | 35 | } |
36 | 36 | ||
37 | this.eventRegistrations[ name ].registrations.push(handler) | 37 | this.eventRegistrations[name].registrations.push(handler) |
38 | return true | 38 | return true |
39 | } | 39 | } |
40 | 40 | ||
41 | public removeListener<T> (name: string, handler: EventHandler<T>) { | 41 | public removeListener<T> (name: string, handler: EventHandler<T>) { |
42 | if (!this.eventRegistrations[ name ]) return false | 42 | if (!this.eventRegistrations[name]) return false |
43 | 43 | ||
44 | this.eventRegistrations[ name ].registrations = this.eventRegistrations[ name ].registrations.filter(x => x === handler) | 44 | this.eventRegistrations[name].registrations = this.eventRegistrations[name].registrations.filter(x => x === handler) |
45 | 45 | ||
46 | return true | 46 | return true |
47 | } | 47 | } |
diff --git a/client/src/standalone/player/player.ts b/client/src/standalone/player/player.ts index 9776fda12..bbe37a42b 100644 --- a/client/src/standalone/player/player.ts +++ b/client/src/standalone/player/player.ts | |||
@@ -17,7 +17,7 @@ const PASSTHROUGH_EVENTS = [ | |||
17 | */ | 17 | */ |
18 | export class PeerTubePlayer { | 18 | export class PeerTubePlayer { |
19 | 19 | ||
20 | private eventRegistrar: EventRegistrar = new EventRegistrar() | 20 | private readonly eventRegistrar: EventRegistrar = new EventRegistrar() |
21 | private channel: Channel.MessagingChannel | 21 | private channel: Channel.MessagingChannel |
22 | private readyPromise: Promise<void> | 22 | private readyPromise: Promise<void> |
23 | 23 | ||
@@ -31,8 +31,8 @@ export class PeerTubePlayer { | |||
31 | * @param scope | 31 | * @param scope |
32 | */ | 32 | */ |
33 | constructor ( | 33 | constructor ( |
34 | private embedElement: HTMLIFrameElement, | 34 | private readonly embedElement: HTMLIFrameElement, |
35 | private scope?: string | 35 | private readonly scope?: string |
36 | ) { | 36 | ) { |
37 | this.eventRegistrar.registerTypes(PASSTHROUGH_EVENTS) | 37 | this.eventRegistrar.registerTypes(PASSTHROUGH_EVENTS) |
38 | 38 | ||
@@ -90,6 +90,7 @@ export class PeerTubePlayer { | |||
90 | 90 | ||
91 | /** | 91 | /** |
92 | * Tell the embed to change the audio volume | 92 | * Tell the embed to change the audio volume |
93 | * | ||
93 | * @param value A number from 0 to 1 | 94 | * @param value A number from 0 to 1 |
94 | */ | 95 | */ |
95 | async setVolume (value: number) { | 96 | async setVolume (value: number) { |
@@ -98,14 +99,16 @@ export class PeerTubePlayer { | |||
98 | 99 | ||
99 | /** | 100 | /** |
100 | * Get the current volume level in the embed. | 101 | * Get the current volume level in the embed. |
102 | * | ||
101 | * @param value A number from 0 to 1 | 103 | * @param value A number from 0 to 1 |
102 | */ | 104 | */ |
103 | async getVolume (): Promise<number> { | 105 | async getVolume (): Promise<number> { |
104 | return this.sendMessage<void, number>('getVolume') | 106 | return this.sendMessage<undefined, number>('getVolume') |
105 | } | 107 | } |
106 | 108 | ||
107 | /** | 109 | /** |
108 | * Tell the embed to change the current caption | 110 | * Tell the embed to change the current caption |
111 | * | ||
109 | * @param value Caption id | 112 | * @param value Caption id |
110 | */ | 113 | */ |
111 | async setCaption (value: string) { | 114 | async setCaption (value: string) { |
@@ -116,11 +119,12 @@ export class PeerTubePlayer { | |||
116 | * Get video captions | 119 | * Get video captions |
117 | */ | 120 | */ |
118 | async getCaptions (): Promise<PeerTubeTextTrack[]> { | 121 | async getCaptions (): Promise<PeerTubeTextTrack[]> { |
119 | return this.sendMessage<void, PeerTubeTextTrack[]>('getCaptions') | 122 | return this.sendMessage<undefined, PeerTubeTextTrack[]>('getCaptions') |
120 | } | 123 | } |
121 | 124 | ||
122 | /** | 125 | /** |
123 | * Tell the embed to seek to a specific position (in seconds) | 126 | * Tell the embed to seek to a specific position (in seconds) |
127 | * | ||
124 | * @param seconds | 128 | * @param seconds |
125 | */ | 129 | */ |
126 | async seek (seconds: number) { | 130 | async seek (seconds: number) { |
@@ -143,21 +147,21 @@ export class PeerTubePlayer { | |||
143 | * resolutions change. | 147 | * resolutions change. |
144 | */ | 148 | */ |
145 | async getResolutions (): Promise<PeerTubeResolution[]> { | 149 | async getResolutions (): Promise<PeerTubeResolution[]> { |
146 | return this.sendMessage<void, PeerTubeResolution[]>('getResolutions') | 150 | return this.sendMessage<undefined, PeerTubeResolution[]>('getResolutions') |
147 | } | 151 | } |
148 | 152 | ||
149 | /** | 153 | /** |
150 | * Retrieve a list of available playback rates. | 154 | * Retrieve a list of available playback rates. |
151 | */ | 155 | */ |
152 | async getPlaybackRates (): Promise<number[]> { | 156 | async getPlaybackRates (): Promise<number[]> { |
153 | return this.sendMessage<void, number[]>('getPlaybackRates') | 157 | return this.sendMessage<undefined, number[]>('getPlaybackRates') |
154 | } | 158 | } |
155 | 159 | ||
156 | /** | 160 | /** |
157 | * Get the current playback rate. Defaults to 1 (1x playback rate). | 161 | * Get the current playback rate. Defaults to 1 (1x playback rate). |
158 | */ | 162 | */ |
159 | async getPlaybackRate (): Promise<number> { | 163 | async getPlaybackRate (): Promise<number> { |
160 | return this.sendMessage<void, number>('getPlaybackRate') | 164 | return this.sendMessage<undefined, number>('getPlaybackRate') |
161 | } | 165 | } |
162 | 166 | ||
163 | /** | 167 | /** |
@@ -188,7 +192,7 @@ export class PeerTubePlayer { | |||
188 | * Get video position currently played (starts from 1) | 192 | * Get video position currently played (starts from 1) |
189 | */ | 193 | */ |
190 | async getCurrentPosition () { | 194 | async getCurrentPosition () { |
191 | return this.sendMessage<void, number>('getCurrentPosition') | 195 | return this.sendMessage<undefined, number>('getCurrentPosition') |
192 | } | 196 | } |
193 | 197 | ||
194 | private constructChannel () { | 198 | private constructChannel () { |
@@ -201,8 +205,8 @@ export class PeerTubePlayer { | |||
201 | } | 205 | } |
202 | 206 | ||
203 | private prepareToBeReady () { | 207 | private prepareToBeReady () { |
204 | let readyResolve: Function | 208 | let readyResolve: () => void |
205 | let readyReject: Function | 209 | let readyReject: () => void |
206 | 210 | ||
207 | this.readyPromise = new Promise<void>((res, rej) => { | 211 | this.readyPromise = new Promise<void>((res, rej) => { |
208 | readyResolve = res | 212 | readyResolve = res |
@@ -219,7 +223,8 @@ export class PeerTubePlayer { | |||
219 | private sendMessage<TIn, TOut> (method: string, params?: TIn): Promise<TOut> { | 223 | private sendMessage<TIn, TOut> (method: string, params?: TIn): Promise<TOut> { |
220 | return new Promise<TOut>((resolve, reject) => { | 224 | return new Promise<TOut>((resolve, reject) => { |
221 | this.channel.call({ | 225 | this.channel.call({ |
222 | method, params, | 226 | method, |
227 | params, | ||
223 | success: result => resolve(result), | 228 | success: result => resolve(result), |
224 | error: error => reject(error) | 229 | error: error => reject(error) |
225 | }) | 230 | }) |
@@ -228,4 +233,4 @@ export class PeerTubePlayer { | |||
228 | } | 233 | } |
229 | 234 | ||
230 | // put it on the window as well as the export | 235 | // put it on the window as well as the export |
231 | (window[ 'PeerTubePlayer' ] as any) = PeerTubePlayer | 236 | (window['PeerTubePlayer'] as any) = PeerTubePlayer |