diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index 5778dd03..b7ad172c 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -80,6 +80,10 @@ class _PipelinesAppState extends State /// list once; the run itself is then opened from its route. int _runsRefresh = 0; + /// Prevents duplicate resumed notifications from refreshing twice before + /// the next background transition starts another foreground visit. + bool _foregroundVisitHandled = false; + /// One run detail request from Search, delivered through the shell so the /// mounted run detail can resolve it once the named run's steps have loaded. /// A field or artifact hit names no addressable URL of its own, so the request @@ -198,9 +202,29 @@ class _PipelinesAppState extends State /// 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()); + if (state != AppLifecycleState.resumed) { + _foregroundVisitHandled = false; + return; + } + if (_foregroundVisitHandled) return; + _foregroundVisitHandled = true; + final connection = _session.connection.kind; + if (shouldReconnectOnForeground(connection)) { + unawaited(_session.retry()); + return; + } + if (connection != ConnectionKind.connected) return; + + // A return to the foreground refreshes only the visible list. Detail routes + // (including pipeline definitions and run visits) keep their current data. + final route = _delegate.route; + if (route.kind == AppRouteKind.pipelines && + route.pipelineId == null && + route.stageKey == null) { + setState(() => _pipelinesRefresh++); + } else if (route.kind == AppRouteKind.runs && route.runId == null) { + setState(() => _runsRefresh++); + } } /// A replaced socket drops the attention status baseline so the gap cannot diff --git a/app/test/application/app_lifecycle_reconnect_test.dart b/app/test/application/app_lifecycle_reconnect_test.dart index 73cb9962..cdff20b4 100644 --- a/app/test/application/app_lifecycle_reconnect_test.dart +++ b/app/test/application/app_lifecycle_reconnect_test.dart @@ -48,6 +48,9 @@ List types(FakeChannel channel) => [ (jsonDecode(frame as String) as Map)['type'] as String, ]; +int countType(FakeChannel channel, String type) => + types(channel).where((value) => value == type).length; + void main() { tearDown(() { WidgetsBinding.instance.handleAppLifecycleStateChanged( @@ -100,21 +103,75 @@ void main() { }, ); - testWidgets('foreground resume does not churn a still-connected socket', ( + testWidgets('foreground resume refreshes the visible Pipelines list once', ( tester, ) async { final channel = FakeChannel()..readyCompleter.complete(); final client = PipelinesClient(channelFactory: (_) => channel); await pumpApp(tester, provider: routeProvider(), client: client); await replyPipelines(tester, channel, PipelineFixture()); + final before = countType(channel, 'list_pipelines'); expect(types(channel).where((type) => type == 'subscribe'), hasLength(1)); await resume(tester); + expect(countType(channel, 'list_pipelines'), before + 1); expect(types(channel).where((type) => type == 'subscribe'), hasLength(1)); expect(client.state.kind, ConnectionKind.connected); await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); }); + + testWidgets('foreground resume refreshes the visible Runs list once', ( + tester, + ) async { + final channel = FakeChannel()..readyCompleter.complete(); + final provider = PlatformRouteInformationProvider( + initialRouteInformation: RouteInformation(uri: Uri.parse('/runs')), + ); + addTearDown(provider.dispose); + final client = PipelinesClient(channelFactory: (_) => channel); + await pumpApp(tester, provider: provider, client: client); + await waitForRequest(tester, channel, 'list_runs'); + final initialRuns = pipelineRequest(channel, 'list_runs')!; + channel.incoming.add( + jsonEncode({ + 'id': initialRuns['id'], + 'type': 'runs', + 'runs': PipelineFixture().runs, + }), + ); + await tester.pump(); + final before = countType(channel, 'list_runs'); + + await resume(tester); + + expect(countType(channel, 'list_runs'), before + 1); + expect(countType(channel, 'subscribe'), 1); + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pump(); + }); + + testWidgets('foreground resume does not refresh an open run detail', ( + tester, + ) async { + final channel = FakeChannel()..readyCompleter.complete(); + final provider = routeProvider(); + final client = PipelinesClient(channelFactory: (_) => channel); + await pumpApp(tester, provider: provider, client: client); + await replyPipelines(tester, channel, PipelineFixture()); + provider.routerReportsNewRouteInformation( + RouteInformation(uri: Uri.parse('/runs/1')), + type: RouteInformationReportingType.navigate, + ); + await tester.pump(); + final before = countType(channel, 'list_runs'); + + await resume(tester); + + expect(countType(channel, 'list_runs'), before); + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pump(); + }); }