Repository navigation
File watch service with a polling backend (#1301) - #1302
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)andwatchDirectory(path, DirectoryWatchOptions(recursive, include, exclude), handler, target)return aFileWatchSubscriptionwithupdate(newBaseline),cancel(),cancelled,path,isDirectory,target.FileChange { kind, path, state };FileChangeHandler = void delegate(FileWatchSubscription, const(FileChange)[]).mode(FileWatchMode.threaddefault,manual+poll()),pollInterval(2 s),settleTime(300 ms),checkNow(),watchedPathCount; staticdefaultTarget,cancelTarget(),shutdown(),hasInstance.targetis aCustomEventTarget, whichWindowimplements (the issue saidWindow); null = the platform's first window at subscribe time.Behaviour
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 aftercancel()returns (posts happen under the subscription's lock).update().poll(), sooner while a change settles, restarted bycheckNow()/ a new subscription if an error ended it; scan failures are logged (first few) in debug builds.setInstancesetsdefaultTarget; a window's subscriptions are cancelled at the start of the Win32, X11, SDL and DSFML window destructors (before the native window goes) and inWindow's destructor; the service stops when the platform changes and inreleaseResourcesOnAppExit().Not in this PR (as planned): editors and the DlangIDE Workspace Explorer subscribing, native backends. Instead of a "UI timer" mode,
FileWatchMode.manuallets the application callpoll()(a UI timer needs a widget, which the core module doesn't have).watchDirectoryreads its baseline listing on the UI thread (documented; follow-up issue proposed).Testing
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 teston Linux (37 modules),-c console(34), dip1000, Windows (38);dub build -c x11; D-Scanner.winapp.d, started and closed: exits, no leak lines inui.log.🤖 Generated with Claude Code