diff --git a/packages/flutter_tools/lib/src/commands/start.dart b/packages/flutter_tools/lib/src/commands/start.dart index 9b9259e4a7..62fd1d0ee1 100644 --- a/packages/flutter_tools/lib/src/commands/start.dart +++ b/packages/flutter_tools/lib/src/commands/start.dart @@ -190,6 +190,13 @@ Future startApp( printError('Error starting application on ${device.name}.'); } else { startedSomething = true; + + // If the user specified --start-paused (and the device supports it) then + // wait for the observatory port to become available before returning from + // `startApp()`. + if (startPaused && device.supportsStartPaused) { + await _delayUntilObservatoryAvailable('localhost', debugPort); + } } } @@ -204,6 +211,34 @@ Future startApp( return startedSomething ? 0 : 2; } +/// Delay until the Observatory / service protocol is available. +/// +/// This does not fail if we're unable to connect, and times out after the given +/// [timeout]. +Future _delayUntilObservatoryAvailable(String host, int port, { + Duration timeout: const Duration(seconds: 10) +}) async { + Stopwatch stopwatch = new Stopwatch()..start(); + + final String url = 'ws://$host:$port/ws'; + printTrace('Looking for the observatory at $url.'); + + while (stopwatch.elapsed <= timeout) { + try { + WebSocket ws = await WebSocket.connect(url); + + printTrace('Connected to the observatory port (${stopwatch.elapsedMilliseconds}ms).'); + ws.close().catchError((error) => null); + + return; + } catch (error) { + await new Future.delayed(new Duration(milliseconds: 250)); + } + } + + printTrace('Unable to connect to the observatory.'); +} + /// Return a relative path if [fullPath] is contained by the cwd, else return an /// absolute path. String _getDisplayPath(String fullPath) { diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index 7c865b3a97..274d1fff69 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -98,6 +98,8 @@ abstract class Device { String get name; + bool get supportsStartPaused => true; + /// Install an app package on the current device bool installApp(ApplicationPackage app); diff --git a/packages/flutter_tools/lib/src/ios/device_ios.dart b/packages/flutter_tools/lib/src/ios/device_ios.dart index 9dedb6d739..08ac9e63a9 100644 --- a/packages/flutter_tools/lib/src/ios/device_ios.dart +++ b/packages/flutter_tools/lib/src/ios/device_ios.dart @@ -82,6 +82,8 @@ class IOSDevice extends Device { final String name; + bool get supportsStartPaused => false; + static List getAttachedDevices([IOSDevice mockIOS]) { List devices = []; for (String id in _getAttachedDeviceIDs(mockIOS)) {