fzf/src/util/eventbox.go

97 lines
1.9 KiB
Go
Raw Normal View History

2015-01-12 03:56:17 +00:00
package util
2015-01-01 19:49:30 +00:00
import "sync"
2015-01-12 03:56:17 +00:00
// EventType is the type for fzf events
type EventType int
2015-01-11 18:01:24 +00:00
// Events is a type that associates EventType to any data
2015-01-01 19:49:30 +00:00
type Events map[EventType]interface{}
2015-01-11 18:01:24 +00:00
// EventBox is used for coordinating events
2015-01-01 19:49:30 +00:00
type EventBox struct {
events Events
cond *sync.Cond
ignore map[EventType]bool
2015-01-01 19:49:30 +00:00
}
2015-01-11 18:01:24 +00:00
// NewEventBox returns a new EventBox
2015-01-01 19:49:30 +00:00
func NewEventBox() *EventBox {
return &EventBox{
events: make(Events),
cond: sync.NewCond(&sync.Mutex{}),
ignore: make(map[EventType]bool)}
2015-01-01 19:49:30 +00:00
}
2015-01-11 18:01:24 +00:00
// Wait blocks the goroutine until signaled
2015-01-01 19:49:30 +00:00
func (b *EventBox) Wait(callback func(*Events)) {
b.cond.L.Lock()
if len(b.events) == 0 {
b.cond.Wait()
}
callback(&b.events)
b.cond.L.Unlock()
2015-01-01 19:49:30 +00:00
}
2015-01-11 18:01:24 +00:00
// Set turns on the event type on the box
2015-01-01 19:49:30 +00:00
func (b *EventBox) Set(event EventType, value interface{}) {
b.cond.L.Lock()
b.events[event] = value
if _, found := b.ignore[event]; !found {
b.cond.Broadcast()
}
b.cond.L.Unlock()
2015-01-01 19:49:30 +00:00
}
2015-01-11 18:01:24 +00:00
// Clear clears the events
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)
}
}
2015-02-17 10:28:10 +00:00
// Peek peeks at the event box if the given event is set
func (b *EventBox) Peek(event EventType) bool {
2015-01-01 19:49:30 +00:00
b.cond.L.Lock()
_, ok := b.events[event]
b.cond.L.Unlock()
2015-01-01 19:49:30 +00:00
return ok
}
2015-01-11 18:01:24 +00:00
// Watch deletes the events from the ignore list
func (b *EventBox) Watch(events ...EventType) {
b.cond.L.Lock()
for _, event := range events {
delete(b.ignore, event)
}
b.cond.L.Unlock()
}
2015-01-11 18:01:24 +00:00
// Unwatch adds the events to the ignore list
func (b *EventBox) Unwatch(events ...EventType) {
b.cond.L.Lock()
for _, event := range events {
b.ignore[event] = true
}
b.cond.L.Unlock()
}
2015-02-13 03:25:19 +00:00
2015-03-22 07:05:54 +00:00
// WaitFor blocks the execution until the event is received
2015-02-13 03:25:19 +00:00
func (b *EventBox) WaitFor(event EventType) {
looping := true
for looping {
b.Wait(func(events *Events) {
for evt := range *events {
switch evt {
case event:
looping = false
return
}
}
})
}
}