| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 //go:generate stringer -type=State | 5 //go:generate stringer -type=State |
| 6 | 6 |
| 7 package attempt | 7 package attempt |
| 8 | 8 |
| 9 import ( | 9 import ( |
| 10 "fmt" | 10 "fmt" |
| 11 ) | 11 ) |
| 12 | 12 |
| 13 // State is the enumeration of possible states for an Attempt. | 13 // State is the enumeration of possible states for an Attempt. |
| 14 type State int8 | 14 type State int8 |
| 15 | 15 |
| 16 // These are the only valid enumerated values for State. | 16 // These are the only valid enumerated values for State. |
| 17 const ( | 17 const ( |
| 18 UnknownState State = iota | 18 UnknownState State = iota |
| 19 NeedsExecution | 19 NeedsExecution |
| 20 Executing | 20 Executing |
| 21 AddingDeps | 21 AddingDeps |
| 22 Blocked | 22 Blocked |
| 23 AwaitingExecutionState |
| 23 Finished | 24 Finished |
| 24 ) | 25 ) |
| 25 | 26 |
| 26 // validStateEvolution defines all valid {From -> []To} state transitions. The | 27 // validStateEvolution defines all valid {From -> []To} state transitions. The |
| 27 // identity transition (X -> X) is implied, as long as X has an entry in this | 28 // identity transition (X -> X) is implied, as long as X has an entry in this |
| 28 // mapping. | 29 // mapping. |
| 29 var validStateEvolution = map[State][]State{ | 30 var validStateEvolution = map[State][]State{ |
| 30 » AddingDeps: {Blocked, NeedsExecution}, | 31 » AddingDeps: {Blocked, NeedsExecution}, |
| 31 » Blocked: {NeedsExecution}, | 32 » Blocked: {AwaitingExecutionState, NeedsExecution}, |
| 32 » Executing: {AddingDeps, Finished}, | 33 » AwaitingExecutionState: {NeedsExecution}, |
| 33 » Finished: {}, | 34 » Executing: {AddingDeps, Finished}, |
| 34 » NeedsExecution: {Executing}, | 35 » Finished: {}, |
| 36 » NeedsExecution: {Executing}, |
| 35 } | 37 } |
| 36 | 38 |
| 37 // Evolve attempts to evolve the state of this Attempt. If the state | 39 // Evolve attempts to evolve the state of this Attempt. If the state |
| 38 // evolution is not allowed (e.g. invalid state transition), this returns an | 40 // evolution is not allowed (e.g. invalid state transition), this returns an |
| 39 // error. | 41 // error. |
| 40 func (s *State) Evolve(newState State) error { | 42 func (s *State) Evolve(newState State) error { |
| 41 nextStates := validStateEvolution[*s] | 43 nextStates := validStateEvolution[*s] |
| 42 if nextStates == nil { | 44 if nextStates == nil { |
| 43 return fmt.Errorf("invalid state transition: no transitions defi
ned for %s", *s) | 45 return fmt.Errorf("invalid state transition: no transitions defi
ned for %s", *s) |
| 44 } | 46 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 57 return fmt.Errorf("invalid state transition %v -> %v", *s, newState) | 59 return fmt.Errorf("invalid state transition %v -> %v", *s, newState) |
| 58 } | 60 } |
| 59 | 61 |
| 60 // MustEvolve is a panic'ing version of Evolve. | 62 // MustEvolve is a panic'ing version of Evolve. |
| 61 func (s *State) MustEvolve(newState State) { | 63 func (s *State) MustEvolve(newState State) { |
| 62 err := s.Evolve(newState) | 64 err := s.Evolve(newState) |
| 63 if err != nil { | 65 if err != nil { |
| 64 panic(err) | 66 panic(err) |
| 65 } | 67 } |
| 66 } | 68 } |
| OLD | NEW |