32 lines
579 B
Go
32 lines
579 B
Go
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||
|
|
||
|
package machine
|
||
|
|
||
|
type Modeler interface {
|
||
|
Trigger(transition Transitioner)
|
||
|
ChangeState(target State) State
|
||
|
InspectState() State
|
||
|
}
|
||
|
|
||
|
type Model struct {
|
||
|
state State
|
||
|
}
|
||
|
|
||
|
func NewModel(initial State) Modeler {
|
||
|
return &Model{ state: initial }
|
||
|
}
|
||
|
|
||
|
func (m *Model) Trigger(transition Transitioner) {
|
||
|
transition.Run(m)
|
||
|
}
|
||
|
|
||
|
func (m *Model) ChangeState(target State) State {
|
||
|
oldState := m.state
|
||
|
m.state = target
|
||
|
return oldState
|
||
|
}
|
||
|
|
||
|
func (m *Model) InspectState() State {
|
||
|
return m.state
|
||
|
}
|