Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
polina-c committed Apr 23, 2024
1 parent c8ed523 commit 33e06dd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:async';

import 'package:devtools_app_shared/service.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/foundation.dart';

Expand All @@ -12,6 +13,8 @@ import '../../../shared/primitives/memory_timeline.dart';
import '../data/primitives.dart';
import 'memory_tracker.dart';

typedef _AsyncVoidCallback = Future<void> Function();

/// Connection between chart and application.
///
/// The connection consists of listeners to events from vm and
Expand All @@ -38,28 +41,59 @@ class ChartConnection extends DisposableController
Timer? _pollingTimer;
bool _connected = false;

late final isDeviceAndroid =
serviceConnection.serviceManager.vm?.operatingSystem == 'android';
/// If true, the connection to application was lost.
bool _disconnected = false;

late final isDeviceAndroid = _isDevToolsCurrentlyConnected()
? serviceConnection.serviceManager.vm?.operatingSystem == 'android'
: false;

/// True if DevTools is in connected mode and the connection to the app is alive.
bool _isDevToolsCurrentlyConnected() =>
!offlineDataController.showingOfflineData.value &&
serviceConnection.serviceManager.connectedState.value.connected &&
serviceConnection.serviceManager.connectedApp != null;

Future<void> maybeConnect() async {
if (_connected) return;
await serviceConnection.serviceManager.onServiceAvailable;
autoDisposeStreamSubscription(
serviceConnection.serviceManager.service!.onExtensionEvent
.listen(_memoryTracker.onMemoryData),
);
autoDisposeStreamSubscription(
serviceConnection.serviceManager.service!.onGCEvent
.listen(_memoryTracker.onGCEvent),
);
await _onPoll();
if (_connected || _disconnected) return;
_connected = true;
await _runSafely(() async {
await serviceConnection.serviceManager.onServiceAvailable;
autoDisposeStreamSubscription(
serviceConnection.serviceManager.service!.onExtensionEvent
.listen(_memoryTracker.onMemoryData),
);
autoDisposeStreamSubscription(
serviceConnection.serviceManager.service!.onGCEvent
.listen(_memoryTracker.onGCEvent),
);
await _onPoll();
});
}

Future<void> _onPoll() async {
_pollingTimer = null;
await _memoryTracker.pollMemory();
_pollingTimer = Timer(chartUpdateDelay, _onPoll);
assert(_connected);
if (_disconnected) return;
await _runSafely(() async {
_pollingTimer = null;
await _memoryTracker.pollMemory();
_pollingTimer = Timer(chartUpdateDelay, _onPoll);
});
}

/// Run callback resiliently to disconnect.
Future<void> _runSafely(_AsyncVoidCallback callback) async {
if (_disconnected) return;
try {
await callback();
} catch (e) {
if (_isDevToolsCurrentlyConnected()) {
rethrow;
} else {
_disconnected = true;
_pollingTimer?.cancel();
}
}
}

@override
Expand Down
4 changes: 2 additions & 2 deletions packages/devtools_app/lib/src/service/service_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ class ServiceConnectionManager {
/// layerBytes - layer raster cache entries in bytes
/// pictureBytes - picture raster cache entries in bytes
Future<Response?> get rasterCacheMetrics async {
if (serviceManager.connectedApp == null ||
!await serviceManager.connectedApp!.isFlutterApp) {
final app = serviceManager.connectedApp;
if (app == null || !await app.isFlutterApp) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ class ConnectedApp {
String? _operatingSystem;

// TODO(kenz): investigate if we can use `libraryUriAvailableNow` instead.
Future<bool> get isFlutterApp async => _isFlutterApp ??=
await serviceManager!.libraryUriAvailable(flutterLibraryUri);
Future<bool> get isFlutterApp async =>
(_isFlutterApp ??=
await serviceManager?.libraryUriAvailable(flutterLibraryUri)) ??
false;

bool? get isFlutterAppNow {
assert(_isFlutterApp != null);
Expand Down

0 comments on commit 33e06dd

Please sign in to comment.