Skip to content

Commit

Permalink
Update stream lookup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sferra committed Mar 16, 2024
1 parent 3de0b4d commit 47353a1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
30 changes: 20 additions & 10 deletions packages/app/app/actions/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,26 @@ export const selectNewStream = (index: number, streamId: string) => async (dispa

export const findStreamsForTrack = (index: number) => async (dispatch, getState) => {
const {queue, settings}: RootState = getState();

const track = queue.queueItems[index];

if (track && !track.local && isEmpty(track.streams)) {
dispatch(updateQueueItem({
...track,
loading: true
}));
if (track && !track.local && trackHasNoFirstStream(track)) {
if (!track.loading) {
dispatch(updateQueueItem({
...track,
loading: true
}));
}
const selectedStreamProvider = getSelectedStreamProvider(getState);
try {
let streamData = await getTrackStreams(
track,
selectedStreamProvider
);
let streamData: TrackStream[];
if (isEmpty(track.streams)) {
streamData = await getTrackStreams(
track,
selectedStreamProvider
);
} else {
streamData = track.streams;
}

if (settings.useStreamVerification) {
try {
Expand Down Expand Up @@ -247,6 +253,10 @@ export const findStreamsForTrack = (index: number) => async (dispatch, getState)
}
};

export function trackHasNoFirstStream(track: QueueItem): boolean {
return isEmpty(track?.streams) || isEmpty(track.streams[0].stream);
}

function removeFirstStream(track: QueueItem, trackIndex: number, remainingStreams: TrackStream[], dispatch): void {
if (remainingStreams.length === 0) {
// no more streams are available
Expand Down
27 changes: 20 additions & 7 deletions packages/app/app/containers/PlayerBarContainer/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import * as playerActions from '../../actions/player';
import * as queueActions from '../../actions/queue';
import * as settingsActions from '../../actions/settings';
import * as favoritesActions from '../../actions/favorites';
import { favoritesSelectors } from '../../selectors/favorites';
import { favoritesSelectors, getFavoriteTrack } from '../../selectors/favorites';
import { playerSelectors } from '../../selectors/player';
import { queue as queueSelector } from '../../selectors/queue';
import { settingsSelector } from '../../selectors/settings';
import { getFavoriteTrack } from '../../selectors/favorites';
import { QueueItem } from '../../reducers/queue';
import { QueueItem, QueueStore } from '../../reducers/queue';

export const useSeekbarProps = () => {
const dispatch = useDispatch();
Expand Down Expand Up @@ -255,12 +254,10 @@ export const useStreamLookup = () => {
const queue = useSelector(queueSelector);

useEffect(() => {
const isStreamLoading = Boolean(queue.queueItems.find((item) => item.loading));

if (!isStreamLoading) {
if (shouldSearchForStreams(queue)) {
const currentSong: QueueItem = queue.queueItems[queue.currentSong];

if (currentSong && isEmpty(currentSong.streams)) {
if (currentSong && queueActions.trackHasNoFirstStream(currentSong)) {
dispatch(queueActions.findStreamsForTrack(queue.currentSong));
return;
}
Expand All @@ -273,3 +270,19 @@ export const useStreamLookup = () => {
}
}, [queue]);
};

const shouldSearchForStreams = (queue: QueueStore): boolean => {
const currentlyLoadingTrack = queue.queueItems.find((item) => item.loading);
if (!currentlyLoadingTrack) {
// No track is currently "loading": start searching for steams.
return true;
}
if (isEmpty(currentlyLoadingTrack.streams)) {
// Streams are not yet resolved: this happens when the track is being marked as "loading"
// while still resolving streams. We don't need to start a search until that operation completes or fails.
return false;
}
const firstStream = currentlyLoadingTrack.streams[0];
// A search should be performed if the first stream URL wasn't yet resolved.
return !firstStream?.stream;
};

0 comments on commit 47353a1

Please sign in to comment.