Skip to content

Commit f7a9511

Browse files
committed
feat: added debounce to watcher
1 parent e09673c commit f7a9511

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

internal/watcher/watcher.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"squish/internal/utils"
99
"squish/pkg/esbuild"
1010
"syscall"
11+
"time"
1112
)
1213

1314
type Watcher struct {
@@ -29,6 +30,9 @@ func (w *Watcher) Watch() error {
2930
}
3031
defer watcher.Close()
3132

33+
var rebuildTimer *time.Timer
34+
debounceDuration := 100 * time.Millisecond
35+
3236
done := make(chan bool)
3337
go func() {
3438
for {
@@ -38,10 +42,15 @@ func (w *Watcher) Watch() error {
3842
return
3943
}
4044
if event.Op&fsnotify.Write == fsnotify.Write {
41-
utils.Log("Modified file:", event.Name)
42-
if err := w.bundler.Bundle(); err != nil {
43-
utils.Log("Error bundling:", err)
45+
if rebuildTimer != nil {
46+
rebuildTimer.Stop()
4447
}
48+
rebuildTimer = time.AfterFunc(debounceDuration, func() {
49+
utils.Log("Detected changes, rebuilding...")
50+
if err := w.bundler.Bundle(); err != nil {
51+
utils.Log("Error bundling:", err)
52+
}
53+
})
4554
}
4655
case err, ok := <-watcher.Errors:
4756
if !ok {

0 commit comments

Comments
 (0)