Browse Source

Refactor player functions using useCallback

- Previously was using local state with useEffect to update
master
jeffvli 3 years ago
committed by Jeff
parent
commit
ea72dd9ac2
  1. 68
      src/components/player/Player.tsx

68
src/components/player/Player.tsx

@ -241,13 +241,8 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
const playQueue = useAppSelector((state) => state.playQueue); const playQueue = useAppSelector((state) => state.playQueue);
const player = useAppSelector((state) => state.player); const player = useAppSelector((state) => state.player);
const cacheSongs = settings.getSync('cacheSongs'); const cacheSongs = settings.getSync('cacheSongs');
const [debug, setDebug] = useState(playQueue.showDebugWindow);
const [title] = useState(''); const [title] = useState('');
const [cachePath] = useState(path.join(getSongCachePath(), '/')); const [cachePath] = useState(path.join(getSongCachePath(), '/'));
const [fadeDuration, setFadeDuration] = useState(playQueue.fadeDuration);
const [fadeType, setFadeType] = useState(playQueue.fadeType);
const [volumeFade, setVolumeFade] = useState(playQueue.volumeFade);
const [pollingInterval, setPollingInterval] = useState(playQueue.pollingInterval);
const [scrobbled, setScrobbled] = useState(false); const [scrobbled, setScrobbled] = useState(false);
const getSrc1 = useCallback(() => { const getSrc1 = useCallback(() => {
@ -342,21 +337,6 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
return undefined; return undefined;
}, [currentEntryList, dispatch, getSrc1, getSrc2, playQueue]); }, [currentEntryList, dispatch, getSrc1, getSrc2, playQueue]);
useEffect(() => {
// Update playback settings when changed in redux store
setDebug(playQueue.showDebugWindow);
setFadeDuration(playQueue.fadeDuration);
setFadeType(playQueue.fadeType);
setVolumeFade(playQueue.volumeFade);
setPollingInterval(playQueue.pollingInterval);
}, [
playQueue.fadeDuration,
playQueue.fadeType,
playQueue.pollingInterval,
playQueue.showDebugWindow,
playQueue.volumeFade,
]);
const handleListenPlayer1 = useCallback(() => { const handleListenPlayer1 = useCallback(() => {
listenHandler( listenHandler(
player1Ref, player1Ref,
@ -365,15 +345,15 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
currentEntryList, currentEntryList,
dispatch, dispatch,
1, 1,
fadeDuration, playQueue.fadeDuration,
fadeType, playQueue.fadeType,
volumeFade, playQueue.volumeFade,
debug, playQueue.showDebugWindow,
playQueue.scrobble, playQueue.scrobble,
scrobbled, scrobbled,
setScrobbled setScrobbled
); );
}, [currentEntryList, debug, dispatch, fadeDuration, fadeType, playQueue, scrobbled, volumeFade]); }, [currentEntryList, dispatch, playQueue, scrobbled]);
const handleListenPlayer2 = useCallback(() => { const handleListenPlayer2 = useCallback(() => {
listenHandler( listenHandler(
@ -383,17 +363,17 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
currentEntryList, currentEntryList,
dispatch, dispatch,
2, 2,
fadeDuration, playQueue.fadeDuration,
fadeType, playQueue.fadeType,
volumeFade, playQueue.volumeFade,
debug, playQueue.showDebugWindow,
playQueue.scrobble, playQueue.scrobble,
scrobbled, scrobbled,
setScrobbled setScrobbled
); );
}, [currentEntryList, debug, dispatch, fadeDuration, fadeType, playQueue, scrobbled, volumeFade]); }, [currentEntryList, dispatch, playQueue, scrobbled]);
const handleOnEndedPlayer1 = () => { const handleOnEndedPlayer1 = useCallback(() => {
player1Ref.current.audioEl.current.currentTime = 0; player1Ref.current.audioEl.current.currentTime = 0;
if (cacheSongs) { if (cacheSongs) {
cacheSong( cacheSong(
@ -416,7 +396,7 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
if (playQueue[currentEntryList].length > 1 || playQueue.repeat === 'all') { if (playQueue[currentEntryList].length > 1 || playQueue.repeat === 'all') {
dispatch(setCurrentPlayer(2)); dispatch(setCurrentPlayer(2));
dispatch(incrementPlayerIndex(1)); dispatch(incrementPlayerIndex(1));
if (fadeDuration !== 0) { if (playQueue.fadeDuration !== 0) {
dispatch(setPlayerVolume({ player: 1, volume: 0 })); dispatch(setPlayerVolume({ player: 1, volume: 0 }));
dispatch(setPlayerVolume({ player: 2, volume: playQueue.volume })); dispatch(setPlayerVolume({ player: 2, volume: playQueue.volume }));
dispatch(setIsFading(false)); dispatch(setIsFading(false));
@ -425,9 +405,9 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
dispatch(setAutoIncremented(false)); dispatch(setAutoIncremented(false));
} }
} }
}; }, [cacheSongs, currentEntryList, dispatch, playQueue]);
const handleOnEndedPlayer2 = () => { const handleOnEndedPlayer2 = useCallback(() => {
player2Ref.current.audioEl.current.currentTime = 0; player2Ref.current.audioEl.current.currentTime = 0;
if (cacheSongs) { if (cacheSongs) {
cacheSong( cacheSong(
@ -450,7 +430,7 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
if (playQueue[currentEntryList].length > 1 || playQueue.repeat === 'all') { if (playQueue[currentEntryList].length > 1 || playQueue.repeat === 'all') {
dispatch(setCurrentPlayer(1)); dispatch(setCurrentPlayer(1));
dispatch(incrementPlayerIndex(2)); dispatch(incrementPlayerIndex(2));
if (fadeDuration !== 0) { if (playQueue.fadeDuration !== 0) {
dispatch(setPlayerVolume({ player: 1, volume: playQueue.volume })); dispatch(setPlayerVolume({ player: 1, volume: playQueue.volume }));
dispatch(setPlayerVolume({ player: 2, volume: 0 })); dispatch(setPlayerVolume({ player: 2, volume: 0 }));
dispatch(setIsFading(false)); dispatch(setIsFading(false));
@ -458,7 +438,7 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
dispatch(setAutoIncremented(false)); dispatch(setAutoIncremented(false));
} }
} }
}; }, [cacheSongs, currentEntryList, dispatch, playQueue]);
const handleGaplessPlayer1 = useCallback(() => { const handleGaplessPlayer1 = useCallback(() => {
gaplessListenHandler( gaplessListenHandler(
@ -467,12 +447,12 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
playQueue, playQueue,
1, 1,
dispatch, dispatch,
pollingInterval, playQueue.pollingInterval,
playQueue.scrobble, playQueue.scrobble,
scrobbled, scrobbled,
setScrobbled setScrobbled
); );
}, [dispatch, playQueue, scrobbled, pollingInterval]); }, [dispatch, playQueue, scrobbled]);
const handleGaplessPlayer2 = useCallback(() => { const handleGaplessPlayer2 = useCallback(() => {
gaplessListenHandler( gaplessListenHandler(
@ -481,12 +461,12 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
playQueue, playQueue,
2, 2,
dispatch, dispatch,
pollingInterval, playQueue.pollingInterval,
playQueue.scrobble, playQueue.scrobble,
scrobbled, scrobbled,
setScrobbled setScrobbled
); );
}, [dispatch, playQueue, scrobbled, pollingInterval]); }, [dispatch, playQueue, scrobbled]);
const handleOnPlay = (playerNumber: 1 | 2) => { const handleOnPlay = (playerNumber: 1 | 2) => {
setScrobbled(false); setScrobbled(false);
@ -515,9 +495,9 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
ref={player1Ref} ref={player1Ref}
src={playQueue.player1.src} src={playQueue.player1.src}
onPlay={() => handleOnPlay(1)} onPlay={() => handleOnPlay(1)}
listenInterval={pollingInterval} listenInterval={playQueue.pollingInterval}
preload="auto" preload="auto"
onListen={fadeDuration === 0 ? handleGaplessPlayer1 : handleListenPlayer1} onListen={playQueue.fadeDuration === 0 ? handleGaplessPlayer1 : handleListenPlayer1}
onEnded={handleOnEndedPlayer1} onEnded={handleOnEndedPlayer1}
volume={playQueue.player1.volume} volume={playQueue.player1.volume}
autoPlay={ autoPlay={
@ -537,9 +517,9 @@ const Player = ({ currentEntryList, children }: any, ref: any) => {
ref={player2Ref} ref={player2Ref}
src={playQueue.player2.src} src={playQueue.player2.src}
onPlay={() => handleOnPlay(2)} onPlay={() => handleOnPlay(2)}
listenInterval={pollingInterval} listenInterval={playQueue.pollingInterval}
preload="auto" preload="auto"
onListen={fadeDuration === 0 ? handleGaplessPlayer2 : handleListenPlayer2} onListen={playQueue.fadeDuration === 0 ? handleGaplessPlayer2 : handleListenPlayer2}
onEnded={handleOnEndedPlayer2} onEnded={handleOnEndedPlayer2}
volume={playQueue.player2.volume} volume={playQueue.player2.volume}
autoPlay={ autoPlay={

Loading…
Cancel
Save