fswatch

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func New(debounce time.Duration) (*Watcher, error)

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

func (w *Watcher) AddFile(path string) error

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

func (w *Watcher) AddTree(root string) error

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

func (w *Watcher) Close() error

Close releases the underlying fsnotify watcher. Safe to call multiple times.

func (*Watcher) Run

func (w *Watcher) Run(ctx context.Context, onChange func(path string))

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL