From 9abaf20217fcd5e46ba1fc9ffe85f2d245740791 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Thu, 8 Feb 2024 09:23:14 -0500 Subject: refactor broadcast --- broadcast.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'broadcast.go') 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 + } +} -- cgit v1.2.3