diff --git a/bar.go b/bar.go index eb5be9a..588d287 100644 --- a/bar.go +++ b/bar.go @@ -187,13 +187,16 @@ } // SetTotal sets total to an arbitrary value. It's effective only for -// bar which was constructed with `total <= 0`. Setting total to negative -// value is equivalent to (*Bar).SetTotal((*Bar).Current(), bool). -// If triggerCompleteNow is true then total value is set to current -// and complete event is triggered right away. +// bar which was constructed with `total <= 0` and if +// (*Bar).EnableTriggerComplete hasn't been called yet. Setting total +// to negative value is equivalent to (*Bar).SetTotal((*Bar).Current(), +// bool). If triggerCompleteNow is true then total value is set to +// current and complete event is triggered right away. func (b *Bar) SetTotal(total int64, triggerCompleteNow bool) { - select { - case b.operateState <- func(s *bState) { + triggerComplete := make(chan bool, 1) + select { + case b.operateState <- func(s *bState) { + triggerComplete <- s.triggerComplete if s.triggerComplete { return } @@ -209,7 +212,10 @@ } }: if triggerCompleteNow { - <-b.done + triggerComplete := <-triggerComplete + if !triggerComplete { + <-b.done + } } case <-b.done: }