fixed error handling and docs

This commit is contained in:
2024-09-09 16:38:58 +01:00
parent dba01de425
commit 9283306006
5 changed files with 52 additions and 17 deletions

15
feed.go
View File

@@ -1,10 +1,12 @@
package gubgub
import "iter"
import (
"iter"
)
// Feed allows the usage of for/range to consume future published messages.
// Feed allows the usage of for/range loop to consume future published messages.
// The supporting subscriber will eventually be discarded after you exit the for loop.
func Feed[T any](t Subscribable[T], buffered bool) iter.Seq[T] {
func Feed[T any](t Subscribable[T], buffered bool) (iter.Seq[T], error) {
feed := make(chan T) // closed by the subscriber
unsubscribe := make(chan struct{}) // closed by the iterator
@@ -22,7 +24,10 @@ func Feed[T any](t Subscribable[T], buffered bool) iter.Seq[T] {
subscriber = Buffered(subscriber)
}
t.Subscribe(subscriber)
err := t.Subscribe(subscriber)
if err != nil {
return nil, err
}
// Iterator
return func(yield func(T) bool) {
@@ -33,5 +38,5 @@ func Feed[T any](t Subscribable[T], buffered bool) iter.Seq[T] {
return
}
}
}
}, nil
}