From 2a16dcba5a31a45a12853a15254719c357baae6d Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 31 May 2023 20:53:12 +0200 Subject: use an atomic pointer to track the current song Also fix showing finished songs as current playing on new clients. --- wrms.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'wrms.go') diff --git a/wrms.go b/wrms.go index 0a844cb..fd1d10f 100644 --- a/wrms.go +++ b/wrms.go @@ -112,7 +112,7 @@ type Wrms struct { Connections sync.Map Songs []Song queue Playlist - CurrentSong Song + CurrentSong atomic.Pointer[Song] Player Player Playing bool Config Config @@ -149,12 +149,13 @@ func (wrms *Wrms) Broadcast(ev Event) { } func (wrms *Wrms) startPlaying() { - wrms.Broadcast(newEvent("play", []Song{wrms.CurrentSong})) - wrms.Player.Play(&wrms.CurrentSong) + currentSong := wrms.CurrentSong.Load() + wrms.Broadcast(newEvent("play", []Song{*currentSong})) + wrms.Player.Play(currentSong) } func (wrms *Wrms) AddSong(song Song) { - startPlayingAgain := wrms.Playing && wrms.CurrentSong.Uri == "" + startPlayingAgain := wrms.Playing && wrms.CurrentSong.Load() == nil wrms.Songs = append(wrms.Songs, song) s := &wrms.Songs[len(wrms.Songs)-1] wrms.queue.Add(s) @@ -187,17 +188,17 @@ func (wrms *Wrms) Next() { next := wrms.queue.PopSong() if next == nil { - wrms.CurrentSong.Uri = "" + wrms.CurrentSong.Store(nil) wrms.Broadcast(newNotification("stop")) return } - wrms.CurrentSong = *next + wrms.CurrentSong.Store(next) llog.Info("popped next song and removing it from the song list %v", next) for i, s := range wrms.Songs { - if s.Uri == wrms.CurrentSong.Uri { + if s.Uri == next.Uri { wrms.Songs[i] = wrms.Songs[len(wrms.Songs)-1] wrms.Songs = wrms.Songs[:len(wrms.Songs)-1] break @@ -213,7 +214,7 @@ func (wrms *Wrms) PlayPause() { wrms.Playing = !wrms.Playing if wrms.Playing { - if wrms.CurrentSong.Uri == "" { + if wrms.CurrentSong.Load() == nil { llog.Info("No song currently playing play the next") wrms.Next() } else { -- cgit v1.2.3