Documentation
¶
Index ¶
- Constants
- Variables
- type ActionHookArgs
- type Event
- type FSM
- func (fsm *FSM) AddEvent(eventID string) error
- func (fsm *FSM) AddState(state State) error
- func (fsm *FSM) AddTransition(from State, evId string, to State, action func(interface{}, Event) error, ...) error
- func (fsm *FSM) CurrentState() State
- func (fsm *FSM) DumpGraphviz() string
- func (fsm *FSM) HasEvent(evID string) bool
- func (fsm *FSM) HasState(state State) bool
- func (fsm *FSM) ProcessEvent(ev Event) error
- type PreemptiveFSM
- type QueuedFSM
- type State
- type StringEvent
- type StringState
Constants ¶
const (
ShouldNotReEnterPanic = "the process event should not re-enter. " +
"i.e., ProcessEvent should not be invoked in action/guard"
)
Variables ¶
var (
AlreadyExists = errors.New("already exists")
)
Functions ¶
This section is empty.
Types ¶
type ActionHookArgs ¶
type Event ¶
type Event interface {
FSMEventID() string
}
Event defines the FSM event. NOTE: the `FSMEventID` in one FSM should be unique. NOTE: the Event may carry data and can be mutable for each `ProcessEvent`.
FSM support filter event by its carrying data. See `AddTransition` for details.
type FSM ¶
type FSM struct {
GlobalBeforeAction delegate.Delegate
GlobalAfterAction delegate.Delegate
// contains filtered or unexported fields
}
FSM is a finite state machine. NOTE: It is not thread-safe. It is caller's duty to add mutex/shared mutex when calling FSM concurrently.
func NewFSM ¶
NewFSM will create a new fsm with initialize state. The nullable `payload` will pass to each `action`/`guard` methods.
func (*FSM) AddTransition ¶
func (fsm *FSM) AddTransition(from State, evId string, to State, action func(interface{}, Event) error, guard func(interface{}, Event) bool) error
AddTransition will append a transition to fsm.
- The states and event should be added before.
- The `guard` will invoke when the current state is from, and the event is triggered. The action will be invoked if the `guard` returns true, otherwise, the next transition guard for the same state/event will be invoked.
- The fsm.ProcessEvent should not be invoked in action/guard
- If the action returns an error, the state will be not changed and the process event will returns that error.
func (*FSM) CurrentState ¶
func (*FSM) DumpGraphviz ¶
func (*FSM) ProcessEvent ¶
ProcessEvent will invoke the binding transition and change the current state. See `AddTransition` for more information. It may return NoTransition when there is no binding transition for this event.
type PreemptiveFSM ¶
type PreemptiveFSM struct {
*FSM
// contains filtered or unexported fields
}
PreemptiveFSM is a thread safe FSM. If there is a processing event, the `ProcessEvent` will be wait until the processing complete. If `ProcessEvent` is invoked more than once together, old events will be ignored and ProcessEvent will return error. i.e., the event is preemptive.
func NewPreemptiveFSM ¶
func NewPreemptiveFSM(initState State, payload interface{}) *PreemptiveFSM
func (*PreemptiveFSM) Close ¶
func (p *PreemptiveFSM) Close() error
func (*PreemptiveFSM) ProcessEvent ¶
func (p *PreemptiveFSM) ProcessEvent(event Event) error
type QueuedFSM ¶
type QueuedFSM struct {
*FSM
// contains filtered or unexported fields
}
func NewQueuedFSM ¶
func (*QueuedFSM) ProcessEvent ¶
type State ¶
type State interface {
FSMStateID() string
}
State defines the FSM state. NOTE: the `FSMStateID` in one FSM should be unique. NOTE: the State should be immutable. Maybe a global variable is good for state.
type StringEvent ¶
type StringEvent string
func (StringEvent) FSMEventID ¶
func (s StringEvent) FSMEventID() string
type StringState ¶
type StringState string
func (StringState) FSMStateID ¶
func (s StringState) FSMStateID() string