(new): sneklog migration (refactor): idiomatic names (fix): polling lifecycle (tests): polling regressions (doc): rc16 changelog
This commit is contained in:
+20
@@ -3,6 +3,7 @@ package laniakea
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"iter"
|
||||
|
||||
"git.scuroneko.dev/scuroneko/laniakea/tgapi"
|
||||
)
|
||||
@@ -67,3 +68,22 @@ func (bot *Bot[T]) Updates(ctx context.Context) ([]tgapi.Update, error) {
|
||||
}
|
||||
return updates, err
|
||||
}
|
||||
|
||||
// UpdatesIter fetches updates once and yields each update in order.
|
||||
//
|
||||
// If fetching updates fails, the iterator yields the error once with a zero
|
||||
// update and then stops.
|
||||
func (bot *Bot[T]) UpdatesIter(ctx context.Context) iter.Seq2[tgapi.Update, error] {
|
||||
return func(yield func(tgapi.Update, error) bool) {
|
||||
updates, err := bot.Updates(ctx)
|
||||
if err != nil {
|
||||
yield(tgapi.Update{}, err)
|
||||
return
|
||||
}
|
||||
for _, u := range updates {
|
||||
if !yield(u, nil) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user