From 06cc63fcb6ae4795fd941d5ea4906aef26696870 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Fri, 13 Sep 2024 19:32:54 -0400 Subject: [PATCH] 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. --- util/recwatch/recwatch.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/util/recwatch/recwatch.go b/util/recwatch/recwatch.go index f9aff50e..bade846b 100644 --- a/util/recwatch/recwatch.go +++ b/util/recwatch/recwatch.go @@ -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 +}