| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library watcher.file_watcher.polling; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:io'; | |
| 9 | |
| 10 import '../file_watcher.dart'; | |
| 11 import '../resubscribable.dart'; | |
| 12 import '../stat.dart'; | |
| 13 import '../watch_event.dart'; | |
| 14 | |
| 15 /// Periodically polls a file for changes. | |
| 16 class PollingFileWatcher extends ResubscribableWatcher implements FileWatcher { | |
| 17 PollingFileWatcher(String path, {Duration pollingDelay}) | |
| 18 : super(path, () { | |
| 19 return new _PollingFileWatcher(path, | |
| 20 pollingDelay != null ? pollingDelay : new Duration(seconds: 1)); | |
| 21 }); | |
| 22 } | |
| 23 | |
| 24 class _PollingFileWatcher implements FileWatcher, ManuallyClosedWatcher { | |
| 25 final String path; | |
| 26 | |
| 27 Stream<WatchEvent> get events => _eventsController.stream; | |
| 28 final _eventsController = new StreamController<WatchEvent>.broadcast(); | |
| 29 | |
| 30 bool get isReady => _readyCompleter.isCompleted; | |
| 31 | |
| 32 Future get ready => _readyCompleter.future; | |
| 33 final _readyCompleter = new Completer(); | |
| 34 | |
| 35 /// The timer that controls polling. | |
| 36 Timer _timer; | |
| 37 | |
| 38 /// The previous modification time of the file. | |
| 39 /// | |
| 40 /// Used to tell when the file was modified. This is `null` before the file's | |
| 41 /// mtime has first been checked. | |
| 42 DateTime _lastModified; | |
| 43 | |
| 44 _PollingFileWatcher(this.path, Duration pollingDelay) { | |
| 45 _timer = new Timer.periodic(pollingDelay, (_) => _poll()); | |
| 46 _poll(); | |
| 47 } | |
| 48 | |
| 49 /// Checks the mtime of the file and whether it's been removed. | |
| 50 Future _poll() async { | |
| 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 | |
| 53 // that comes from trying to read the mtime below. | |
| 54 var pathExists = await new File(path).exists(); | |
| 55 if (_eventsController.isClosed) return; | |
| 56 | |
| 57 if (_lastModified != null && !pathExists) { | |
| 58 _eventsController.add(new WatchEvent(ChangeType.REMOVE, path)); | |
| 59 close(); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 var modified; | |
| 64 try { | |
| 65 try { | |
| 66 modified = await getModificationTime(path); | |
| 67 } finally { | |
| 68 if (_eventsController.isClosed) return; | |
| 69 } | |
| 70 } on FileSystemException catch (error, stackTrace) { | |
| 71 _eventsController.addError(error, stackTrace); | |
| 72 close(); | |
| 73 return; | |
| 74 } | |
| 75 | |
| 76 if (_lastModified == modified) return; | |
| 77 | |
| 78 if (_lastModified == null) { | |
| 79 // If this is the first poll, don't emit an event, just set the last mtime | |
| 80 // and complete the completer. | |
| 81 _lastModified = modified; | |
| 82 _readyCompleter.complete(); | |
| 83 } else { | |
| 84 _lastModified = modified; | |
| 85 _eventsController.add(new WatchEvent(ChangeType.MODIFY, path)); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void close() { | |
| 90 _timer.cancel(); | |
| 91 _eventsController.close(); | |
| 92 } | |
| 93 } | |
| OLD | NEW |