-
Notifications
You must be signed in to change notification settings - Fork 84
/
unbind.go
74 lines (66 loc) · 1.78 KB
/
unbind.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
package event
// Unbind on a binding is a rewriting of bus.Unbind(b)
func (b binding) unbind() {
thisBus.unbind(b)
}
func (eb *Bus) unbind(b binding) {
pendingMutex.Lock()
unbinds = append(unbinds, b)
pendingMutex.Unlock()
}
// UnbindAll removes all events with the given cid from the event bus
func (cid *CID) UnbindAll() {
bo := BindingOption{
Event{
"",
int(*cid),
},
0,
}
UnbindAll(bo)
}
// UnbindAll removes all events that match the given bindingOption from the
// event bus
func UnbindAll(opt BindingOption) {
pendingMutex.Lock()
partUnbinds = append(partUnbinds, opt)
pendingMutex.Unlock()
}
// UnbindAllAndRebind on a CID is equivalent to bus.UnbindAllAndRebind(..., cid)
func (cid *CID) UnbindAllAndRebind(binds []Bindable, events []string) {
bo := BindingOption{
Event{
"",
int(*cid),
},
0,
}
UnbindAllAndRebind(bo, binds, int(*cid), events)
}
// UnbindAllAndRebind is a way to reset the bindings on a CID efficiently,
// given a new set of equal length binding and event slices. This is equivalent
// to callign UnbindAll and then looping over Bind calls for the pairs of
// bindables and event names, but uses less mutex time.
func UnbindAllAndRebind(bo BindingOption, binds []Bindable, cid int, events []string) {
opts := make([]BindingOption, len(events))
for k, v := range events {
opts[k].Event = Event{
Name: v,
CallerID: cid,
}
}
pendingMutex.Lock()
unbindAllAndRebinds = append(unbindAllAndRebinds, UnbindAllOption{
ub: bo,
bs: opts,
bnds: binds,
})
pendingMutex.Unlock()
}
// UnbindBindable is a manual way to unbind a function Bindable. Use of
// this with closures will cause unexpected behavior.
func UnbindBindable(opt UnbindOption) {
pendingMutex.Lock()
fullUnbinds = append(fullUnbinds, opt)
pendingMutex.Unlock()
}