Skip to content

File watch service with a polling backend (#1301) - #1302

Merged
buggins merged 1 commit into
masterfrom
file-watch-service-1301
Nov 28, 2026
Merged

buggins merged 1 commit into
masterfrom
file-watch-service-1301

Conversation

@buggins

@buggins buggins commented Nov 28, 2026

Copy link
Copy Markdown
Owner

Fixes #1301. Phase 3 of the file watch plan (#785); phases 1-2 were #1273 and #1274.

API (dlangui.core.filewatch)

  • FileWatchService.instance; watchFile(FileState baseline, FileChangeHandler handler, CustomEventTarget target = null) and watchDirectory(path, DirectoryWatchOptions(recursive, include, exclude), handler, target) return a FileWatchSubscription with update(newBaseline), cancel(), cancelled, path, isDirectory, target.
  • FileChange { kind, path, state }; FileChangeHandler = void delegate(FileWatchSubscription, const(FileChange)[]).
  • mode (FileWatchMode.thread default, manual + poll()), pollInterval (2 s), settleTime (300 ms), checkNow(), watchedPathCount; static defaultTarget, cancelTarget(), shutdown(), hasInstance.
  • target is a CustomEventTarget, which Window implements (the issue said Window); null = the platform's first window at subscribe time.

Behaviour

  • Delivery on the UI thread: changes queued per subscription, one posted event per burst; a changed path is reported after it stayed the same for settleTime; before a file subscription's handler runs, the state is read again (size and time, plus the checksum the watcher computed) and the call is dropped when it equals the baseline (the app's own save, or changed and changed back); the handler runs outside any lock; nothing is posted or delivered after cancel() returns (posts happen under the subscription's lock).
  • Watches are shared per path; each subscription keeps its own baseline and is compared with the disk at the next scan when it is new or after update().
  • Polling backend: files by existence, size, time and file id, plus a checksum when the subscription's baseline has one (a touch or an identical rewrite isn't reported); directories by listing (name, size, time), recursive or not, exclude globs skip names (excluded directories aren't walked), include globs select files (then directories themselves aren't reported).
  • Watcher thread: daemon, scans serialized with poll(), sooner while a change settles, restarted by checkNow() / a new subscription if an error ended it; scan failures are logged (first few) in debug builds.
  • Platform: setInstance sets defaultTarget; a window's subscriptions are cancelled at the start of the Win32, X11, SDL and DSFML window destructors (before the native window goes) and in Window's destructor; the service stops when the platform changes and in releaseResourcesOnAppExit().

Not in this PR (as planned): editors and the DlangIDE Workspace Explorer subscribing, native backends. Instead of a "UI timer" mode, FileWatchMode.manual lets the application call poll() (a UI timer needs a widget, which the core module doesn't have). watchDirectory reads its baseline listing on the UI thread (documented; follow-up issue proposed).

Testing

  • Unittests (temporary directories, the service in manual mode with a test target standing in for the UI thread): modified, deleted, created, rename-over, touch with a checksum, the app's own save before and after update(), changed and changed back, cancel after a delivery was posted, shared watches, a destroyed target, settle time, directory changes with recursive / exclude / include, a subscriber joining with a stale baseline, update() with a stale state, checksums after delete and create, the real thread delivering, a dead thread restarted, and the platform hooks (default target, window destroyed, platform replaced). Mutation checks: the recheck, the checksum handling, the stale-baseline check and the baseline-driven checksums each fail the tests when removed; the platform test runs to its end.
  • dub test on Linux (37 modules), -c console (34), dip1000, Windows (38); dub build -c x11; D-Scanner.
  • Windows: dmledit built with the changed winapp.d, started and closed: exits, no leak lines in ui.log.
  • Not verified at runtime: X11, SDL and DSFML window destructors (compiled for X11/SDL; DSFML doesn't build on master, DSFML: windows are never redrawn #1157).

🤖 Generated with Claude Code

Phase 3 of the file watch plan (#785): FileWatchService in
dlangui.core.filewatch.

- watchFile(baseline, handler, target) and watchDirectory(path, options,
  handler, target) return a FileWatchSubscription (update, cancel,
  cancelled); watches are shared per path, each subscription keeps its
  own baseline and is compared with the disk at the next scan.
- Delivery on the target's UI thread (CustomEventTarget, e.g. a Window):
  one posted event per burst, settle time (300 ms), the file state read
  again before the handler runs (drops the app's own saves and changes
  undone), the handler called outside any lock; nothing is posted or
  delivered after cancel() returns.
- Polling backend: files by size and time, plus a checksum when the
  baseline has one; directories by listing, recursive, with include and
  exclude filters; a daemon thread by default (restarted if an error
  ended it), or FileWatchMode.manual with poll(); checkNow().
- Platform: null targets go to the first window; a window's subscriptions
  are cancelled before its native window goes (backend destructors) and
  in Window's destructor; the service stops with the platform and at exit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@buggins buggins self-assigned this Nov 28, 2026
@buggins
buggins marked this pull request as ready for review November 28, 2026 16:42
@buggins
buggins merged commit 02d4ba1 into master Nov 28, 2026
13 checks passed
@buggins
buggins deleted the file-watch-service-1301 branch November 28, 2026 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

File watch service with a polling backend (phase 3 of #785)

1 participant