-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.go
49 lines (42 loc) · 924 Bytes
/
bridge.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
//+build js
package gonobridge
import (
"syscall/js"
"time"
)
var (
quit = "quit"
jsProcess = js.Global().Get("process")
eventEmitter = js.Global().Get("Emitter")
)
//Emit Emit an event to nodejs
//if no listeners return false
func Emit(name string, value interface{}) bool {
if name == quit {
panic("quit is an reserved")
}
return eventEmitter.Call("emit", name, value).Bool()
}
func Listen(name string, callback js.Callback) {
eventEmitter.Call("on", name, callback)
}
//Wait keep the process running until SIGTERM is triggered or quit signal is emitted
func Wait() {
//avoid deadlock
//todo better way
go func() {
for {
time.Sleep(time.Hour)
}
}()
var done = make(chan struct{})
Listen("SIGTERM", js.NewCallback(func(args []js.Value) {
done <- struct{}{}
close(done)
}))
Listen(quit, js.NewCallback(func(args []js.Value) {
done <- struct{}{}
close(done)
}))
<-done
}