From 567a40ef50db15e7627b9cce433b50e1bc28f232 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Wed, 29 Nov 2017 19:35:02 -0800 Subject: [PATCH] Don't repeat tracks when going to next random track --- js/actionCreators.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/js/actionCreators.js b/js/actionCreators.js index 74370ea1..e061eea5 100644 --- a/js/actionCreators.js +++ b/js/actionCreators.js @@ -49,10 +49,14 @@ import { function playRandomTrack() { return (dispatch, getState) => { - const { playlist: { trackOrder } } = getState(); - const nextIndex = Math.floor(trackOrder.length * Math.random()); - // TODO: Ensure we don't repeat the same track. - dispatch({ type: PLAY_TRACK, id: trackOrder[nextIndex] }); + const { playlist: { trackOrder, currentTrack } } = getState(); + let nextId; + do { + nextId = trackOrder[Math.floor(trackOrder.length * Math.random())]; + } while (nextId === currentTrack && trackOrder.length > 1); + // TODO: Sigh... Technically, we should detect if we are looping only repeat if we are. + // I think this would require pre-computing the "random" order of a playlist. + dispatch({ type: PLAY_TRACK, id: nextId }); }; }