|
|
@ -26,23 +26,23 @@ func NewEventBox() *EventBox {
|
|
|
|
// Wait blocks the goroutine until signaled
|
|
|
|
// Wait blocks the goroutine until signaled
|
|
|
|
func (b *EventBox) Wait(callback func(*Events)) {
|
|
|
|
func (b *EventBox) Wait(callback func(*Events)) {
|
|
|
|
b.cond.L.Lock()
|
|
|
|
b.cond.L.Lock()
|
|
|
|
defer b.cond.L.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(b.events) == 0 {
|
|
|
|
if len(b.events) == 0 {
|
|
|
|
b.cond.Wait()
|
|
|
|
b.cond.Wait()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
callback(&b.events)
|
|
|
|
callback(&b.events)
|
|
|
|
|
|
|
|
b.cond.L.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set turns on the event type on the box
|
|
|
|
// Set turns on the event type on the box
|
|
|
|
func (b *EventBox) Set(event EventType, value interface{}) {
|
|
|
|
func (b *EventBox) Set(event EventType, value interface{}) {
|
|
|
|
b.cond.L.Lock()
|
|
|
|
b.cond.L.Lock()
|
|
|
|
defer b.cond.L.Unlock()
|
|
|
|
|
|
|
|
b.events[event] = value
|
|
|
|
b.events[event] = value
|
|
|
|
if _, found := b.ignore[event]; !found {
|
|
|
|
if _, found := b.ignore[event]; !found {
|
|
|
|
b.cond.Broadcast()
|
|
|
|
b.cond.Broadcast()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.cond.L.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Clear clears the events
|
|
|
|
// Clear clears the events
|
|
|
@ -56,27 +56,27 @@ func (events *Events) Clear() {
|
|
|
|
// Peek peeks at the event box if the given event is set
|
|
|
|
// Peek peeks at the event box if the given event is set
|
|
|
|
func (b *EventBox) Peek(event EventType) bool {
|
|
|
|
func (b *EventBox) Peek(event EventType) bool {
|
|
|
|
b.cond.L.Lock()
|
|
|
|
b.cond.L.Lock()
|
|
|
|
defer b.cond.L.Unlock()
|
|
|
|
|
|
|
|
_, ok := b.events[event]
|
|
|
|
_, ok := b.events[event]
|
|
|
|
|
|
|
|
b.cond.L.Unlock()
|
|
|
|
return ok
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Watch deletes the events from the ignore list
|
|
|
|
// Watch deletes the events from the ignore list
|
|
|
|
func (b *EventBox) Watch(events ...EventType) {
|
|
|
|
func (b *EventBox) Watch(events ...EventType) {
|
|
|
|
b.cond.L.Lock()
|
|
|
|
b.cond.L.Lock()
|
|
|
|
defer b.cond.L.Unlock()
|
|
|
|
|
|
|
|
for _, event := range events {
|
|
|
|
for _, event := range events {
|
|
|
|
delete(b.ignore, event)
|
|
|
|
delete(b.ignore, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.cond.L.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Unwatch adds the events to the ignore list
|
|
|
|
// Unwatch adds the events to the ignore list
|
|
|
|
func (b *EventBox) Unwatch(events ...EventType) {
|
|
|
|
func (b *EventBox) Unwatch(events ...EventType) {
|
|
|
|
b.cond.L.Lock()
|
|
|
|
b.cond.L.Lock()
|
|
|
|
defer b.cond.L.Unlock()
|
|
|
|
|
|
|
|
for _, event := range events {
|
|
|
|
for _, event := range events {
|
|
|
|
b.ignore[event] = true
|
|
|
|
b.ignore[event] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.cond.L.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WaitFor blocks the execution until the event is received
|
|
|
|
// WaitFor blocks the execution until the event is received
|
|
|
|