util: recwatch: Add a helper function for merging these

I should really rework the recwatch package and API, but I wasn't in the
mood to touch this code today, so this will have to do for now.
This commit is contained in:
James Shubin
2024-09-13 19:32:54 -04:00
parent e34212a10b
commit 06cc63fcb6

View File

@@ -387,3 +387,27 @@ func isDir(path string) bool {
}
return finfo.IsDir()
}
// MergeChannels is a helper function to combine different recwatch events. It
// would be preferable to actually fix the horrible recwatch code to allow it to
// monitor more than one path from the start.
func MergeChannels(chanList ...<-chan Event) <-chan Event {
out := make(chan Event)
wg := &sync.WaitGroup{}
wg.Add(len(chanList)) // do them all together
for _, ch := range chanList {
//wg.Add(1)
go func(ch <-chan Event) {
defer wg.Done()
for v := range ch {
out <- v
}
}(ch)
}
go func() {
// Last one closes!
wg.Wait()
close(out)
}()
return out
}