Codebase list golang-github-vbauerster-mpb / 07611e7
pass total over channel Vladimir Bauer 8 years ago
1 changed file(s) with 12 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
2828 ),
2929 )
3030
31 totalUpd1 := make(chan struct{})
32 totalUpd2 := make(chan struct{})
31 totalUpd := make(chan int64)
3332 go func() {
34 <-totalUpd1
35 // intermediate not final total update
36 bar.SetTotal(200, false)
37 <-totalUpd2
38 // final total update
39 bar.SetTotal(300, true)
33 for {
34 total, ok := <-totalUpd
35 bar.SetTotal(total, !ok)
36 if !ok {
37 break
38 }
39 }
4040 }()
4141
4242 max := 100 * time.Millisecond
4343 for i := 0; i < 300; i++ {
4444 if i == 140 {
45 close(totalUpd1)
45 totalUpd <- 190
4646 }
4747 if i == 250 {
48 close(totalUpd2)
48 totalUpd <- 300
49 // final upd, so closing channel
50 close(totalUpd)
4951 }
5052 time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
5153 bar.Increment()