Added a flow control struct

This commit is contained in:
Sasha Koshka 2023-01-12 14:45:20 -05:00
parent 00a20c5e1c
commit 0f9153e496

31
flow/flow.go Normal file
View File

@ -0,0 +1,31 @@
package flow
// Flow represents any multi-stage process that relies on callbacks to advance
// to other stages. It allows for such a process to be laid flat instead of
// nested.
type Flow struct {
// Transition specifies a function to call before moving on to another
// stage, such as clearing out a container so that new elements may be
// added to it.
Transition func ()
// Stages is a map that pairs stage names with stage functions.
Stages map [string] func ()
stage string
}
// Switch transitions the flow to a different stage, running the specified
// transition callback first.
func (flow Flow) Switch (stage string) {
stageCallback := flow.Stages[stage]
if stageCallback == nil { return }
if flow.Transition != nil { flow.Transition() }
flow.stage = stage
stageCallback()
}
// Stage returns the name of the current stage the flow is on.
func (flow Flow) Stage () (name string) {
return flow.stage
}