/
unbind.go
50 lines (44 loc) · 1.4 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
package event
// Unbind on a binding is a rewriting of bus.Unbind(b)
func (b binding) unbind(eb *Bus) {
eb.unbind(b)
}
func (eb *Bus) unbind(b binding) {
eb.pendingMutex.Lock()
eb.unbinds = append(eb.unbinds, b)
eb.pendingMutex.Unlock()
}
// 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 calling UnbindAll and then looping over Bind calls for the pairs of
// bindables and event names, but uses less mutex time.
func (eb *Bus) UnbindAllAndRebind(bo Event, binds []Bindable, cid CID, events []string) {
opts := make([]Event, len(events))
for k, v := range events {
opts[k] = Event{
Name: v,
CallerID: cid,
}
}
eb.pendingMutex.Lock()
eb.unbindAllAndRebinds = append(eb.unbindAllAndRebinds, UnbindAllOption{
ub: bo,
bs: opts,
bnds: binds,
})
eb.pendingMutex.Unlock()
}
// UnbindAll removes all events that match the given bindingOption from the
// default event bus
func (eb *Bus) UnbindAll(opt Event) {
eb.pendingMutex.Lock()
eb.partUnbinds = append(eb.partUnbinds, opt)
eb.pendingMutex.Unlock()
}
// UnbindBindable is a manual way to unbind a function Bindable. Use of
// this with closures will result in undefined behavior.
func (eb *Bus) UnbindBindable(opt UnbindOption) {
eb.pendingMutex.Lock()
eb.fullUnbinds = append(eb.fullUnbinds, opt)
eb.pendingMutex.Unlock()
}