Skip to content

Commit

Permalink
sort decorators before starting b.serve
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Aug 15, 2024
1 parent 83a2bb9 commit 6cc740c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
17 changes: 17 additions & 0 deletions bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,23 @@ func (s *bState) wSyncTable() (table syncTable) {
return table
}

func (s *bState) sortDecorators() {
for _, decorators := range s.decorators {
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) triggerCompletion(b *Bar) {
s.triggerComplete = true
if s.autoRefresh {
Expand Down
14 changes: 2 additions & 12 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"time"

"github.com/vbauerster/mpb/v8/cwriter"
"github.com/vbauerster/mpb/v8/decor"
)

const defaultRefreshRate = 150 * time.Millisecond
Expand Down Expand Up @@ -153,17 +152,6 @@ func (p *Progress) Add(total int64, filler BarFiller, options ...BarOption) (*Ba
case p.operateState <- func(ps *pState) {
bs := ps.makeBarState(total, filler, options...)
bar := newBar(ps.ctx, p, bs)
bar.TraverseDecorators(func(d decor.Decorator) {
if d, ok := d.(decor.AverageDecorator); ok {
bs.averageDecorators = append(bs.averageDecorators, d)
}
if d, ok := d.(decor.EwmaDecorator); ok {
bs.ewmaDecorators = append(bs.ewmaDecorators, d)
}
if d, ok := d.(decor.ShutdownListener); ok {
bs.shutdownListeners = append(bs.shutdownListeners, d)
}
})
if bs.waitBar != nil {
ps.queueBars[bs.waitBar] = bar
} else {
Expand Down Expand Up @@ -463,5 +451,7 @@ func (s pState) makeBarState(total int64, filler BarFiller, options ...BarOption
bs.buffers[1] = bytes.NewBuffer(make([]byte, 0, 128)) // append
bs.buffers[2] = bytes.NewBuffer(make([]byte, 0, 256)) // filler

bs.sortDecorators()

return bs
}

0 comments on commit 6cc740c

Please sign in to comment.