[bugfix]: Fix repeated track scrobbling (#480)

* [bugfix]: Fix repeated track scrobbling

This commit fixes scrobbling tracks that are repeated in the queue (either due
to a REPEAT_ONE or just enqueued multiple times). There are two extra cases:

1. The current player changes. The player alternates from 1 -> 2 -> 1 whenever playback
finishes normally, so this is a sort of epoch that can be used to detect both repeat one
and track being enqueued multiple times.
2. The current index changes. The player gets set back to 1 in a variety of cases (most often
previous/next), so the check in (1) is not enough. However, the index changing will help identify
that this is a new song.

* use unique id instead
This commit is contained in:
Kendall Garner 2024-02-01 04:12:39 +00:00 committed by GitHub
parent 9995b2e774
commit eff1cee6a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -315,12 +315,19 @@ export const useScrobble = () => {
useEffect(() => {
const unsubSongChange = usePlayerStore.subscribe(
(state) => [state.current.song, state.current.time],
(state) => [state.current.song, state.current.time, state.current.player],
handleScrobbleFromSongChange,
{
// We need the current time to check the scrobble condition, but we only want to
// trigger the callback when the song changes
equalityFn: (a, b) => (a[0] as QueueSong)?.id === (b[0] as QueueSong)?.id,
// There are two conditions where this should trigger:
// 1. The song actually changes (the common case)
// 2. The song does not change, but the player dows. This would either be
// a single track on repeat one, or one track added to the queue
// multiple times in a row and playback goes normally (no next/previous)
equalityFn: (a, b) =>
(a[0] as QueueSong)?.uniqueId === (b[0] as QueueSong)?.uniqueId &&
a[2] === b[2],
},
);