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