-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove.go
62 lines (48 loc) · 1.37 KB
/
remove.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
package events
import "reflect"
// RemoveOptions is used by the Remove method on a Manager
// and the package-level Remove function.
// The Name and Names fields are used together to uniquely
// identify an event.
// The Handler field contains the function that will be
// removed.
type RemoveOptions struct {
Name string
Names []string
Handler HandlerFunc
}
// Remove stops a handler function from being called
// when a named event is run.
func (m *Manager) Remove(options RemoveOptions) {
if options.Handler == nil {
return
}
initManagerMethod(&m)
bitmaskValue := m.bitmaskValueFromNames(options.Name, options.Names)
defer m.dataMutex.Unlock()
m.dataMutex.Lock()
{
p1 := reflect.ValueOf(options.Handler).Pointer()
for i, datum := range m.inner.data {
if p2 := reflect.ValueOf(datum.handler).Pointer(); p1 == p2 {
func() {
defer datum.mainMutex.Unlock()
datum.mainMutex.Lock()
datum.bitmaskValue.Uncombine(bitmaskValue)
if datum.bitmaskValue.IsEmpty() {
lastIndex := len(m.inner.data) - 1
if i != lastIndex {
m.inner.data[lastIndex], m.inner.data[i] = m.inner.data[i], m.inner.data[lastIndex]
}
m.inner.data = m.inner.data[:lastIndex]
}
}()
return
}
}
}
}
// Remove calls the Remove method on the default manager.
func Remove(options RemoveOptions) {
defaultManager.Remove(options)
}