fswatch

package
v0.6.21 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 14 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

Rendered for darwin/amd64

Overview

Package fswatch wraps macOS FSEvents without cgo, adding event coalescing, debouncing, typed event flags, and gitignore-style path filtering.

It is intended for workspace triggers such as live RAG re-indexing, coding-agent file watchers, and Metal shader hot-reload.

Watch starts an FSEvents stream on a dedicated Grand Central Dispatch serial queue and delivers coalesced batches of Event values on Watcher.Events. Call Watcher.Close or cancel the context passed to Watch to stop the stream and release underlying system resources.

Basic usage:

w, err := fswatch.Watch(ctx, []string{"."}, fswatch.Options{
	Latency:      50 * time.Millisecond,
	Debounce:     100 * time.Millisecond,
	FileEvents:   true,
	IgnoreDotGit: true,
	Ignore:       []string{"build/", "*.tmp"},
})
if err != nil {
	log.Fatal(err)
}
defer w.Close()

for batch := range w.Events() {
	for _, ev := range batch {
		fmt.Printf("%s %s\n", ev.Flags, ev.Path)
	}
}
Example
package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/tmc/apple/x/fswatch"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	w, err := fswatch.Watch(ctx, []string{"."}, fswatch.Options{
		Latency:      50 * time.Millisecond,
		Debounce:     100 * time.Millisecond,
		FileEvents:   true,
		IgnoreDotGit: true,
		Ignore:       []string{"build/", "*.tmp"},
	})
	if err != nil {
		log.Fatal(err)
	}
	defer w.Close()

	for batch := range w.Events() {
		for _, ev := range batch {
			fmt.Printf("%s %s\n", ev.Flags, ev.Path)
		}
	}
}

Index

Examples

Constants

View Source
const DefaultLatency = 50 * time.Millisecond

DefaultLatency is the FSEvents kernel coalescing latency used when Options.Latency is zero.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	Path    string
	Flags   Flags
	EventID uint64
	Time    time.Time
}

Event describes a single filesystem change delivered by FSEvents.

type Flags

type Flags uint32

Flags is a bitmask of typed FSEvents item and stream flags.

const (
	Created Flags = 1 << iota
	Modified
	Removed
	Renamed
	IsFile
	IsDir
	MustScanSubDirs
)

func (Flags) Has

func (f Flags) Has(other Flags) bool

Has reports whether all bits in other are set in f.

func (Flags) String

func (f Flags) String() string

String returns a human-readable pipe-separated representation of f.

Example
package main

import (
	"fmt"

	"github.com/tmc/apple/x/fswatch"
)

func main() {
	flags := fswatch.Created | fswatch.Modified | fswatch.IsFile
	fmt.Println(flags)
	fmt.Println(flags.Has(fswatch.Modified))
}
Output:
Created|Modified|IsFile
true

type Options

type Options struct {
	// Latency is the FSEvents kernel coalescing window.
	// Zero uses [DefaultLatency].
	Latency time.Duration

	// Debounce is an optional user-space coalescing window for rapid edits
	// to the same path. Zero delivers each coalesced FSEvents batch
	// immediately.
	Debounce time.Duration

	// FileEvents requests per-file events instead of directory-level events.
	FileEvents bool

	// Ignore is a list of gitignore-style patterns to exclude from delivered
	// events (for example, "node_modules/", "build/", "*.tmp", "!keep.tmp").
	Ignore []string

	// IgnoreDotGit excludes .git directories and all files inside them.
	IgnoreDotGit bool
}

Options configures a Watcher.

type Watcher

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

Watcher delivers coalesced filesystem events from a Darwin FSEventStream. A zero Watcher is not usable; create one with Watch.

func Watch

func Watch(ctx context.Context, paths []string, opts Options) (*Watcher, error)

Watch starts watching paths with macOS FSEvents and returns a Watcher. Canceling ctx stops the watcher and closes Watcher.Events.

func (*Watcher) Close

func (w *Watcher) Close() error

Close stops the watcher, releases its FSEvents stream, and closes Watcher.Events. Close is safe to call multiple times and concurrently.

func (*Watcher) Events

func (w *Watcher) Events() <-chan []Event

Events returns the channel on which coalesced event batches are delivered. The channel is closed after the watcher stops.

Jump to

Keyboard shortcuts

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