engine refactoring

This commit is contained in:
Settel 2021-08-05 01:00:31 +02:00
parent d4fc7e0563
commit 1d823af134
3 changed files with 5 additions and 8 deletions

View File

@ -9,6 +9,7 @@ import (
func NewEngine(id string) *Engine { func NewEngine(id string) *Engine {
engine := Engine{ engine := Engine{
id: id,
obs: observer.NewProperty(syncdata.SyncData{}), obs: observer.NewProperty(syncdata.SyncData{}),
} }
return &engine return &engine
@ -18,12 +19,8 @@ func (engine *Engine) Run() {
for { for {
value := engine.obs.Value().(syncdata.SyncData) 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(2 * time.Second)
value.VersionRef++ value.VersionRef++
engine.obs.Update(value) engine.obs.Update(value)
} }
} }
func (engine *Engine) GetHttpHandler() HandleFunc {
return SyncFactory(engine.obs)
}

View File

@ -8,5 +8,6 @@ import (
type HandleFunc func(http.ResponseWriter, *http.Request) type HandleFunc func(http.ResponseWriter, *http.Request)
type Engine struct { type Engine struct {
id string
obs observer.Property obs observer.Property
} }

View File

@ -3,20 +3,19 @@ package engine
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/imkira/go-observer"
"net/http" "net/http"
"sirlab.de/go/knyt/syncdata" "sirlab.de/go/knyt/syncdata"
"strconv" "strconv"
) )
func SyncFactory(obs observer.Property) HandleFunc { func (eng *Engine) GetHttpHandler() HandleFunc {
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"))
if err != nil { if err != nil {
versionRef = -1 versionRef = -1
} }
stream := obs.Observe() stream := eng.obs.Observe()
var value syncdata.SyncData var value syncdata.SyncData
for { for {
value = stream.Value().(syncdata.SyncData) value = stream.Value().(syncdata.SyncData)