Skip to content

Commit

Permalink
no need for iter chan
Browse files Browse the repository at this point in the history
Inline iteration inside func(s *bState)
  • Loading branch information
vbauerster committed Aug 17, 2024
1 parent c31e10e commit 5d0c7d2
Showing 1 changed file with 19 additions and 37 deletions.
56 changes: 19 additions & 37 deletions bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,25 +159,21 @@ func (b *Bar) SetRefill(amount int64) {

// TraverseDecorators traverses all available decorators and calls cb func on each.
func (b *Bar) TraverseDecorators(cb func(decor.Decorator)) {
var wg sync.WaitGroup
iter := make(chan decor.Decorator)
select {
case b.operateState <- func(s *bState) {
var wg sync.WaitGroup
for _, decorators := range s.decorators {
wg.Add(len(decorators))
for _, d := range decorators {
iter <- d
d := d
go func() {
cb(unwrap(d))
wg.Done()
}()
}
}
close(iter)
wg.Wait()
}:
for d := range iter {
d := d
go func() {
cb(unwrap(d))
wg.Done()
}()
}
case <-b.ctx.Done():
}
}
Expand Down Expand Up @@ -281,29 +277,24 @@ func (b *Bar) EwmaIncrBy(n int, iterDur time.Duration) {
// EwmaIncrInt64 increments progress by amount of n and updates EWMA based
// decorators by dur of a single iteration.
func (b *Bar) EwmaIncrInt64(n int64, iterDur time.Duration) {
var wg sync.WaitGroup
iter := make(chan decor.EwmaDecorator)
select {
case b.operateState <- func(s *bState) {
var wg sync.WaitGroup
wg.Add(len(s.ewmaDecorators))
for _, d := range s.ewmaDecorators {
iter <- d
d := d
go func() {
d.EwmaUpdate(n, iterDur)
wg.Done()
}()
}
close(iter)
s.current += n
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(n, iterDur)
wg.Done()
}()
}
case <-b.ctx.Done():
}
}
Expand All @@ -314,34 +305,25 @@ 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
var wg sync.WaitGroup
wg.Add(len(s.ewmaDecorators))
for _, d := range s.ewmaDecorators {
iter <- item{d, n}
d := d
go func() {
d.EwmaUpdate(n, iterDur)
wg.Done()
}()
}
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():
}
}
Expand Down

0 comments on commit 5d0c7d2

Please sign in to comment.