2023-08-08 02:03:26 -04:00
|
|
|
import 'dart:async';
|
|
|
|
|
2023-09-30 21:22:12 -04:00
|
|
|
import 'package:flutter/foundation.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
|
|
|
|
2023-12-27 22:56:24 -05:00
|
|
|
import 'init.dart';
|
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 {
|
2023-08-08 02:03:26 -04:00
|
|
|
const BackgroundTicker({required this.builder, super.key});
|
|
|
|
|
|
|
|
final Widget Function(BuildContext) builder;
|
|
|
|
|
|
|
|
@override
|
|
|
|
BackgroundTickerState createState() => BackgroundTickerState();
|
2023-09-29 22:45:50 -04:00
|
|
|
@override
|
|
|
|
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
|
|
|
|
super.debugFillProperties(properties);
|
2023-09-30 21:22:12 -04:00
|
|
|
properties.add(ObjectFlagProperty<Widget Function(BuildContext p1)>.has(
|
|
|
|
'builder', builder));
|
2023-09-29 22:45:50 -04:00
|
|
|
}
|
2023-08-08 02:03:26 -04:00
|
|
|
}
|
|
|
|
|
2023-12-26 20:26:54 -05:00
|
|
|
class BackgroundTickerState extends State<BackgroundTicker> {
|
2023-08-08 02:03:26 -04:00
|
|
|
Timer? _tickTimer;
|
|
|
|
bool _inTick = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_tickTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
|
|
|
if (!_inTick) {
|
|
|
|
unawaited(_onTick());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
final tickTimer = _tickTimer;
|
|
|
|
if (tickTimer != null) {
|
|
|
|
tickTimer.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
// ignore: prefer_expression_function_bodies
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return widget.builder(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _onTick() async {
|
2024-01-04 22:29:43 -05:00
|
|
|
// Don't tick until we are initialized
|
|
|
|
if (!eventualInitialized.isCompleted) {
|
2023-09-26 18:46:02 -04:00
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
2023-08-08 02:03:26 -04:00
|
|
|
_inTick = true;
|
|
|
|
try {
|
2024-01-04 22:29:43 -05:00
|
|
|
// Tick DHT record pool
|
2024-02-28 20:32:37 -05:00
|
|
|
unawaited(DHTRecordPool.instance.tick());
|
2023-08-08 02:03:26 -04:00
|
|
|
} finally {
|
|
|
|
_inTick = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|