summaryrefslogtreecommitdiff
path: root/src/util/eventbox.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-01-12 12:56:17 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-01-12 12:56:17 +0900
commitcd847affb79ea6438c9721635724efc6f58e2215 (patch)
treed1e631e3dca8832ee4c495924789f6697c3629cf /src/util/eventbox.go
parent7a2bc2cada971c7a390d09b0afda34780ff56fb6 (diff)
downloadfzf-cd847affb79ea6438c9721635724efc6f58e2215.tar.gz
Reorganize source code
Diffstat (limited to 'src/util/eventbox.go')
-rw-r--r--src/util/eventbox.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/util/eventbox.go b/src/util/eventbox.go
new file mode 100644
index 00000000..568ad9f7
--- /dev/null
+++ b/src/util/eventbox.go
@@ -0,0 +1,80 @@
+package util
+
+import "sync"
+
+// EventType is the type for fzf events
+type EventType int
+
+// Events is a type that associates EventType to any data
+type Events map[EventType]interface{}
+
+// EventBox is used for coordinating events
+type EventBox struct {
+ events Events
+ cond *sync.Cond
+ ignore map[EventType]bool
+}
+
+// NewEventBox returns a new EventBox
+func NewEventBox() *EventBox {
+ return &EventBox{
+ events: make(Events),
+ cond: sync.NewCond(&sync.Mutex{}),
+ ignore: make(map[EventType]bool)}
+}
+
+// Wait blocks the goroutine until signaled
+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)
+}
+
+// Set turns on the event type on the box
+func (b *EventBox) Set(event EventType, value interface{}) {
+ b.cond.L.Lock()
+ defer b.cond.L.Unlock()
+ b.events[event] = value
+ if _, found := b.ignore[event]; !found {
+ b.cond.Broadcast()
+ }
+}
+
+// Clear clears the events
+// Unsynchronized; should be called within Wait routine
+func (events *Events) Clear() {
+ for event := range *events {
+ delete(*events, event)
+ }
+}
+
+// Peak peaks at the event box if the given event is set
+func (b *EventBox) Peak(event EventType) bool {
+ b.cond.L.Lock()
+ defer b.cond.L.Unlock()
+ _, ok := b.events[event]
+ return ok
+}
+
+// Watch deletes the events from the ignore list
+func (b *EventBox) Watch(events ...EventType) {
+ b.cond.L.Lock()
+ defer b.cond.L.Unlock()
+ for _, event := range events {
+ delete(b.ignore, event)
+ }
+}
+
+// Unwatch adds the events to the ignore list
+func (b *EventBox) Unwatch(events ...EventType) {
+ b.cond.L.Lock()
+ defer b.cond.L.Unlock()
+ for _, event := range events {
+ b.ignore[event] = true
+ }
+}