-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (65 loc) · 1.69 KB
/
Copy pathmain.go
File metadata and controls
81 lines (65 loc) · 1.69 KB
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
71
72
73
74
75
76
77
78
79
80
81
package main
//go:generate sh -c "GOOS=js GOARCH=wasm go build -o basic.wasm ."
import (
"strconv"
"time"
"github.com/speier/gowasm/pkg/client"
"github.com/speier/gowasm/pkg/component"
"github.com/speier/gowasm/pkg/dom"
"github.com/speier/gowasm/pkg/vdom"
)
func main() {
// simple components
dom.Render(HelloMessage("World!"), dom.QuerySelector("#s1"))
dom.Render(TextBox("type here..."), dom.QuerySelector("#s2"))
// stateful components
go client.Mount(TimerFn(0), dom.QuerySelector("#s3"))
client.Mount(Timer(0), dom.QuerySelector("#s4"))
}
var h = vdom.H
func HelloMessage(name string) *vdom.VNode {
return h("div", nil, h("Hello ", nil), h(name, nil))
}
func TextBox(placeholder string) *vdom.VNode {
textbox := h("input", &vdom.Attrs{Props: &vdom.Props{"type": "text", "placeholder": placeholder}})
textbox.OnCreate = func() {
println("oncreate")
}
return textbox
}
func TimerFn(seconds int) func(updateHandler func()) *vdom.VNode {
update := func() {}
ticker := time.NewTicker(time.Second * 1)
go func() {
for range ticker.C {
seconds++
update()
}
}()
return func(updateHandler func()) *vdom.VNode {
update = updateHandler
return h("div", nil, h("Seconds: ", nil), h(strconv.Itoa(seconds), nil))
}
}
// component
type TimerComp struct {
seconds int
ctx component.Context
}
func Timer(seconds int) *TimerComp {
t := &TimerComp{seconds: seconds}
ticker := time.NewTicker(time.Second * 1)
go func() {
for range ticker.C {
t.seconds++
t.ctx.Update()
}
}()
return t
}
func (t *TimerComp) Init(ctx component.Context) {
t.ctx = ctx
}
func (t *TimerComp) Render() *vdom.VNode {
return h("div", nil, h("Seconds: ", nil), h(strconv.Itoa(t.seconds), nil))
}