folderwatch

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 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.

README

go-folderwatch

Go


Watch a folder tree for changes, debounced, with holds while the caller writes into it. Package folderwatch, module github.com/Rethunk-Tech/go-folderwatch.

folderwatch turns a stream of filesystem events into one callback per burst. You describe the folders you care about as Targets, it watches exactly those, and when one goes quiet it hands that Target back through Emit — so the caller, not the library, decides what the change means. Folders that do not exist yet are waited for, a caller can take a folder out of the watch while it moves it, and an optional size-stability window keeps a file that is still being written from being announced early.

Quick start

go get github.com/Rethunk-Tech/go-folderwatch

A worked example and the contracts: HUMANS.md.

Features

  • One callback per burst. Changes inside Quiet of each other collapse into a single Emit for the folder.
  • Opt-in size stability. A Target marked Settle is announced only once its written files keep their size for Stable, so a slow writer is not mistaken for a finished one; a file renamed in is announced on the first quiet tick.
  • Targets are re-read, not captured. Targets is polled every Retarget and immediately on a Changed signal, so the watched set follows the caller's state without a restart.
  • Folders that are not there yet. A missing folder is retried on every tick and fires once when it appears.
  • Holds. Holds.Hold closes the watches at and below a root and returns only when they are closed, so a caller can move or delete a folder that a watch would otherwise pin open.
  • Every change, not just the settled ones. Touched sees each path as it happens, for callers that cache a directory listing and need to drop it promptly.

Documentation

Doc Audience
HUMANS.md Running and using the package
AGENTS.md Internals, invariants, and why
CONTRIBUTING.md Process — commits, tests, docs
SECURITY.md Vulnerability disclosure
CHANGELOG.md Release notes

License

MIT — see LICENSE. © 2026 Rethunk.Tech, LLC.

Documentation

Overview

Package folderwatch watches folders for changes and calls back once per burst, with holds that take folders out of the watch while the caller writes into them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, d Deps) error

Run watches until ctx ends. fsnotify watches each folder's top level only; a folder that is missing is retried at every retarget tick and fires once when it appears.

Types

type Deps

type Deps struct {
	// Targets is read at start, on every Retarget tick and on every Changed signal; the folders it returns are
	// exactly the ones watched. A target with an empty Dir is skipped.
	Targets func() []Target
	// Emit is called once per settled folder, from the watcher's own goroutine; it must not block.
	Emit func(Target)
	// Quiet is how long a folder stays unchanged before it settles. Zero or less means 500ms.
	Quiet time.Duration
	// Stable is how long a file written in place must keep its size before a settling target is announced. Zero
	// or less means 1s.
	Stable time.Duration
	// Retarget is how often Targets is re-read, which also retries folders that did not exist. Zero or less means
	// 2s.
	Retarget time.Duration
	// Changed re-reads Targets at once, so a folder is watched from the moment it becomes a target and a file that
	// lands right after is not missed.
	Changed <-chan struct{}
	// Holds takes folders out of the watch while a caller moves them; nil holds none.
	Holds *Holds
	// Touched is called at once with the path of every change the watch sees, so a caller that caches what Targets
	// found can drop it; nil ignores them.
	Touched func(path string)
}

Deps wires the watcher to the caller's targets and callback.

type Holds

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

Holds takes folders out of the watch for a while. Windows refuses to move a folder while a watch has a handle open on it or on a folder below it, so a caller about to move one to the trash holds it first.

func NewHolds

func NewHolds() *Holds

NewHolds makes the holds a Run takes in its Deps.

func (*Holds) Hold

func (h *Holds) Hold(ctx context.Context, root string) (release func())

Hold stops watching root and every folder below it, and returns once their watches are closed (or ctx ends); they stay unwatched until release is called.

type Target

type Target struct {
	// Key names this folder in the caller's vocabulary; the watcher never interprets it.
	Key string
	// Dir is the folder to watch. Only its top level is watched, so a change deeper in the tree surfaces as a
	// change to the folder holding it.
	Dir string
	// Report is the folder the change is reported under when it is not Dir, such as the root of a project whose
	// subfolder changed. The watcher passes it back untouched; empty, the caller reports under Dir.
	Report string
	// Data is the caller's payload, passed back untouched.
	Data any
	// Settle announces a file written in place only once its size holds for Deps.Stable, so a writer that pauses
	// mid-file is not mistaken for one that finished. A file renamed into the folder is announced on the first
	// quiet tick either way.
	Settle bool
}

Target is one folder to watch. The watcher hands the Target back unchanged when the folder settles, so Key names the change and Data is whatever the caller needs to make sense of it.

Jump to

Keyboard shortcuts

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