-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathechartsgoja.go
292 lines (263 loc) · 6.93 KB
/
echartsgoja.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Package echartsgoja renders [Apache ECharts] as SVGs using the [goja]
// JavaScript runtime. Developed for use by [usql] for rendering charts.
//
// [Apache ECharts]: https://echarts.apache.org/examples/en/index.html
// [goja]: https://github.com/dop251/goja
// [usql]: https://github.com/xo/usql
package echartsgoja
import (
"context"
"embed"
"fmt"
"regexp"
"sync"
"github.com/dop251/goja"
gojaparser "github.com/dop251/goja/parser"
"github.com/dop251/goja_nodejs/console"
"github.com/dop251/goja_nodejs/eventloop"
"github.com/dop251/goja_nodejs/require"
)
// DefaultWidth is the default chart width.
var DefaultWidth = 400
// DefaultHeight is the default chart height.
var DefaultHeight = 300
// ECharts handles rendering [Apache ECharts] as SVGs, wrapping a [goja]
// runtime vm.
//
// [Apache ECharts]: https://echarts.apache.org/examples/en/index.html
// [goja]: https://github.com/dop251/goja
type ECharts struct {
logger func(...interface{})
echartsJs *goja.Program
echartsgojaJs *goja.Program
width int
height int
once sync.Once
err error
}
// New creates a new echarts instance.
func New(opts ...Option) *ECharts {
vm := &ECharts{
width: DefaultWidth,
height: DefaultHeight,
}
for _, o := range opts {
o(vm)
}
return vm
}
// init initializes the echarts instance.
func (vm *ECharts) init() error {
vm.once.Do(func() {
if vm.echartsJs, vm.err = compileEmbeddedScript("echarts.min.js"); vm.err != nil {
return
}
if vm.echartsgojaJs, vm.err = compileEmbeddedScript("echartsgoja.js"); vm.err != nil {
return
}
})
return vm.err
}
// run runs the embedded javascripts on the goja runtime, and exports a symbol
// name to v.
func (vm *ECharts) run(r *goja.Runtime, name string, v interface{}) error {
if _, err := r.RunProgram(vm.echartsJs); err != nil {
return err
}
if _, err := r.RunProgram(vm.echartsgojaJs); err != nil {
return err
}
if name != "" {
return r.ExportTo(r.Get(name), v)
}
return nil
}
// loop instantiates a new loop, and waits for it to terminate, or until the
// context is closed.
func (vm *ECharts) loop(ctx context.Context, f func(*goja.Runtime) error) error {
// init
if err := vm.init(); err != nil {
return err
}
mod := console.RequireWithPrinter(vm)
registry := new(require.Registry)
registry.RegisterNativeModule(console.ModuleName, mod)
loop := eventloop.NewEventLoop(
eventloop.WithRegistry(registry),
)
errch := make(chan error, 1)
go func() {
loop.Run(func(r *goja.Runtime) {
defer close(errch)
errch <- f(r)
})
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errch:
return err
}
}
// Version returns the embedded echarts version.
func (vm *ECharts) Version() (string, error) {
if err := vm.init(); err != nil {
return "", err
}
var f func() (string, error)
if err := vm.run(goja.New(), "version", &f); err != nil {
return "", err
}
return f()
}
// RenderOptions renders a echarts options as a SVG.
func (vm *ECharts) RenderOptions(ctx context.Context, opts string) (res string, err error) {
defer func() {
if e := recover(); e != nil {
if ex, ok := e.(*goja.Exception); ok {
res, err = "", ex.Unwrap()
} else {
res, err = "", fmt.Errorf("recovered from: %v", e)
}
}
}()
err = vm.loop(ctx, func(r *goja.Runtime) error {
var render renderOptionsFunc
if err := vm.run(r, "render_options", &render); err != nil {
return err
}
var err error
if res, err = render(vm.width, vm.height, opts); err != nil {
return err
}
return nil
})
return
}
// RenderScript renders a echarts script as a SVG.
func (vm *ECharts) RenderScript(ctx context.Context, src string) (res string, err error) {
// compile
var p *goja.Program
if p, err = compile("script", fmt.Sprintf(renderScript, cleanRE.ReplaceAllString(src, ""))); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
if ex, ok := e.(*goja.Exception); ok {
res, err = "", ex.Unwrap()
} else {
res, err = "", fmt.Errorf("recovered from: %v", e)
}
}
}()
var opts string
err = vm.loop(ctx, func(r *goja.Runtime) error {
if err := vm.run(r, "", nil); err != nil {
return err
}
v, err := r.RunProgram(p)
if err != nil {
return err
}
buf, err := v.ToObject(r).MarshalJSON()
if err != nil {
return err
}
opts = string(buf)
return nil
})
switch {
case err != nil:
return "", err
case opts == "", opts == "{}":
return "", ErrCouldNotExecuteScript
}
return vm.RenderOptions(ctx, opts)
}
// Log satisfies the [goja.Printer] interface.
func (vm *ECharts) Log(s string) {
vm.log([]string{"LOG", s})
}
// Warn satisfies the [goja.Printer] interface.
func (vm *ECharts) Warn(s string) {
vm.log([]string{"WARN", s})
}
// Error satisfies the [goja.Printer] interface.
func (vm *ECharts) Error(s string) {
vm.log([]string{"ERROR", s})
}
// log is the script callback for logging a message.
func (vm *ECharts) log(s []string) {
if vm.logger != nil {
v := make([]interface{}, len(s))
for i, ss := range s {
v[i] = ss
}
vm.logger(v...)
}
}
// Option is a echarts option.
type Option func(*ECharts)
// WithLogger is a echarts option to set the logger.
func WithLogger(logger func(...interface{})) Option {
return func(vm *ECharts) {
vm.logger = logger
}
}
// WithWidthHeight is a echarts option to set the width, height.
func WithWidthHeight(width, height int) Option {
return func(vm *ECharts) {
vm.width, vm.height = width, height
}
}
// Error is a error.
type Error string
// Errors.
const (
// ErrCouldNotExecuteScript is the could not execute script error.
ErrCouldNotExecuteScript Error = "could not execute script"
)
// Error satisfies the [error] interface.
func (err Error) Error() string {
return string(err)
}
// compile compiles a goja program.
func compile(name, src string) (*goja.Program, error) {
prg, err := goja.Parse(name, src, gojaparser.WithDisableSourceMaps)
if err != nil {
return nil, fmt.Errorf("unable to parse %s: %w", name, err)
}
p, err := goja.CompileAST(prg, true)
if err != nil {
return nil, fmt.Errorf("unable to compile %s: %w", name, err)
}
return p, nil
}
// compileEmbeddedScript compiles the embedded script as a goja program.
func compileEmbeddedScript(name string) (*goja.Program, error) {
buf, err := jsScripts.ReadFile(name)
if err != nil {
return nil, fmt.Errorf("unable to load %s: %w", name, err)
}
return compile(name, string(buf))
}
// cleanRE is a regexp for removing export {} from scripts.
var cleanRE = regexp.MustCompile(`export\s+\{\};`)
// renderScript is the render script.
const renderScript = `(function() {
let exports = {};
let app = {};
let option = {};
%s
return option;
})();`
// renderOptionsFunc is the signature for the render_options func.
type renderOptionsFunc func(width, height int, opts string) (string, error)
// versionTxt is the embedded version.txt.
//
//go:embed version.txt
var versionTxt string
// jsScripts are the embedded echarts javascripts.
//
//go:embed *.js
var jsScripts embed.FS