Refactoring: better check for uncompleted bars
Vladimir Bauer
9 years ago
| 26 | 26 | completeReqCh chan struct{} |
| 27 | 27 | removeReqCh chan struct{} |
| 28 | 28 | done chan struct{} |
| 29 | completed chan struct{} | |
| 29 | inProgress chan struct{} | |
| 30 | 30 | cancel <-chan struct{} |
| 31 | 31 | |
| 32 | 32 | // follawing are used after (*Bar.done) is closed |
| 39 | 39 | type Statistics struct { |
| 40 | 40 | ID int |
| 41 | 41 | Completed bool |
| 42 | Aborted bool | |
| 42 | 43 | Total int64 |
| 43 | 44 | Current int64 |
| 44 | 45 | StartTime time.Time |
| 72 | 73 | trimLeftSpace bool |
| 73 | 74 | trimRightSpace bool |
| 74 | 75 | completed bool |
| 76 | aborted bool | |
| 75 | 77 | startTime time.Time |
| 76 | 78 | timeElapsed time.Duration |
| 77 | 79 | timePerItem time.Duration |
| 91 | 93 | removeReqCh: make(chan struct{}), |
| 92 | 94 | completeReqCh: make(chan struct{}), |
| 93 | 95 | done: make(chan struct{}), |
| 94 | completed: make(chan struct{}), | |
| 96 | inProgress: make(chan struct{}), | |
| 95 | 97 | cancel: conf.cancel, |
| 96 | 98 | } |
| 97 | 99 | |
| 236 | 238 | // Can be used as condition in for loop |
| 237 | 239 | func (b *Bar) InProgress() bool { |
| 238 | 240 | select { |
| 239 | case <-b.completed: | |
| 241 | case <-b.inProgress: | |
| 240 | 242 | return false |
| 241 | 243 | default: |
| 242 | 244 | return true |
| 323 | 325 | s.updateTimePerItemEstimate(incrStartTime, r.amount) |
| 324 | 326 | if n == s.total { |
| 325 | 327 | s.completed = true |
| 326 | close(b.completed) | |
| 328 | close(b.inProgress) | |
| 327 | 329 | } |
| 328 | 330 | s.current = n |
| 329 | 331 | if r.refill != nil { |
| 341 | 343 | case <-b.removeReqCh: |
| 342 | 344 | return |
| 343 | 345 | case <-b.cancel: |
| 344 | close(b.completed) | |
| 346 | s.aborted = true | |
| 347 | close(b.inProgress) | |
| 345 | 348 | return |
| 346 | 349 | } |
| 347 | 350 | } |
| 491 | 494 | return &Statistics{ |
| 492 | 495 | ID: s.id, |
| 493 | 496 | Completed: s.completed, |
| 497 | Aborted: s.aborted, | |
| 494 | 498 | Total: s.total, |
| 495 | 499 | Current: s.current, |
| 496 | 500 | StartTime: s.startTime, |