forked from sadlil/go-trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.go
More file actions
87 lines (74 loc) · 1.81 KB
/
core.go
File metadata and controls
87 lines (74 loc) · 1.81 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package trigger
import (
"reflect"
"errors"
"runtime"
)
var functionMap map[string][]interface{}
func init() {
functionMap = make(map[string][]interface{})
runtime.GOMAXPROCS(runtime.NumCPU())
}
func add(event string, task interface{}) {
functionMap[event] = append(functionMap[event], task);
}
func invoke(event string, params ...interface{}) ([][]reflect.Value, error) {
result := make([][]reflect.Value, 0)
for _, regif := range(functionMap[event]) {
f := reflect.ValueOf(regif)
if len(params) != f.Type().NumIn() {
return nil, errors.New("Parameter Mismatched")
}
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
res := f.Call(in)
result = append(result, res)
}
return result, nil
}
func invokeParallel(event string, params ...interface{}) ([]chan []reflect.Value, error) {
result := make([]chan []reflect.Value, 0)
for _, regif := range(functionMap[event]) {
f := reflect.ValueOf(regif)
if len(params) != f.Type().NumIn() {
return nil, errors.New("Parameter Mismatched")
}
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
results := make(chan []reflect.Value)
result = append(result, results)
go func() {
results <- f.Call(in)
}()
}
return result, nil
}
func clear(event string) error {
if _, ok := functionMap[event]; !ok {
return errors.New("Event Not Defined")
}
delete(functionMap, event)
return nil
}
func deleteAll() error {
functionMap = make(map[string][]interface{})
return nil
}
func eventList() []string {
events := make([]string, 0)
for k := range functionMap {
events = append(events, k)
}
return events
}
func eventCount() int {
return len(functionMap)
}
func hasEvent(event string) bool {
_, ok := functionMap[event]
return ok
}