diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2023-05-31 22:29:12 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2023-05-31 22:29:12 +0200 |
| commit | 1bab32969f6e7bf538a923cf176d2ce2b9c3a092 (patch) | |
| tree | f708c0fa855bb915018ec2f384f07a964cda9e28 /wrms.go | |
| parent | 039881fdbfa69a33b27f84e8db3671fe8cfac5e6 (diff) | |
| download | WRMS-1bab32969f6e7bf538a923cf176d2ce2b9c3a092.tar.gz WRMS-1bab32969f6e7bf538a923cf176d2ce2b9c3a092.zip | |
tag all events with a monotonic id in order to ignore outdated ones
Diffstat (limited to 'wrms.go')
| -rw-r--r-- | wrms.go | 27 |
1 files changed, 12 insertions, 15 deletions
@@ -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 } } |
