mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
types: add callbacks to the stream order book
This commit is contained in:
parent
1cd48177ae
commit
96b10caa8c
|
@ -114,12 +114,16 @@ func (b *MutexOrderBook) Update(update SliceOrderBook) {
|
|||
b.Unlock()
|
||||
}
|
||||
|
||||
//go:generate callbackgen -type StreamOrderBook
|
||||
// StreamOrderBook receives streaming data from websocket connection and
|
||||
// update the order book with mutex lock, so you can safely access it.
|
||||
type StreamOrderBook struct {
|
||||
*MutexOrderBook
|
||||
|
||||
C sigchan.Chan
|
||||
|
||||
updateCallbacks []func(update SliceOrderBook)
|
||||
snapshotCallbacks []func(snapshot SliceOrderBook)
|
||||
}
|
||||
|
||||
func NewStreamBook(symbol string) *StreamOrderBook {
|
||||
|
@ -136,6 +140,7 @@ func (sb *StreamOrderBook) BindStream(stream Stream) {
|
|||
}
|
||||
|
||||
sb.Load(book)
|
||||
sb.EmitSnapshot(book)
|
||||
sb.C.Emit()
|
||||
})
|
||||
|
||||
|
@ -145,6 +150,7 @@ func (sb *StreamOrderBook) BindStream(stream Stream) {
|
|||
}
|
||||
|
||||
sb.Update(book)
|
||||
sb.EmitUpdate(book)
|
||||
sb.C.Emit()
|
||||
})
|
||||
}
|
||||
|
|
25
pkg/types/streamorderbook_callbacks.go
Normal file
25
pkg/types/streamorderbook_callbacks.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
// Code generated by "callbackgen -type StreamOrderBook"; DO NOT EDIT.
|
||||
|
||||
package types
|
||||
|
||||
import ()
|
||||
|
||||
func (sb *StreamOrderBook) OnUpdate(cb func(update SliceOrderBook)) {
|
||||
sb.updateCallbacks = append(sb.updateCallbacks, cb)
|
||||
}
|
||||
|
||||
func (sb *StreamOrderBook) EmitUpdate(update SliceOrderBook) {
|
||||
for _, cb := range sb.updateCallbacks {
|
||||
cb(update)
|
||||
}
|
||||
}
|
||||
|
||||
func (sb *StreamOrderBook) OnSnapshot(cb func(snapshot SliceOrderBook)) {
|
||||
sb.snapshotCallbacks = append(sb.snapshotCallbacks, cb)
|
||||
}
|
||||
|
||||
func (sb *StreamOrderBook) EmitSnapshot(snapshot SliceOrderBook) {
|
||||
for _, cb := range sb.snapshotCallbacks {
|
||||
cb(snapshot)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user