Skip to content

Commit

Permalink
Ensure we already mark ourselves as ready
Browse files Browse the repository at this point in the history
  • Loading branch information
DanTup committed Nov 22, 2023
1 parent 6095f6e commit 4774d16
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/src/file_watcher/polling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class _PollingFileWatcher implements FileWatcher, ManuallyClosedWatcher {
if (_eventsController.isClosed) return;

if (_lastModified != null && !pathExists) {
_flagReady();
_eventsController.add(WatchEvent(ChangeType.REMOVE, path));
unawaited(close());
return;
Expand All @@ -66,17 +67,21 @@ class _PollingFileWatcher implements FileWatcher, ManuallyClosedWatcher {
modified = await modificationTime(path);
} on FileSystemException catch (error, stackTrace) {
if (!_eventsController.isClosed) {
_flagReady();
_eventsController.addError(error, stackTrace);
await close();
}
}
if (_eventsController.isClosed) return;
if (_eventsController.isClosed) {
_flagReady();
return;
}

if (!isReady) {
// If this is the first poll, don't emit an event, just set the last mtime
// and complete the completer.
_lastModified = modified;
_readyCompleter.complete();
_flagReady();
return;
}

Expand All @@ -86,6 +91,13 @@ class _PollingFileWatcher implements FileWatcher, ManuallyClosedWatcher {
_eventsController.add(WatchEvent(ChangeType.MODIFY, path));
}

/// Flags this watcher as ready if it has not already been done.
void _flagReady() {
if (!isReady) {
_readyCompleter.complete();
}
}

@override
Future<void> close() async {
_timer.cancel();
Expand Down

0 comments on commit 4774d16

Please sign in to comment.