Remove Poller class from flutter_tools (#43685)

This commit is contained in:
Jonah Williams
2019-10-28 19:35:43 -07:00
committed by GitHub
parent ea4da39fab
commit 19e551c7a6
3 changed files with 17 additions and 105 deletions

View File

@@ -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<String, dynamic> castStringKeyedMap(dynamic untyped) {
typedef AsyncCallback = Future<void> 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<void>.delayed(initialDelay, _handleCallback);
}
final AsyncCallback callback;
final Duration initialDelay;
final Duration pollingInterval;
bool _canceled = false;
Timer _timer;
Future<void> _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

View File

@@ -251,28 +251,32 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
final String name;
ItemListNotifier<Device> _items;
Poller _poller;
Timer _timer;
Future<List<Device>> pollingGetDevices();
void startPolling() {
if (_poller == null) {
if (_timer == null) {
_items ??= ItemListNotifier<Device>();
_poller = Poller(() async {
try {
final List<Device> 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<Device> 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