refactoring internal types
Vladimir Bauer
7 years ago
| 36 | 36 | runningBar *Bar |
| 37 | 37 | cacheState *bState |
| 38 | 38 | operateState chan func(*bState) |
| 39 | int64Ch chan int64 | |
| 40 | boolCh chan bool | |
| 41 | 39 | bFrameCh chan *bFrame |
| 42 | 40 | syncTableCh chan [][]chan int |
| 41 | intValue chan int64 | |
| 42 | completed chan bool | |
| 43 | 43 | |
| 44 | 44 | // done is closed by Bar's goroutine, after cacheState is written |
| 45 | 45 | done chan struct{} |
| 118 | 118 | priority: s.priority, |
| 119 | 119 | runningBar: s.runningBar, |
| 120 | 120 | operateState: make(chan func(*bState)), |
| 121 | int64Ch: make(chan int64), | |
| 122 | boolCh: make(chan bool), | |
| 123 | 121 | bFrameCh: make(chan *bFrame, 1), |
| 124 | 122 | syncTableCh: make(chan [][]chan int), |
| 123 | intValue: make(chan int64), | |
| 124 | completed: make(chan bool), | |
| 125 | 125 | done: make(chan struct{}), |
| 126 | 126 | shutdown: make(chan struct{}), |
| 127 | 127 | } |
| 165 | 165 | // ID returs id of the bar. |
| 166 | 166 | func (b *Bar) ID() int { |
| 167 | 167 | select { |
| 168 | case b.operateState <- func(s *bState) { b.int64Ch <- int64(s.id) }: | |
| 169 | return int(<-b.int64Ch) | |
| 168 | case b.operateState <- func(s *bState) { b.intValue <- int64(s.id) }: | |
| 169 | return int(<-b.intValue) | |
| 170 | 170 | case <-b.done: |
| 171 | 171 | return b.cacheState.id |
| 172 | 172 | } |
| 175 | 175 | // Current returns bar's current number, in other words sum of all increments. |
| 176 | 176 | func (b *Bar) Current() int64 { |
| 177 | 177 | select { |
| 178 | case b.operateState <- func(s *bState) { b.int64Ch <- s.current }: | |
| 179 | return <-b.int64Ch | |
| 178 | case b.operateState <- func(s *bState) { b.intValue <- s.current }: | |
| 179 | return <-b.intValue | |
| 180 | 180 | case <-b.done: |
| 181 | 181 | return b.cacheState.current |
| 182 | 182 | } |
| 240 | 240 | // condition, like for !bar.Completed() {...} so when toComplete=true |
| 241 | 241 | // it is called once (at which time, the bar is still alive), then |
| 242 | 242 | // quits the loop and never suppose to be called afterwards. |
| 243 | return <-b.boolCh | |
| 243 | return <-b.completed | |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | func (b *Bar) wSyncTable() [][]chan int { |
| 259 | 259 | select { |
| 260 | 260 | case op := <-b.operateState: |
| 261 | 261 | op(s) |
| 262 | case b.boolCh <- s.toComplete: | |
| 262 | case b.completed <- s.toComplete: | |
| 263 | 263 | case <-cancel: |
| 264 | 264 | s.toComplete = true |
| 265 | 265 | cancel = nil |