-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventago.go
117 lines (104 loc) · 2.71 KB
/
eventago.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
111
112
113
114
115
116
117
package eventago
import (
"reflect"
"strings"
"fmt"
)
//Eventa interface
type Eventa interface {
On(string, interface{})
Fire(string, ...interface{}) [][]interface{}
GoFire(string, ...interface{})
HaveListener(string) bool
CountListeners(string) int
}
type tEventa struct {
handlers map[string][]interface{}
}
var container *tEventa
func init() {
if container == nil {
container = new(tEventa)
container.handlers = make(map[string][]interface{})
}
}
/*
On gives a Handler for specific Event and adds it to listening bus.
Set multiple handlers for one event is supported.
*/
func On(event string, handler interface{}) {
event = strings.ToLower(event)
if reflect.ValueOf(handler).Kind() != reflect.Func {
panic("handler is not func!")
}
if _, ok := container.handlers[event]; ok {
container.handlers[event] = append(container.handlers[event], handler)
} else {
container.handlers[event] = []interface{}{handler}
}
fmt.Println("EVENTA: A handler added for:", event)
}
/*
Fire will run all of given event's handlers with parameters concurrent and collect results of them and return as an [][]interface{}
*/
func Fire(event string, params ...interface{}) (res [][]interface{}) {
event = strings.ToLower(event)
if handlers, ok := container.handlers[event]; ok {
var resultsChan = make(chan []interface{}, len(container.handlers))
var rParams = toR(params)
for _, handler := range handlers {
go runner(resultsChan, handler, rParams...)
}
for i := 0; i < len(container.handlers[event]); i++ {
select {
case result := <-resultsChan:
res = append(res, result)
if len(res) >= len(container.handlers[event]) {
break
}
}
}
}
return
}
/*
GoFire will fire events with full concurrency, But with this method can't receive handler's result!
*/
func GoFire(event string, params ...interface{}) {
event = strings.ToLower(event)
go Fire(event, params...)
}
func runner(result chan <- []interface{}, handler interface{}, rParams ...reflect.Value) {
var res []interface{}
var rRes = reflect.ValueOf(handler).Call(rParams)
for _, value := range rRes {
res = append(res, value.Interface())
}
result <- res
}
/*
HaveListener checks if given event have any listener
*/
func HaveListener(event string) bool {
event = strings.ToLower(event)
if handlers, ok := container.handlers[event]; ok {
return len(handlers) > 0
}
return false
}
/*
CountListeners returns count of handlers are listening for given event
*/
func CountListeners(event string) int {
event = strings.ToLower(event)
if handlers, ok := container.handlers[event]; ok {
return len(handlers)
}
return 0
}
func toR(i []interface{}) (r []reflect.Value) {
for _, item := range i {
r = append(r, reflect.ValueOf(item))
}
return
}