-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
client.go
101 lines (81 loc) · 1.91 KB
/
client.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// +build js
// Package web is the driver to be used for web applications.
package web
import (
"context"
"os"
"path"
"github.com/gopherjs/gopherjs/js"
"github.com/murlokswarm/app"
)
var (
driver *Driver
)
func init() {
app.Loggers = []app.Logger{
app.NewLogger(os.Stdout, os.Stderr, true, false),
}
}
// Run satisfies the app.Driver interface.
func (d *Driver) Run(f app.Factory) error {
d.factory = f
elements := app.NewElemDB()
elements = app.ConcurrentElemDB(elements)
d.elements = elements
d.uichan = make(chan func(), 255)
driver = d
var ctx context.Context
ctx, d.cancel = context.WithCancel(context.Background())
go d.runLoop(ctx)
var err error
page, err := newPage(app.PageConfig{})
if err != nil {
return err
}
d.page = page
return nil
}
func (d *Driver) runLoop(ctx context.Context) {
for {
select {
case f := <-d.uichan:
f()
case <-ctx.Done():
return
}
}
}
// AppName satisfies the app.Driver interface.
func (d *Driver) AppName() string {
return "goapp"
}
// Resources satisfies the app.Driver interface.
func (d *Driver) Resources(p ...string) string {
resources := path.Join(p...)
resources = path.Join("resources", resources)
return resources
}
// Storage satisfies the app.Driver interface.
func (d *Driver) Storage(p ...string) string {
return ""
}
func (d *Driver) NewPage(c app.PageConfig) error {
js.Global.Get("location").Set("href", c.DefaultURL)
return nil
}
// Render satisfies the app.Driver interface.
func (d *Driver) Render(c app.Component) error {
elem, err := d.ElementByComponent(c)
if err != nil {
return err
}
return elem.Render(c)
}
// ElementByComponent satisfies the app.Driver interface.
func (d *Driver) ElementByComponent(c app.Component) (app.ElementWithComponent, error) {
return d.elements.ElementByComponent(c)
}
// CallOnUIGoroutine satisfies the app.Driver interface.
func (d *Driver) CallOnUIGoroutine(f func()) {
d.uichan <- f
}