remove iterator related changes
This commit is contained in:
27
async.go
27
async.go
@@ -3,7 +3,6 @@ package gubgub
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"iter"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -121,32 +120,6 @@ func (t *AsyncTopic[T]) Subscribe(fn Subscriber[T]) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Feed allows the usage of for/range to consume future published messages. The supporting subscriber will eventually be discarded after you exit the for loop.
|
||||
func (t *AsyncTopic[T]) Feed() iter.Seq[T] {
|
||||
feed := make(chan T, 1)
|
||||
done := make(chan struct{})
|
||||
|
||||
t.Subscribe(func(msg T) bool {
|
||||
select {
|
||||
case feed <- msg:
|
||||
return true
|
||||
case <-done:
|
||||
close(feed)
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
return func(yield func(T) bool) {
|
||||
defer close(done)
|
||||
|
||||
for msg := range feed {
|
||||
if !yield(msg) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type AsyncTopicOptions struct {
|
||||
onClose func()
|
||||
onSubscribe func(count int)
|
||||
|
||||
@@ -249,50 +249,6 @@ func TestAsyncTopic_AllPublishedBeforeClosedAreDeliveredAfterClosed(t *testing.T
|
||||
}
|
||||
}
|
||||
|
||||
func TestAsyncTopic_Feed(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
subscriberReady := make(chan struct{}, 1)
|
||||
defer close(subscriberReady)
|
||||
|
||||
topic := NewAsyncTopic[int](ctx,
|
||||
WithOnSubscribe(func(count int) {
|
||||
subscriberReady <- struct{}{}
|
||||
}),
|
||||
)
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
seen := make(map[int]struct{})
|
||||
for i := range topic.Feed() {
|
||||
seen[i] = struct{}{}
|
||||
if len(seen) >= 9 {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
<-subscriberReady
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
for i := range 10 {
|
||||
topic.Publish(i)
|
||||
}
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
func testTimer(t testing.TB, d time.Duration) *time.Timer {
|
||||
t.Helper()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user