remove iterator related changes

This commit is contained in:
2024-08-22 16:15:09 +01:00
parent 3f38749085
commit baf4630ce7
3 changed files with 1 additions and 72 deletions

View File

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

View File

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

2
go.mod
View File

@@ -1,6 +1,6 @@
module gitlab.com/naterciom/gubgub
go 1.23
go 1.22.5
require github.com/stretchr/testify v1.9.0