SetTotal waits for b.done if triggering complete event
Vladimir Bauer
4 years ago
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | // SetTotal sets total to an arbitrary value. It's effective only for |
| 189 | // bar which was constructed with `total <= 0`. Setting total to negative | |
| 190 | // value is equivalent to (*Bar).SetTotal((*Bar).Current(), bool). | |
| 191 | // If triggerCompleteNow is true then total value is set to current | |
| 192 | // and complete event is triggered right away. | |
| 189 | // bar which was constructed with `total <= 0` and if | |
| 190 | // (*Bar).EnableTriggerComplete hasn't been called yet. Setting total | |
| 191 | // to negative value is equivalent to (*Bar).SetTotal((*Bar).Current(), | |
| 192 | // bool). If triggerCompleteNow is true then total value is set to | |
| 193 | // current and complete event is triggered right away. | |
| 193 | 194 | func (b *Bar) SetTotal(total int64, triggerCompleteNow bool) { |
| 194 | select { | |
| 195 | case b.operateState <- func(s *bState) { | |
| 195 | triggerComplete := make(chan bool, 1) | |
| 196 | select { | |
| 197 | case b.operateState <- func(s *bState) { | |
| 198 | triggerComplete <- s.triggerComplete | |
| 196 | 199 | if s.triggerComplete { |
| 197 | 200 | return |
| 198 | 201 | } |
| 208 | 211 | } |
| 209 | 212 | }: |
| 210 | 213 | if triggerCompleteNow { |
| 211 | <-b.done | |
| 214 | triggerComplete := <-triggerComplete | |
| 215 | if !triggerComplete { | |
| 216 | <-b.done | |
| 217 | } | |
| 212 | 218 | } |
| 213 | 219 | case <-b.done: |
| 214 | 220 | } |