machine/model.go

32 lines
584 B
Go
Raw Normal View History

2024-04-04 17:33:22 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
2024-04-04 20:08:50 +00:00
2024-04-04 17:33:22 +00:00
package machine
type Modeler interface {
2024-04-04 20:08:50 +00:00
Trigger(transition Transitioner) error
ChangeState(target State) State
InspectState() State
2024-04-04 17:33:22 +00:00
}
type Model struct {
2024-04-04 20:08:50 +00:00
state State
2024-04-04 17:33:22 +00:00
}
func NewModel(initial State) Modeler {
2024-04-04 20:08:50 +00:00
return &Model{state: initial}
2024-04-04 17:33:22 +00:00
}
2024-04-04 20:06:45 +00:00
func (m *Model) Trigger(transition Transitioner) error {
2024-04-04 20:08:50 +00:00
return transition.Run(m)
2024-04-04 17:33:22 +00:00
}
func (m *Model) ChangeState(target State) State {
2024-04-04 20:08:50 +00:00
oldState := m.state
m.state = target
return oldState
2024-04-04 17:33:22 +00:00
}
func (m *Model) InspectState() State {
2024-04-04 20:08:50 +00:00
return m.state
2024-04-04 17:33:22 +00:00
}