forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonPipe.go
41 lines (37 loc) · 909 Bytes
/
jsonPipe.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
package playwright
import (
"encoding/json"
"log"
)
type jsonPipe struct {
channelOwner
}
func (j *jsonPipe) Send(message map[string]interface{}) error {
_, err := j.channel.Send("send", map[string]interface{}{
"message": message})
return err
}
func (j *jsonPipe) Close() error {
_, err := j.channel.Send("close")
return err
}
func newJsonPipe(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *jsonPipe {
j := &jsonPipe{}
j.createChannelOwner(j, parent, objectType, guid, initializer)
j.channel.On("message", func(ev map[string]interface{}) {
m, err := json.Marshal(ev["message"])
if err != nil {
log.Fatal(err)
}
var msg message
err = json.Unmarshal(m, &msg)
if err != nil {
log.Fatal(err)
}
j.Emit("message", &msg)
})
j.channel.On("closed", func() {
j.Emit("closed")
})
return j
}