@@ -6,7 +6,6 @@ import React, {
6
6
useImperativeHandle ,
7
7
useMemo ,
8
8
useRef ,
9
- useState ,
10
9
} from 'react' ;
11
10
import { Platform , StyleSheet , View } from 'react-native' ;
12
11
import {
@@ -21,19 +20,9 @@ import {
21
20
playMode ,
22
21
soundMode ,
23
22
} from './PlayerScripts' ;
23
+ import { deepComparePlayList } from './utils' ;
24
24
import { WebView } from './WebView' ;
25
25
26
- const deepComparePlayList = ( lastPlayList , playList ) => {
27
- return (
28
- typeof lastPlayList === typeof playList &&
29
- ( Array . isArray ( lastPlayList )
30
- ? lastPlayList . join ( '' )
31
- : lastPlayList === Array . isArray ( playList )
32
- ? playList . join ( '' )
33
- : playList )
34
- ) ;
35
- } ;
36
-
37
26
const YoutubeIframe = ( props , ref ) => {
38
27
const {
39
28
height,
@@ -61,13 +50,13 @@ const YoutubeIframe = (props, ref) => {
61
50
onPlaybackRateChange = _playbackRate => { } ,
62
51
} = props ;
63
52
53
+ const playerReady = useRef ( false ) ;
64
54
const lastVideoIdRef = useRef ( videoId ) ;
65
55
const lastPlayListRef = useRef ( playList ) ;
66
56
const initialPlayerParamsRef = useRef ( initialPlayerParams || { } ) ;
67
57
68
58
const webViewRef = useRef ( null ) ;
69
59
const eventEmitter = useRef ( new EventEmitter ( ) ) ;
70
- const [ playerReady , setPlayerReady ] = useState ( 0 ) ;
71
60
72
61
useImperativeHandle (
73
62
ref ,
@@ -128,7 +117,7 @@ const YoutubeIframe = (props, ref) => {
128
117
) ;
129
118
130
119
useEffect ( ( ) => {
131
- if ( playerReady < 1 ) {
120
+ if ( ! playerReady . current ) {
132
121
// no instance of player is ready
133
122
return ;
134
123
}
@@ -139,10 +128,10 @@ const YoutubeIframe = (props, ref) => {
139
128
PLAYER_FUNCTIONS . setVolume ( volume ) ,
140
129
PLAYER_FUNCTIONS . setPlaybackRate ( playbackRate ) ,
141
130
] . forEach ( webViewRef . current . injectJavaScript ) ;
142
- } , [ play , playerReady , mute , volume , playbackRate ] ) ;
131
+ } , [ play , mute , volume , playbackRate ] ) ;
143
132
144
133
useEffect ( ( ) => {
145
- if ( playerReady < 1 || lastVideoIdRef . current === videoId ) {
134
+ if ( ! playerReady . current || lastVideoIdRef . current === videoId ) {
146
135
// no instance of player is ready
147
136
// or videoId has not changed
148
137
return ;
@@ -153,10 +142,10 @@ const YoutubeIframe = (props, ref) => {
153
142
webViewRef . current . injectJavaScript (
154
143
PLAYER_FUNCTIONS . loadVideoById ( videoId , play ) ,
155
144
) ;
156
- } , [ videoId , play , playerReady ] ) ;
145
+ } , [ videoId , play ] ) ;
157
146
158
147
useEffect ( ( ) => {
159
- if ( playerReady < 1 ) {
148
+ if ( ! playerReady . current < 1 ) {
160
149
// no instance of player is ready
161
150
return ;
162
151
}
@@ -172,7 +161,7 @@ const YoutubeIframe = (props, ref) => {
172
161
webViewRef . current . injectJavaScript (
173
162
PLAYER_FUNCTIONS . loadPlaylist ( playList , playListStartIndex , play ) ,
174
163
) ;
175
- } , [ playList , play , playListStartIndex , playerReady ] ) ;
164
+ } , [ playList , play , playListStartIndex ] ) ;
176
165
177
166
const onWebMessage = useCallback (
178
167
event => {
@@ -188,7 +177,7 @@ const YoutubeIframe = (props, ref) => {
188
177
break ;
189
178
case 'playerReady' :
190
179
onReady ( ) ;
191
- setPlayerReady ( prev => prev + 1 ) ;
180
+ playerReady . current = true ;
192
181
break ;
193
182
case 'playerQualityChange' :
194
183
onPlaybackQualityChange ( message . data ) ;
@@ -204,7 +193,7 @@ const YoutubeIframe = (props, ref) => {
204
193
break ;
205
194
}
206
195
} catch ( error ) {
207
- console . warn ( error ) ;
196
+ console . warn ( '[rn-youtube-iframe]' , error ) ;
208
197
}
209
198
} ,
210
199
[
@@ -280,7 +269,7 @@ const YoutubeIframe = (props, ref) => {
280
269
{ ...webViewProps }
281
270
// --
282
271
283
- //add props that should not be allowed to be overridden below
272
+ // add props that should not be allowed to be overridden below
284
273
source = { source }
285
274
ref = { webViewRef }
286
275
onMessage = { onWebMessage }
0 commit comments