@@ -20,6 +20,8 @@ import 'directory_watcher/windows.dart';
2020/// the message "Directory watcher closed unexpectedly" on the event stream. The
2121/// code using the watcher needs to do additional work to account for the
2222/// dropped events, for example by recomputing interesting files from scratch.
23+ /// By default, the watcher is started in a separate isolate to make this less
24+ /// likely. Pass `runInIsolateOnWindows = false` to not launch an isolate.
2325///
2426/// On Linux, the underlying SDK `Directory.watch` fails if the system limit on
2527/// watchers has been reached. If this happens the SDK exception is thrown, it
@@ -40,7 +42,11 @@ abstract class DirectoryWatcher implements Watcher {
4042 /// shorter will give more immediate feedback at the expense of doing more IO
4143 /// and higher CPU usage. Defaults to one second. Ignored for non-polling
4244 /// watchers.
43- factory DirectoryWatcher (String directory, {Duration ? pollingDelay}) {
45+ ///
46+ /// On Windows, pass [runInIsolateOnWindows] `false` to not run the watcher
47+ /// in a separate isolate to reduce buffer exhaustion failures.
48+ factory DirectoryWatcher (String directory,
49+ {Duration ? pollingDelay, bool runInIsolateOnWindows = true }) {
4450 if (FileSystemEntity .isWatchSupported) {
4551 var customWatcher = createCustomDirectoryWatcher (
4652 directory,
@@ -49,7 +55,10 @@ abstract class DirectoryWatcher implements Watcher {
4955 if (customWatcher != null ) return customWatcher;
5056 if (Platform .isLinux) return LinuxDirectoryWatcher (directory);
5157 if (Platform .isMacOS) return MacOSDirectoryWatcher (directory);
52- if (Platform .isWindows) return WindowsDirectoryWatcher (directory);
58+ if (Platform .isWindows) {
59+ return WindowsDirectoryWatcher (directory,
60+ runInIsolate: runInIsolateOnWindows);
61+ }
5362 }
5463 return PollingDirectoryWatcher (directory, pollingDelay: pollingDelay);
5564 }
0 commit comments