@@ -7,12 +7,15 @@ import 'dart:io';
77
88import '../async_queue.dart' ;
99import '../directory_watcher.dart' ;
10+ import '../polling.dart' ;
1011import '../resubscribable.dart' ;
11- import '../stat.dart' ;
1212import '../utils.dart' ;
1313import '../watch_event.dart' ;
1414
1515/// Periodically polls a directory for changes.
16+ ///
17+ /// Changes are noticed if the "last modified" time of a file changes or if its
18+ /// size changes.
1619class PollingDirectoryWatcher extends ResubscribableWatcher
1720 implements DirectoryWatcher {
1821 @override
@@ -53,10 +56,7 @@ class _PollingDirectoryWatcher
5356 /// directory contents.
5457 final Duration _pollingDelay;
5558
56- /// The previous modification times of the files in the directory.
57- ///
58- /// Used to tell which files have been modified.
59- final _lastModifieds = < String , DateTime ? > {};
59+ final _previousPollResults = < String , PollResult > {};
6060
6161 /// The subscription used while [directory] is being listed.
6262 ///
@@ -78,7 +78,8 @@ class _PollingDirectoryWatcher
7878 /// The set of files that have been seen in the current directory listing.
7979 ///
8080 /// Used to tell which files have been removed: files that are in
81- /// [_lastModifieds] but not in here when a poll completes have been removed.
81+ /// [_previousPollResults] but not in here when a poll completes have been
82+ /// removed.
8283 final _polledFiles = < String > {};
8384
8485 _PollingDirectoryWatcher (this .path, this ._pollingDelay) {
@@ -95,7 +96,7 @@ class _PollingDirectoryWatcher
9596 // Don't process any remaining files.
9697 _filesToProcess.clear ();
9798 _polledFiles.clear ();
98- _lastModifieds .clear ();
99+ _previousPollResults .clear ();
99100 }
100101
101102 /// Scans the contents of the directory once to see which files have been
@@ -145,14 +146,14 @@ class _PollingDirectoryWatcher
145146 return ;
146147 }
147148
148- final modified = await modificationTime (file);
149+ final pollResult = await PollResult . poll (file);
149150
150151 if (_events.isClosed) return ;
151152
152- var lastModified = _lastModifieds [file];
153+ var previousPollResult = _previousPollResults [file];
153154
154155 // If its modification time hasn't changed, assume the file is unchanged.
155- if (lastModified != null && lastModified == modified ) {
156+ if (previousPollResult != null && previousPollResult == pollResult ) {
156157 // The file is still here.
157158 _polledFiles.add (file);
158159 return ;
@@ -161,17 +162,17 @@ class _PollingDirectoryWatcher
161162 if (_events.isClosed) return ;
162163
163164 _polledFiles.add (file);
164- if (modified == null ) {
165+ if (! pollResult.fileExists ) {
165166 // The file was in the directory listing but has been removed since then.
166167 // Don't add to _lastModifieds, it will be reported as a REMOVE.
167168 return ;
168169 }
169- _lastModifieds [file] = modified ;
170+ _previousPollResults [file] = pollResult ;
170171
171172 // Only notify if we're ready to emit events.
172173 if (! isReady) return ;
173174
174- var type = lastModified == null ? ChangeType .ADD : ChangeType .MODIFY ;
175+ var type = previousPollResult == null ? ChangeType .ADD : ChangeType .MODIFY ;
175176 _events.add (WatchEvent (type, file));
176177 }
177178
@@ -180,10 +181,11 @@ class _PollingDirectoryWatcher
180181 Future <void > _completePoll () async {
181182 // Any files that were not seen in the last poll but that we have a
182183 // status for must have been removed.
183- var removedFiles = _lastModifieds.keys.toSet ().difference (_polledFiles);
184+ var removedFiles =
185+ _previousPollResults.keys.toSet ().difference (_polledFiles);
184186 for (var removed in removedFiles) {
185187 if (isReady) _events.add (WatchEvent (ChangeType .REMOVE , removed));
186- _lastModifieds .remove (removed);
188+ _previousPollResults .remove (removed);
187189 }
188190
189191 if (! isReady) _readyCompleter.complete ();
0 commit comments