fixed bugs in TeeMany
This commit is contained in:
parent
81d06f550c
commit
0a42c8cf0a
15
utils.go
15
utils.go
@ -39,21 +39,28 @@ func Tee[T any](source <-chan T) (<-chan T, <-chan T) {
|
||||
// TeeMany returns a given amount of channels which all receive all values from source.
|
||||
// It's basically a copy function for channels
|
||||
func TeeMany[T any](source <-chan T, amount int) []<-chan T {
|
||||
outs := make([]chan T, amount)
|
||||
outputs := make([]chan T, amount)
|
||||
for i := range outputs {
|
||||
outputs[i] = make(chan T, cap(source))
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
for _, out := range outs {
|
||||
for _, out := range outputs {
|
||||
close(out)
|
||||
}
|
||||
}()
|
||||
|
||||
for value := range source {
|
||||
for _, out := range outs {
|
||||
for _, out := range outputs {
|
||||
out <- value
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return (interface{}(outs)).([]<-chan T)
|
||||
readOnlyOutputs := make([]<-chan T, 0, amount)
|
||||
for _, out := range outputs {
|
||||
readOnlyOutputs = append(readOnlyOutputs, out)
|
||||
}
|
||||
return readOnlyOutputs
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user