-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.go
More file actions
33 lines (28 loc) · 794 Bytes
/
Copy pathinstance.go
File metadata and controls
33 lines (28 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package fsm
// An instance of a state machine.
type Instance struct {
StateMachine *StateMachine
CurrentState State
}
// Transition to the given state from the current state.
//
// If the transition is not allowed by the state machine, IllegalTransitionError is produced.
func (this *Instance) Transition(to State) error {
if !this.StateMachine.IsAllowed(this.CurrentState, to) {
return &IllegalTransitionError{
From: this.CurrentState,
To: to,
}
}
this.CurrentState = to
return nil
}
// Like Transition(State) but panic on error.
//
// This method is a convenience for cases where a failure to transition means there is a panic-worthy bug in the
// program.
func (this *Instance) MustTransition(to State) {
if err := this.Transition(to); err != nil {
panic(err)
}
}