/
resolve.go
135 lines (121 loc) · 3.19 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
package event
import "time"
// ResolvePending is a constant loop that tracks slices of bind or unbind calls
// and resolves them individually such that they don't break the bus.
// Each section of the loop waits for the predetermined refreshrate prior to attempting to flush.
// Todo: this 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() {
go func() {
for {
time.Sleep(eb.refreshRate)
eb.Flush()
}
}()
})
}
// SetRefreshRate on the event bus detailing the time to wait per attempt to ResolvePending.
func (eb *Bus) SetRefreshRate(refreshRate time.Duration) {
eb.refreshRate = refreshRate
}
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()
}