-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstateful.go
53 lines (47 loc) · 894 Bytes
/
stateful.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
package wasabi
type stateful struct {
base
builder func() Element
state eventPublisher
view Element
}
func newStateful(state eventPublisher, builder func() Element) *stateful {
c := &stateful{
base: newBase(),
builder: builder,
state: state,
}
state.AddListener(c)
c.base.SetElement(c)
return c
}
func (c *stateful) Attach(a App) {
c.base.Attach(a)
if c.view != nil {
c.view.Attach(a)
}
}
func (c *stateful) Detach() {
c.base.Detach()
if c.view != nil {
c.view.Detach()
}
}
func (c *stateful) View() string {
// return c.genHTML("div", c.build().View())
content := c.build()
content.MergeStyles(c.styles)
content.MergeAttrs(c.attrs)
return content.View()
}
func (c *stateful) build() Element {
if c.view != nil {
c.view.Detach()
c.view = c.builder()
c.view.Attach(c.app)
return c.view
} else {
c.view = c.builder()
return c.view
}
}