Skip to content

Commit 0c84e56

Browse files
committed
Add queueId to QueueItems
1 parent ba5c72e commit 0c84e56

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

packages/app/app/actions/queue.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,22 @@ const localTrackToQueueItem = (track: LocalTrack, local: LocalLibraryState): Que
4646
};
4747

4848

49-
export const toQueueItem = (track: Track): QueueItem => ({
50-
...track,
51-
artist: isString(track.artist) ? track.artist : track.artist.name,
52-
name: track.title ? track.title : track.name,
53-
streams: track.streams ?? []
54-
});
49+
export const toQueueItem = (track: Track): QueueItem => {
50+
const queueItem: QueueItem = {
51+
...track,
52+
artist: isString(track.artist) ? track.artist : track.artist.name,
53+
name: track.title ? track.title : track.name,
54+
streams: track.streams ?? []
55+
};
56+
57+
safeAddUuid(queueItem);
58+
59+
if (!queueItem.queueId) {
60+
queueItem.queueId = v4();
61+
}
62+
63+
return queueItem;
64+
};
5565

5666
// Exported to facilitate testing.
5767
export const getSelectedStreamProvider = (getState) => {
@@ -122,6 +132,7 @@ export const addToQueue =
122132
const { local }: RootState = getState();
123133
item = {
124134
...safeAddUuid(item),
135+
queueId: v4(),
125136
streams: item.local ? item.streams : [],
126137
loading: false
127138
};
@@ -140,7 +151,8 @@ export const addToQueue =
140151
};
141152

142153
export const selectNewStream = (index: number, streamId: string) => async (dispatch, getState) => {
143-
const track = queueSelector(getState()).queueItems[index];
154+
const getLatestTrack = () => queueSelector(getState()).queueItems[index];
155+
let track = getLatestTrack();
144156
const selectedStreamProvider: StreamProviderPlugin = getSelectedStreamProvider(getState);
145157

146158
const oldStreamData = track.streams.find(stream => stream.id === streamId);
@@ -149,6 +161,7 @@ export const selectNewStream = (index: number, streamId: string) => async (dispa
149161
if (!streamData) {
150162
dispatch(removeFromQueue(index));
151163
} else {
164+
track = getLatestTrack();
152165
dispatch(
153166
updateQueueItem({
154167
...track,
@@ -211,8 +224,8 @@ const resolveStreams = async (
211224
};
212225

213226
export const findStreamsForTrack = (index: number) => async (dispatch, getState) => {
214-
const {queue, settings}: RootState = getState();
215-
const track = queue.queueItems[index];
227+
const getLatestTrack = () => queueSelector(getState()).queueItems[index];
228+
let track = getLatestTrack();
216229

217230
if (!track || track.local || !trackHasNoFirstStream(track)) {
218231
return;
@@ -232,15 +245,18 @@ export const findStreamsForTrack = (index: number) => async (dispatch, getState)
232245
return;
233246
}
234247

248+
const settings = getState().settings;
235249
streamData = await verifyStreamWithService(track, streamData, selectedStreamProvider, settings);
236250

237251
const firstStream = streamData[0];
238252
if (!firstStream?.stream) {
239253
const remainingStreams = streamData.slice(1);
254+
track = getLatestTrack();
240255
removeFirstStream(track, index, remainingStreams, dispatch);
241256
return;
242257
}
243258

259+
track = getLatestTrack();
244260
dispatch(updateQueueItem({
245261
...track,
246262
loading: false,
@@ -253,6 +269,7 @@ export const findStreamsForTrack = (index: number) => async (dispatch, getState)
253269
);
254270
logger.error(e);
255271

272+
track = getLatestTrack();
256273
dispatch(updateQueueItem({
257274
...track,
258275
loading: false,

packages/app/app/containers/DashboardContainer/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const DashboardContainer: React.FC = () => {
1717

1818
const dashboard = useSelector(dashboardSelector);
1919
const isConnected = useSelector(connectivity);
20-
const settings = useSelector(settingsSelector);
2120

2221
const loadTopTags = () => dispatch(DashboardActions.loadTopTags());
2322
const loadTopTracks = () => dispatch(DashboardActions.loadTopTracks());

packages/app/app/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Provider } from 'react-redux';
1212
import { I18nextProvider } from 'react-i18next';
1313
import { getOption, setOption } from '@nuclear/core';
1414
import i18n, { setupI18n } from '@nuclear/i18n';
15+
import { setTimeout, setInterval } from 'timers';
1516

1617
import App from './App';
1718
import configureStore from './store/configureStore';
@@ -22,8 +23,8 @@ const store = configureStore({});
2223
/*
2324
* The `setTimeout` and `setInterval` functions are overridden to use the Node.js timers module. This is necessary because the default browser implementations of `setTimeout` and `setInterval` will not work correctly with unidici, so we use the Node versions.
2425
*/
25-
window.setTimeout = ((handler: TimerHandler, timeout?: number, ...args: any[]) => require('timers').setTimeout(handler, timeout, ...args)) as typeof setTimeout;
26-
window.setInterval = ((handler: TimerHandler, timeout?: number, ...args: any[]) => require('timers').setInterval(handler, timeout, ...args)) as typeof setInterval;
26+
window.setTimeout = ((handler: TimerHandler, timeout?: number, ...args: any[]) => setTimeout(handler, timeout, ...args)) as typeof setTimeout;
27+
window.setInterval = ((handler: TimerHandler, timeout?: number, ...args: any[]) => setInterval(handler, timeout, ...args)) as typeof setInterval;
2728

2829
i18n.on('languageChanged', lng => setOption('language', lng));
2930

packages/app/app/reducers/queue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type TrackStream = {
1818

1919
export type QueueItem = {
2020
uuid?: string;
21+
queueId?: string;
2122
loading?: boolean;
2223
error?:
2324
| boolean
@@ -116,7 +117,7 @@ function reduceSelectStreamProviders(state) {
116117

117118
const reduceUpdateQueueItem = (state, action) => {
118119
const { item } = action.payload;
119-
const itemIndex = _.findIndex(state.queueItems, { uuid: item.uuid });
120+
const itemIndex = _.findIndex(state.queueItems, { queueId: item.queueId });
120121
const queueClone = _.cloneDeep(state.queueItems);
121122
queueClone[itemIndex] = item;
122123

@@ -138,7 +139,6 @@ const reduceAddPlayNextItem = (state, action) => {
138139
};
139140

140141
export default function QueueReducer(state = defaultState, action): QueueStore {
141-
logger.log('QueueReducer', action);
142142
switch (action.type) {
143143
case Queue.ADD_QUEUE_ITEM:
144144
return {

0 commit comments

Comments
 (0)