diff options
Diffstat (limited to 'internal/server/dev.go')
-rw-r--r-- | internal/server/dev.go | 55 |
1 files changed, 44 insertions, 11 deletions
diff --git a/internal/server/dev.go b/internal/server/dev.go index f7ebb82..ce718eb 100644 --- a/internal/server/dev.go +++ b/internal/server/dev.go @@ -5,7 +5,9 @@ import ( "io/fs" "log/slog" "os" + "path" "path/filepath" + "slices" "time" "website/internal/log" @@ -17,6 +19,29 @@ type FileWatcher struct { *fsnotify.Watcher } +var ( + ignores = []string{ + "*.templ", + "*.go", + } + checkSettleInterval = 200 * time.Millisecond +) + +func matches(name string) func(string) bool { + return func(pattern string) bool { + matched, err := path.Match(pattern, name) + if err != nil { + log.Warn("error checking watcher ignores", "error", err) + } + + return matched + } +} + +func ignored(pathname string) bool { + return slices.ContainsFunc(ignores, matches(path.Base(pathname))) +} + func NewFileWatcher() (*FileWatcher, error) { watcher, err := fsnotify.NewWatcher() if err != nil { @@ -46,23 +71,31 @@ func (watcher FileWatcher) AddRecursive(from string) error { } func (watcher FileWatcher) Start(callback func(string)) { + var timer *time.Timer for { select { case event := <-watcher.Events: - if event.Has(fsnotify.Create) || event.Has(fsnotify.Rename) { - f, err := os.Stat(event.Name) - if err != nil { - slog.Error(fmt.Sprintf("error handling %s event: %v", event.Op.String(), err)) - } else if f.IsDir() { - err = watcher.Add(event.Name) + if !ignored(event.Name) { + log.Info("watcher event", "name", event.Name, "op", event.Op.String()) + if event.Has(fsnotify.Create) || event.Has(fsnotify.Rename) { + f, err := os.Stat(event.Name) if err != nil { - slog.Error(fmt.Sprintf("error adding new folder to watcher: %v", err)) + slog.Error(fmt.Sprintf("error handling %s event: %v", event.Op.String(), err)) + } else if f.IsDir() { + err = watcher.Add(event.Name) + if err != nil { + slog.Error(fmt.Sprintf("error adding new folder to watcher: %v", err)) + } } } - } - if event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) { - callback(event.Name) - time.Sleep(500 * time.Millisecond) + if event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) { + if timer == nil { + timer = time.AfterFunc(checkSettleInterval, func() { + callback(event.Name) + }) + } + timer.Reset(checkSettleInterval) + } } case err := <-watcher.Errors: slog.Error(fmt.Sprintf("error in watcher: %v", err)) |