@@ -405,35 +405,42 @@ class _WindowsDirectoryWatcher
405
405
406
406
/// Start or restart the underlying [Directory.watch] stream.
407
407
void _startWatch () {
408
- // Note: "watcher closed" exceptions do not get sent over the stream
409
- // returned by watch, and must be caught via a zone handler.
408
+ // Note: in older SDKs "watcher closed" exceptions might not get sent over
409
+ // the stream returned by watch, and must be caught via a zone handler.
410
410
runZonedGuarded (
411
411
() {
412
412
var innerStream = Directory (path).watch (recursive: true );
413
413
_watchSubscription = innerStream.listen (
414
414
_onEvent,
415
- onError: _eventsController.addError,
415
+ onError: _restartWatchOnOverflowOr ( _eventsController.addError) ,
416
416
onDone: _onDone,
417
417
);
418
418
},
419
- (error, stackTrace) async {
420
- if (error is FileSystemException &&
421
- error.message.startsWith ('Directory watcher closed unexpectedly' )) {
422
- // Wait to work around https://github.com/dart-lang/sdk/issues/61378.
423
- // Give the VM time to reset state after the error. See the issue for
424
- // more discussion of the workaround.
425
- await _watchSubscription? .cancel ();
426
- await Future <void >.delayed (const Duration (milliseconds: 1 ));
427
- _eventsController.addError (error, stackTrace);
428
- _startWatch ();
429
- } else {
430
- // ignore: only_throw_errors
431
- throw error;
432
- }
433
- },
419
+ _restartWatchOnOverflowOr ((error, stackTrace) {
420
+ // ignore: only_throw_errors
421
+ throw error;
422
+ }),
434
423
);
435
424
}
436
425
426
+ void Function (Object , StackTrace ) _restartWatchOnOverflowOr (
427
+ void Function (Object , StackTrace ) otherwise) {
428
+ return (Object error, StackTrace stackTrace) async {
429
+ if (error is FileSystemException &&
430
+ error.message.startsWith ('Directory watcher closed unexpectedly' )) {
431
+ // Wait to work around https://github.com/dart-lang/sdk/issues/61378.
432
+ // Give the VM time to reset state after the error. See the issue for
433
+ // more discussion of the workaround.
434
+ await _watchSubscription? .cancel ();
435
+ await Future <void >.delayed (const Duration (milliseconds: 1 ));
436
+ _eventsController.addError (error, stackTrace);
437
+ _startWatch ();
438
+ } else {
439
+ otherwise (error, stackTrace);
440
+ }
441
+ };
442
+ }
443
+
437
444
/// Starts or restarts listing the watched directory to get an initial picture
438
445
/// of its state.
439
446
Future <void > _listDir () {
0 commit comments