Documentation
¶
Overview ¶
Package fswatch wraps fsnotify with the boilerplate every dev- reload / config-watch use case needs: recursive directory adds, single-file watches that survive editor save-by-rename, debounce coalescing of bursty editor writes, and a single onChange callback.
Typical wiring:
w, err := fswatch.New(100 * time.Millisecond)
if err != nil { log.Fatal(err) }
defer w.Close()
w.AddTree("static") // watch directory + subdirs
w.AddFile("config.yaml") // watch a specific file
w.Run(ctx, func(path string) {
// fired once per debounced burst, with the most-recent path
})
Not safe for concurrent setup: call AddTree/AddFile before Run. After Run starts, the watcher is fixed for its lifetime — adding paths after the loop is running is a future-iteration concern.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Watcher ¶
type Watcher struct {
// contains filtered or unexported fields
}
Watcher coalesces fsnotify events through a debounce window and invokes the user's callback once per burst. See package doc for usage.
func New ¶
New creates a Watcher with the given debounce window. A 100ms window collapses the 3–5 writes most editors emit per ⌘S into a single onChange call.
func (*Watcher) AddFile ¶
AddFile registers a single file to watch. fsnotify is pointed at the parent directory (not the file itself) because many editors save via rename, which invalidates a direct-file watch. Other files in that directory are filtered out by basename.
func (*Watcher) AddTree ¶
AddTree adds `root` plus every subdirectory to the watcher. fsnotify isn't recursive on macOS/Linux, so walking once at setup is the standard workaround. New subdirs created after Run starts won't be auto-watched; re-create the Watcher to pick them up.
func (*Watcher) Close ¶
Close releases the underlying fsnotify watcher. Safe to call multiple times.
func (*Watcher) Run ¶
Run blocks until ctx is done. Every event that passes the path filter starts/extends a debounce timer; when the timer fires, onChange is invoked with the most recent matching path.
Errors from fsnotify are logged via stdlib log and don't stop the loop — a single bad event shouldn't tear down the watcher.