Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: debounce generate watch events #555

Merged
merged 5 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions cmd/templ/generatecmd/watcher/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import (
"path"
"path/filepath"
"strings"
"sync"
"time"

"github.com/fsnotify/fsnotify"
)

func Recursive(ctx context.Context, path string, out chan fsnotify.Event, errors chan error) (w *RecursiveWatcher, err error) {
func Recursive(
ctx context.Context,
path string,
out chan fsnotify.Event,
errors chan error,
) (w *RecursiveWatcher, err error) {
fsnw, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
Expand All @@ -20,6 +27,7 @@ func Recursive(ctx context.Context, path string, out chan fsnotify.Event, errors
w: fsnw,
Events: out,
Errors: errors,
timers: make(map[timerKey]*time.Timer),
}
go w.loop()
return w, w.Add(path)
Expand Down Expand Up @@ -60,10 +68,24 @@ func shouldIncludeFile(name string) bool {
}

type RecursiveWatcher struct {
ctx context.Context
w *fsnotify.Watcher
Events chan fsnotify.Event
Errors chan error
ctx context.Context
w *fsnotify.Watcher
Events chan fsnotify.Event
Errors chan error
timerMu sync.Mutex
joerdav marked this conversation as resolved.
Show resolved Hide resolved
timers map[timerKey]*time.Timer
}

type timerKey struct {
joerdav marked this conversation as resolved.
Show resolved Hide resolved
name string
op fsnotify.Op
}

func timerKeyFromEvent(event fsnotify.Event) timerKey {
return timerKey{
name: event.Name,
op: event.Op,
}
}

func (w *RecursiveWatcher) Close() error {
Expand All @@ -88,7 +110,20 @@ func (w *RecursiveWatcher) loop() {
if !shouldIncludeFile(event.Name) {
continue
}
w.Events <- event
tk := timerKeyFromEvent(event)
w.timerMu.Lock()
t, ok := w.timers[tk]
w.timerMu.Unlock()
if !ok {
t = time.AfterFunc(100*time.Millisecond, func() {
w.Events <- event
})
w.timerMu.Lock()
w.timers[tk] = t
w.timerMu.Unlock()
continue
}
t.Reset(100 * time.Millisecond)
case err, ok := <-w.w.Errors:
if !ok {
return
Expand Down
125 changes: 125 additions & 0 deletions cmd/templ/generatecmd/watcher/watch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package watcher

import (
"context"
"testing"
"time"

"github.com/fsnotify/fsnotify"
)

func TestWatchDebouncesDuplicates(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
rw := &RecursiveWatcher{
ctx: ctx,
w: &fsnotify.Watcher{
Events: make(chan fsnotify.Event),
},
Events: make(chan fsnotify.Event, 2),
timers: make(map[timerKey]*time.Timer),
}
go func() {
rw.w.Events <- fsnotify.Event{Name: "test.templ"}
rw.w.Events <- fsnotify.Event{Name: "test.templ"}
cancel()
close(rw.w.Events)
}()
rw.loop()
count := 0
exp := time.After(300 * time.Millisecond)
for {
select {
case <-rw.Events:
count++
case <-exp:
if count != 1 {
t.Errorf("expected 1 event, got %d", count)
}
return
}
}
}

func TestWatchDoesNotDebounceDifferentEvents(t *testing.T) {
tests := []struct {
event1 fsnotify.Event
event2 fsnotify.Event
}{
// Different files
{fsnotify.Event{Name: "test.templ"}, fsnotify.Event{Name: "test2.templ"}},
// Different operations
{
fsnotify.Event{Name: "test.templ", Op: fsnotify.Create},
fsnotify.Event{Name: "test.templ", Op: fsnotify.Write},
},
// Different operations and files
{
fsnotify.Event{Name: "test.templ", Op: fsnotify.Create},
fsnotify.Event{Name: "test2.templ", Op: fsnotify.Write},
},
}
for _, test := range tests {
ctx, cancel := context.WithCancel(context.Background())
rw := &RecursiveWatcher{
ctx: ctx,
w: &fsnotify.Watcher{
Events: make(chan fsnotify.Event),
},
Events: make(chan fsnotify.Event, 2),
timers: make(map[timerKey]*time.Timer),
}
go func() {
rw.w.Events <- test.event1
rw.w.Events <- test.event2
cancel()
close(rw.w.Events)
}()
rw.loop()
count := 0
exp := time.After(300 * time.Millisecond)
for {
select {
case <-rw.Events:
count++
case <-exp:
if count != 2 {
t.Errorf("expected 2 event, got %d", count)
}
return
}
}
}
}

func TestWatchDoesNotDebounceSeparateEvents(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
rw := &RecursiveWatcher{
ctx: ctx,
w: &fsnotify.Watcher{
Events: make(chan fsnotify.Event),
},
Events: make(chan fsnotify.Event, 2),
timers: make(map[timerKey]*time.Timer),
}
go func() {
rw.w.Events <- fsnotify.Event{Name: "test.templ"}
<-time.After(200 * time.Millisecond)
rw.w.Events <- fsnotify.Event{Name: "test.templ"}
cancel()
close(rw.w.Events)
}()
rw.loop()
count := 0
exp := time.After(500 * time.Millisecond)
for {
select {
case <-rw.Events:
count++
case <-exp:
if count != 2 {
t.Errorf("expected 2 event, got %d", count)
}
return
}
}
}
Loading