Make PollingDeviceDiscovery start the initial poll faster. (#130755)

This will speed up the initial population of the device list.
This commit is contained in:
Lau Ching Jun 2023-07-19 22:01:24 -07:00 committed by GitHub
parent 86c8abf88b
commit d1d78bc917
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 8 deletions

View File

@ -504,12 +504,13 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
if (_timer == null) { if (_timer == null) {
deviceNotifier ??= ItemListNotifier<Device>(); deviceNotifier ??= ItemListNotifier<Device>();
// Make initial population the default, fast polling timeout. // Make initial population the default, fast polling timeout.
_timer = _initTimer(null); _timer = _initTimer(null, initialCall: true);
} }
} }
Timer _initTimer(Duration? pollingTimeout) { Timer _initTimer(Duration? pollingTimeout, {bool initialCall = false}) {
return Timer(_pollingInterval, () async { // Poll for devices immediately on the initial call for faster initial population.
return Timer(initialCall ? Duration.zero : _pollingInterval, () async {
try { try {
final List<Device> devices = await pollingGetDevices(timeout: pollingTimeout); final List<Device> devices = await pollingGetDevices(timeout: pollingTimeout);
deviceNotifier!.updateWithNewList(devices); deviceNotifier!.updateWithNewList(devices);

View File

@ -234,7 +234,6 @@ void main() {
FakeAsync().run((FakeAsync time) { FakeAsync().run((FakeAsync time) {
final FakePollingDeviceDiscovery pollingDeviceDiscovery = FakePollingDeviceDiscovery(); final FakePollingDeviceDiscovery pollingDeviceDiscovery = FakePollingDeviceDiscovery();
pollingDeviceDiscovery.startPolling(); pollingDeviceDiscovery.startPolling();
time.elapse(const Duration(milliseconds: 4001));
// First check should use the default polling timeout // First check should use the default polling timeout
// to quickly populate the list. // to quickly populate the list.

View File

@ -397,10 +397,7 @@ void main() {
expect(devicesAdded[0].id, fakeDevice['id']); expect(devicesAdded[0].id, fakeDevice['id']);
expect(devicesAdded[1].id, fakeDevice2['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', () { group('ProxiedDartDevelopmentService', () {
testWithoutContext('forwards start and shutdown to remote', () async { testWithoutContext('forwards start and shutdown to remote', () async {