-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
90 lines (71 loc) · 1.91 KB
/
main.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
// +build js,wasm
package main
import (
"github.com/MontFerret/ferret-wasm/ferret"
"github.com/pkg/errors"
"syscall/js"
)
const namespace = "ferret"
var (
version = "undefined"
ferretVersion = "undefined"
)
func notify(callback js.Value, res *ferret.Result) {
if res.Ok() {
callback.Invoke(js.Undefined(), res.Data())
} else {
callback.Invoke(res.Error(), js.Undefined())
}
}
func main() {
c := make(chan struct{}, 0)
f := ferret.New(ferret.Version{version, ferretVersion})
js.Global().Set(namespace, make(map[string]interface{}))
module := js.Global().Get(namespace)
module.Set("version", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return js.ValueOf(f.Version())
}))
module.Set("compile", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(args) < 1 {
return ferret.Error(errors.New("Missed query"))
}
return f.Compile(args[0])
}))
module.Set("destroy", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(args) < 1 {
return ferret.Error(errors.New("Missed query"))
}
return f.Destroy(args[0])
}))
module.Set("run", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(args) < 3 {
return ferret.Error(errors.New("Missed arguments"))
}
id := args[0]
params := args[1]
callback := args[2]
go func() {
notify(callback, f.Run(id, params))
}()
return ferret.OkEmpty()
}))
module.Set("exec", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(args) < 3 {
return ferret.Error(errors.New("Missed arguments"))
}
query := args[0]
params := args[1]
callback := args[2]
go func() {
notify(callback, f.Execute(query, params))
}()
return ferret.OkEmpty()
}))
module.Set("register", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(args) < 2 {
return ferret.Error(errors.New("Missed arguments"))
}
return f.Register(args[0], args[1])
}))
<-c
}