aboutsummaryrefslogtreecommitdiffstats
path: root/broadcast.go
diff options
context:
space:
mode:
Diffstat (limited to 'broadcast.go')
-rw-r--r--broadcast.go17
1 files changed, 9 insertions, 8 deletions
diff --git a/broadcast.go b/broadcast.go
index 9ad47f9..dc0dc84 100644
--- a/broadcast.go
+++ b/broadcast.go
@@ -23,21 +23,14 @@ func NewBroadcast[T any](source chan T) *Broadcast[T] {
go func() {
bc.wg.Add(1)
-
for v := range bc.source {
- bc.mu.Lock()
- for _, dest := range bc.destinations {
- dest <- v
- }
- bc.mu.Unlock()
+ bc.broadcast(v)
}
-
bc.mu.Lock()
for _, dest := range bc.destinations {
close(dest)
}
bc.mu.Unlock()
-
bc.wg.Done()
}()
return bc
@@ -57,3 +50,11 @@ func (bc *Broadcast[T]) AddDestination() <-chan T {
func (bc *Broadcast[T]) Wait() {
bc.wg.Wait()
}
+
+func (bc *Broadcast[T]) broadcast(v T) {
+ bc.mu.Lock()
+ defer bc.mu.Unlock()
+ for _, dest := range bc.destinations {
+ dest <- v
+ }
+}