Skip to main content

Module event_watch

Module event_watch 

Source
Expand description

Event-driven file watcher (issue #526).

Wraps [notify::recommended_watcher] so the dev server can react to OS filesystem events (FSEvents / inotify / ReadDirectoryChangesW) instead of polling. Compared to the legacy crate::watch::FileWatcher, this:

  • Costs ~0% idle CPU (kernel pushes events; no mtime scan loop).
  • Wakes within ~5 ms of the OS event vs. the 1-2 s polling interval.
  • Coalesces rapid saves through a 100 ms debounce window — a cargo fmt storm that touches one file four times in 200 ms produces exactly one drain (AC6).

§Architecture

notify backend ──▶ raw_tx ──▶ debounce thread ──▶ batched_tx ──▶ caller
 (OS event)        mpsc          (100 ms window)      mpsc

The debounce thread:

  1. Blocks on raw_rx.recv() until the first event arrives.
  2. Records the wall-clock instant of that first event.
  3. Drains every subsequent event with recv_timeout(remaining) until 100 ms have elapsed since the first event.
  4. Sends the de-duplicated path set to batched_tx.

Last-write-wins is implicit: only the path set is forwarded, the per-event ordering is discarded.

§Why not notify-debouncer-mini?

That crate brings a tokio dep transitively; ssg is rayon-only. The hand-rolled debouncer here is ~30 lines, deterministic, and unit-testable without touching the filesystem (see debounce_paths).

Structs§

ChangeBatch
A batched set of changed paths produced by EventWatcher.
EventWatcher
Event-driven watcher built on top of notify.

Enums§

RecvOutcome
Result of a EventWatcher::recv_timeout call.

Constants§

DEFAULT_DEBOUNCE
Default debounce window. 100 ms is the issue-#526 AC6 target — long enough to collapse a cargo fmt save storm into one rebuild, short enough to feel instant in the browser.
MAX_BATCH_PATHS
Cap on how many distinct paths can be debounced into a single batch before the watcher forces a drain. Prevents pathological build-output storms from delaying delivery beyond a reasonable bound.

Functions§

debounce_paths
Pure helper: collapse a stream of (path, instant) events into a list of debounced batches. Used by tests so the debouncer logic can be verified without spawning threads.
event_should_propagate
Returns whether a notify event represents a real change.