Codebase list golang-github-vbauerster-mpb / 435b57f
move newTicker method down Vladimir Bauer 4 years ago
1 changed file(s) with 72 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
243243 }
244244 }
245245
246 func (s *pState) render(cw *cwriter.Writer) error {
247 if s.heapUpdated {
248 s.updateSyncMatrix()
249 s.heapUpdated = false
250 }
251 syncWidth(s.pMatrix)
252 syncWidth(s.aMatrix)
253
254 tw, err := cw.GetWidth()
255 if err != nil {
256 tw = s.reqWidth
257 }
258 for i := 0; i < s.bHeap.Len(); i++ {
259 bar := s.bHeap[i]
260 go bar.render(tw)
261 }
262
263 return s.flush(cw)
264 }
265
266 func (s *pState) flush(cw *cwriter.Writer) error {
267 var totalLines int
268 bm := make(map[*Bar]*frame, s.bHeap.Len())
269 for s.bHeap.Len() > 0 {
270 b := heap.Pop(&s.bHeap).(*Bar)
271 frame := <-b.frameCh
272 _, err := cw.ReadFrom(frame.reader)
273 if err != nil {
274 return err
275 }
276 if frame.complete {
277 // shutdown at next flush
278 // this ensures no bar ends up with less than 100% rendered
279 defer func() {
280 s.barShutdownQueue = append(s.barShutdownQueue, b)
281 }()
282 } else if frame.abort {
283 s.barShutdownQueue = append(s.barShutdownQueue, b)
284 }
285 totalLines += frame.lines
286 bm[b] = frame
287 }
288
289 for _, b := range s.barShutdownQueue {
290 b.cancel()
291 <-b.done // waiting for b.done, so it's safe to read b.bs
292 var toDrop bool
293 if qb, ok := s.queueBars[b]; ok {
294 delete(s.queueBars, b)
295 qb.bar.priority = b.priority
296 heap.Push(&s.bHeap, qb.bar)
297 go qb.serve()
298 toDrop = true
299 } else if s.popCompleted && !b.bs.noPop {
300 frame := bm[b]
301 totalLines -= frame.lines
302 toDrop = true
303 }
304 if toDrop || b.bs.dropOnComplete {
305 delete(bm, b)
306 s.heapUpdated = true
307 }
308 }
309 s.barShutdownQueue = s.barShutdownQueue[0:0]
310
311 for b := range bm {
312 heap.Push(&s.bHeap, b)
313 }
314
315 return cw.Flush(totalLines)
316 }
317
246318 func (s *pState) newTicker(done <-chan struct{}) chan time.Time {
247319 ch := make(chan time.Time)
248320 if s.shutdownNotifier == nil {
281353 return ch
282354 }
283355
284 func (s *pState) render(cw *cwriter.Writer) error {
285 if s.heapUpdated {
286 s.updateSyncMatrix()
287 s.heapUpdated = false
288 }
289 syncWidth(s.pMatrix)
290 syncWidth(s.aMatrix)
291
292 tw, err := cw.GetWidth()
293 if err != nil {
294 tw = s.reqWidth
295 }
296 for i := 0; i < s.bHeap.Len(); i++ {
297 bar := s.bHeap[i]
298 go bar.render(tw)
299 }
300
301 return s.flush(cw)
302 }
303
304 func (s *pState) flush(cw *cwriter.Writer) error {
305 var totalLines int
306 bm := make(map[*Bar]*frame, s.bHeap.Len())
307 for s.bHeap.Len() > 0 {
308 b := heap.Pop(&s.bHeap).(*Bar)
309 frame := <-b.frameCh
310 _, err := cw.ReadFrom(frame.reader)
311 if err != nil {
312 return err
313 }
314 if frame.complete {
315 // shutdown at next flush
316 // this ensures no bar ends up with less than 100% rendered
317 defer func() {
318 s.barShutdownQueue = append(s.barShutdownQueue, b)
319 }()
320 } else if frame.abort {
321 s.barShutdownQueue = append(s.barShutdownQueue, b)
322 }
323 totalLines += frame.lines
324 bm[b] = frame
325 }
326
327 for _, b := range s.barShutdownQueue {
328 b.cancel()
329 <-b.done // waiting for b.done, so it's safe to read b.bs
330 var toDrop bool
331 if qb, ok := s.queueBars[b]; ok {
332 delete(s.queueBars, b)
333 qb.bar.priority = b.priority
334 heap.Push(&s.bHeap, qb.bar)
335 go qb.serve()
336 toDrop = true
337 } else if s.popCompleted && !b.bs.noPop {
338 frame := bm[b]
339 totalLines -= frame.lines
340 toDrop = true
341 }
342 if toDrop || b.bs.dropOnComplete {
343 delete(bm, b)
344 s.heapUpdated = true
345 }
346 }
347 s.barShutdownQueue = s.barShutdownQueue[0:0]
348
349 for b := range bm {
350 heap.Push(&s.bHeap, b)
351 }
352
353 return cw.Flush(totalLines)
354 }
355
356356 func (s *pState) updateSyncMatrix() {
357357 s.pMatrix = make(map[int][]chan int)
358358 s.aMatrix = make(map[int][]chan int)