Added a flow control struct
This commit is contained in:
parent
00a20c5e1c
commit
0f9153e496
31
flow/flow.go
Normal file
31
flow/flow.go
Normal 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
|
||||||
|
}
|
Reference in New Issue
Block a user