fix mpv autoNext and next song replacement behavior (part 2)

This commit is contained in:
jeffvli
2025-12-12 18:44:14 -08:00
parent 29a5fa3f74
commit 578083d994
2 changed files with 24 additions and 19 deletions
+7 -1
View File
@@ -371,8 +371,14 @@ ipcMain.on('player-set-queue', async (_event, current?: string, next?: string, p
// Replaces the queue in position 1 to the given data
ipcMain.on('player-set-queue-next', async (_event, url?: string) => {
try {
const size = await getMpvInstance()?.getPlaylistSize();
if (size && size > 1) {
await getMpvInstance()?.playlistRemove(1);
}
if (url) {
await getMpvInstance()?.load(url, 'append');
getMpvInstance()?.load(url, 'append');
}
} catch (err: any | NodeMpvError) {
mpvLog({ action: `Failed to set play queue` }, err);
+17 -18
View File
@@ -2383,12 +2383,7 @@ export const subscribeCurrentTrack = (
);
};
export const subscribeNextSong = (
onChange: (
properties: { index: number; song: QueueSong | undefined },
prev: { index: number; song: QueueSong | undefined },
) => void,
) => {
export const subscribeNextSongInsertion = (onChange: (song: QueueSong | undefined) => void) => {
return usePlayerStoreBase.subscribe(
(state) => {
const queue = state.getQueue();
@@ -2417,21 +2412,25 @@ export const subscribeNextSong = (
nextSong = calculateNextSong(queueIndex, queue.items, repeat);
}
// Calculate the index for the next song
let nextIndex: number | undefined;
if (nextSong) {
nextIndex = queue.items.findIndex((item) => item._uniqueId === nextSong?._uniqueId);
}
return { index: nextIndex ?? -1, song: nextSong };
return { index: queueIndex, song: nextSong };
},
(nextSong, prevNextSong) => {
onChange(nextSong, prevNextSong);
(current, prev) => {
// Only trigger if:
// 1. We have a previous value (not the first call)
// 2. Index hasn't changed (not a natural advance)
// 3. Next song has changed (song was inserted)
if (
prev &&
current.index === prev.index &&
current.song?._uniqueId !== prev.song?._uniqueId
) {
// Index stayed the same but next song changed = insertion at next position
onChange(current.song);
}
},
{
equalityFn: (a, b) => {
return a.song?._uniqueId === b.song?._uniqueId;
},
// Always allow the subscription to fire so we can check conditions in the callback
equalityFn: () => false,
},
);
};