Skip to content

Commit 2698ca4

Browse files
committed
refactor: make "playerReady" a mutable ref
this has no real value being a state variable since the updation does not required a re-render and its not being used to trigger side effects. but since other effects depend on this, they will no longer have to track this as a dependency
1 parent 02424b8 commit 2698ca4

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/YoutubeIframe.js

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import React, {
66
useImperativeHandle,
77
useMemo,
88
useRef,
9-
useState,
109
} from 'react';
1110
import {Platform, StyleSheet, View} from 'react-native';
1211
import {
@@ -21,19 +20,9 @@ import {
2120
playMode,
2221
soundMode,
2322
} from './PlayerScripts';
23+
import {deepComparePlayList} from './utils';
2424
import {WebView} from './WebView';
2525

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-
3726
const YoutubeIframe = (props, ref) => {
3827
const {
3928
height,
@@ -61,13 +50,13 @@ const YoutubeIframe = (props, ref) => {
6150
onPlaybackRateChange = _playbackRate => {},
6251
} = props;
6352

53+
const playerReady = useRef(false);
6454
const lastVideoIdRef = useRef(videoId);
6555
const lastPlayListRef = useRef(playList);
6656
const initialPlayerParamsRef = useRef(initialPlayerParams || {});
6757

6858
const webViewRef = useRef(null);
6959
const eventEmitter = useRef(new EventEmitter());
70-
const [playerReady, setPlayerReady] = useState(0);
7160

7261
useImperativeHandle(
7362
ref,
@@ -128,7 +117,7 @@ const YoutubeIframe = (props, ref) => {
128117
);
129118

130119
useEffect(() => {
131-
if (playerReady < 1) {
120+
if (!playerReady.current) {
132121
// no instance of player is ready
133122
return;
134123
}
@@ -139,10 +128,10 @@ const YoutubeIframe = (props, ref) => {
139128
PLAYER_FUNCTIONS.setVolume(volume),
140129
PLAYER_FUNCTIONS.setPlaybackRate(playbackRate),
141130
].forEach(webViewRef.current.injectJavaScript);
142-
}, [play, playerReady, mute, volume, playbackRate]);
131+
}, [play, mute, volume, playbackRate]);
143132

144133
useEffect(() => {
145-
if (playerReady < 1 || lastVideoIdRef.current === videoId) {
134+
if (!playerReady.current || lastVideoIdRef.current === videoId) {
146135
// no instance of player is ready
147136
// or videoId has not changed
148137
return;
@@ -153,10 +142,10 @@ const YoutubeIframe = (props, ref) => {
153142
webViewRef.current.injectJavaScript(
154143
PLAYER_FUNCTIONS.loadVideoById(videoId, play),
155144
);
156-
}, [videoId, play, playerReady]);
145+
}, [videoId, play]);
157146

158147
useEffect(() => {
159-
if (playerReady < 1) {
148+
if (!playerReady.current < 1) {
160149
// no instance of player is ready
161150
return;
162151
}
@@ -172,7 +161,7 @@ const YoutubeIframe = (props, ref) => {
172161
webViewRef.current.injectJavaScript(
173162
PLAYER_FUNCTIONS.loadPlaylist(playList, playListStartIndex, play),
174163
);
175-
}, [playList, play, playListStartIndex, playerReady]);
164+
}, [playList, play, playListStartIndex]);
176165

177166
const onWebMessage = useCallback(
178167
event => {
@@ -188,7 +177,7 @@ const YoutubeIframe = (props, ref) => {
188177
break;
189178
case 'playerReady':
190179
onReady();
191-
setPlayerReady(prev => prev + 1);
180+
playerReady.current = true;
192181
break;
193182
case 'playerQualityChange':
194183
onPlaybackQualityChange(message.data);
@@ -204,7 +193,7 @@ const YoutubeIframe = (props, ref) => {
204193
break;
205194
}
206195
} catch (error) {
207-
console.warn(error);
196+
console.warn('[rn-youtube-iframe]', error);
208197
}
209198
},
210199
[
@@ -280,7 +269,7 @@ const YoutubeIframe = (props, ref) => {
280269
{...webViewProps}
281270
// --
282271

283-
//add props that should not be allowed to be overridden below
272+
// add props that should not be allowed to be overridden below
284273
source={source}
285274
ref={webViewRef}
286275
onMessage={onWebMessage}

0 commit comments

Comments
 (0)