Skip to content

Commit

Permalink
Refactor for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
sferra committed Mar 16, 2024
1 parent 2fb9ab9 commit ef55147
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/app/app/actions/queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ describe('Queue actions tests', () => {

describe('finds streams for track', () => {
const getSelectedStreamProvider = jest.spyOn(QueueOperations, 'getSelectedStreamProvider');
const getTrackStreams = jest.spyOn(QueueOperations, 'getTrackStreams');
const resolveTrackStreams = jest.spyOn(QueueOperations, 'resolveTrackStreams');

afterEach(() => {
getSelectedStreamProvider.mockReset();
getTrackStreams.mockReset();
resolveTrackStreams.mockReset();
});

test('remote track is removed from the queue when no streams are available', () => {
// Mock an empty search result for streams.
getTrackStreams.mockResolvedValueOnce([]);
resolveTrackStreams.mockResolvedValueOnce([]);

// Configure a dummy stream provider. It is not actually used in this execution path.
getSelectedStreamProvider.mockReturnValueOnce({});
Expand Down
41 changes: 25 additions & 16 deletions packages/app/app/actions/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export const getSelectedStreamProvider = (getState) => {
return _.find(streamProviders, { sourceName: selected.streamProviders });
};

export const getTrackStreams = async (
// Exported to facilitate testing.
export const resolveTrackStreams = async (
track: Track | LocalTrack,
selectedStreamProvider: StreamProvider
) => {
Expand Down Expand Up @@ -179,15 +180,7 @@ export const findStreamsForTrack = (index: number) => async (dispatch, getState)
}
const selectedStreamProvider = getSelectedStreamProvider(getState);
try {
let streamData: TrackStream[];
if (isEmpty(track.streams)) {
streamData = await getTrackStreams(
track,
selectedStreamProvider
);
} else {
streamData = track.streams;
}
let streamData: TrackStream[] = await getTrackStreams(track, selectedStreamProvider);

if (settings.useStreamVerification) {
try {
Expand All @@ -213,12 +206,7 @@ export const findStreamsForTrack = (index: number) => async (dispatch, getState)
if (streamData?.length === 0) {
dispatch(removeFromQueue(index));
} else {
if (!streamData[0].stream) {
streamData = [
await selectedStreamProvider.getStreamForId(streamData.find(Boolean)?.id),
...streamData.slice(1)
];
}
streamData = await resolveSourceUrlForTheFirstStream(streamData, selectedStreamProvider);

const firstStream = streamData[0];
if (!firstStream?.stream) {
Expand Down Expand Up @@ -255,6 +243,27 @@ export const findStreamsForTrack = (index: number) => async (dispatch, getState)
}
};

async function getTrackStreams(track: QueueItem, streamProvider: StreamProvider): Promise<TrackStream[]> {
if (isEmpty(track.streams)) {
return resolveTrackStreams(
track,
streamProvider
);
}
return track.streams;
}

async function resolveSourceUrlForTheFirstStream(trackStreams: TrackStream[], streamProvider: StreamProvider): Promise<TrackStream[]> {
if (isEmpty(trackStreams[0].stream)) {
return [
await streamProvider.getStreamForId(trackStreams.find(Boolean)?.id),
...trackStreams.slice(1)
];
}
// The stream URL might already be resolved, for example for a previously played track.
return trackStreams;
}

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

0 comments on commit ef55147

Please sign in to comment.