-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtevent.go
61 lines (53 loc) · 1.18 KB
/
tevent.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
package tevent
import "reflect"
type EventFunc interface {}
type TEvent struct {
EventList map[int][]EventFunc
}
func getArgs(args ...interface{}) []reflect.Value {
var ma = make([]reflect.Value, len(args))
for k, v := range args {
ma[k] = reflect.ValueOf(v)
}
return ma
}
//某事件发生
//eid 事件id
//args 参数
func (this *TEvent) Happen(eid int, args ...interface{}) {
fs,ok := this.EventList[eid]
if len(fs) == 0||!ok {
return
}
argvs := getArgs(args...)
for _, f := range fs {
go reflect.ValueOf(f).Call(argvs)
}
}
//添加事件处理函数
//eid 事件id
//fh 事件处理函数
func (this *TEvent) AddEventHandler(eid int, fh EventFunc) {
this.EventList[eid] = append(this.EventList[eid], fh)
}
//移除事件处理函数
//eid 事件id
//fh 事件处理函数
func (this *TEvent) ReMoveEventHandler(eid int, fh EventFunc) {
fs,ok := this.EventList[eid]
if len(fs) == 0||!ok {
return
}
for i, f := range fs {
if reflect.ValueOf(fh) == reflect.ValueOf(f) {
this.EventList[eid] = append(fs[:i], fs[i+1:]...)
break
}
}
}
//工厂函数,生成一个事件类
func NewEvent() (e *TEvent) {
e = new(TEvent)
e.EventList = make(map[int][]EventFunc)
return
}