@@ -55,71 +55,78 @@ YouTubePlayer.promisifyPlayer = (playerAPIReady: Promise<YouTubePlayerType>, str
55
55
56
56
for ( const functionName of functionNames ) {
57
57
if ( strictState && FunctionStateMap [ functionName ] ) {
58
- functions [ functionName ] = async ( ...args ) => {
59
- const stateInfo = FunctionStateMap [ functionName ] ;
60
- const player = await playerAPIReady ;
61
- const playerState = player . getPlayerState ( ) ;
62
-
63
- // eslint-disable-next-line no-warning-comments
64
- // TODO: Just spread the args into the function once Babel is fixed:
65
- // https://github.com/babel/babel/issues/4270
66
- //
67
- // eslint-disable-next-line prefer-spread
68
- const value = player [ functionName ] . apply ( player , args ) ;
69
-
70
- // TRICKY: For functions like `seekTo`, a change in state must be
71
- // triggered given that the resulting state could match the initial
72
- // state.
73
- if (
74
- stateInfo . stateChangeRequired ||
75
-
76
- // eslint-disable-next-line no-extra-parens
77
- (
78
- Array . isArray ( stateInfo . acceptableStates ) &&
79
- stateInfo . acceptableStates . indexOf ( playerState ) === - 1
80
- )
81
- ) {
82
- await new Promise ( ( resolve ) => {
83
- const onPlayerStateChange = ( ) => {
84
- const playerStateAfterChange = player . getPlayerState ( ) ;
85
-
86
- let timeout ;
87
-
88
- if ( typeof stateInfo . timeout === 'number' ) {
89
- timeout = setTimeout ( ( ) => {
90
- player . removeEventListener ( 'onStateChange' , onPlayerStateChange ) ;
91
-
92
- resolve ( ) ;
93
- } , stateInfo . timeout ) ;
94
- }
95
-
96
- if (
58
+ functions [ functionName ] = ( ...args ) => {
59
+ return playerAPIReady
60
+ . then ( ( player ) => {
61
+ const stateInfo = FunctionStateMap [ functionName ] ;
62
+ const playerState = player . getPlayerState ( ) ;
63
+
64
+ // eslint-disable-next-line no-warning-comments
65
+ // TODO: Just spread the args into the function once Babel is fixed:
66
+ // https://github.com/babel/babel/issues/4270
67
+ //
68
+ // eslint-disable-next-line prefer-spread
69
+ const value = player [ functionName ] . apply ( player , args ) ;
70
+
71
+ // TRICKY: For functions like `seekTo`, a change in state must be
72
+ // triggered given that the resulting state could match the initial
73
+ // state.
74
+ if (
75
+ stateInfo . stateChangeRequired ||
76
+
77
+ // eslint-disable-next-line no-extra-parens
78
+ (
97
79
Array . isArray ( stateInfo . acceptableStates ) &&
98
- stateInfo . acceptableStates . indexOf ( playerStateAfterChange ) !== - 1
99
- ) {
100
- player . removeEventListener ( 'onStateChange' , onPlayerStateChange ) ;
101
-
102
- clearTimeout ( timeout ) ;
103
- resolve ( ) ;
104
- }
105
- } ;
106
-
107
- player . addEventListener ( 'onStateChange' , onPlayerStateChange ) ;
80
+ stateInfo . acceptableStates . indexOf ( playerState ) === - 1
81
+ )
82
+ ) {
83
+ return new Promise ( ( resolve ) => {
84
+ const onPlayerStateChange = ( ) => {
85
+ const playerStateAfterChange = player . getPlayerState ( ) ;
86
+
87
+ let timeout ;
88
+
89
+ if ( typeof stateInfo . timeout === 'number' ) {
90
+ timeout = setTimeout ( ( ) => {
91
+ player . removeEventListener ( 'onStateChange' , onPlayerStateChange ) ;
92
+
93
+ resolve ( ) ;
94
+ } , stateInfo . timeout ) ;
95
+ }
96
+
97
+ if (
98
+ Array . isArray ( stateInfo . acceptableStates ) &&
99
+ stateInfo . acceptableStates . indexOf ( playerStateAfterChange ) !== - 1
100
+ ) {
101
+ player . removeEventListener ( 'onStateChange' , onPlayerStateChange ) ;
102
+
103
+ clearTimeout ( timeout ) ;
104
+
105
+ resolve ( ) ;
106
+ }
107
+ } ;
108
+
109
+ player . addEventListener ( 'onStateChange' , onPlayerStateChange ) ;
110
+ } )
111
+ . then ( ( ) => {
112
+ return value ;
113
+ } ) ;
114
+ }
115
+
116
+ return value ;
108
117
} ) ;
109
- }
110
-
111
- return value ;
112
118
} ;
113
119
} else {
114
- functions [ functionName ] = async ( ...args ) => {
115
- const player = await playerAPIReady ;
116
-
117
- // eslint-disable-next-line no-warning-comments
118
- // TODO: Just spread the args into the function once Babel is fixed:
119
- // https://github.com/babel/babel/issues/4270
120
- //
121
- // eslint-disable-next-line prefer-spread
122
- return player [ functionName ] . apply ( player , args ) ;
120
+ functions [ functionName ] = ( ...args ) => {
121
+ return playerAPIReady
122
+ . then ( ( player ) => {
123
+ // eslint-disable-next-line no-warning-comments
124
+ // TODO: Just spread the args into the function once Babel is fixed:
125
+ // https://github.com/babel/babel/issues/4270
126
+ //
127
+ // eslint-disable-next-line prefer-spread
128
+ return player [ functionName ] . apply ( player , args ) ;
129
+ } ) ;
123
130
} ;
124
131
}
125
132
}
0 commit comments