knowyt/server/handler/sync.go
2021-07-29 21:14:54 +02:00

39 lines
661 B
Go

package handler
import (
"fmt"
"github.com/imkira/go-observer"
"net/http"
"strconv"
)
type Value struct {
VersionRef int
}
func SyncFactory(obs observer.Property) HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
versionRef, err := strconv.Atoi(r.URL.Query().Get("v"))
if err != nil {
versionRef = -1
}
stream := obs.Observe()
var value Value
for {
value = stream.Value().(Value)
if value.VersionRef >= versionRef {
break
}
select {
case <-stream.Changes():
stream.Next()
}
}
w.Header().Add("Content-Type", "text/plain")
fmt.Fprintf(w, "%d (%d)\n", value.VersionRef, versionRef)
}
}