Skip to content

Commit 97014f3

Browse files
authored
Move files, rename classes: "MacOS" watcher is used on Windows too. (#2274)
1 parent db8f0b8 commit 97014f3

24 files changed

+93
-138
lines changed

pkgs/watcher/lib/src/directory_watcher.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import 'dart:io';
66

77
import '../watcher.dart';
88
import 'custom_watcher_factory.dart';
9-
import 'directory_watcher/linux.dart';
10-
import 'directory_watcher/macos.dart';
11-
import 'directory_watcher/windows_resubscribable_watcher.dart';
9+
import 'directory_watcher/linux/linux_directory_watcher.dart';
10+
import 'directory_watcher/recursive/recursive_directory_watcher.dart';
1211

1312
/// Watches the contents of a directory and emits [WatchEvent]s when something
1413
/// in the directory has changed.
@@ -52,9 +51,11 @@ abstract class DirectoryWatcher implements Watcher {
5251
);
5352
if (customWatcher != null) return customWatcher;
5453
if (Platform.isLinux) return LinuxDirectoryWatcher(directory);
55-
if (Platform.isMacOS) return MacosDirectoryWatcher(directory);
54+
if (Platform.isMacOS) {
55+
return RecursiveDirectoryWatcher(directory, runInIsolate: false);
56+
}
5657
if (Platform.isWindows) {
57-
return WindowsDirectoryWatcher(directory,
58+
return RecursiveDirectoryWatcher(directory,
5859
runInIsolate: runInIsolateOnWindows);
5960
}
6061
}

pkgs/watcher/lib/src/directory_watcher/linux.dart renamed to pkgs/watcher/lib/src/directory_watcher/linux/linux_directory_watcher.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
import 'dart:async';
66

7-
import '../directory_watcher.dart';
8-
import '../resubscribable.dart';
9-
import '../watch_event.dart';
10-
import 'linux/watch_tree_root.dart';
7+
import '../../directory_watcher.dart';
8+
import '../../resubscribable.dart';
9+
import '../../watch_event.dart';
10+
import 'watch_tree_root.dart';
1111

1212
/// Resubscribable Linux directory watcher that watches using
1313
/// [_LinuxDirectoryWatcher].

pkgs/watcher/lib/src/directory_watcher/polling.dart renamed to pkgs/watcher/lib/src/directory_watcher/polling/polling_directory_watcher.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import 'dart:async';
66
import 'dart:io';
77

8-
import '../async_queue.dart';
9-
import '../directory_watcher.dart';
10-
import '../polling.dart';
11-
import '../resubscribable.dart';
12-
import '../watch_event.dart';
8+
import '../../async_queue.dart';
9+
import '../../directory_watcher.dart';
10+
import '../../polling.dart';
11+
import '../../resubscribable.dart';
12+
import '../../watch_event.dart';
1313
import 'directory_list.dart';
1414

1515
/// Periodically polls a directory for changes.

pkgs/watcher/lib/src/directory_watcher/macos/directory_tree.dart renamed to pkgs/watcher/lib/src/directory_watcher/recursive/directory_tree.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'dart:io';
77
import '../../paths.dart';
88
import '../../testing.dart';
99
import '../../watch_event.dart';
10-
import '../event_tree.dart';
10+
import 'event_tree.dart';
1111

1212
/// MacOS or Windows directory tree.
1313
///

pkgs/watcher/lib/src/directory_watcher/event_tree.dart renamed to pkgs/watcher/lib/src/directory_watcher/recursive/event_tree.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import '../paths.dart';
5+
import '../../paths.dart';
66

77
/// Tree of event paths relative to the watched path.
88
///

pkgs/watcher/lib/src/directory_watcher/windows_isolate_directory_watcher.dart renamed to pkgs/watcher/lib/src/directory_watcher/recursive/isolate_recursive_directory_watcher.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import 'dart:async';
66
import 'dart:isolate';
77

8-
import '../resubscribable.dart';
9-
import '../testing.dart';
10-
import '../watch_event.dart';
11-
import 'windows.dart';
8+
import '../../resubscribable.dart';
9+
import '../../testing.dart';
10+
import '../../watch_event.dart';
11+
import 'recursive_directory_watcher.dart';
1212

13-
/// Runs [WindowsManuallyClosedDirectoryWatcher] in an isolate to work around
13+
/// Runs [ManuallyClosedRecursiveDirectoryWatcher] in an isolate to work around
1414
/// a platform limitation.
1515
///
1616
/// On Windows, Directory.watch fails if too many events arrive without being
@@ -21,7 +21,7 @@ import 'windows.dart';
2121
/// Running the watcher in an isolate makes buffer exhaustion much less likely
2222
/// as there is no unrelated work happening in the isolate that would block
2323
/// processing of events.
24-
class WindowsIsolateDirectoryWatcher implements ManuallyClosedWatcher {
24+
class IsolateRecursiveDirectoryWatcher implements ManuallyClosedWatcher {
2525
@override
2626
final String path;
2727
final ReceivePort _receivePort = ReceivePort();
@@ -33,7 +33,7 @@ class WindowsIsolateDirectoryWatcher implements ManuallyClosedWatcher {
3333

3434
final void Function(LogEntry)? _log;
3535

36-
WindowsIsolateDirectoryWatcher(this.path)
36+
IsolateRecursiveDirectoryWatcher(this.path)
3737
: _log = logSeparateIsolateForTesting {
3838
_startIsolate(path, _receivePort.sendPort, log: _log != null);
3939
_receivePort.listen((event) => _receiveFromIsolate(event as Event));
@@ -87,7 +87,7 @@ void _startIsolate(String path, SendPort sendPort, {required bool log}) async {
8787

8888
class _WatcherIsolate {
8989
final String path;
90-
final WindowsManuallyClosedDirectoryWatcher watcher;
90+
final ManuallyClosedRecursiveDirectoryWatcher watcher;
9191
final SendPort sendPort;
9292
final bool log;
9393

@@ -96,7 +96,7 @@ class _WatcherIsolate {
9696
final Completer<void> _closeCompleter = Completer();
9797

9898
_WatcherIsolate(this.path, this.sendPort, {required this.log})
99-
: watcher = WindowsManuallyClosedDirectoryWatcher(path) {
99+
: watcher = ManuallyClosedRecursiveDirectoryWatcher(path) {
100100
if (log) {
101101
logForTesting = (message) => sendPort.send(Event.log(message));
102102
}

pkgs/watcher/lib/src/directory_watcher/macos.dart renamed to pkgs/watcher/lib/src/directory_watcher/recursive/recursive_directory_watcher.dart

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,34 @@
44

55
import 'dart:async';
66

7-
import '../directory_watcher.dart';
8-
import '../resubscribable.dart';
9-
import '../watch_event.dart';
10-
import 'macos/watched_directory_tree.dart';
11-
12-
/// Resubscribable MacOS directory watcher that watches using
13-
/// [_MacosDirectoryWatcher].
14-
class MacosDirectoryWatcher extends ResubscribableWatcher
7+
import '../../directory_watcher.dart';
8+
import '../../resubscribable.dart';
9+
import '../../watch_event.dart';
10+
import 'isolate_recursive_directory_watcher.dart';
11+
import 'watched_directory_tree.dart';
12+
13+
/// Directory watcher that watches using [WatchedDirectoryTree].
14+
///
15+
/// Optionally, runs the watcher in a new isolate.
16+
class RecursiveDirectoryWatcher extends ResubscribableWatcher
1517
implements DirectoryWatcher {
1618
@override
1719
String get directory => path;
1820

19-
MacosDirectoryWatcher(String directory)
20-
: super(directory, () => _MacosDirectoryWatcher(directory));
21+
/// Watches [directory].
22+
///
23+
/// If [runInIsolate], runs the watcher in an isolate to reduce the chance of
24+
/// hitting the Windows-specific buffer exhaustion failure.
25+
RecursiveDirectoryWatcher(String directory, {required bool runInIsolate})
26+
: super(
27+
directory,
28+
() => runInIsolate
29+
? IsolateRecursiveDirectoryWatcher(directory)
30+
: ManuallyClosedRecursiveDirectoryWatcher(directory));
2131
}
2232

23-
/// Macos directory watcher that watches using [WatchedDirectoryTree].
24-
class _MacosDirectoryWatcher
33+
/// Manually closed directory watcher that watches using [WatchedDirectoryTree].
34+
class ManuallyClosedRecursiveDirectoryWatcher
2535
implements DirectoryWatcher, ManuallyClosedWatcher {
2636
@override
2737
final String path;
@@ -41,7 +51,7 @@ class _MacosDirectoryWatcher
4151

4252
late final WatchedDirectoryTree _watchTree;
4353

44-
_MacosDirectoryWatcher(this.path) {
54+
ManuallyClosedRecursiveDirectoryWatcher(this.path) {
4555
_watchTree = WatchedDirectoryTree(
4656
watchedDirectory: path,
4757
eventsController: _eventsController,

pkgs/watcher/lib/src/directory_watcher/macos/native_watch.dart renamed to pkgs/watcher/lib/src/directory_watcher/recursive/recursive_native_watch.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ import '../../event.dart';
99
import '../../event_batching.dart';
1010
import '../../paths.dart';
1111
import '../../testing.dart';
12-
import '../event_tree.dart';
12+
import 'event_tree.dart';
1313

14-
/// Recursively watches a directory with `Directory.watch` on MacOS or Windows.
14+
/// Watches a directory with `Directory.watch(recursive: true)` on MacOS or
15+
/// Windows.
1516
///
1617
/// Handles incorrect closure of the watch due to a delete event from before
1718
/// the watch started, by re-opening the watch if the directory still exists.
1819
/// See https://github.com/dart-lang/sdk/issues/14373.
1920
///
2021
/// Handles deletion of the watched directory on Windows by watching the parent
2122
/// directory.
22-
class NativeWatch {
23+
class RecursiveNativeWatch {
2324
final AbsolutePath watchedDirectory;
2425

2526
/// Called when [watchedDirectory] is recreated.
@@ -43,7 +44,7 @@ class NativeWatch {
4344
/// Watches [watchedDirectory].
4445
///
4546
/// Pass [watchedDirectoryWasDeleted], [onEvents] and [onError] handlers.
46-
NativeWatch({
47+
RecursiveNativeWatch({
4748
required this.watchedDirectory,
4849
required void Function() watchedDirectoryWasRecreated,
4950
required void Function() watchedDirectoryWasDeleted,

pkgs/watcher/lib/src/directory_watcher/macos/watched_directory_tree.dart renamed to pkgs/watcher/lib/src/directory_watcher/recursive/watched_directory_tree.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import 'dart:async';
77
import '../../paths.dart';
88
import '../../testing.dart';
99
import '../../watch_event.dart';
10-
import '../event_tree.dart';
1110
import 'directory_tree.dart';
12-
import 'native_watch.dart';
11+
import 'event_tree.dart';
12+
import 'recursive_native_watch.dart';
1313

14-
/// MacOS or Windows directory watcher using a [DirectoryTree].
14+
/// Directory watcher using `Directory.watch(recursive: true)` and
15+
/// [DirectoryTree].
1516
///
16-
/// Various platform-specific issues are worked around.
17+
/// Recursive watch is available on MacOS and Windows; contains
18+
/// platform-specific workarounds for both.
1719
///
1820
/// MacOS events from a native watcher can arrive out of order, including in
1921
/// different batches. For example, a modification of `a/1` followed by a
@@ -48,7 +50,7 @@ class WatchedDirectoryTree {
4850
final StreamController<WatchEvent> _eventsController;
4951
final Completer<void> _readyCompleter;
5052

51-
late final NativeWatch nativeWatch;
53+
late final RecursiveNativeWatch nativeWatch;
5254
late final DirectoryTree directoryTree;
5355

5456
WatchedDirectoryTree(
@@ -63,7 +65,7 @@ class WatchedDirectoryTree {
6365
}
6466

6567
void _watch() async {
66-
nativeWatch = NativeWatch(
68+
nativeWatch = RecursiveNativeWatch(
6769
watchedDirectory: watchedDirectory,
6870
watchedDirectoryWasRecreated: _watchedDirectoryWasRecreated,
6971
watchedDirectoryWasDeleted: _watchedDirectoryWasDeleted,

0 commit comments

Comments
 (0)