OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library watcher.file_watcher.polling; | 5 library watcher.file_watcher.polling; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import '../file_watcher.dart'; | 10 import '../file_watcher.dart'; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 _PollingFileWatcher(this.path, Duration pollingDelay) { | 44 _PollingFileWatcher(this.path, Duration pollingDelay) { |
45 _timer = new Timer.periodic(pollingDelay, (_) => _poll()); | 45 _timer = new Timer.periodic(pollingDelay, (_) => _poll()); |
46 _poll(); | 46 _poll(); |
47 } | 47 } |
48 | 48 |
49 /// Checks the mtime of the file and whether it's been removed. | 49 /// Checks the mtime of the file and whether it's been removed. |
50 Future _poll() async { | 50 Future _poll() async { |
51 // We don't mark the file as removed if this is the first poll (indicated by | 51 // We don't mark the file as removed if this is the first poll (indicated by |
52 // [_lastModified] being null). Instead, below we forward the dart:io error | 52 // [_lastModified] being null). Instead, below we forward the dart:io error |
53 // that comes from trying to read the mtime below. | 53 // that comes from trying to read the mtime below. |
54 if (_lastModified != null && !await new File(path).exists()) { | 54 var pathExists = await new File(path).exists(); |
55 if (_eventsController.isClosed) return; | |
56 | |
57 if (_lastModified != null && !pathExists) { | |
55 _eventsController.add(new WatchEvent(ChangeType.REMOVE, path)); | 58 _eventsController.add(new WatchEvent(ChangeType.REMOVE, path)); |
56 close(); | 59 close(); |
57 return; | 60 return; |
58 } | 61 } |
59 | 62 |
60 var modified; | 63 var modified; |
61 try { | 64 try { |
62 modified = await getModificationTime(path); | 65 try { |
66 modified = await getModificationTime(path); | |
67 } finally { | |
Bob Nystrom
2015/07/17 15:58:22
Can you just add this to the outer try?
nweiz
2015/07/17 20:42:18
No, it needs to happen before "_eventsController.a
| |
68 if (_eventsController.isClosed) return; | |
69 } | |
63 } on FileSystemException catch (error, stackTrace) { | 70 } on FileSystemException catch (error, stackTrace) { |
64 _eventsController.addError(error, stackTrace); | 71 _eventsController.addError(error, stackTrace); |
65 close(); | 72 close(); |
66 return; | 73 return; |
67 } | 74 } |
68 | 75 |
69 if (_eventsController.isClosed) return; | |
70 if (_lastModified == modified) return; | 76 if (_lastModified == modified) return; |
71 | 77 |
72 if (_lastModified == null) { | 78 if (_lastModified == null) { |
73 // If this is the first poll, don't emit an event, just set the last mtime | 79 // If this is the first poll, don't emit an event, just set the last mtime |
74 // and complete the completer. | 80 // and complete the completer. |
75 _lastModified = modified; | 81 _lastModified = modified; |
76 _readyCompleter.complete(); | 82 _readyCompleter.complete(); |
77 } else { | 83 } else { |
78 _lastModified = modified; | 84 _lastModified = modified; |
79 _eventsController.add(new WatchEvent(ChangeType.MODIFY, path)); | 85 _eventsController.add(new WatchEvent(ChangeType.MODIFY, path)); |
80 } | 86 } |
81 } | 87 } |
82 | 88 |
83 void close() { | 89 void close() { |
84 _timer.cancel(); | 90 _timer.cancel(); |
85 _eventsController.close(); | 91 _eventsController.close(); |
86 } | 92 } |
87 } | 93 } |
OLD | NEW |