This repository has been archived by the owner on Jun 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
148 lines (134 loc) · 3.46 KB
/
events.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package webx
import (
"sync"
)
var Events *EventsInstance = NewEvents()
func NewEvents() *EventsInstance {
return &EventsInstance{
listeners: make(map[string][]func(interface{}, func(bool))),
lock: new(sync.RWMutex),
}
}
//并发执行事件
func GoEvent(eventName string, session interface{}, next func(bool)) {
Events.GoExecute(eventName, session, next)
}
//顺序执行事件
func Event(eventName string, session interface{}, next func(bool)) {
Events.Execute(eventName, session, next)
}
//删除事件
func DelEvent(eventName string) {
Events.Delete(eventName)
}
//添加事件
func AddEvent(eventName string, handler func(interface{}, func(bool))) {
Events.Register(eventName, handler)
}
type EventsInstance struct {
listeners map[string][]func(interface{}, func(bool))
lock *sync.RWMutex
}
/*
注册事件
[Examle:]
Events.Register("AfterResponse", func(session interface{}, next func(bool)) {
log.Println("Got AfterResponse event!")
isSuccess := true
next(isSuccess) //这里的next函数无论什么情况下必须执行。
})
采用不同的方式执行事件时,此处的next函数的作用也是不同的:
1、在并发执行事件的时候,next函数的作用是通知程序我已经执行完了(不理会这一步是否执行成功);
2、在顺序执行事件的时候,next函数的作用是通知程序是否继续执行下一步,next(true)是继续执行下一步,next(false)是终止执行下一步
*/
func (e *EventsInstance) Register(eventName string, handler func(interface{}, func(bool))) {
e.lock.Lock()
defer e.lock.Unlock()
if e.listeners == nil {
e.listeners = make(map[string][]func(interface{}, func(bool)))
}
_, ok := e.listeners[eventName]
if !ok {
e.listeners[eventName] = make([]func(interface{}, func(bool)), 0)
}
e.listeners[eventName] = append(e.listeners[eventName], handler)
}
func (e *EventsInstance) Delete(eventName string) {
e.lock.Lock()
defer e.lock.Unlock()
if e.listeners == nil {
return
}
_, ok := e.listeners[eventName]
if ok {
delete(e.listeners, eventName)
}
}
/*
并发执行事件
[Examle 1:]
Events.GoExecute("AfterHandler", session, func(_ bool) {//此匿名函数在本事件的最后执行
session.Response.Send()
session.Response.Close()
})
[Examle 2:]
Events.Execute("AfterResponse", session, func(_ bool) {})
*/
func (e *EventsInstance) GoExecute(eventName string, session interface{}, next func(bool)) {
if e.listeners == nil {
next(true)
return
}
c := make(chan int)
n := 0
e.lock.RLock()
defer e.lock.RUnlock()
if l, ok := e.listeners[eventName]; ok {
if len(l) > 0 {
for _, h := range l {
n++
//h 的原型为 func(interface{}, func(bool))
go h(session, func(_ bool) {
c <- 1
})
}
}
}
for n > 0 {
i := <-c
if i == 1 {
n--
}
}
next(true)
}
/**
* 顺序执行事件
*/
func (e *EventsInstance) Execute(eventName string, session interface{}, next func(bool)) {
if e.listeners == nil {
next(true)
return
}
e.lock.RLock()
defer e.lock.RUnlock()
var nextStep bool = false
if l, ok := e.listeners[eventName]; ok {
if len(l) > 0 {
for _, h := range l {
h(session, func(ok bool) {
nextStep = ok
})
//一旦传入false,后面的全部忽略执行
if !nextStep {
break
}
}
} else {
nextStep = true
}
} else {
nextStep = true
}
next(nextStep)
}