diff --git a/bar.go b/bar.go index be5d05c..9e61f04 100644 --- a/bar.go +++ b/bar.go @@ -587,26 +587,6 @@ return table } -func (s *bState) subscribeDecorators() { - for _, decorators := range [][]decor.Decorator{ - s.pDecorators, - s.aDecorators, - } { - for _, d := range decorators { - d = unwrap(d) - if d, ok := d.(decor.AverageDecorator); ok { - s.averageDecorators = append(s.averageDecorators, d) - } - if d, ok := d.(decor.EwmaDecorator); ok { - s.ewmaDecorators = append(s.ewmaDecorators, d) - } - if d, ok := d.(decor.ShutdownListener); ok { - s.shutdownListeners = append(s.shutdownListeners, d) - } - } - } -} - func (s bState) ewmaUpdate(n int64, dur time.Duration) { var wg sync.WaitGroup for i := 0; i < len(s.ewmaDecorators); i++ { diff --git a/progress.go b/progress.go index 2ed9674..29cc277 100644 --- a/progress.go +++ b/progress.go @@ -11,6 +11,7 @@ "time" "github.com/vbauerster/mpb/v8/cwriter" + "github.com/vbauerster/mpb/v8/decor" ) const ( @@ -122,8 +123,11 @@ if filler == nil { filler = NopStyle().Build() } - p.bwg.Add(1) - result := make(chan *Bar) + type result struct { + bar *Bar + bs *bState + } + ch := make(chan result) select { case p.operateState <- func(ps *pState) { bs := ps.makeBarState(total, filler, options...) @@ -133,13 +137,24 @@ } else { ps.hm.push(bar, true) } + ch <- result{bar, bs} ps.idCount++ - result <- bar }: - bar := <-result - return bar + res := <-ch + res.bar.TraverseDecorators(func(d decor.Decorator) { + if d, ok := d.(decor.AverageDecorator); ok { + res.bs.averageDecorators = append(res.bs.averageDecorators, d) + } + if d, ok := d.(decor.EwmaDecorator); ok { + res.bs.ewmaDecorators = append(res.bs.ewmaDecorators, d) + } + if d, ok := d.(decor.ShutdownListener); ok { + res.bs.shutdownListeners = append(res.bs.shutdownListeners, d) + } + }) + p.bwg.Add(1) + return res.bar case <-p.done: - p.bwg.Done() panic(DoneError) } } @@ -407,7 +422,5 @@ bs.buffers[i] = bytes.NewBuffer(make([]byte, 0, 512)) } - bs.subscribeDecorators() - return bs }