diff --git a/packages/flutter_tools/lib/src/base/utils.dart b/packages/flutter_tools/lib/src/base/utils.dart index 08db07cd4f3..f9901c739e3 100644 --- a/packages/flutter_tools/lib/src/base/utils.dart +++ b/packages/flutter_tools/lib/src/base/utils.dart @@ -8,7 +8,6 @@ import 'dart:math' show Random, max; import 'package:intl/intl.dart'; import '../convert.dart'; -import '../globals.dart'; import 'context.dart'; import 'file_system.dart'; import 'io.dart' as io; @@ -258,45 +257,6 @@ Map castStringKeyedMap(dynamic untyped) { typedef AsyncCallback = Future Function(); -/// A [Timer] inspired class that: -/// - has a different initial value for the first callback delay -/// - waits for a callback to be complete before it starts the next timer -class Poller { - Poller(this.callback, this.pollingInterval, { this.initialDelay = Duration.zero }) { - Future.delayed(initialDelay, _handleCallback); - } - - final AsyncCallback callback; - final Duration initialDelay; - final Duration pollingInterval; - - bool _canceled = false; - Timer _timer; - - Future _handleCallback() async { - if (_canceled) { - return; - } - - try { - await callback(); - } catch (error) { - printTrace('Error from poller: $error'); - } - - if (!_canceled) { - _timer = Timer(pollingInterval, _handleCallback); - } - } - - /// Cancels the poller. - void cancel() { - _canceled = true; - _timer?.cancel(); - _timer = null; - } -} - /// Returns a [Future] that completes when all given [Future]s complete. /// /// Uses [Future.wait] but removes null elements from the provided diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index f9ee5404741..8c2bf96329b 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -251,28 +251,32 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery { final String name; ItemListNotifier _items; - Poller _poller; + Timer _timer; Future> pollingGetDevices(); void startPolling() { - if (_poller == null) { + if (_timer == null) { _items ??= ItemListNotifier(); - - _poller = Poller(() async { - try { - final List devices = await pollingGetDevices().timeout(_pollingTimeout); - _items.updateWithNewList(devices); - } on TimeoutException { - printTrace('Device poll timed out. Will retry.'); - } - }, _pollingInterval); + _timer = _initTimer(); } } + Timer _initTimer() { + return Timer(_pollingInterval, () async { + try { + final List devices = await pollingGetDevices().timeout(_pollingTimeout); + _items.updateWithNewList(devices); + } on TimeoutException { + printTrace('Device poll timed out. Will retry.'); + } + _timer = _initTimer(); + }); + } + void stopPolling() { - _poller?.cancel(); - _poller = null; + _timer?.cancel(); + _timer = null; } @override diff --git a/packages/flutter_tools/test/general.shard/utils_test.dart b/packages/flutter_tools/test/general.shard/utils_test.dart index b74b47f9769..509a8aa8ca2 100644 --- a/packages/flutter_tools/test/general.shard/utils_test.dart +++ b/packages/flutter_tools/test/general.shard/utils_test.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:async'; - import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/utils.dart'; import 'package:flutter_tools/src/base/version.dart'; @@ -122,56 +120,6 @@ baz=qux }); }); - group('Poller', () { - const Duration kShortDelay = Duration(milliseconds: 100); - - Poller poller; - - tearDown(() { - poller?.cancel(); - }); - - test('fires at start', () async { - bool called = false; - poller = Poller(() async { - called = true; - }, const Duration(seconds: 1)); - expect(called, false); - await Future.delayed(kShortDelay); - expect(called, true); - }); - - test('runs periodically', () async { - // Ensure we get the first (no-delay) callback, and one of the periodic callbacks. - int callCount = 0; - poller = Poller(() async { - callCount++; - }, Duration(milliseconds: kShortDelay.inMilliseconds ~/ 2)); - expect(callCount, 0); - await Future.delayed(kShortDelay); - expect(callCount, greaterThanOrEqualTo(2)); - }); - - test('no quicker then the periodic delay', () async { - // Make sure that the poller polls at delay + the time it took to run the callback. - final Completer completer = Completer(); - DateTime firstTime; - poller = Poller(() async { - if (firstTime == null) { - firstTime = DateTime.now(); - } else { - completer.complete(DateTime.now().difference(firstTime)); - } - - // introduce a delay - await Future.delayed(kShortDelay); - }, kShortDelay); - final Duration duration = await completer.future; - expect( - duration, greaterThanOrEqualTo(Duration(milliseconds: kShortDelay.inMilliseconds * 2))); - }); - }); - group('Misc', () { test('snakeCase', () async { expect(snakeCase('abc'), equals('abc'));