indicator: improve kline stream backfill

This commit is contained in:
c9s 2023-06-30 10:58:07 +08:00
parent dcb091cab1
commit 775ad7d906
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -28,16 +28,21 @@ func (s *KLineStream) Last(i int) *types.KLine {
func (s *KLineStream) AddSubscriber(f func(k types.KLine)) {
s.OnUpdate(f)
if len(s.kLines) > 0 {
// push historical klines to the subscriber
for _, k := range s.kLines {
f(k)
}
if len(s.kLines) == 0 {
return
}
// push historical klines to the subscriber
for _, k := range s.kLines {
f(k)
}
}
func (s *KLineStream) BackFill(kLines []types.KLine) {
s.kLines = append(s.kLines, kLines...)
for _, k := range kLines {
s.kLines = append(s.kLines, k)
s.EmitUpdate(k)
}
}
// KLines creates a KLine stream that pushes the klines to the subscribers