-
Notifications
You must be signed in to change notification settings - Fork 43
/
app.go
70 lines (62 loc) · 1.14 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package gowd
import (
"crypto/md5"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"sync"
"golang.org/x/net/html"
)
var renderMutex sync.Mutex
var execJSBuffer []string
func render(e *Element, w io.Writer) error {
renderMutex.Lock()
defer renderMutex.Unlock()
node := e.toNode()
h := md5.New()
err := html.Render(h, node)
if err != nil {
return err
}
sum := h.Sum(nil)
if reflect.DeepEqual(e.renderHash, sum) {
return nil //already rendered
}
e.renderHash = sum
err = html.Render(w, node)
if err != nil {
return err
}
_, err = fmt.Fprintln(w)
for _, value := range execJSBuffer {
ExecJSNow(value)
}
execJSBuffer = execJSBuffer[:0]
return err
}
func processEvents(e *Element, r io.Reader) error {
decoder := json.NewDecoder(r)
var event Event
err := decoder.Decode(&event)
if err != nil {
return err
}
e.ProcessEvent(&event)
return nil
}
//Run starts the message loop with body as the root element. This function never exits.
func Run(body *Element) error {
for true {
err := body.Render()
if err != nil {
return err
}
err = processEvents(body, os.Stdin)
if err != nil {
return err
}
}
return nil
}