Codebase list golang-github-vbauerster-mpb / 11978f4
refactoring move ctx to pState Vladimir Bauer 3 years ago
2 changed file(s) with 12 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
6464 err error
6565 }
6666
67 func newBar(container *Progress, bs *bState) *Bar {
68 ctx, cancel := context.WithCancel(container.ctx)
67 func newBar(ctx context.Context, container *Progress, bs *bState) *Bar {
68 ctx, cancel := context.WithCancel(ctx)
6969
7070 bar := &Bar{
7171 priority: bs.priority,
2222
2323 // Progress represents a container that renders one or more progress bars.
2424 type Progress struct {
25 ctx context.Context
2625 uwg *sync.WaitGroup
2726 bwg *sync.WaitGroup
2827 operateState chan func(*pState)
3433
3534 // pState holds bars in its priorityQueue, it gets passed to (*Progress).serve monitor goroutine.
3635 type pState struct {
36 ctx context.Context
3737 hm heapManager
3838 rows []io.Reader
3939
6464 // context. It's not possible to reuse instance after (*Progress).Wait
6565 // method has been called.
6666 func NewWithContext(ctx context.Context, options ...ContainerOption) *Progress {
67 ctx, cancel := context.WithCancel(ctx)
6768 s := &pState{
69 ctx: ctx,
6870 hm: make(heapManager),
6971 rows: make([]io.Reader, 32),
7072 refreshRate: defaultRefreshRate,
8486 s.manualRefresh = make(chan interface{})
8587 }
8688
87 ctx, cancel := context.WithCancel(ctx)
8889 p := &Progress{
89 ctx: ctx,
9090 uwg: s.uwg,
9191 bwg: new(sync.WaitGroup),
9292 operateState: make(chan func(*pState)),
9595 shutdown: make(chan struct{}),
9696 cancel: cancel,
9797 }
98
99 go p.serve(s, cwriter.New(s.output))
98 cw := cwriter.New(s.output)
99 go p.serve(s, cw, s.newTicker(cw.IsTerminal(), p.done))
100 go s.hm.run()
100101 return p
101102 }
102103
130131 select {
131132 case p.operateState <- func(ps *pState) {
132133 bs := ps.makeBarState(total, filler, options...)
133 bar := newBar(p, bs)
134 bar := newBar(ps.ctx, p, bs)
134135 if bs.wait.bar != nil {
135136 ps.queueBars[bs.wait.bar] = bar
136137 } else {
229230 <-p.shutdown
230231 }
231232
232 func (p *Progress) serve(s *pState, cw *cwriter.Writer) {
233 func (p *Progress) serve(s *pState, cw *cwriter.Writer, tickerC <-chan time.Time) {
233234 var err error
234235 render := func() error { return s.render(cw) }
235 tickerC := s.newTicker(p.ctx, cw.IsTerminal(), p.done)
236
237 go s.hm.run()
238236
239237 for {
240238 select {
269267 }
270268 }
271269
272 func (s *pState) newTicker(ctx context.Context, isTerminal bool, done chan struct{}) chan time.Time {
270 func (s *pState) newTicker(isTerminal bool, done chan struct{}) chan time.Time {
273271 ch := make(chan time.Time, 1)
274272 go func() {
275273 var autoRefresh <-chan time.Time
291289 } else {
292290 ch <- time.Now()
293291 }
294 case <-ctx.Done():
292 case <-s.ctx.Done():
295293 close(done)
296294 return
297295 }