From 1bab32969f6e7bf538a923cf176d2ce2b9c3a092 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 31 May 2023 22:29:12 +0200 Subject: tag all events with a monotonic id in order to ignore outdated ones --- wrms.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'wrms.go') diff --git a/wrms.go b/wrms.go index 9409b49..c983eb5 100644 --- a/wrms.go +++ b/wrms.go @@ -42,20 +42,16 @@ func NewSongFromJson(data []byte) (Song, error) { type Event struct { Event string `json:"cmd"` - Id int `json:"id"` + Id uint64 `json:"id"` Songs []Song `json:"songs"` } -func newEvent(event string, songs []Song) Event { - return Event{Event: event, Songs: songs} +func (wrms *Wrms) newEvent(event string, songs []Song) Event { + return Event{Event: event, Id: wrms.eventId.Add(1), Songs: songs} } func newNotification(notification string) Event { - return Event{Event: notification} -} - -func newSearchResultEvent(id int, songs []Song) Event { - return Event{Event: "search", Id: id, Songs: songs} + return Event{Event: notification, Id: wrms.eventId.Add(1)} } type Connection struct { @@ -116,6 +112,7 @@ type Wrms struct { Player Player Playing bool Config Config + eventId atomic.Uint64 } func NewWrms(config Config) *Wrms { @@ -150,7 +147,7 @@ func (wrms *Wrms) Broadcast(ev Event) { func (wrms *Wrms) startPlaying() { currentSong := wrms.CurrentSong.Load() - wrms.Broadcast(newEvent("play", []Song{*currentSong})) + wrms.Broadcast(wrms.newEvent("play", []Song{*currentSong})) } func (wrms *Wrms) AddSong(song Song) { @@ -159,7 +156,7 @@ func (wrms *Wrms) AddSong(song Song) { s := &wrms.Songs[len(wrms.Songs)-1] wrms.queue.Add(s) llog.Info("Added song %s (ptr=%p) to Songs", s.Uri, s) - wrms.Broadcast(newEvent("add", []Song{song})) + wrms.Broadcast(wrms.newEvent("add", []Song{song})) if startPlayingAgain { wrms.Next() @@ -177,7 +174,7 @@ func (wrms *Wrms) DeleteSong(songUri string) { wrms.Songs = wrms.Songs[:len(wrms.Songs)-1] wrms.queue.RemoveSong(s) - wrms.Broadcast(newEvent("delete", []Song{*s})) + wrms.Broadcast(wrms.newEvent("delete", []Song{*s})) break } } @@ -205,10 +202,10 @@ func (wrms *Wrms) Next() { } if wrms.Playing { - wrms.Broadcast(newEvent("play", []Song{*next})) + wrms.Broadcast(wrms.newEvent("play", []Song{*next})) wrms.Player.Play(next) } else if wrms.Player.mpv != nil { - wrms.Broadcast(newEvent("next", []Song{*next})) + wrms.Broadcast(wrms.newEvent("next", []Song{*next})) wrms.Player.terminateMpv() } } @@ -222,7 +219,7 @@ func (wrms *Wrms) PlayPause() { llog.Info("No song currently playing play the next") wrms.Next() } else { - wrms.Broadcast(newEvent("play", []Song{*currentSong})) + wrms.Broadcast(wrms.newEvent("play", []Song{*currentSong})) if wrms.Player.mpv != nil { wrms.Player.Continue() } else { @@ -293,7 +290,7 @@ func (wrms *Wrms) AdjustSongWeight(connId uuid.UUID, songUri string, vote string } wrms.queue.Adjust(s) - wrms.Broadcast(newEvent("update", []Song{*s})) + wrms.Broadcast(wrms.newEvent("update", []Song{*s})) break } } -- cgit v1.2.3