refactoring

This commit is contained in:
Settel 2021-07-30 21:49:18 +02:00
parent 56cd5b4698
commit caa1c06ccf
3 changed files with 11 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/imkira/go-observer" "github.com/imkira/go-observer"
"sirlab.de/go/knyt/handler" "sirlab.de/go/knyt/handler"
"sirlab.de/go/knyt/syncdata"
"time" "time"
) )
@ -13,14 +14,14 @@ type Engine struct {
func NewEngine() *Engine { func NewEngine() *Engine {
engine := Engine{ engine := Engine{
obs: observer.NewProperty(handler.Value{}), obs: observer.NewProperty(syncdata.SyncData{}),
} }
return &engine return &engine
} }
func (engine *Engine) Run() { func (engine *Engine) Run() {
for { for {
value := engine.obs.Value().(handler.Value) value := engine.obs.Value().(syncdata.SyncData)
fmt.Printf("sleep: %d\n", value.VersionRef) fmt.Printf("sleep: %d\n", value.VersionRef)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
value.VersionRef++ value.VersionRef++

View File

@ -5,13 +5,10 @@ import (
"fmt" "fmt"
"github.com/imkira/go-observer" "github.com/imkira/go-observer"
"net/http" "net/http"
"sirlab.de/go/knyt/syncdata"
"strconv" "strconv"
) )
type Value struct {
VersionRef int `json:"version"`
}
func SyncFactory(obs observer.Property) HandlerFunc { func SyncFactory(obs observer.Property) HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
versionRef, err := strconv.Atoi(r.URL.Query().Get("v")) versionRef, err := strconv.Atoi(r.URL.Query().Get("v"))
@ -20,9 +17,9 @@ func SyncFactory(obs observer.Property) HandlerFunc {
} }
stream := obs.Observe() stream := obs.Observe()
var value Value var value syncdata.SyncData
for { for {
value = stream.Value().(Value) value = stream.Value().(syncdata.SyncData)
if value.VersionRef >= versionRef { if value.VersionRef >= versionRef {
break break
} }

View File

@ -0,0 +1,5 @@
package syncdata
type SyncData struct {
VersionRef int `json:"version"`
}