Change iOS device discovery from polling to long-running observation (#58137)
diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index cef0855..6c19930 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart
@@ -82,6 +82,7 @@ platform: globals.platform, xcdevice: globals.xcdevice, iosWorkflow: globals.iosWorkflow, + logger: globals.logger, ), IOSSimulators(iosSimulatorUtils: globals.iosSimulatorUtils), FuchsiaDevices( @@ -289,14 +290,18 @@ static const Duration _pollingTimeout = Duration(seconds: 30); final String name; - ItemListNotifier<Device> _items; + + @protected + @visibleForTesting + ItemListNotifier<Device> deviceNotifier; + Timer _timer; Future<List<Device>> pollingGetDevices({ Duration timeout }); - void startPolling() { + Future<void> startPolling() async { if (_timer == null) { - _items ??= ItemListNotifier<Device>(); + deviceNotifier ??= ItemListNotifier<Device>(); // Make initial population the default, fast polling timeout. _timer = _initTimer(null); } @@ -306,7 +311,7 @@ return Timer(_pollingInterval, () async { try { final List<Device> devices = await pollingGetDevices(timeout: pollingTimeout); - _items.updateWithNewList(devices); + deviceNotifier.updateWithNewList(devices); } on TimeoutException { globals.printTrace('Device poll timed out. Will retry.'); } @@ -315,7 +320,7 @@ }); } - void stopPolling() { + Future<void> stopPolling() async { _timer?.cancel(); _timer = null; } @@ -327,23 +332,23 @@ @override Future<List<Device>> discoverDevices({ Duration timeout }) async { - _items = null; + deviceNotifier = null; return _populateDevices(timeout: timeout); } Future<List<Device>> _populateDevices({ Duration timeout }) async { - _items ??= ItemListNotifier<Device>.from(await pollingGetDevices(timeout: timeout)); - return _items.items; + deviceNotifier ??= ItemListNotifier<Device>.from(await pollingGetDevices(timeout: timeout)); + return deviceNotifier.items; } Stream<Device> get onAdded { - _items ??= ItemListNotifier<Device>(); - return _items.onAdded; + deviceNotifier ??= ItemListNotifier<Device>(); + return deviceNotifier.onAdded; } Stream<Device> get onRemoved { - _items ??= ItemListNotifier<Device>(); - return _items.onRemoved; + deviceNotifier ??= ItemListNotifier<Device>(); + return deviceNotifier.onRemoved; } void dispose() => stopPolling();