Skip to content

Commit

Permalink
minor: move EwmaSetCurrent close to EwmaIncrInt64
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Aug 17, 2024
1 parent 111651a commit ff80242
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,44 +244,6 @@ func (b *Bar) SetCurrent(current int64) {
}
}

// EwmaSetCurrent sets progress' current to an arbitrary value and updates
// EWMA based decorators by dur of a single iteration.
func (b *Bar) EwmaSetCurrent(current int64, iterDur time.Duration) {
if current < 0 {
return
}
type item struct {
decor.EwmaDecorator
n int64
}
var wg sync.WaitGroup
iter := make(chan item)
select {
case b.operateState <- func(s *bState) {
n := current - s.current
wg.Add(len(s.ewmaDecorators))
for _, d := range s.ewmaDecorators {
iter <- item{d, n}
}
close(iter)
s.current = current
if s.triggerComplete && s.current >= s.total {
s.current = s.total
s.triggerCompletion(b)
}
wg.Wait()
}:
for d := range iter {
d := d
go func() {
d.EwmaUpdate(d.n, iterDur)
wg.Done()
}()
}
case <-b.ctx.Done():
}
}

// Increment is a shorthand for b.IncrInt64(1).
func (b *Bar) Increment() {
b.IncrInt64(1)
Expand Down Expand Up @@ -346,6 +308,44 @@ func (b *Bar) EwmaIncrInt64(n int64, iterDur time.Duration) {
}
}

// EwmaSetCurrent sets progress' current to an arbitrary value and updates
// EWMA based decorators by dur of a single iteration.
func (b *Bar) EwmaSetCurrent(current int64, iterDur time.Duration) {
if current < 0 {
return
}
type item struct {
decor.EwmaDecorator
n int64
}
var wg sync.WaitGroup
iter := make(chan item)
select {
case b.operateState <- func(s *bState) {
n := current - s.current
wg.Add(len(s.ewmaDecorators))
for _, d := range s.ewmaDecorators {
iter <- item{d, n}
}
close(iter)
s.current = current
if s.triggerComplete && s.current >= s.total {
s.current = s.total
s.triggerCompletion(b)
}
wg.Wait()
}:
for d := range iter {
d := d
go func() {
d.EwmaUpdate(d.n, iterDur)
wg.Done()
}()
}
case <-b.ctx.Done():
}
}

// DecoratorAverageAdjust adjusts decorators which implement decor.AverageDecorator interface.
// Call if there is need to set start time after decorators have been constructed.
func (b *Bar) DecoratorAverageAdjust(start time.Time) {
Expand Down

0 comments on commit ff80242

Please sign in to comment.