1
- import _ from 'lodash' ;
1
+ // @flow
2
+
2
3
import Sister from 'sister' ;
3
4
import loadYouTubeIframeApi from './loadYouTubeIframeApi' ;
4
5
import YouTubePlayer from './YouTubePlayer' ;
6
+ import type {
7
+ YouTubePlayerType
8
+ } from './types' ;
5
9
6
10
/**
7
- * @typedef options
8
11
* @see https://developers.google.com/youtube/iframe_api_reference#Loading_a_Video_Player
9
- * @param {Number } width
10
- * @param {Number } height
11
- * @param {String } videoId
12
- * @param {Object } playerVars
13
- * @param {Object } events
14
12
*/
13
+ type OptionsType = {
14
+ events ?: Object ,
15
+ height ?: number ,
16
+ playerVars ?: Object ,
17
+ videoId ?: string ,
18
+ width ?: number
19
+ } ;
15
20
16
21
/**
17
22
* @typedef YT.Player
@@ -23,15 +28,14 @@ let youtubeIframeAPI;
23
28
/**
24
29
* A factory function used to produce an instance of YT.Player and queue function calls and proxy events of the resulting object.
25
30
*
26
- * @param { YT.Player|HTMLElement|String } elementId Either An existing YT.Player instance,
31
+ * @param elementId Either An existing YT.Player instance,
27
32
* the DOM element or the id of the HTML element where the API will insert an <iframe>.
28
- * @param { YouTubePlayer~options } options See `options` (Ignored when using an existing YT.Player instance).
29
- * @param { boolean } strictState A flag designating whether or not to wait for
33
+ * @param options See `options` (Ignored when using an existing YT.Player instance).
34
+ * @param strictState A flag designating whether or not to wait for
30
35
* an acceptable state when calling supported functions. Default: `false`.
31
36
* See `FunctionStateMap.js` for supported functions and acceptable states.
32
- * @returns {Object }
33
37
*/
34
- export default ( elementId , options = { } , strictState = false ) => {
38
+ export default ( maybeElementId : YouTubePlayerType | HTMLElement | string , options : OptionsType = { } , strictState : boolean = false ) => {
35
39
const emitter = Sister ( ) ;
36
40
37
41
if ( ! youtubeIframeAPI ) {
@@ -42,37 +46,34 @@ export default (elementId, options = {}, strictState = false) => {
42
46
throw new Error ( 'Event handlers cannot be overwritten.' ) ;
43
47
}
44
48
45
- if ( _ . isString ( elementId ) && ! document . getElementById ( elementId ) ) {
46
- throw new Error ( 'Element "' + elementId + '" does not exist.' ) ;
49
+ if ( typeof maybeElementId === 'string' && ! document . getElementById ( maybeElementId ) ) {
50
+ throw new Error ( 'Element "' + maybeElementId + '" does not exist.' ) ;
47
51
}
48
52
49
53
options . events = YouTubePlayer . proxyEvents ( emitter ) ;
50
54
51
- const playerAPIReady = new Promise ( async ( resolve ) => {
52
- let player ;
53
-
54
- if (
55
- elementId instanceof Object &&
56
- elementId . playVideo instanceof Function
57
- ) {
58
- player = elementId ;
59
-
60
- resolve ( player ) ;
61
- } else {
55
+ const playerAPIReady = new Promise ( async ( resolve : ( result : YouTubePlayerType ) = > void ) => {
56
+ if ( typeof maybeElementId === 'string' || maybeElementId instanceof HTMLElement ) {
62
57
const YT = await youtubeIframeAPI ;
63
58
64
- player = new YT . Player ( elementId , options ) ;
59
+ const player : YouTubePlayerType = new YT . Player ( maybeElementId , options ) ;
65
60
66
61
emitter . on ( 'ready' , ( ) => {
67
62
resolve ( player ) ;
68
63
} ) ;
64
+ } else if ( typeof maybeElementId === 'object' && maybeElementId . playVideo instanceof Function ) {
65
+ const player : YouTubePlayerType = maybeElementId ;
66
+
67
+ resolve ( player ) ;
68
+ } else {
69
+ throw new TypeError ( 'Unexpected state.' ) ;
69
70
}
70
71
} ) ;
71
72
72
- const playerAPI = YouTubePlayer . promisifyPlayer ( playerAPIReady , strictState ) ;
73
+ const playerApi = YouTubePlayer . promisifyPlayer ( playerAPIReady , strictState ) ;
73
74
74
- playerAPI . on = emitter . on ;
75
- playerAPI . off = emitter . off ;
75
+ playerApi . on = emitter . on ;
76
+ playerApi . off = emitter . off ;
76
77
77
- return playerAPI ;
78
+ return playerApi ;
78
79
} ;
0 commit comments