32 lines
625 B
Dart
32 lines
625 B
Dart
import 'dart:async';
|
|
|
|
class PrefetchDebouncer {
|
|
PrefetchDebouncer({
|
|
required this.onPrefetch,
|
|
this.delay = const Duration(milliseconds: 700),
|
|
});
|
|
|
|
final Future<void> Function(String host) onPrefetch;
|
|
final Duration delay;
|
|
|
|
Timer? _timer;
|
|
bool _disposed = false;
|
|
|
|
void schedule(String rawHost) {
|
|
if (_disposed) return;
|
|
_timer?.cancel();
|
|
final host = rawHost.trim();
|
|
if (host.isEmpty) return;
|
|
_timer = Timer(delay, () {
|
|
if (_disposed) return;
|
|
unawaited(onPrefetch(host));
|
|
});
|
|
}
|
|
|
|
void dispose() {
|
|
_disposed = true;
|
|
_timer?.cancel();
|
|
_timer = null;
|
|
}
|
|
}
|