engine refactoring
This commit is contained in:
parent
d4fc7e0563
commit
1d823af134
@ -9,6 +9,7 @@ import (
|
||||
|
||||
func NewEngine(id string) *Engine {
|
||||
engine := Engine{
|
||||
id: id,
|
||||
obs: observer.NewProperty(syncdata.SyncData{}),
|
||||
}
|
||||
return &engine
|
||||
@ -18,12 +19,8 @@ func (engine *Engine) Run() {
|
||||
for {
|
||||
value := engine.obs.Value().(syncdata.SyncData)
|
||||
fmt.Printf("sleep: %d\n", value.VersionRef)
|
||||
time.Sleep(1 * time.Second)
|
||||
time.Sleep(2 * time.Second)
|
||||
value.VersionRef++
|
||||
engine.obs.Update(value)
|
||||
}
|
||||
}
|
||||
|
||||
func (engine *Engine) GetHttpHandler() HandleFunc {
|
||||
return SyncFactory(engine.obs)
|
||||
}
|
||||
|
@ -8,5 +8,6 @@ import (
|
||||
type HandleFunc func(http.ResponseWriter, *http.Request)
|
||||
|
||||
type Engine struct {
|
||||
id string
|
||||
obs observer.Property
|
||||
}
|
||||
|
@ -3,20 +3,19 @@ package engine
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/imkira/go-observer"
|
||||
"net/http"
|
||||
"sirlab.de/go/knyt/syncdata"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func SyncFactory(obs observer.Property) HandleFunc {
|
||||
func (eng *Engine) GetHttpHandler() HandleFunc {
|
||||
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()
|
||||
stream := eng.obs.Observe()
|
||||
var value syncdata.SyncData
|
||||
for {
|
||||
value = stream.Value().(syncdata.SyncData)
|
||||
|
Loading…
Reference in New Issue
Block a user