types: add callbacks to the stream order book

This commit is contained in:
c9s 2022-08-04 17:57:23 +08:00
parent 1cd48177ae
commit 96b10caa8c
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 31 additions and 0 deletions

View File

@ -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()
})
}

View 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)
}
}