Skip to content
Snippets Groups Projects
Verified Commit 8a072c4c authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: debouncing work wrong #6

parent ddb13564
No related branches found
No related tags found
No related merge requests found
......@@ -38,7 +38,7 @@ func (l *lighthouse) StartWatching() error {
go func() {
eventChannel := make(chan fsnotify.Event, 100)
errorChannel := make(chan error, 100)
debounceTimers := make(map[string]*time.Timer)
debounceTimers := make(map[string]map[fsnotify.Op]*time.Timer)
var debounceMutex sync.Mutex
......@@ -72,14 +72,20 @@ func (l *lighthouse) StartWatching() error {
case event := <-eventChannel:
debounceMutex.Lock()
if timer, ok := debounceTimers[event.Name]; ok {
if _, ok := debounceTimers[event.Name]; !ok {
debounceTimers[event.Name] = make(map[fsnotify.Op]*time.Timer)
}
if timer, ok := debounceTimers[event.Name][event.Op]; ok {
timer.Stop()
}
debounceTimers[event.Name] = time.AfterFunc(l.debounce, func() {
//debounceTimers[event.Name] = time.AfterFunc(l.debounce, func() {
debounceTimers[event.Name][event.Op] = time.AfterFunc(l.debounce, func() {
debounceMutex.Lock()
delete(debounceTimers, event.Name)
delete(debounceTimers[event.Name], event.Op)
if len(debounceTimers[event.Name]) == 0 {
delete(debounceTimers, event.Name)
}
debounceMutex.Unlock()
var watch *Watch
......
......@@ -97,7 +97,7 @@ func TestRecreateEvent(t *testing.T) {
t.Fatalf("Failed to add watch: %v", err)
}
l.StartWatching()
_ = l.StartWatching()
timer := time.NewTimer(20 * time.Second)
go func() {
......@@ -135,8 +135,8 @@ func TestRecreateEvent(t *testing.T) {
case <-ch:
timer.Stop()
assert.Equal(t, 0, callCounterCreate)
assert.Equal(t, 2, callCounterChange)
assert.Equal(t, 1, callCounterCreate)
assert.Equal(t, 1, callCounterChange)
case <-timeout:
t.Log("Timeout 2")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment