refactoring subscribe decorators via bar.TraverseDecorators
Vladimir Bauer
3 years ago
| 586 | 586 |
return table
|
| 587 | 587 |
}
|
| 588 | 588 |
|
| 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 | |
|
| 609 | 589 |
func (s bState) ewmaUpdate(n int64, dur time.Duration) {
|
| 610 | 590 |
var wg sync.WaitGroup
|
| 611 | 591 |
for i := 0; i < len(s.ewmaDecorators); i++ {
|
| 10 | 10 |
"time"
|
| 11 | 11 |
|
| 12 | 12 |
"github.com/vbauerster/mpb/v8/cwriter"
|
|
13 |
"github.com/vbauerster/mpb/v8/decor"
|
| 13 | 14 |
)
|
| 14 | 15 |
|
| 15 | 16 |
const (
|
|
| 121 | 122 |
if filler == nil {
|
| 122 | 123 |
filler = NopStyle().Build()
|
| 123 | 124 |
}
|
| 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)
|
| 126 | 130 |
select {
|
| 127 | 131 |
case p.operateState <- func(ps *pState) {
|
| 128 | 132 |
bs := ps.makeBarState(total, filler, options...)
|
|
| 132 | 136 |
} else {
|
| 133 | 137 |
ps.hm.push(bar, true)
|
| 134 | 138 |
}
|
|
139 |
ch <- result{bar, bs}
|
| 135 | 140 |
ps.idCount++
|
| 136 | |
result <- bar
|
| 137 | 141 |
}:
|
| 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
|
| 140 | 156 |
case <-p.done:
|
| 141 | |
p.bwg.Done()
|
| 142 | 157 |
panic(DoneError)
|
| 143 | 158 |
}
|
| 144 | 159 |
}
|
|
| 406 | 421 |
bs.buffers[i] = bytes.NewBuffer(make([]byte, 0, 512))
|
| 407 | 422 |
}
|
| 408 | 423 |
|
| 409 | |
bs.subscribeDecorators()
|
| 410 | |
|
| 411 | 424 |
return bs
|
| 412 | 425 |
}
|