waitForVmServiceInfoFile method
Waits for vmServiceInfoFile
to exist and become valid before returning
the VM Service URI contained within.
Implementation
Future<Uri> waitForVmServiceInfoFile(
Logger? logger,
File vmServiceInfoFile,
) async {
final completer = Completer<Uri>();
late final StreamSubscription<FileSystemEvent> vmServiceInfoFileWatcher;
void tryParseServiceInfoFile(FileSystemEvent event) {
final uri = _readVmServiceInfoFile(logger, vmServiceInfoFile);
if (uri != null && !completer.isCompleted) {
vmServiceInfoFileWatcher.cancel();
completer.complete(uri);
}
}
vmServiceInfoFileWatcher = vmServiceInfoFile.parent
.watch(events: FileSystemEvent.all)
.where((event) => event.path == vmServiceInfoFile.path)
.listen(
tryParseServiceInfoFile,
onError: (e) => logger?.call('Ignoring exception from watcher: $e'),
);
// After setting up the watcher, also check if the file already exists to
// ensure we don't miss it if it was created right before we set the
// watched up.
final uri = _readVmServiceInfoFile(logger, vmServiceInfoFile);
if (uri != null && !completer.isCompleted) {
unawaited(vmServiceInfoFileWatcher.cancel());
completer.complete(uri);
}
return completer.future;
}