refactor play count update to be cleaner, code-wise

This commit is contained in:
Drew Weymouth
2023-02-20 19:24:47 -08:00
parent 2f4c9ccf34
commit cd52a893d4
8 changed files with 28 additions and 36 deletions
+4 -8
View File
@@ -28,10 +28,7 @@ type PlaybackManager struct {
playQueue []*subsonic.Child
nowPlayingIdx int64
// to pass to onSongChange listeners; clear once listeners have been called
lastScrobbled *subsonic.Child
onSongChange []func(nowPlaying *subsonic.Child, justScrobbledIfAny *subsonic.Child)
onSongChange []func(nowPlaying *subsonic.Child)
onPlayTimeUpdate []func(float64, float64)
}
@@ -98,7 +95,7 @@ func (p *PlaybackManager) NowPlaying() *subsonic.Child {
}
// Sets a callback that is notified whenever a new song begins playing.
func (p *PlaybackManager) OnSongChange(cb func(nowPlaying *subsonic.Child, justScrobbledIfAny *subsonic.Child)) {
func (p *PlaybackManager) OnSongChange(cb func(nowPlaying *subsonic.Child)) {
p.onSongChange = append(p.onSongChange, cb)
}
@@ -228,16 +225,15 @@ func (p *PlaybackManager) checkScrobble(playDur time.Duration) {
song := p.playQueue[p.nowPlayingIdx]
if playDur.Seconds()/p.curTrackTime > ScrobbleThreshold {
log.Printf("Scrobbling %q", song.Title)
p.lastScrobbled = song
song.PlayCount += 1
p.sm.Server.Scrobble(song.ID, map[string]string{"time": strconv.FormatInt(time.Now().Unix()*1000, 10)})
}
}
func (p *PlaybackManager) invokeOnSongChangeCallbacks() {
for _, cb := range p.onSongChange {
cb(p.NowPlaying(), p.lastScrobbled)
cb(p.NowPlaying())
}
p.lastScrobbled = nil
}
func (p *PlaybackManager) startPollTimePos() {