1- import _ from 'lodash' ;
1+ // @flow
2+
23import Sister from 'sister' ;
34import loadYouTubeIframeApi from './loadYouTubeIframeApi' ;
45import YouTubePlayer from './YouTubePlayer' ;
6+ import type {
7+ YouTubePlayerType
8+ } from './types' ;
59
610/**
7- * @typedef options
811 * @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
1412 */
13+ type OptionsType = {
14+ events ?: Object ,
15+ height ?: number ,
16+ playerVars ?: Object ,
17+ videoId ?: string ,
18+ width ?: number
19+ } ;
1520
1621/**
1722 * @typedef YT.Player
@@ -23,15 +28,14 @@ let youtubeIframeAPI;
2328/**
2429 * A factory function used to produce an instance of YT.Player and queue function calls and proxy events of the resulting object.
2530 *
26- * @param { YT.Player|HTMLElement|String } elementId Either An existing YT.Player instance,
31+ * @param elementId Either An existing YT.Player instance,
2732 * 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
3035 * an acceptable state when calling supported functions. Default: `false`.
3136 * See `FunctionStateMap.js` for supported functions and acceptable states.
32- * @returns {Object }
3337 */
34- export default ( elementId , options = { } , strictState = false ) => {
38+ export default ( maybeElementId : YouTubePlayerType | HTMLElement | string , options : OptionsType = { } , strictState : boolean = false ) => {
3539 const emitter = Sister ( ) ;
3640
3741 if ( ! youtubeIframeAPI ) {
@@ -42,37 +46,34 @@ export default (elementId, options = {}, strictState = false) => {
4246 throw new Error ( 'Event handlers cannot be overwritten.' ) ;
4347 }
4448
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.' ) ;
4751 }
4852
4953 options . events = YouTubePlayer . proxyEvents ( emitter ) ;
5054
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 ) {
6257 const YT = await youtubeIframeAPI ;
6358
64- player = new YT . Player ( elementId , options ) ;
59+ const player : YouTubePlayerType = new YT . Player ( maybeElementId , options ) ;
6560
6661 emitter . on ( 'ready' , ( ) => {
6762 resolve ( player ) ;
6863 } ) ;
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.' ) ;
6970 }
7071 } ) ;
7172
72- const playerAPI = YouTubePlayer . promisifyPlayer ( playerAPIReady , strictState ) ;
73+ const playerApi = YouTubePlayer . promisifyPlayer ( playerAPIReady , strictState ) ;
7374
74- playerAPI . on = emitter . on ;
75- playerAPI . off = emitter . off ;
75+ playerApi . on = emitter . on ;
76+ playerApi . off = emitter . off ;
7677
77- return playerAPI ;
78+ return playerApi ;
7879} ;
0 commit comments