veilidchat/lib/tick.dart
Christien Rioux 8edccb8a0f fix deadlock
clean up async handling
improve styled alerts
2024-08-04 18:49:49 -05:00

51 lines
1.1 KiB
Dart

import 'dart:async';
import 'package:async_tools/async_tools.dart';
import 'package:flutter/material.dart';
import 'package:veilid_support/veilid_support.dart';
import 'veilid_processor/veilid_processor.dart';
class BackgroundTicker extends StatefulWidget {
const BackgroundTicker({required this.child, super.key});
final Widget child;
@override
BackgroundTickerState createState() => BackgroundTickerState();
}
class BackgroundTickerState extends State<BackgroundTicker> {
Timer? _tickTimer;
@override
void initState() {
super.initState();
_tickTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
singleFuture(this, _onTick);
});
}
@override
void dispose() {
_tickTimer?.cancel();
super.dispose();
}
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return widget.child;
}
Future<void> _onTick() async {
if (!ProcessorRepository
.instance.processorConnectionState.isPublicInternetReady) {
return;
}
// Tick DHT record pool
await DHTRecordPool.instance.tick();
}
}