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);
@@ -11,6 +11,21 @@ import 'settings_store.dart';
/// the shared client's own `ping` request; tests inject a recorded callback.
typedef PingCallback = Future<Map<String, dynamic>> Function();
/// The integer wire protocol this app speaks. Mirrors `PROTOCOL_VERSION` in
/// `web/src/protocol.js` and `ProtocolVersion` in `internal/protocol/identity.go`.
const int clientProtocolVersion = 1;
/// The warning naming both values when the last `pong` reported a different
/// protocol. A server that omits the field (an older build) is never flagged,
/// and the values are compared as numbers, never as strings — the web's
/// `protocolMismatch` rule.
String? protocolMismatchWarning(num? serverProtocol) {
if (serverProtocol == null) return null;
if (serverProtocol == clientProtocolVersion) return null;
return 'Protocol mismatch: this page speaks protocol '
'$clientProtocolVersion, the server reports $serverProtocol.';
}
/// Owns the Settings screen's draft text, validation and the actions that drive
/// the shared [transport.PipelinesClient].
///
@@ -55,6 +70,7 @@ class SettingsController extends ChangeNotifier {
String _buildVersion = '';
String _buildCommit = '';
String _serverTime = '';
num? _serverProtocol;
String _pingError = '';
bool _pingInFlight = false;
bool _pingRequestedForConnected = false;
@@ -76,6 +92,12 @@ class SettingsController extends ChangeNotifier {
String get buildVersion => _buildVersion;
String get buildCommit => _buildCommit;
String get serverTime => _serverTime;
/// The warning for a last `pong` whose protocol differs from this app's.
/// The last reported value is kept across a drop (as the web Settings page
/// keeps `serverProtocol`), so a foreground reconnect cannot hide a known
/// mismatch while the new socket is still connecting.
String? get protocolWarning => protocolMismatchWarning(_serverProtocol);
String get pingError => _pingError;
bool get pingInFlight => _pingInFlight;
@@ -236,6 +258,7 @@ class SettingsController extends ChangeNotifier {
: '${build ?? ''}'.trim();
_buildCommit = build is Map ? '${build['commit'] ?? ''}'.trim() : '';
_serverTime = '${reply['time'] ?? ''}'.trim();
_serverProtocol = _reportedProtocol(reply['protocol']);
_pingError = '';
} catch (error) {
if (!_isCurrentPing(token)) return;
@@ -251,6 +274,16 @@ class SettingsController extends ChangeNotifier {
bool _isCurrentPing(int token) =>
!_disposed && token == _pingToken && _pingRequestedForConnected;
/// The `pong.protocol` value as the number the comparison needs. A missing,
/// blank or non-numeric value is absent — never coerced to zero, which would
/// invent a mismatch.
num? _reportedProtocol(Object? value) {
if (value is num) return value;
final text = '${value ?? ''}'.trim();
if (text.isEmpty) return null;
return num.tryParse(text);
}
void _clearPingDetails() {
_pingRequestedForConnected = false;
_pingInFlight = false;
@@ -258,6 +291,9 @@ class SettingsController extends ChangeNotifier {
_buildVersion = '';
_buildCommit = '';
_serverTime = '';
// _serverProtocol is deliberately retained: it belongs to the server the
// last pong described, and its warning must stay visible through the
// reconnect the app performs when it returns to the foreground.
}
String _describe(Object error) {
@@ -77,6 +77,18 @@ class SettingsConnectionSection extends StatelessWidget {
SettingsStatusCard(controller: controller),
const SizedBox(height: settingsTitleGap),
const SettingsTrustWarning(),
if (controller.protocolWarning case final warning?) ...<Widget>[
const SizedBox(height: settingsTitleGap),
// The web page appends the same alert after the trust card. A live
// region announces a mismatch the operator did not ask to reveal.
Semantics(
liveRegion: true,
child: Text(
warning,
style: settingsTextStyle(context, error: true),
),
),
],
],
);
}