-
Notifications
You must be signed in to change notification settings - Fork 41
/
trigger.go
110 lines (97 loc) · 2.25 KB
/
trigger.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
100
101
102
103
104
105
106
107
108
109
110
package trigger
import (
"errors"
"reflect"
"sync"
)
func New() Trigger {
return &trigger{
functionMap: make(map[string]interface{}),
}
}
type trigger struct {
functionMap map[string]interface{}
mu sync.Mutex
}
func (t *trigger) On(event string, task interface{}) error {
t.mu.Lock()
defer t.mu.Unlock()
if _, ok := t.functionMap[event]; ok {
return errors.New("event already defined")
}
if reflect.ValueOf(task).Type().Kind() != reflect.Func {
return errors.New("task is not a function")
}
t.functionMap[event] = task
return nil
}
func (t *trigger) Fire(event string, params ...interface{}) ([]reflect.Value, error) {
f, in, err := t.read(event, params...)
if err != nil {
return nil, err
}
result := f.Call(in)
return result, nil
}
func (t *trigger) FireBackground(event string, params ...interface{}) (chan []reflect.Value, error) {
f, in, err := t.read(event, params...)
if err != nil {
return nil, err
}
results := make(chan []reflect.Value)
go func() {
results <- f.Call(in)
}()
return results, nil
}
func (t *trigger) Clear(event string) error {
t.mu.Lock()
defer t.mu.Unlock()
if _, ok := t.functionMap[event]; !ok {
return errors.New("event not defined")
}
delete(t.functionMap, event)
return nil
}
func (t *trigger) ClearEvents() {
t.mu.Lock()
defer t.mu.Unlock()
t.functionMap = make(map[string]interface{})
}
func (t *trigger) HasEvent(event string) bool {
t.mu.Lock()
defer t.mu.Unlock()
_, ok := t.functionMap[event]
return ok
}
func (t *trigger) Events() []string {
t.mu.Lock()
defer t.mu.Unlock()
events := make([]string, 0)
for k := range t.functionMap {
events = append(events, k)
}
return events
}
func (t *trigger) EventCount() int {
t.mu.Lock()
defer t.mu.Unlock()
return len(t.functionMap)
}
func (t *trigger) read(event string, params ...interface{}) (reflect.Value, []reflect.Value, error) {
t.mu.Lock()
task, ok := t.functionMap[event]
t.mu.Unlock()
if !ok {
return reflect.Value{}, nil, errors.New("no task found for event")
}
f := reflect.ValueOf(task)
if len(params) != f.Type().NumIn() {
return reflect.Value{}, nil, errors.New("parameter mismatched")
}
in := make([]reflect.Value, len(params))
for k, param := range params {
in[k] = reflect.ValueOf(param)
}
return f, in, nil
}