Codebase list golang-github-vbauerster-mpb / 69756ff
delegate force refresh to bars Vladimir Bauer 7 years ago
2 changed file(s) with 36 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
3535 priority int
3636 index int
3737
38 runningBar *Bar
39 cacheState *bState
40 operateState chan func(*bState)
41 bFrameCh chan *bFrame
42 syncTableCh chan [][]chan int
43 intValue chan int64
44 completed chan bool
38 runningBar *Bar
39 cacheState *bState
40 operateState chan func(*bState)
41 bFrameCh chan *bFrame
42 syncTableCh chan [][]chan int
43 completed chan bool
44 forceRefreshCh chan time.Time
4545
4646 // done is closed by Bar's goroutine, after cacheState is written
4747 done chan struct{}
9090 func newBar(
9191 ctx context.Context,
9292 wg *sync.WaitGroup,
93 forceRefreshCh chan time.Time,
9394 filler Filler,
9495 id, width int,
9596 total int64,
121122 }
122123
123124 b := &Bar{
124 priority: s.priority,
125 runningBar: s.runningBar,
126 operateState: make(chan func(*bState)),
127 bFrameCh: make(chan *bFrame, 1),
128 syncTableCh: make(chan [][]chan int),
129 intValue: make(chan int64),
130 completed: make(chan bool),
131 done: make(chan struct{}),
132 shutdown: make(chan struct{}),
125 priority: s.priority,
126 runningBar: s.runningBar,
127 operateState: make(chan func(*bState)),
128 bFrameCh: make(chan *bFrame, 1),
129 syncTableCh: make(chan [][]chan int),
130 completed: make(chan bool),
131 done: make(chan struct{}),
132 shutdown: make(chan struct{}),
133 forceRefreshCh: forceRefreshCh,
133134 }
134135
135136 if b.runningBar != nil {
170171
171172 // ID returs id of the bar.
172173 func (b *Bar) ID() int {
173 select {
174 case b.operateState <- func(s *bState) { b.intValue <- int64(s.id) }:
175 return int(<-b.intValue)
174 result := make(chan int)
175 select {
176 case b.operateState <- func(s *bState) { result <- s.id }:
177 return <-result
176178 case <-b.done:
177179 return b.cacheState.id
178180 }
180182
181183 // Current returns bar's current number, in other words sum of all increments.
182184 func (b *Bar) Current() int64 {
183 select {
184 case b.operateState <- func(s *bState) { b.intValue <- s.current }:
185 return <-b.intValue
185 result := make(chan int64)
186 select {
187 case b.operateState <- func(s *bState) { result <- s.current }:
188 return <-result
186189 case <-b.done:
187190 return b.cacheState.current
188191 }
208211 if final {
209212 s.current = s.total
210213 s.toComplete = true
214 go b.forceRefresh()
211215 }
212216 }:
213217 return true
245249 if s.current >= s.total {
246250 s.current = s.total
247251 s.toComplete = true
252 go b.forceRefresh()
248253 }
249254 for _, ar := range s.amountReceivers {
250255 ar.NextAmount(n, wdd...)
405410 return table
406411 }
407412
413 func (b *Bar) forceRefresh() {
414 select {
415 case b.forceRefreshCh <- time.Now():
416 case <-b.shutdown:
417 }
418 }
419
408420 func newStatistics(s *bState) *decor.Statistics {
409421 return &decor.Statistics{
410422 ID: s.id,
6161 width: pwidth,
6262 rr: prr,
6363 waitBars: make(map[*Bar]*Bar),
64 forceRefreshCh: make(chan time.Time),
6465 debugOut: ioutil.Discard,
65 forceRefreshCh: make(chan time.Time),
6666 output: os.Stdout,
6767 }
6868
104104 result := make(chan *Bar)
105105 select {
106106 case p.operateState <- func(s *pState) {
107 b := newBar(s.ctx, p.bwg, filler, s.idCounter, s.width, total, options...)
107 b := newBar(s.ctx, p.bwg, s.forceRefreshCh, filler, s.idCounter, s.width, total, options...)
108108 if b.runningBar != nil {
109109 s.waitBars[b.runningBar] = b
110110 } else {
231231 frame := <-bar.bFrameCh
232232 defer func() {
233233 if frame.toShutdown {
234 go func() {
235 // force next refresh, so it will be triggered either by ticker or by
236 // this goroutine, whichever comes first
237 select {
238 case s.forceRefreshCh <- time.Now():
239 case <-bar.done:
240 }
241 }()
242234 // shutdown at next flush, in other words decrement underlying WaitGroup
243235 // only after the bar with completed state has been flushed. this
244236 // ensures no bar ends up with less than 100% rendered.