-
Notifications
You must be signed in to change notification settings - Fork 59
/
event.go
210 lines (177 loc) · 4.2 KB
/
event.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Package event is lightweight event manager and dispatcher implements by Go.
package event
// wildcard event name
const (
Wildcard = "*"
AnyNode = "*"
AllNode = "**"
)
const (
// ModeSimple old mode, simple match group listener.
//
// - "*" only allow one and must at end
//
// Support: "user.*" -> match "user.created" "user.updated"
ModeSimple uint8 = iota
// ModePath path mode.
//
// - "*" matches any sequence of non . characters (like at path.Match())
// - "**" match all characters to end, only allow at start or end on pattern.
//
// Support like this:
// "eve.some.*.*" -> match "eve.some.thing.run" "eve.some.thing.do"
// "eve.some.*.run" -> match "eve.some.thing.run", but not match "eve.some.thing.do"
// "eve.some.**" -> match any start with "eve.some.". eg: "eve.some.thing.run" "eve.some.thing.do"
// "**.thing.run" -> match any ends with ".thing.run". eg: "eve.some.thing.run"
ModePath
)
// M is short name for map[string]...
type M = map[string]any
// ManagerFace event manager interface
type ManagerFace interface {
// AddEvent events: add event
AddEvent(Event)
// On listeners: add listeners
On(name string, listener Listener, priority ...int)
// Fire event
Fire(name string, params M) (error, Event)
}
// Options event manager config options
type Options struct {
// EnableLock enable lock on fire event. default is False.
EnableLock bool
// ChannelSize for fire events by goroutine
ChannelSize int
ConsumerNum int
// MatchMode event name match mode. default is ModeSimple
MatchMode uint8
}
// OptionFn event manager config option func
type OptionFn func(o *Options)
// UsePathMode set event name match mode to ModePath
func UsePathMode(o *Options) {
o.MatchMode = ModePath
}
// EnableLock enable lock on fire event.
func EnableLock(enable bool) OptionFn {
return func(o *Options) {
o.EnableLock = enable
}
}
// Event interface
type Event interface {
Name() string
Get(key string) any
Set(key string, val any)
Add(key string, val any)
Data() map[string]any
SetData(M) Event
Abort(bool)
IsAborted() bool
}
// Cloneable interface. event can be cloned.
type Cloneable interface {
Event
Clone() Event
}
// FactoryFunc for create event instance.
type FactoryFunc func() Event
// BasicEvent a built-in implements Event interface
type BasicEvent struct {
// event name
name string
// user data.
data map[string]any
// target
target any
// mark is aborted
aborted bool
}
// New create an event instance
func New(name string, data M) *BasicEvent {
return NewBasic(name, data)
}
// NewBasic new a basic event instance
func NewBasic(name string, data M) *BasicEvent {
if data == nil {
data = make(map[string]any)
}
return &BasicEvent{
name: name,
data: data,
}
}
// Abort event loop exec
func (e *BasicEvent) Abort(abort bool) {
e.aborted = abort
}
// Fill event data
func (e *BasicEvent) Fill(target any, data M) *BasicEvent {
if data != nil {
e.data = data
}
e.target = target
return e
}
// AttachTo add current event to the event manager.
func (e *BasicEvent) AttachTo(em ManagerFace) {
em.AddEvent(e)
}
// Get data by index
func (e *BasicEvent) Get(key string) any {
if v, ok := e.data[key]; ok {
return v
}
return nil
}
// Add value by key
func (e *BasicEvent) Add(key string, val any) {
if _, ok := e.data[key]; !ok {
e.Set(key, val)
}
}
// Set value by key
func (e *BasicEvent) Set(key string, val any) {
if e.data == nil {
e.data = make(map[string]any)
}
e.data[key] = val
}
// Name get event name
func (e *BasicEvent) Name() string {
return e.name
}
// Data get all data
func (e *BasicEvent) Data() map[string]any {
return e.data
}
// IsAborted check.
func (e *BasicEvent) IsAborted() bool {
return e.aborted
}
// Clone new instance
func (e *BasicEvent) Clone() Event {
var cp = *e
return &cp
}
// Target get target
func (e *BasicEvent) Target() any {
return e.target
}
// SetName set event name
func (e *BasicEvent) SetName(name string) *BasicEvent {
e.name = name
return e
}
// SetData set data to the event
func (e *BasicEvent) SetData(data M) Event {
if data != nil {
e.data = data
}
return e
}
// SetTarget set event target
func (e *BasicEvent) SetTarget(target any) *BasicEvent {
e.target = target
return e
}