/
resolve.go
139 lines (125 loc) · 3.22 KB
/
resolve.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
package event
import (
"runtime"
)
// ResolvePending is a contant loop that tracks slices of bind or unbind calls
// and resolves them individually such that they don't break the bus
// Todo: this should be a function on the event bus itself, and should have a better name
// If you ask "Why does this not use select over channels, share memory by communicating",
// the answer is we tried, and it was cripplingly slow.
func (eb *Bus) ResolvePending() {
eb.init.Do(func() {
schedCt := 0
for {
eb.Flush()
// This is a tight loop that can cause a pseudo-deadlock
// by refusing to release control to the go scheduler.
// This code prevents this from happening.
// See https://github.com/golang/go/issues/10958
schedCt++
if schedCt > 1000 {
schedCt = 0
runtime.Gosched()
}
}
})
}
func (eb *Bus) resolveUnbindAllAndRebinds() {
eb.mutex.Lock()
eb.pendingMutex.Lock()
for _, ubaarb := range eb.unbindAllAndRebinds {
unbind := ubaarb.ub
orderedBindables := ubaarb.bnds
orderedBindOptions := ubaarb.bs
var namekeys []string
// If we were given a name,
// we'll just iterate with that name.
if unbind.Name != "" {
namekeys = append(namekeys, unbind.Name)
// Otherwise, iterate through all events.
} else {
for k := range eb.bindingMap {
namekeys = append(namekeys, k)
}
}
if unbind.CallerID != 0 {
for _, k := range namekeys {
delete(eb.bindingMap[k], unbind.CallerID)
}
} else {
for _, k := range namekeys {
delete(eb.bindingMap, k)
}
}
// Bindings
for i := 0; i < len(orderedBindables); i++ {
fn := orderedBindables[i]
opt := orderedBindOptions[i]
list := eb.getBindableList(opt)
list.storeBindable(fn)
}
}
eb.unbindAllAndRebinds = []UnbindAllOption{}
eb.pendingMutex.Unlock()
eb.mutex.Unlock()
}
func (eb *Bus) resolveUnbinds() {
eb.mutex.Lock()
eb.pendingMutex.Lock()
for _, bnd := range eb.unbinds {
eb.getBindableList(bnd.BindingOption).removeBinding(bnd)
}
eb.unbinds = []binding{}
eb.pendingMutex.Unlock()
eb.mutex.Unlock()
}
func (eb *Bus) resolveFullUnbinds() {
eb.mutex.Lock()
eb.pendingMutex.Lock()
for _, opt := range eb.fullUnbinds {
eb.getBindableList(opt.BindingOption).removeBindable(opt.Fn)
}
eb.fullUnbinds = []UnbindOption{}
eb.pendingMutex.Unlock()
eb.mutex.Unlock()
}
func (eb *Bus) resolvePartialUnbinds() {
eb.mutex.Lock()
eb.pendingMutex.Lock()
for _, opt := range eb.partUnbinds {
var namekeys []string
// If we were given a name,
// we'll just iterate with that name.
if opt.Name != "" {
namekeys = append(namekeys, opt.Name)
// Otherwise, iterate through all events.
} else {
for k := range eb.bindingMap {
namekeys = append(namekeys, k)
}
}
if opt.CallerID != 0 {
for _, k := range namekeys {
delete(eb.bindingMap[k], opt.CallerID)
}
} else {
for _, k := range namekeys {
delete(eb.bindingMap, k)
}
}
}
eb.partUnbinds = []BindingOption{}
eb.pendingMutex.Unlock()
eb.mutex.Unlock()
}
func (eb *Bus) resolveBindings() {
eb.mutex.Lock()
eb.pendingMutex.Lock()
for _, bindSet := range eb.binds {
list := eb.getBindableList(bindSet.BindingOption)
list.storeBindable(bindSet.Fn)
}
eb.binds = []UnbindOption{}
eb.pendingMutex.Unlock()
eb.mutex.Unlock()
}