-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
stream.go
74 lines (53 loc) · 1.58 KB
/
stream.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
package types
import (
"context"
)
type Stream interface {
StandardStreamEventHub
Subscribe(channel Channel, symbol string, options SubscribeOptions)
SetPublicOnly()
Connect(ctx context.Context) error
Close() error
}
type Channel string
var BookChannel = Channel("book")
var KLineChannel = Channel("kline")
//go:generate callbackgen -type StandardStream -interface
type StandardStream struct {
Subscriptions []Subscription
connectCallbacks []func()
// private trade update callbacks
tradeUpdateCallbacks []func(trade Trade)
// private order update callbacks
orderUpdateCallbacks []func(order Order)
// balance snapshot callbacks
balanceSnapshotCallbacks []func(balances BalanceMap)
balanceUpdateCallbacks []func(balances BalanceMap)
kLineClosedCallbacks []func(kline KLine)
kLineCallbacks []func(kline KLine)
bookUpdateCallbacks []func(book OrderBook)
bookSnapshotCallbacks []func(book OrderBook)
}
func (stream *StandardStream) Subscribe(channel Channel, symbol string, options SubscribeOptions) {
stream.Subscriptions = append(stream.Subscriptions, Subscription{
Channel: channel,
Symbol: symbol,
Options: options,
})
}
// SubscribeOptions provides the standard stream options
type SubscribeOptions struct {
Interval string `json:"interval,omitempty"`
Depth string `json:"depth,omitempty"`
}
func (o SubscribeOptions) String() string {
if len(o.Interval) > 0 {
return o.Interval
}
return o.Depth
}
type Subscription struct {
Symbol string `json:"symbol"`
Channel Channel `json:"channel"`
Options SubscribeOptions `json:"options"`
}