watch

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 10 Imported by: 0 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 watch reports changes under a directory tree. It is deliberately coarse: it reports which paths may have changed, and leaves deciding what actually changed to the caller, which compares content hashes. That keeps it correct across the many ways editors and tools write files (in place, write-then-rename, delete-then-create) without per-platform special cases.

Index

Constants

This section is empty.

Variables

ErrOverflow is reported when the OS dropped events, so changes may have been missed. See Options.OnError.

Functions

This section is empty.

Types

type Batcher

type Batcher struct {
	// contains filtered or unexported fields
}

Batcher coalesces changed paths into batches. A batch is flushed once changes stop for the idle duration, or after maxWait during a sustained burst. Flushes run one at a time, in the order their batches closed, so checkpoint uploads never overlap or reorder.

A Batcher starts paused: paths added before Batcher.Start accumulate and flush once it is called.

func NewBatcher

func NewBatcher(idle, maxWait time.Duration, flush func(ctx context.Context, paths []string)) *Batcher

NewBatcher returns a paused batcher that hands each batch to flush. The callback may call Batcher.Add to requeue paths that failed to sync.

func (*Batcher) Add

func (b *Batcher) Add(paths ...string)

Add marks paths as changed. It is a no-op after Batcher.Close.

func (*Batcher) Close

func (b *Batcher) Close()

Close flushes whatever is pending, waits for any flush in progress, and stops the batcher. Paths requeued by that final flush are dropped.

func (*Batcher) Start

func (b *Batcher) Start(ctx context.Context)

Start begins flushing; ctx is passed to every flush.

type Options

type Options struct {
	// Root is the absolute directory to watch.
	Root string
	// Ignore reports whether a slash-separated path relative to Root is
	// excluded. Ignored directories are not descended into.
	Ignore func(rel string, isDir bool) bool
	// Stability is how long a path must go without events before it is
	// reported, so a file is not read while it is still being written.
	Stability time.Duration
	// OnChange receives the relative path of each file that may have been
	// created, modified, or removed, including directories that were removed
	// or renamed away. It is called from timer goroutines.
	OnChange func(rel string)
	// OnError receives watch errors. On [ErrOverflow] the watcher has already
	// started a rescan that reports every existing file; the caller should
	// also re-check files it knows of, since deletions cannot be rediscovered.
	OnError func(error)
}

Options configures a Watcher.

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

Watcher watches a directory tree recursively.

func New

func New(opts Options) (*Watcher, error)

New starts watching opts.Root and every directory beneath it that is not ignored. Existing files are not reported.

func (*Watcher) Close

func (w *Watcher) Close() error

Close stops watching. Paths still inside their stability window are reported before Close returns, so a save made just before shutdown is not lost; no OnChange calls start after that.

func (*Watcher) Rescan

func (w *Watcher) Rescan()

Rescan reports every file in the tree as changed and watches any directory that was missed.

Jump to

Keyboard shortcuts

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