feat(app): reconnect on foreground and keep the protocol warning

This commit is contained in:
2026-10-22 15:48:07 +01:00
parent 6b021f1483
commit 8b9a298dd1
6 changed files with 262 additions and 1 deletions
+27 -1
View File
@@ -24,6 +24,15 @@ import 'search_destination.dart';
import 'server_endpoint.dart';
import 'templates_destination.dart';
/// The application's return-to-foreground policy for the shared socket.
///
/// Only a socket the client dropped (disconnected) is reopened. Idle is a
/// deliberate Disconnect the operator made, so returning never undoes it, and
/// connected/connecting already have their own answer — foregrounding neither
/// churns a live socket nor restarts an attempt already in flight.
bool shouldReconnectOnForeground(ConnectionKind kind) =>
kind == ConnectionKind.disconnected;
/// Composes the live application around one [AppSession] and one [Router].
///
/// Tests may inject the client and route-information provider; the session
@@ -48,7 +57,8 @@ class PipelinesApp extends StatefulWidget {
State<PipelinesApp> createState() => _PipelinesAppState();
}
class _PipelinesAppState extends State<PipelinesApp> {
class _PipelinesAppState extends State<PipelinesApp>
with WidgetsBindingObserver {
late final AppSession _session;
late final AppRouterDelegate _delegate;
late final RouteInformationProvider _provider;
@@ -74,6 +84,7 @@ class _PipelinesAppState extends State<PipelinesApp> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_session = AppSession(endpoint: widget.server.uri, client: widget.client);
_ownsProvider = widget.routeInformationProvider == null;
_provider =
@@ -116,6 +127,7 @@ class _PipelinesAppState extends State<PipelinesApp> {
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_notifications.dispose();
_delegate.dispose();
if (_ownsProvider) {
@@ -125,6 +137,20 @@ class _PipelinesAppState extends State<PipelinesApp> {
super.dispose();
}
/// Returning to the foreground recovers a socket the client dropped while
/// the app was hidden. A deliberate Disconnect reaches idle and is left
/// alone, and a connected or connecting socket is not churned: only the
/// disconnected state reopens the validated endpoint now, cancelling the
/// pending backoff delay. The session's connected transition then re-sends
/// the send-only run watch (and Settings re-pings for its build/protocol
/// facts), so no screen re-subscribes on its own.
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state != AppLifecycleState.resumed) return;
if (!shouldReconnectOnForeground(_session.connection.kind)) return;
unawaited(_session.retry());
}
@override
Widget build(BuildContext context) =>
MyApp.router(cupertino: _cupertino, routerConfig: _config);