Skip to content

Commit

Permalink
Refactor queue handling: replace 'currentSong' with 'currentTrack' an…
Browse files Browse the repository at this point in the history
…d add stream lookup retries limit
  • Loading branch information
nukeop committed Feb 24, 2025
1 parent adbf429 commit ba5c72e
Show file tree
Hide file tree
Showing 30 changed files with 235 additions and 175 deletions.
14 changes: 7 additions & 7 deletions packages/app/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ class App extends React.PureComponent {
}

scrobbleLastFm() {
const currentSong = this.props.queue.queueItems[
this.props.queue.currentSong
const currentTrack = this.props.queue.queueItems[
this.props.queue.currentTrack
];
this.props.actions.updateNowPlayingAction(
currentSong.artist,
currentSong.name,
currentTrack.artist,
currentTrack.name,
this.props.scrobbling.lastFmSessionKey
);
}
Expand All @@ -126,9 +126,9 @@ class App extends React.PureComponent {
);
}

getCurrentSongParameter(parameter) {
return this.props.queue.queueItems[this.props.queue.currentSong]
? this.props.queue.queueItems[this.props.queue.currentSong][parameter]
getCurrentTrackParameter(parameter) {
return this.props.queue.queueItems[this.props.queue.currentTrack]
? this.props.queue.queueItems[this.props.queue.currentTrack][parameter]
: null;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/app/app/actions/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type LocalTrack = Track & {
local: true;
};

export const streamLookupRetriesLimit = 3;

const isLocalTrack = (track: Track): track is LocalTrack => track.local;

const localTrackToQueueItem = (track: LocalTrack, local: LocalLibraryState): QueueItem => {
Expand Down
28 changes: 14 additions & 14 deletions packages/app/app/actions/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { store, setOption } from '@nuclear/core';
import { store, setOption, Setting } from '@nuclear/core';

import { Settings } from './actionTypes';

Expand All @@ -10,39 +10,39 @@ export function readSettings() {
};
}

export function setBooleanOption(option, state, fromMain?) {
setOption(option, state);
export function setBooleanOption(key: string, value: unknown, fromMain?: boolean) {
setOption(key, value);

return {
type: Settings.SET_BOOLEAN_OPTION,
payload: {option, state},
payload: {option: key, state: value},
meta: { fromMain }
};
}

export function setStringOption(option, state, fromMain?) {
setOption(option, state);
export function setStringOption(key: string, value: unknown, fromMain?: boolean) {
setOption(key, value);

return {
type: Settings.SET_STRING_OPTION,
payload: {option, state},
payload: {option: key, state: value},
meta: { fromMain }
};
}

export function setNumberOption(option, state, fromMain) {
setOption(option, state);
export function setNumberOption(key: string, value: unknown, fromMain?: boolean) {
setOption(key, value);

return {
type: Settings.SET_NUMBER_OPTION,
payload: {option, state},
payload: {option: key, state: value},
meta: { fromMain }
};
}

export function toggleOption(option, state) {
const optionState = state[option.name];
export function toggleOption(key: Setting, state: unknown) {
const optionState = state[key.name];
return optionState !== undefined
? setBooleanOption(option.name, !optionState)
: setBooleanOption(option.name, !option.default);
? setBooleanOption(key.name, !optionState)
: setBooleanOption(key.name, !key.default);
}
17 changes: 10 additions & 7 deletions packages/app/app/components/PlayQueue/QueueItemClone/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { QueueItem } from '@nuclear/ui';
import { TrackStream } from '@nuclear/ui/lib/types';
import React from 'react';
import { DraggableChildrenFn } from 'react-beautiful-dnd';
import { QueueItem as QueueItemType, QueueStore } from '../../../reducers/queue';
import { SettingsState } from '../../../reducers/settings';

export type QueueItemCloneProps = {
settings: SettingsState;
queue: QueueStore;
onSelectTrack: (index: number) => () => void;
onRemoveTrack: (index: number) => () => void;
formatTrackDuration: (track: QueueItemType) => string;
settings: SettingsState;
queue: QueueStore;
streamLookupRetriesLimit: number;
onSelectTrack: (index: number) => () => void;
onRemoveTrack: (index: number) => () => void;
formatTrackDuration: (track: QueueItemType) => string;
}

export const QueueItemClone: (props: QueueItemCloneProps) => DraggableChildrenFn = ({
settings,
queue,
streamLookupRetriesLimit,
onSelectTrack,
onRemoveTrack,
formatTrackDuration
Expand All @@ -26,8 +27,10 @@ export const QueueItemClone: (props: QueueItemCloneProps) => DraggableChildrenFn
>
<QueueItem
isCompact={settings.compactQueueBar as boolean}
isCurrent={queue.currentSong === rubric.source.index}
isCurrent={queue.currentTrack === rubric.source.index}
track={queue.queueItems[rubric.source.index]}
streamLookupRetries={queue.queueItems[rubric.source.index].streamLookupRetries}
streamLookupRetriesLimit={streamLookupRetriesLimit}
onSelect={onSelectTrack(rubric.source.index)}
onRemove={onRemoveTrack(rubric.source.index)}
duration={formatTrackDuration(queue.queueItems[rubric.source.index])}
Expand Down
6 changes: 3 additions & 3 deletions packages/app/app/components/PlayQueue/QueueMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type QueueMenuProps = {
resetPlayer: PlayQueueActions['resetPlayer'];
addFavoriteTrack: PlayQueueActions['addFavoriteTrack'];
addToDownloads: (track: QueueItem) => void;
currentSong: number;
currentTrack: number;
success: PlayQueueActions['success'];
items: QueueItem[];
toggleOption: PlayQueueActions['toggleOption'];
Expand All @@ -34,7 +34,7 @@ const QueueMenu: React.FC<QueueMenuProps> = ({
resetPlayer,
addFavoriteTrack,
addToDownloads,
currentSong,
currentTrack,
success,
items,
toggleOption,
Expand Down Expand Up @@ -84,7 +84,7 @@ const QueueMenu: React.FC<QueueMenuProps> = ({
addFavoriteTrack={addFavoriteTrack}
addToDownloads={addToDownloads}
playlists={playlists}
currentItem={items[currentSong]}
currentItem={items[currentTrack]}
items={items}
savePlaylistDialog={
<InputDialog
Expand Down
1 change: 0 additions & 1 deletion packages/app/app/components/PlayQueue/QueuePopup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export type QueuePopupProps = {
track: QueueItem;
index: number;

actions: typeof QueueActions;
plugins: PluginsState;
copyToClipboard: (text: string) => void;
onSelectStream: (stream: StreamData) => void;
Expand Down
10 changes: 7 additions & 3 deletions packages/app/app/components/PlayQueue/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { StreamVerificationContainer } from '../../containers/StreamVerification

import styles from './styles.scss';
import { QueueItemClone } from './QueueItemClone';
import { streamLookupRetriesLimit } from '../../actions/queue';

type PlayQueueProps = {
actions: PlayQueueActions;
Expand Down Expand Up @@ -166,11 +167,13 @@ const PlayQueue: React.FC<PlayQueueProps> = ({
trigger={
<QueueItem
isCompact={data.settings.compactQueueBar as boolean}
isCurrent={data.queue.currentSong === index}
isCurrent={data.queue.currentTrack === index}
track={item}
duration={formatTrackDuration(t)(item)}
streamLookupRetries={item.streamLookupRetries}
streamLookupRetriesLimit={streamLookupRetriesLimit}
onSelect={onSelectTrack(index)}
onRemove={onRemoveTrack(index)}
duration={formatTrackDuration(t)(item)}
/>
}
isQueueItemCompact={data.settings.compactQueueBar as boolean}
Expand Down Expand Up @@ -201,7 +204,7 @@ const PlayQueue: React.FC<PlayQueueProps> = ({
toggleOption={toggleOption}
addFavoriteTrack={addFavoriteTrack}
addToDownloads={onAddToDownloads}
currentSong={queue.currentSong}
currentTrack={queue.currentTrack}
success={success}
settings={settings}
playlists={playlists}
Expand All @@ -215,6 +218,7 @@ const PlayQueue: React.FC<PlayQueueProps> = ({
renderClone={QueueItemClone({
settings,
queue,
streamLookupRetriesLimit,
onSelectTrack,
onRemoveTrack,
formatTrackDuration: formatTrackDuration(t)
Expand Down
6 changes: 3 additions & 3 deletions packages/app/app/components/Seekbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import styles from './styles.scss';
const Seekbar = ({ seek, queue, fill, children }) => {
const handleClick = useCallback(event => {
const percent = (event.pageX - event.target.offsetLeft) / document.body.clientWidth;
const duration = queue.queueItems[queue.currentSong].streams?.[0]?.duration;
const duration = queue.queueItems[queue.currentTrack].streams?.[0]?.duration;

seek(percent * duration);
}, [queue.queueItems, queue.currentSong, seek]);
}, [queue.queueItems, queue.currentTrack, seek]);

return (
<div onClick={handleClick} className={styles.seekbar_container}>
Expand All @@ -22,7 +22,7 @@ const Seekbar = ({ seek, queue, fill, children }) => {
Seekbar.propTypes = {
seek: PropTypes.func,
queue: PropTypes.shape({
currentSong: PropTypes.number,
currentTrack: PropTypes.number,
queueItems: PropTypes.array
}),
fill: PropTypes.string,
Expand Down
12 changes: 6 additions & 6 deletions packages/app/app/containers/IpcContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface RootState {
thumbnail: string;
streams: Array<{ duration: number }>;
}>;
currentSong: number;
currentTrack: number;
};
settings: {
shuffleQueue: boolean;
Expand Down Expand Up @@ -139,7 +139,7 @@ const IpcContainer: React.FC = () => {
[IpcEvents.PLAYING_STATUS]: () => {
const { shuffleQueue, loopAfterQueueEnd } = settings;
try {
const currentItem = queue.queueItems[queue.currentSong];
const currentItem = queue.queueItems[queue.currentTrack];
const { artist, name, thumbnail } = currentItem;
const duration = head(currentItem.streams)?.duration;

Expand Down Expand Up @@ -185,11 +185,11 @@ const IpcContainer: React.FC = () => {

// Track changes and notify IPC
useEffect(() => {
const currentSong = queue.queueItems[queue.currentSong];
if (currentSong) {
ipcRenderer.send(IpcEvents.SONG_CHANGE, currentSong);
const currentTrack = queue.queueItems[queue.currentTrack];
if (currentTrack) {
ipcRenderer.send(IpcEvents.SONG_CHANGE, currentTrack);
}
}, [queue.currentSong, queue.queueItems]);
}, [queue.currentTrack, queue.queueItems]);

return null;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Lyrics container', () => {
lyricsSearchResult: ''
},
queue: {
currentSong: 0,
currentTrack: 0,
queueItems: [{}]
}
});
Expand All @@ -47,7 +47,7 @@ describe('Lyrics container', () => {
lyricsSearchResult: 'test song lyrics'
},
queue: {
currentSong: 0,
currentTrack: 0,
queueItems: [
{
name: 'test track',
Expand Down
2 changes: 1 addition & 1 deletion packages/app/app/containers/LyricsContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const LyricsContainer = () => {
return <LyricsView
track={_.get(
queue.queueItems,
queue.currentSong
queue.currentTrack
)}
lyricsSearchResult={lyricsSearchResult}
/>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Main content container', () => {

state = store.getState();
expect(state.settings.loopAfterQueueEnd).toEqual(true);
expect(state.queue.currentSong).toEqual(0);
expect(state.queue.currentTrack).toEqual(0);
});

it('should loop with a single track in the queue', () => {
Expand All @@ -36,7 +36,7 @@ describe('Main content container', () => {
})
.build(),
queue: {
currentSong: 0,
currentTrack: 0,
queueItems: [storeWithTracks.queue.queueItems[0]]
}
}
Expand All @@ -49,7 +49,7 @@ describe('Main content container', () => {

state = store.getState();
expect(state.settings.loopAfterQueueEnd).toEqual(true);
expect(state.queue.currentSong).toEqual(0);
expect(state.queue.currentTrack).toEqual(0);
expect(state.player.seek).toEqual(0);
expect(state.player.playbackStatus).toEqual(Sound.status.PLAYING);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/app/app/containers/PlayQueueContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ export type PlayQueueActions = {
resetPlayer: () => void;
addToDownloads: (providers: StreamProviderPlugin[], track: QueueItem) => void;
info: (title: string, details: string, icon: React.ReactElement<{ src: string }>, settings: SettingsState) => void;
success: (title: string, details: string) => void;
success: typeof success;
addFavoriteTrack: (track: QueueItem) => void;
addPlaylist: (tracks: PlaylistTrack[], name: string) => void;
updatePlaylist: (playlist: Playlist) => void;
toggleOption: (option: string, value?: boolean) => void;
toggleOption: typeof toggleOption;
}

const PlayQueueContainer: React.FC = () => {
Expand Down
Loading

0 comments on commit ba5c72e

Please sign in to comment.