-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard_renderer.go
57 lines (49 loc) · 1010 Bytes
/
standard_renderer.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
package masc
import (
"sync"
)
// standardRenderer uses vecty's rendering model
type standardRenderer struct {
rootNode jsObject
rendered bool
mtx *sync.Mutex
}
// newRenderer creates a new renderer. Normally you'll want to initialize it
// with os.Stdout as the first argument.
func newRenderer() renderer {
r := &standardRenderer{
mtx: &sync.Mutex{},
}
return r
}
func newNodeRenderer(node jsObject) renderer {
r := &standardRenderer{
mtx: &sync.Mutex{},
rootNode: node,
}
return r
}
// start starts the renderer.
func (r *standardRenderer) start() {
r.rendered = false
}
func isZeroValue(v jsObject) bool {
return v == nil || !v.Truthy()
}
func (r *standardRenderer) render(c Component, send func(Msg)) {
r.mtx.Lock()
defer r.mtx.Unlock()
if r.rendered {
rerender(c, send)
return
}
r.rendered = true
if !isZeroValue(r.rootNode) {
err := renderIntoNode("RenderIntoNode", r.rootNode, c, send)
if err != nil {
panic(err)
}
} else {
RenderBody(c, send)
}
}