This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
120 lines (104 loc) · 2.74 KB
/
session.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
package autobahn
import (
"github.com/gopherjs/gopherjs/js"
)
type Session struct {
js.Object
}
type SubMessage struct {
args []js.Object
kwargs js.Object
details js.Object
}
func (s *Session) GetId() int {
return s.Get("id").Int()
}
func (s *Session) IsOpen() bool {
return s.Get("isOpen").Bool()
}
func (s *Session) GetSubscriptions() []Subscription {
obj := s.Get("subscriptions")
l := s.Length()
ret := make([]Subscription, l)
for i := 0; i < l; i++ {
ret[i] = Subscription{obj.Index(i)}
}
return ret
}
func (s *Session) Log(obj js.Object) {
s.Call("log", obj)
}
func (s *Session) Prefix(prefix, uri string) {
s.Call("prefix", prefix, uri)
}
func (s *Session) Subscribe(topic string, ch chan<- *SubMessage, options ...Option) (*Subscription, error) {
// Create the callback-to-channel handler.
handler := func(argsobj js.Object, kwargs, details js.Object) {
l := argsobj.Length()
args := make([]js.Object, l)
for i := 0; i < l; i++ {
args[i] = argsobj.Index(i)
}
ch <- &SubMessage{args, kwargs, details}
}
// Create subscription with handler.
obj, err := HandlePromise(s.Call("subscribe", handler))
if err != nil {
return nil, err
}
return &Subscription{obj}, nil
}
func (s *Session) Unsubscribe(sub *Subscription) (bool, error) {
ret, err := HandlePromise(s.Call("unsubscribe", sub))
if err != nil {
return false, err
}
return ret.Bool(), nil
}
func (s *Session) Publish(topic string, args []js.Object, kwargs js.Object, options ...Option) (js.Object, JSError) {
opts := map[string]js.Object{}
for _, o := range options {
opts[o.key] = o.value
}
return HandlePromise(s.Call("publish", topic, args, kwargs, opts))
}
func (s *Session) CallRPC(procedure string, args []js.Object, kwargs js.Object, options ...Option) (*Result, JSError) {
opts := make(map[string]interface{})
for _, o := range options {
opts[o.key] = o.value
}
opts["receive_progress"] = false
result, err := HandlePromise(s.Call("call", procedure, args, kwargs, opts))
if err != nil {
return nil, err
}
return &Result{result}, nil
}
func (s *Session) CallRPCProgressive(procedure string, args []js.Object, kwargs js.Object, options ...Option) (<-chan *Result, <-chan JSError, <-chan js.Object) {
opts := make(map[string]interface{})
for _, o := range options {
opts[o.key] = o.value
}
opts["receive_progress"] = false
promise := s.Call("call", procedure, args, kwargs, opts)
prog := make(chan js.Object, 100)
errchan := make(chan JSError)
final := make(chan *Result)
go func() {
promise.Call(
"then",
func(obj js.Object) {
close(prog)
final <- &Result{obj}
},
func(obj js.Object) {
close(prog)
errchan <- &jserror{obj}
},
func(obj js.Object) {
prog <- obj
},
)
}()
return final, errchan, prog
}