-
Notifications
You must be signed in to change notification settings - Fork 7
/
goemitter.go
143 lines (124 loc) · 3.92 KB
/
goemitter.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
// An EventEmitter package for go 1.2 +
// Author: Mohammed Al Ashaal
// License: MIT License
// Version: v1.0.0
package Emitter
import (
"reflect"
"sync"
)
// Emitter - our listeners container
type Emitter struct {
listeners map[interface{}][]Listener
mutex *sync.Mutex
}
// Listener - our callback container and whether it will run once or not
type Listener struct {
callback func(...interface{})
once bool
}
// Construct() - create a new instance of Emitter
func Construct() *Emitter {
return &Emitter{
make(map[interface{}][]Listener),
&sync.Mutex{},
}
}
// Destruct() - free memory from an emitter instance
func (self *Emitter) Destruct() {
self = nil
}
// AddListener() - register a new listener on the specified event
func (self *Emitter) AddListener(event string, callback func(...interface{})) *Emitter {
return self.On(event, callback)
}
// On() - register a new listener on the specified event
func (self *Emitter) On(event string, callback func(...interface{})) *Emitter {
self.mutex.Lock()
if _, ok := self.listeners[event]; !ok {
self.listeners[event] = []Listener{}
}
self.listeners[event] = append(self.listeners[event], Listener{callback, false})
self.mutex.Unlock()
self.EmitSync("newListener", []interface{}{event, callback})
return self
}
// Once() - register a new one-time listener on the specified event
func (self *Emitter) Once(event string, callback func(...interface{})) *Emitter {
self.mutex.Lock()
if _, ok := self.listeners[event]; !ok {
self.listeners[event] = []Listener{}
}
self.listeners[event] = append(self.listeners[event], Listener{callback, true})
self.mutex.Unlock()
self.EmitSync("newListener", []interface{}{event, callback})
return self
}
// RemoveListeners() - remove the specified callback from the specified events' listeners
func (self *Emitter) RemoveListener(event string, callback func(...interface{})) *Emitter {
return self.removeListenerInternal(event, callback, false)
}
func (self *Emitter) removeListenerInternal(event string, callback func(...interface{}), suppress bool) *Emitter {
if _, ok := self.listeners[event]; !ok {
return self
}
for k, v := range self.listeners[event] {
if reflect.ValueOf(v.callback).Pointer() == reflect.ValueOf(callback).Pointer() {
self.listeners[event] = append(self.listeners[event][:k], self.listeners[event][k+1:]...)
if !suppress {
self.EmitSync("removeListener", []interface{}{event, callback})
}
return self
}
}
return self
}
// RemoveAllListeners() - remove all listeners from (all/event)
func (self *Emitter) RemoveAllListeners(event interface{}) *Emitter {
self.mutex.Lock()
defer self.mutex.Unlock()
if event == nil {
self.listeners = make(map[interface{}][]Listener)
return self
}
if _, ok := self.listeners[event]; !ok {
return self
}
delete(self.listeners, event)
return self
}
// Listeners() - return an array with the registered listeners in the specified event
func (self *Emitter) Listeners(event string) []Listener {
if _, ok := self.listeners[event]; !ok {
return nil
}
return self.listeners[event]
}
// ListenersCount() - return the count of listeners in the speicifed event
func (self *Emitter) ListenersCount(event string) int {
return len(self.Listeners(event))
}
// EmitSync() - run all listeners of the specified event in synchronous mode
func (self *Emitter) EmitSync(event string, args ...interface{}) *Emitter {
self.mutex.Lock()
defer self.mutex.Unlock()
for _, v := range self.Listeners(event) {
if v.once {
self.removeListenerInternal(event, v.callback, true)
}
v.callback(args...)
}
return self
}
// EmitAsync() - run all listeners of the specified event in asynchronous mode using goroutines
func (self *Emitter) EmitAsync(event string, args []interface{}) *Emitter {
self.mutex.Lock()
defer self.mutex.Unlock()
for _, v := range self.Listeners(event) {
if v.once {
self.removeListenerInternal(event, v.callback, true)
}
go v.callback(args...)
}
return self
}