diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-05-31 20:53:12 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2023-05-31 20:54:08 +0200 |
| commit | 2a16dcba5a31a45a12853a15254719c357baae6d (patch) | |
| tree | 92271c8ce54846e684cd874c8c2008651c3a9778 /wrms.go | |
| parent | d8bdce51c336f3693121202402ee38ff3f12c0e2 (diff) | |
| download | WRMS-2a16dcba5a31a45a12853a15254719c357baae6d.tar.gz WRMS-2a16dcba5a31a45a12853a15254719c357baae6d.zip | |
use an atomic pointer to track the current song
Also fix showing finished songs as current playing on new
clients.
Diffstat (limited to 'wrms.go')
| -rw-r--r-- | wrms.go | 17 |
1 files changed, 9 insertions, 8 deletions
@@ -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 { |
