no need for iter chan
Inline iteration inside func(s *bState)
Vladimir Bauer
1 year, 10 months ago
| 158 | 158 | |
| 159 | 159 | // TraverseDecorators traverses all available decorators and calls cb func on each. |
| 160 | 160 | func (b *Bar) TraverseDecorators(cb func(decor.Decorator)) { |
| 161 | var wg sync.WaitGroup | |
| 162 | iter := make(chan decor.Decorator) | |
| 163 | select { | |
| 164 | case b.operateState <- func(s *bState) { | |
| 161 | select { | |
| 162 | case b.operateState <- func(s *bState) { | |
| 163 | var wg sync.WaitGroup | |
| 165 | 164 | for _, decorators := range s.decorators { |
| 165 | wg.Add(len(decorators)) | |
| 166 | 166 | for _, d := range decorators { |
| 167 | iter <- d | |
| 167 | d := d | |
| 168 | go func() { | |
| 169 | cb(unwrap(d)) | |
| 170 | wg.Done() | |
| 171 | }() | |
| 168 | 172 | } |
| 169 | 173 | } |
| 170 | close(iter) | |
| 171 | 174 | wg.Wait() |
| 172 | 175 | }: |
| 173 | for d := range iter { | |
| 174 | d := d | |
| 175 | go func() { | |
| 176 | cb(unwrap(d)) | |
| 177 | wg.Done() | |
| 178 | }() | |
| 179 | } | |
| 180 | 176 | case <-b.ctx.Done(): |
| 181 | 177 | } |
| 182 | 178 | } |
| 280 | 276 | // EwmaIncrInt64 increments progress by amount of n and updates EWMA based |
| 281 | 277 | // decorators by dur of a single iteration. |
| 282 | 278 | func (b *Bar) EwmaIncrInt64(n int64, iterDur time.Duration) { |
| 283 | var wg sync.WaitGroup | |
| 284 | iter := make(chan decor.EwmaDecorator) | |
| 285 | select { | |
| 286 | case b.operateState <- func(s *bState) { | |
| 279 | select { | |
| 280 | case b.operateState <- func(s *bState) { | |
| 281 | var wg sync.WaitGroup | |
| 287 | 282 | wg.Add(len(s.ewmaDecorators)) |
| 288 | 283 | for _, d := range s.ewmaDecorators { |
| 289 | iter <- d | |
| 290 | } | |
| 291 | close(iter) | |
| 292 | s.current += n | |
| 293 | if s.triggerComplete && s.current >= s.total { | |
| 294 | s.current = s.total | |
| 295 | s.triggerCompletion(b) | |
| 296 | } | |
| 297 | wg.Wait() | |
| 298 | }: | |
| 299 | for d := range iter { | |
| 300 | 284 | d := d |
| 301 | 285 | go func() { |
| 302 | 286 | d.EwmaUpdate(n, iterDur) |
| 303 | 287 | wg.Done() |
| 304 | 288 | }() |
| 305 | 289 | } |
| 290 | s.current += n | |
| 291 | if s.triggerComplete && s.current >= s.total { | |
| 292 | s.current = s.total | |
| 293 | s.triggerCompletion(b) | |
| 294 | } | |
| 295 | wg.Wait() | |
| 296 | }: | |
| 306 | 297 | case <-b.ctx.Done(): |
| 307 | 298 | } |
| 308 | 299 | } |
| 313 | 304 | if current < 0 { |
| 314 | 305 | return |
| 315 | 306 | } |
| 316 | type item struct { | |
| 317 | decor.EwmaDecorator | |
| 318 | n int64 | |
| 319 | } | |
| 320 | var wg sync.WaitGroup | |
| 321 | iter := make(chan item) | |
| 322 | 307 | select { |
| 323 | 308 | case b.operateState <- func(s *bState) { |
| 324 | 309 | n := current - s.current |
| 310 | var wg sync.WaitGroup | |
| 325 | 311 | wg.Add(len(s.ewmaDecorators)) |
| 326 | 312 | for _, d := range s.ewmaDecorators { |
| 327 | iter <- item{d, n} | |
| 328 | } | |
| 329 | close(iter) | |
| 313 | d := d | |
| 314 | go func() { | |
| 315 | d.EwmaUpdate(n, iterDur) | |
| 316 | wg.Done() | |
| 317 | }() | |
| 318 | } | |
| 330 | 319 | s.current = current |
| 331 | 320 | if s.triggerComplete && s.current >= s.total { |
| 332 | 321 | s.current = s.total |
| 334 | 323 | } |
| 335 | 324 | wg.Wait() |
| 336 | 325 | }: |
| 337 | for d := range iter { | |
| 338 | d := d | |
| 339 | go func() { | |
| 340 | d.EwmaUpdate(d.n, iterDur) | |
| 341 | wg.Done() | |
| 342 | }() | |
| 343 | } | |
| 344 | 326 | case <-b.ctx.Done(): |
| 345 | 327 | } |
| 346 | 328 | } |