-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathrequire.go
112 lines (101 loc) · 2.59 KB
/
require.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
102
103
104
105
106
107
108
109
110
111
112
package require
import (
"errors"
"strings"
"sync"
"github.com/dennwc/dom"
"github.com/dennwc/dom/js"
)
var required = make(map[string]error)
func appendAndWait(e *dom.Element) error {
errc := make(chan error, 1)
e.AddEventListener("error", func(e dom.Event) {
errc <- js.NewError(e)
})
done := make(chan struct{})
e.AddEventListener("load", func(e dom.Event) {
close(done) // TODO: load may happen before an error
})
dom.Head.AppendChild(e)
select {
case err := <-errc:
return err
case <-done:
}
return nil
}
// Require adds a specified file (js or css) into the document and waits for it to load.
//
// The function relies on a file extension to detect the type. If there is no extension in
// the file path, use specific function like Stylesheet or Script. As an alternative,
// append a '#.js' or '#.css' suffix to a file path.
func Require(path string) error {
if strings.HasSuffix(path, ".css") {
return Stylesheet(path)
} else if strings.HasSuffix(path, ".js") {
return Script(path)
}
return errors.New("the file should have an extension specified (or '#.ext')")
}
// Stylesheet add a specified CSS file into the document and waits for it to load.
func Stylesheet(path string) error {
if err, ok := required[path]; ok {
return err
}
s := dom.NewElement("link")
v := s.JSValue()
v.Set("type", "text/css")
v.Set("rel", "stylesheet")
v.Set("href", path)
err := appendAndWait(s)
required[path] = err
return err
}
// Script adds a specified JS file into the document and waits for it to load.
func Script(path string) error {
if err, ok := required[path]; ok {
return err
}
s := dom.NewElement("script")
v := s.JSValue()
v.Set("async", true)
v.Set("src", path)
err := appendAndWait(s)
required[path] = err
return err
}
// MustRequire is the same as Require, but panics on an error.
func MustRequire(path string) {
err := Require(path)
if err != nil {
panic(err)
}
}
// MustRequireValue loads a specified file and returns a global value with a given name.
func MustRequireValue(name, path string) js.Value {
MustRequire(path)
return js.Get(name)
}
// RequireLazy is the same as Require, but returns a function that will load the file on the first call.
func RequireLazy(path string) func() error {
var (
once sync.Once
err error
)
return func() error {
once.Do(func() {
err = Require(path)
})
return err
}
}
// StylesheetString loads a CSS stylesheet string into the DOM.
func StylesheetString(data string) {
s := dom.NewElement("style")
s.JSValue().Set("type", "text/css")
s.SetInnerHTML(data)
err := appendAndWait(s)
if err != nil {
panic(err)
}
}