refactoring
This commit is contained in:
parent
f4bd361e8e
commit
f750d5557d
@ -21,9 +21,9 @@ func NewEngine() *Engine {
|
||||
func (engine *Engine) Run() {
|
||||
for {
|
||||
value := engine.obs.Value().(handler.Value)
|
||||
fmt.Printf("sleep: %d\n", value.Value)
|
||||
fmt.Printf("sleep: %d\n", value.VersionRef)
|
||||
time.Sleep(1 * time.Second)
|
||||
value.Value++
|
||||
value.VersionRef++
|
||||
engine.obs.Update(value)
|
||||
}
|
||||
}
|
||||
|
@ -8,21 +8,21 @@ import (
|
||||
)
|
||||
|
||||
type Value struct {
|
||||
Value int
|
||||
VersionRef int
|
||||
}
|
||||
|
||||
func SyncFactory(obs observer.Property) HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
refId, err := strconv.Atoi(r.URL.Query().Get("ref"))
|
||||
versionRef, err := strconv.Atoi(r.URL.Query().Get("v"))
|
||||
if err != nil {
|
||||
refId = -1
|
||||
versionRef = -1
|
||||
}
|
||||
|
||||
stream := obs.Observe()
|
||||
var value Value
|
||||
for {
|
||||
value = stream.Value().(Value)
|
||||
if value.Value > refId {
|
||||
if value.VersionRef >= versionRef {
|
||||
break
|
||||
}
|
||||
|
||||
@ -33,6 +33,6 @@ func SyncFactory(obs observer.Property) HandlerFunc {
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "text/plain")
|
||||
fmt.Fprintf(w, "%d (%d)\n", value.Value, refId)
|
||||
fmt.Fprintf(w, "%d (%d)\n", value.VersionRef, versionRef)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user