| 32 | 32 |
|
| 33 | 33 |
// pState holds bars in its priorityQueue, it gets passed to (*Progress).serve monitor goroutine.
|
| 34 | 34 |
type pState struct {
|
| 35 | |
bHeap priorityQueue
|
| 36 | |
heapUpdated bool
|
| 37 | |
pMatrix map[int][]chan int
|
| 38 | |
aMatrix map[int][]chan int
|
| 39 | |
barShutdownQueue []*Bar
|
|
35 |
bHeap priorityQueue
|
|
36 |
heapUpdated bool
|
|
37 |
pMatrix map[int][]chan int
|
|
38 |
aMatrix map[int][]chan int
|
| 40 | 39 |
|
| 41 | 40 |
// following are provided/overrided by user
|
| 42 | 41 |
idCount int
|
|
| 259 | 258 |
|
| 260 | 259 |
func (s *pState) flush(cw *cwriter.Writer) error {
|
| 261 | 260 |
var totalLines int
|
| 262 | |
bm := make(map[*Bar]*frame, s.bHeap.Len())
|
|
261 |
pool := make([]*Bar, 0, s.bHeap.Len())
|
| 263 | 262 |
for s.bHeap.Len() > 0 {
|
| 264 | 263 |
b := heap.Pop(&s.bHeap).(*Bar)
|
| 265 | 264 |
frame := <-b.frameCh
|
|
265 |
totalLines += frame.lines
|
| 266 | 266 |
_, err := cw.ReadFrom(frame.reader)
|
| 267 | 267 |
if err != nil {
|
| 268 | 268 |
return err
|
| 269 | 269 |
}
|
| 270 | |
if frame.complete {
|
| 271 | |
// shutdown at next flush
|
| 272 | |
// this ensures no bar ends up with less than 100% rendered
|
| 273 | |
defer func() {
|
| 274 | |
s.barShutdownQueue = append(s.barShutdownQueue, b)
|
| 275 | |
}()
|
| 276 | |
} else if frame.abort {
|
| 277 | |
s.barShutdownQueue = append(s.barShutdownQueue, b)
|
| 278 | |
}
|
| 279 | |
totalLines += frame.lines
|
| 280 | |
bm[b] = frame
|
| 281 | |
}
|
| 282 | |
|
| 283 | |
for _, b := range s.barShutdownQueue {
|
| 284 | |
b.cancel()
|
| 285 | |
<-b.done // waiting for b.done, so it's safe to read b.bs
|
| 286 | |
var toDrop bool
|
| 287 | |
if qb, ok := s.queueBars[b]; ok {
|
| 288 | |
delete(s.queueBars, b)
|
| 289 | |
qb.priority = b.priority
|
| 290 | |
heap.Push(&s.bHeap, qb)
|
| 291 | |
toDrop = true
|
| 292 | |
} else if s.popCompleted && !b.bs.noPop {
|
| 293 | |
frame := bm[b]
|
| 294 | |
totalLines -= frame.lines
|
| 295 | |
toDrop = true
|
| 296 | |
}
|
| 297 | |
if toDrop || b.bs.dropOnComplete {
|
| 298 | |
delete(bm, b)
|
| 299 | |
s.heapUpdated = true
|
| 300 | |
}
|
| 301 | |
}
|
| 302 | |
s.barShutdownQueue = s.barShutdownQueue[0:0]
|
| 303 | |
|
| 304 | |
for b := range bm {
|
|
270 |
if frame.shutdown {
|
|
271 |
b.cancel()
|
|
272 |
<-b.done // waiting for b.done, so it's safe to read b.bs
|
|
273 |
var toDrop bool
|
|
274 |
if qb, ok := s.queueBars[b]; ok {
|
|
275 |
delete(s.queueBars, b)
|
|
276 |
qb.priority = b.priority
|
|
277 |
pool = append(pool, qb)
|
|
278 |
toDrop = true
|
|
279 |
} else if s.popCompleted && !b.bs.noPop {
|
|
280 |
totalLines -= frame.lines
|
|
281 |
toDrop = true
|
|
282 |
}
|
|
283 |
if toDrop || b.bs.dropOnComplete {
|
|
284 |
s.heapUpdated = true
|
|
285 |
continue
|
|
286 |
}
|
|
287 |
}
|
|
288 |
pool = append(pool, b)
|
|
289 |
}
|
|
290 |
|
|
291 |
for _, b := range pool {
|
| 305 | 292 |
heap.Push(&s.bHeap, b)
|
| 306 | 293 |
}
|
| 307 | 294 |
|