Codebase list golang-github-vbauerster-mpb / 1c4423e
refactoring subscribe decorators via bar.TraverseDecorators Vladimir Bauer 3 years ago
2 changed file(s) with 21 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
586586 return table
587587 }
588588
589 func (s *bState) subscribeDecorators() {
590 for _, decorators := range [][]decor.Decorator{
591 s.pDecorators,
592 s.aDecorators,
593 } {
594 for _, d := range decorators {
595 d = unwrap(d)
596 if d, ok := d.(decor.AverageDecorator); ok {
597 s.averageDecorators = append(s.averageDecorators, d)
598 }
599 if d, ok := d.(decor.EwmaDecorator); ok {
600 s.ewmaDecorators = append(s.ewmaDecorators, d)
601 }
602 if d, ok := d.(decor.ShutdownListener); ok {
603 s.shutdownListeners = append(s.shutdownListeners, d)
604 }
605 }
606 }
607 }
608
609589 func (s bState) ewmaUpdate(n int64, dur time.Duration) {
610590 var wg sync.WaitGroup
611591 for i := 0; i < len(s.ewmaDecorators); i++ {
1010 "time"
1111
1212 "github.com/vbauerster/mpb/v8/cwriter"
13 "github.com/vbauerster/mpb/v8/decor"
1314 )
1415
1516 const (
121122 if filler == nil {
122123 filler = NopStyle().Build()
123124 }
124 p.bwg.Add(1)
125 result := make(chan *Bar)
125 type result struct {
126 bar *Bar
127 bs *bState
128 }
129 ch := make(chan result)
126130 select {
127131 case p.operateState <- func(ps *pState) {
128132 bs := ps.makeBarState(total, filler, options...)
132136 } else {
133137 ps.hm.push(bar, true)
134138 }
139 ch <- result{bar, bs}
135140 ps.idCount++
136 result <- bar
137141 }:
138 bar := <-result
139 return bar
142 res := <-ch
143 res.bar.TraverseDecorators(func(d decor.Decorator) {
144 if d, ok := d.(decor.AverageDecorator); ok {
145 res.bs.averageDecorators = append(res.bs.averageDecorators, d)
146 }
147 if d, ok := d.(decor.EwmaDecorator); ok {
148 res.bs.ewmaDecorators = append(res.bs.ewmaDecorators, d)
149 }
150 if d, ok := d.(decor.ShutdownListener); ok {
151 res.bs.shutdownListeners = append(res.bs.shutdownListeners, d)
152 }
153 })
154 p.bwg.Add(1)
155 return res.bar
140156 case <-p.done:
141 p.bwg.Done()
142157 panic(DoneError)
143158 }
144159 }
406421 bs.buffers[i] = bytes.NewBuffer(make([]byte, 0, 512))
407422 }
408423
409 bs.subscribeDecorators()
410
411424 return bs
412425 }