2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
type EventType int
|
|
|
|
|
|
|
|
type Events map[EventType]interface{}
|
|
|
|
|
|
|
|
type EventBox struct {
|
|
|
|
events Events
|
|
|
|
cond *sync.Cond
|
2015-01-03 20:00:28 +00:00
|
|
|
ignore map[EventType]bool
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewEventBox() *EventBox {
|
2015-01-03 20:00:28 +00:00
|
|
|
return &EventBox{
|
|
|
|
events: make(Events),
|
|
|
|
cond: sync.NewCond(&sync.Mutex{}),
|
|
|
|
ignore: make(map[EventType]bool)}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *EventBox) Wait(callback func(*Events)) {
|
|
|
|
b.cond.L.Lock()
|
|
|
|
defer b.cond.L.Unlock()
|
|
|
|
|
|
|
|
if len(b.events) == 0 {
|
|
|
|
b.cond.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(&b.events)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *EventBox) Set(event EventType, value interface{}) {
|
|
|
|
b.cond.L.Lock()
|
|
|
|
defer b.cond.L.Unlock()
|
|
|
|
b.events[event] = value
|
2015-01-03 20:00:28 +00:00
|
|
|
if _, found := b.ignore[event]; !found {
|
|
|
|
b.cond.Broadcast()
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unsynchronized; should be called within Wait routine
|
|
|
|
func (events *Events) Clear() {
|
|
|
|
for event := range *events {
|
|
|
|
delete(*events, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *EventBox) Peak(event EventType) bool {
|
|
|
|
b.cond.L.Lock()
|
|
|
|
defer b.cond.L.Unlock()
|
|
|
|
_, ok := b.events[event]
|
|
|
|
return ok
|
|
|
|
}
|
2015-01-03 20:00:28 +00:00
|
|
|
|
|
|
|
func (b *EventBox) Watch(events ...EventType) {
|
|
|
|
b.cond.L.Lock()
|
|
|
|
defer b.cond.L.Unlock()
|
|
|
|
for _, event := range events {
|
|
|
|
delete(b.ignore, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *EventBox) Unwatch(events ...EventType) {
|
|
|
|
b.cond.L.Lock()
|
|
|
|
defer b.cond.L.Unlock()
|
|
|
|
for _, event := range events {
|
|
|
|
b.ignore[event] = true
|
|
|
|
}
|
|
|
|
}
|