Wait for flushed in render
Vladimir Bauer
9 years ago
| 85 | 85 | prependFuncs []DecoratorFunc |
| 86 | 86 | simpleSpinner func() byte |
| 87 | 87 | refill *refill |
| 88 | flushed chan struct{} | |
| 88 | // flushed chan struct{} | |
| 89 | 89 | } |
| 90 | 90 | ) |
| 91 | 91 | |
| 150 | 150 | } |
| 151 | 151 | select { |
| 152 | 152 | case b.ops <- func(s *state) { |
| 153 | defer func() { | |
| 154 | if s.completed { | |
| 155 | b.Complete() | |
| 156 | } | |
| 157 | s.blockStartTime = time.Now() | |
| 158 | }() | |
| 159 | 153 | if s.current == 0 { |
| 160 | 154 | s.startTime = time.Now() |
| 161 | 155 | s.blockStartTime = s.startTime |
| 169 | 163 | return |
| 170 | 164 | } |
| 171 | 165 | s.current = sum |
| 166 | s.blockStartTime = time.Now() | |
| 172 | 167 | }: |
| 173 | 168 | case <-b.done: |
| 174 | 169 | return |
| 257 | 252 | } |
| 258 | 253 | } |
| 259 | 254 | |
| 260 | // func (b *Bar) getState() state { | |
| 261 | // result := make(chan state, 1) | |
| 262 | // select { | |
| 263 | // case b.ops <- func(s *state) { result <- *s }: | |
| 264 | // return <-result | |
| 265 | // case <-b.done: | |
| 266 | // return b.state | |
| 267 | // } | |
| 268 | // } | |
| 269 | ||
| 270 | 255 | func (b *Bar) server(s state, wg *sync.WaitGroup, cancel <-chan struct{}) { |
| 271 | 256 | |
| 272 | 257 | defer func() { |
| 273 | 258 | b.state = s |
| 274 | close(b.done) | |
| 275 | <-s.flushed | |
| 259 | // <-s.flushed | |
| 276 | 260 | // fmt.Fprintf(os.Stderr, "Bar:%d flushed\n", s.id) |
| 277 | 261 | wg.Done() |
| 262 | close(b.done) | |
| 278 | 263 | }() |
| 279 | 264 | |
| 280 | 265 | for { |
| 291 | 276 | } |
| 292 | 277 | } |
| 293 | 278 | } |
| 279 | ||
| 280 | // func (b *Bar) render(tw int, flushed chan struct{}, prependWs, appendWs *widthSync) <-chan []byte { | |
| 281 | // ch := make(chan []byte) | |
| 282 | ||
| 283 | // go func() { | |
| 284 | // defer func() { | |
| 285 | // // recovering if external decorators panic | |
| 286 | // if p := recover(); p != nil { | |
| 287 | // ch <- []byte(fmt.Sprintln(p)) | |
| 288 | // } | |
| 289 | // close(ch) | |
| 290 | // }() | |
| 291 | // result := make(chan []byte, 1) | |
| 292 | // select { | |
| 293 | // case b.ops <- func(s *state) { | |
| 294 | // buf := draw(s, tw, prependWs, appendWs) | |
| 295 | // buf = append(buf, '\n') | |
| 296 | // result <- buf | |
| 297 | // // wait for flushed | |
| 298 | // if s.completed { | |
| 299 | // <-flushed | |
| 300 | // b.Complete() | |
| 301 | // } | |
| 302 | // }: | |
| 303 | // ch <- <-result | |
| 304 | // case <-b.done: | |
| 305 | // buf := draw(&b.state, tw, prependWs, appendWs) | |
| 306 | // buf = append(buf, '\n') | |
| 307 | // ch <- buf | |
| 308 | // default: | |
| 309 | // ch <- []byte{} | |
| 310 | // } | |
| 311 | // }() | |
| 312 | ||
| 313 | // return ch | |
| 314 | // } | |
| 294 | 315 | |
| 295 | 316 | func (b *Bar) render(tw int, flushed chan struct{}, prependWs, appendWs *widthSync) <-chan []byte { |
| 296 | 317 | ch := make(chan []byte) |
| 307 | 328 | result := make(chan state, 1) |
| 308 | 329 | select { |
| 309 | 330 | case b.ops <- func(s *state) { |
| 310 | s.flushed = flushed | |
| 311 | 331 | result <- *s |
| 332 | if s.completed { | |
| 333 | <-flushed | |
| 334 | b.Complete() | |
| 335 | } | |
| 312 | 336 | }: |
| 313 | 337 | st = <-result |
| 314 | 338 | case <-b.done: |