Handle file watcher closed exception (#75)

Handle file watcher closed exception on windows
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 44fbda5..2b3cd35 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+# 0.9.7+13
+
+* Catch & forward `FileSystemException` from unexpectedly closed file watchers
+  on windows; the watcher will also be automatically restarted when this occurs.
+
 # 0.9.7+12
 
 * Catch `FileSystemException` during `existsSync()` on Windows.
diff --git a/lib/src/directory_watcher/windows.dart b/lib/src/directory_watcher/windows.dart
index 85fef5f..8bf6642 100644
--- a/lib/src/directory_watcher/windows.dart
+++ b/lib/src/directory_watcher/windows.dart
@@ -372,10 +372,22 @@
 
   /// Start or restart the underlying [Directory.watch] stream.
   void _startWatch() {
-    // Batch the events together so that we can dedup events.
-    var innerStream = Directory(path).watch(recursive: true);
-    _watchSubscription = innerStream.listen(_onEvent,
-        onError: _eventsController.addError, onDone: _onDone);
+    // Note: "watcher closed" exceptions do not get sent over the stream
+    // returned by watch, and must be caught via a zone handler.
+    runZoned(() {
+      var innerStream = Directory(path).watch(recursive: true);
+      _watchSubscription = innerStream.listen(_onEvent,
+          onError: _eventsController.addError, onDone: _onDone);
+    }, onError: (error, StackTrace stackTrace) {
+      if (error is FileSystemException &&
+          error.message.startsWith('Directory watcher closed unexpectedly')) {
+        _watchSubscription.cancel();
+        _eventsController.addError(error, stackTrace);
+        _startWatch();
+      } else {
+        throw error;
+      }
+    });
   }
 
   /// Starts or restarts listing the watched directory to get an initial picture
diff --git a/pubspec.yaml b/pubspec.yaml
index f999c2a..f8315bc 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
 name: watcher
-version: 0.9.7+12
+version: 0.9.7+13
 
 description: >-
   A file system watcher. It monitors changes to contents of directories and