Remove unnecessary delay on non/defered interactive mode

This commit is contained in:
Junegunn Choi 2015-01-04 05:00:28 +09:00
parent 0a6cb62169
commit 0dd024a09f
3 changed files with 77 additions and 3 deletions

View File

@ -77,6 +77,7 @@ func Run(options *Options) {
pattern := patternBuilder([]rune(patternString))
looping := true
eventBox.Unwatch(EVT_READ_NEW)
for looping {
eventBox.Wait(func(events *Events) {
for evt, _ := range *events {
@ -87,7 +88,6 @@ func Run(options *Options) {
}
}
})
time.Sleep(COORDINATOR_DELAY)
}
matches, cancelled := matcher.scan(MatchRequest{
@ -116,6 +116,7 @@ func Run(options *Options) {
// Event coordination
reading := true
ticks := 0
eventBox.Watch(EVT_READ_NEW)
for {
delay := true
ticks += 1

View File

@ -9,10 +9,14 @@ type Events map[EventType]interface{}
type EventBox struct {
events Events
cond *sync.Cond
ignore map[EventType]bool
}
func NewEventBox() *EventBox {
return &EventBox{make(Events), sync.NewCond(&sync.Mutex{})}
return &EventBox{
events: make(Events),
cond: sync.NewCond(&sync.Mutex{}),
ignore: make(map[EventType]bool)}
}
func (b *EventBox) Wait(callback func(*Events)) {
@ -30,8 +34,10 @@ 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()
}
}
// Unsynchronized; should be called within Wait routine
func (events *Events) Clear() {
@ -46,3 +52,19 @@ func (b *EventBox) Peak(event EventType) bool {
_, ok := b.events[event]
return ok
}
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
}
}

51
src/eventbox_test.go Normal file
View File

@ -0,0 +1,51 @@
package fzf
import "testing"
func TestEventBox(t *testing.T) {
eb := NewEventBox()
// Wait should return immediately
ch := make(chan bool)
go func() {
eb.Set(EVT_READ_NEW, 10)
ch <- true
<-ch
eb.Set(EVT_SEARCH_NEW, 10)
eb.Set(EVT_SEARCH_NEW, 15)
eb.Set(EVT_SEARCH_NEW, 20)
eb.Set(EVT_SEARCH_PROGRESS, 30)
ch <- true
<-ch
eb.Set(EVT_SEARCH_FIN, 40)
ch <- true
<-ch
}()
count := 0
sum := 0
looping := true
for looping {
<-ch
eb.Wait(func(events *Events) {
for _, value := range *events {
switch val := value.(type) {
case int:
sum += val
looping = sum < 100
}
}
events.Clear()
})
ch <- true
count += 1
}
if count != 3 {
t.Error("Invalid number of events", count)
}
if sum != 100 {
t.Error("Invalid sum", sum)
}
}