diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-02-08 09:23:14 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-02-08 09:23:14 -0500 |
| commit | 9abaf20217fcd5e46ba1fc9ffe85f2d245740791 (patch) | |
| tree | 3acb1faa3d1f4e2a9419e400522b7a2116bdb160 /broadcast.go | |
| parent | 389025c95dbd5470e02cda6ced8a35ad74b3c257 (diff) | |
| download | volute-9abaf20217fcd5e46ba1fc9ffe85f2d245740791.zip | |
refactor broadcast
Diffstat (limited to 'broadcast.go')
| -rw-r--r-- | broadcast.go | 17 |
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 + } +} |