From eb846695264d871edffb0e09b6d57a1a85793f48 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sun, 21 May 2023 16:08:23 +0200 Subject: implement incremental search results TODO: ensure late search results do not show up for future searches --- wrms.go | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'wrms.go') diff --git a/wrms.go b/wrms.go index 9cd0ac4..22cd62a 100644 --- a/wrms.go +++ b/wrms.go @@ -44,12 +44,20 @@ type Event struct { Songs []Song `json:"songs"` } +func newNotification(notification string) Event { + return Event{notification, nil} +} + type Connection struct { Id uuid.UUID Events chan Event C *websocket.Conn } +func (c *Connection) Send(ev Event) { + c.Events <- ev +} + func (c *Connection) Close() { llog.Info("Closing connection %s", c.Id) close(c.Events) @@ -57,7 +65,7 @@ func (c *Connection) Close() { } type Wrms struct { - Connections []Connection + Connections map[uuid.UUID]*Connection Songs []Song queue Playlist CurrentSong Song @@ -69,10 +77,20 @@ type Wrms struct { func NewWrms(config Config) *Wrms { wrms := Wrms{} wrms.Config = config + wrms.Connections = make(map[uuid.UUID]*Connection) wrms.Player = NewPlayer(&wrms, strings.Split(config.Backends, " ")) return &wrms } +func (wrms *Wrms) GetConn(connId string) *Connection { + id, err := uuid.Parse(connId) + if err != nil { + llog.Fatal("Failed to parse connId %s: %v", connId, err) + } + + return wrms.Connections[id] +} + func (wrms *Wrms) Close() { llog.Info("Closing WRMS") for _, conn := range wrms.Connections { @@ -80,10 +98,10 @@ func (wrms *Wrms) Close() { } } -func (wrms *Wrms) Broadcast(cmd Event) { - llog.Info("Broadcasting %v", cmd) - for i := 0; i < len(wrms.Connections); i++ { - wrms.Connections[i].Events <- cmd +func (wrms *Wrms) Broadcast(ev Event) { + llog.Info("Broadcasting %v", ev) + for _, conn := range wrms.Connections { + conn.Send(ev) } } @@ -122,14 +140,14 @@ func (wrms *Wrms) PlayPause() { wrms.Next() if wrms.CurrentSong.Uri == "" { llog.Info("No song left to play") - wrms.Broadcast(Event{"stop", []Song{}}) + wrms.Broadcast(newNotification("stop")) return } } ev = Event{"play", []Song{wrms.CurrentSong}} wrms.Player.Play(&wrms.CurrentSong) } else { - ev = Event{"pause", []Song{}} + ev = newNotification("pause") wrms.Player.Pause() } -- cgit v1.2.3