From d1d78bc91728dfdb06ad409a98427478acb64eb7 Mon Sep 17 00:00:00 2001 From: Lau Ching Jun Date: Wed, 19 Jul 2023 22:01:24 -0700 Subject: [PATCH] Make PollingDeviceDiscovery start the initial poll faster. (#130755) This will speed up the initial population of the device list. --- packages/flutter_tools/lib/src/device.dart | 7 ++++--- packages/flutter_tools/test/general.shard/device_test.dart | 1 - .../proxied_devices/proxied_devices_test.dart | 5 +---- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index a919109367e..83d13746d29 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -504,12 +504,13 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery { if (_timer == null) { deviceNotifier ??= ItemListNotifier(); // Make initial population the default, fast polling timeout. - _timer = _initTimer(null); + _timer = _initTimer(null, initialCall: true); } } - Timer _initTimer(Duration? pollingTimeout) { - return Timer(_pollingInterval, () async { + Timer _initTimer(Duration? pollingTimeout, {bool initialCall = false}) { + // Poll for devices immediately on the initial call for faster initial population. + return Timer(initialCall ? Duration.zero : _pollingInterval, () async { try { final List devices = await pollingGetDevices(timeout: pollingTimeout); deviceNotifier!.updateWithNewList(devices); diff --git a/packages/flutter_tools/test/general.shard/device_test.dart b/packages/flutter_tools/test/general.shard/device_test.dart index 27745363082..fab87b8f710 100644 --- a/packages/flutter_tools/test/general.shard/device_test.dart +++ b/packages/flutter_tools/test/general.shard/device_test.dart @@ -234,7 +234,6 @@ void main() { FakeAsync().run((FakeAsync time) { final FakePollingDeviceDiscovery pollingDeviceDiscovery = FakePollingDeviceDiscovery(); pollingDeviceDiscovery.startPolling(); - time.elapse(const Duration(milliseconds: 4001)); // First check should use the default polling timeout // to quickly populate the list. diff --git a/packages/flutter_tools/test/general.shard/proxied_devices/proxied_devices_test.dart b/packages/flutter_tools/test/general.shard/proxied_devices/proxied_devices_test.dart index 9f601fbbac0..d6f982e35ae 100644 --- a/packages/flutter_tools/test/general.shard/proxied_devices/proxied_devices_test.dart +++ b/packages/flutter_tools/test/general.shard/proxied_devices/proxied_devices_test.dart @@ -397,10 +397,7 @@ void main() { expect(devicesAdded[0].id, fakeDevice['id']); expect(devicesAdded[1].id, fakeDevice2['id']); }); - // Explicit timeout is needed because the default timeout is 2s, but `startPolling` waits for - // 4s before making its first poll. - // TODO(chingjun): Remove the timeout. - }, timeout: const Timeout(Duration(seconds: 6))); + }); group('ProxiedDartDevelopmentService', () { testWithoutContext('forwards start and shutdown to remote', () async {