machine/model.go

32 lines
598 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.
package machine
type Modeler interface {
2024-04-04 20:06:45 +00:00
Trigger(transition Transitioner) error
2024-04-04 17:33:22 +00:00
ChangeState(target State) State
InspectState() State
}
type Model struct {
state State
}
func NewModel(initial State) Modeler {
return &Model{ state: initial }
}
2024-04-04 20:06:45 +00:00
func (m *Model) Trigger(transition Transitioner) error {
return transition.Run(m)
2024-04-04 17:33:22 +00:00
}
func (m *Model) ChangeState(target State) State {
oldState := m.state
m.state = target
return oldState
}
func (m *Model) InspectState() State {
return m.state
}