-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtaskflow.go
More file actions
72 lines (61 loc) · 1.5 KB
/
taskflow.go
File metadata and controls
72 lines (61 loc) · 1.5 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package gotaskflow
import (
"io"
)
// TaskFlow represents a series of tasks
type TaskFlow struct {
graph *eGraph
frozen bool
}
// Reset resets taskflow
func (tf *TaskFlow) Reset() {
// tf.graph.reset()
tf.frozen = false
}
// NewTaskFlow returns a taskflow struct
func NewTaskFlow(name string) *TaskFlow {
return &TaskFlow{
graph: newGraph(name),
}
}
// Push pushs all task into taskflow
func (tf *TaskFlow) push(tasks ...*Task) {
if tf.frozen {
panic("Taskflow is frozen, cannot new tasks")
}
for _, task := range tasks {
tf.graph.push(task.node)
}
}
func (tf *TaskFlow) Name() string {
return tf.graph.name
}
// NewStaticTask returns a attached static task
func (tf *TaskFlow) NewTask(name string, f func()) *Task {
task := &Task{
node: builder.NewStatic(name, f),
}
tf.push(task)
return task
}
// NewSubflow returns a attached subflow task
// NOTICE: instantiate will be invoke only once to instantiate itself
func (tf *TaskFlow) NewSubflow(name string, instantiate func(sf *Subflow)) *Task {
task := &Task{
node: builder.NewSubflow(name, instantiate),
}
tf.push(task)
return task
}
// NewCondition returns a attached condition task. NOTICE: The predict func return value determines its successor.
func (tf *TaskFlow) NewCondition(name string, predict func() uint) *Task {
task := &Task{
node: builder.NewCondition(name, predict),
}
tf.push(task)
return task
}
// Dump writes graph dot data into writer
func (tf *TaskFlow) Dump(writer io.Writer) error {
return dot.Visualize(tf, writer)
}