generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fsm.go
29 lines (23 loc) 路 806 Bytes
/
fsm.go
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
package ftl
import "github.com/TBD54566975/ftl/go-runtime/ftl/reflection"
type FSMHandle struct {
transitions []FSMTransition
}
type FSMTransition struct {
from reflection.Ref
to reflection.Ref
}
// Start specifies a start state in an FSM.
func Start[In any](state Sink[In]) FSMTransition {
return FSMTransition{to: reflection.FuncRef(state)}
}
// Transition specifies a transition in an FSM.
//
// The "event" triggering the transition is the input to the "from" state.
func Transition[FromIn, ToIn any](from Sink[FromIn], to Sink[ToIn]) FSMTransition {
return FSMTransition{from: reflection.FuncRef(from), to: reflection.FuncRef(to)}
}
// FSM creates a new finite-state machine.
func FSM(name string, transitions ...FSMTransition) *FSMHandle {
return &FSMHandle{transitions: transitions}
}