-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathasync.go
99 lines (76 loc) · 1.95 KB
/
async.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package async
import (
"fmt"
"reflect"
)
// Interface fot async to handle user user functions
type taskier interface {
GetFuncs() (interface{}, error)
}
// Type used as a slisce of tasks
type Tasks []interface{}
func (t Tasks) GetFuncs() (interface{}, error) {
var (
l = len(t)
fns = make([]reflect.Value, l)
)
for i := 0; i < l; i++ {
f := reflect.Indirect(reflect.ValueOf(t[i]))
if f.Kind() != reflect.Func {
return fns, fmt.Errorf("%T must be a Function ", f)
}
fns[i] = f
}
return fns, nil
}
// Type used as a map of tasks
type MapTasks map[string]interface{}
func (mt MapTasks) GetFuncs() (interface{}, error) {
var fns = map[string]reflect.Value{}
for k, v := range mt {
f := reflect.Indirect(reflect.ValueOf(v))
if f.Kind() != reflect.Func {
return fns, fmt.Errorf("%T must be a Function ", f)
}
fns[k] = f
}
return fns, nil
}
// Waterfall executes every task sequencially.
// The execution flow may be interrupted by returning an error.
// `firstArgs` is a slice of parameters to be passed to the first task of the stack.
func Waterfall(stack Tasks, firstArgs ...interface{}) ([]interface{}, error) {
var (
err error
args []reflect.Value
f = &funcs{}
)
// Checks if the Tasks passed are valid functions.
f.Stack, err = stack.GetFuncs()
if err != nil {
panic(err.Error())
}
// transform interface{} to reflect.Value for execution
for i := 0; i < len(firstArgs); i++ {
args = append(args, reflect.ValueOf(firstArgs[i]))
}
return f.ExecInSeries(args...)
}
func Parallel(stack taskier) (Results, error) {
return execConcurrentStack(stack, true)
}
func Concurrent(stack taskier) (Results, error) {
return execConcurrentStack(stack, false)
}
func execConcurrentStack(stack taskier, parallel bool) (Results, error) {
var (
err error
f = &funcs{}
)
// Checks if the Tasks passed are valid functions.
f.Stack, err = stack.GetFuncs()
if err != nil {
panic(err)
}
return f.ExecConcurrent(parallel)
}