veilidchat/lib/tick.dart

51 lines
1.1 KiB
Dart
Raw Normal View History

2023-08-08 02:03:26 -04:00
import 'dart:async';
import 'package:async_tools/async_tools.dart';
2023-08-08 02:03:26 -04:00
import 'package:flutter/material.dart';
2024-01-04 22:29:43 -05:00
import 'package:veilid_support/veilid_support.dart';
2023-08-08 02:03:26 -04:00
2024-01-04 22:29:43 -05:00
import 'veilid_processor/veilid_processor.dart';
2023-08-08 02:03:26 -04:00
2023-12-26 20:26:54 -05:00
class BackgroundTicker extends StatefulWidget {
2024-04-10 16:13:08 -04:00
const BackgroundTicker({required this.child, super.key});
2023-08-08 02:03:26 -04:00
2024-04-10 16:13:08 -04:00
final Widget child;
2023-08-08 02:03:26 -04:00
@override
BackgroundTickerState createState() => BackgroundTickerState();
}
2023-12-26 20:26:54 -05:00
class BackgroundTickerState extends State<BackgroundTicker> {
2023-08-08 02:03:26 -04:00
Timer? _tickTimer;
@override
void initState() {
super.initState();
_tickTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
singleFuture(this, _onTick);
2023-08-08 02:03:26 -04:00
});
}
@override
void dispose() {
_tickTimer?.cancel();
2023-08-08 02:03:26 -04:00
super.dispose();
}
@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
2024-04-10 16:13:08 -04:00
return widget.child;
2023-08-08 02:03:26 -04:00
}
Future<void> _onTick() async {
2024-01-04 22:29:43 -05:00
if (!ProcessorRepository
.instance.processorConnectionState.isPublicInternetReady) {
2023-10-21 19:23:43 -04:00
return;
}
2023-09-26 18:46:02 -04:00
// Tick DHT record pool
await DHTRecordPool.instance.tick();
2023-08-08 02:03:26 -04:00
}
}