aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-05-31 22:29:12 +0200
committerFlorian Fischer <florian.fischer@muhq.space>2023-05-31 22:29:12 +0200
commit1bab32969f6e7bf538a923cf176d2ce2b9c3a092 (patch)
treef708c0fa855bb915018ec2f384f07a964cda9e28
parent039881fdbfa69a33b27f84e8db3671fe8cfac5e6 (diff)
downloadWRMS-1bab32969f6e7bf538a923cf176d2ce2b9c3a092.tar.gz
WRMS-1bab32969f6e7bf538a923cf176d2ce2b9c3a092.zip
tag all events with a monotonic id in order to ignore outdated ones
-rw-r--r--TODO.md3
-rw-r--r--main.go17
-rw-r--r--web/client.html3
-rw-r--r--wrms.go27
4 files changed, 22 insertions, 28 deletions
diff --git a/TODO.md b/TODO.md
index 1361126..49f4a23 100644
--- a/TODO.md
+++ b/TODO.md
@@ -14,6 +14,7 @@
- [X] escape HTML
- [X] support incremental search results
- [X] distinguish different search queries
+- [ ] do not apply obsolete events
## Web Backend
@@ -21,7 +22,9 @@
## Backends
+- [ ] cancel old searches
- [ ] make wrms thread safe
+ - [X] tag events with ids
- [X] paralyze search
- [X] report results incrementally
- [X] distinguish different search queries
diff --git a/main.go b/main.go
index 8ff3a81..532cead 100644
--- a/main.go
+++ b/main.go
@@ -8,7 +8,6 @@ import (
"html/template"
"io"
"net/http"
- "strconv"
"strings"
"time"
@@ -85,11 +84,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
return
}
- id, ok := strconv.Atoi(r.URL.Query().Get("id"))
- if ok != err {
- http.Error(w, "No or invalid search id provided", http.StatusBadRequest)
- return
- }
+ id := wrms.eventId.Add(1)
llog.Debug("Searching for %s", pattern)
@@ -99,7 +94,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request) {
go func() {
for result := range resultsChan {
if len(result) > 0 {
- conn.Send(newSearchResultEvent(id, result))
+ conn.Send(Event{Event: "search", Id: id, Songs: result})
}
}
@@ -235,7 +230,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
if currentSong := wrms.CurrentSong.Load(); currentSong != nil {
songs = []Song{*currentSong}
}
- data, err := json.Marshal(newEvent("play", songs))
+ data, err := json.Marshal(wrms.newEvent("play", songs))
if err != nil {
llog.Error("Encoding the play event failed with %s", err)
return
@@ -246,7 +241,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
upvoted := []Song{}
downvoted := []Song{}
if len(wrms.Songs) > 0 {
- data, err := json.Marshal(newEvent("add", wrms.Songs))
+ data, err := json.Marshal(wrms.newEvent("add", wrms.Songs))
if err != nil {
llog.Error("Encoding the add event failed with %s", err)
return
@@ -263,7 +258,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
}
if len(upvoted) > 0 {
- data, err := json.Marshal(newEvent("upvoted", upvoted))
+ data, err := json.Marshal(wrms.newEvent("upvoted", upvoted))
if err != nil {
llog.Error("Encoding the upvoted event failed with %s", err)
return
@@ -272,7 +267,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) {
}
if len(downvoted) > 0 {
- data, err := json.Marshal(newEvent("downvoted", downvoted))
+ data, err := json.Marshal(wrms.newEvent("downvoted", downvoted))
if err != nil {
llog.Error("Encoding the upvoted event failed with %s", err)
return
diff --git a/web/client.html b/web/client.html
index 8e0fa04..41e224f 100644
--- a/web/client.html
+++ b/web/client.html
@@ -291,7 +291,6 @@
const form = new FormData(document.getElementById("searchForm"));
const urlParams = new URLSearchParams(form);
- urlParams.append("id", searchId);
new HttpClient().get("/search?" + urlParams.toString(), console.log);
return false;
}
@@ -320,12 +319,12 @@
searchingIndicator.style.display = "none";
let searchResultsOverlay = document.getElementById("searchResultsOverlay");
searchResultsOverlay['aria-busy'] = false;
- searchId += 1;
}
function handleSearch(id, songs) {
// Ignore stale search results
if (id < searchId) { return; }
+ if (id > searchId) { searchId = id; }
let searchResultsOverlay = document.getElementById("searchResultsOverlay");
let searchResults = document.getElementById("searchResults");
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
}
}