[bugfix]: Resolve MPV next/prev race condition

Resolves #536.

With the previous implementation, next/previous would first update
the current queue and then call next/previous. However, since these were
asynchronous calls it was very likely that the second calls would fail
(and a test of adding delay showed that it actually caused a double skip).
This PR resolves this by just removing the prev/next.

Small other fixes:
- setQueue + pause -> setQueue(..., true)
- make MPV and web player have the same behavior for (pause/stop) where appropriate
This commit is contained in:
Kendall Garner 2024-03-30 21:48:09 -07:00
parent 918842e3a5
commit 65b045df03
No known key found for this signature in database
GPG key ID: 18D2767419676C87

View file

@ -311,7 +311,6 @@ export const useCenterControls = (args: { playersRef: any }) => {
const playerData = next();
mprisUpdateSong({ song: playerData.current.song, status: PlayerStatus.PLAYING });
mpvPlayer!.setQueue(playerData);
mpvPlayer!.next();
},
web: () => {
const playerData = next();
@ -324,8 +323,7 @@ export const useCenterControls = (args: { playersRef: any }) => {
if (isLastTrack) {
const playerData = setCurrentIndex(0);
mprisUpdateSong({ song: playerData.current.song, status: PlayerStatus.PAUSED });
mpvPlayer!.setQueue(playerData);
mpvPlayer!.pause();
mpvPlayer!.setQueue(playerData, true);
pause();
} else {
const playerData = next();
@ -334,7 +332,6 @@ export const useCenterControls = (args: { playersRef: any }) => {
status: PlayerStatus.PLAYING,
});
mpvPlayer!.setQueue(playerData);
mpvPlayer!.next();
}
},
web: () => {
@ -359,10 +356,14 @@ export const useCenterControls = (args: { playersRef: any }) => {
const handleRepeatOne = {
local: () => {
if (!isLastTrack) {
const playerData = next();
mprisUpdateSong({ song: playerData.current.song, status: PlayerStatus.PLAYING });
mprisUpdateSong({
song: playerData.current.song,
status: PlayerStatus.PLAYING,
});
mpvPlayer!.setQueue(playerData);
mpvPlayer!.next();
}
},
web: () => {
if (!isLastTrack) {
@ -429,7 +430,6 @@ export const useCenterControls = (args: { playersRef: any }) => {
status: PlayerStatus.PLAYING,
});
mpvPlayer!.setQueue(playerData);
mpvPlayer!.previous();
} else {
const playerData = setCurrentIndex(queue.length - 1);
mprisUpdateSong({
@ -437,7 +437,6 @@ export const useCenterControls = (args: { playersRef: any }) => {
status: PlayerStatus.PLAYING,
});
mpvPlayer!.setQueue(playerData);
mpvPlayer!.previous();
}
},
web: () => {
@ -461,13 +460,19 @@ export const useCenterControls = (args: { playersRef: any }) => {
const handleRepeatNone = {
local: () => {
if (isFirstTrack) {
const playerData = setCurrentIndex(0);
mprisUpdateSong({ song: playerData.current.song, status: PlayerStatus.PAUSED });
mpvPlayer!.setQueue(playerData, true);
pause();
} else {
const playerData = previous();
remote?.updateSong({
currentTime: usePlayerStore.getState().current.time,
song: playerData.current.song,
});
mpvPlayer!.setQueue(playerData);
mpvPlayer!.previous();
}
},
web: () => {
if (isFirstTrack) {
@ -487,17 +492,12 @@ export const useCenterControls = (args: { playersRef: any }) => {
const handleRepeatOne = {
local: () => {
if (!isFirstTrack) {
const playerData = previous();
mprisUpdateSong({
song: playerData.current.song,
status: PlayerStatus.PLAYING,
});
mpvPlayer!.setQueue(playerData);
mpvPlayer!.previous();
} else {
mpvPlayer!.stop();
}
},
web: () => {
const playerData = previous();