From e91c2898a660f6281c15007990488a40c93ae8ec Mon Sep 17 00:00:00 2001 From: Polina Cherkasova Date: Fri, 26 Apr 2024 16:34:03 -0700 Subject: [PATCH] Update profile_pane_controller.dart --- .../profile/profile_pane_controller.dart | 68 +++++++++++-------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/packages/devtools_app/lib/src/screens/memory/panes/profile/profile_pane_controller.dart b/packages/devtools_app/lib/src/screens/memory/panes/profile/profile_pane_controller.dart index 985de55b890..edd52a6e129 100644 --- a/packages/devtools_app/lib/src/screens/memory/panes/profile/profile_pane_controller.dart +++ b/packages/devtools_app/lib/src/screens/memory/panes/profile/profile_pane_controller.dart @@ -19,10 +19,16 @@ const _jsonProfile = 'profile'; class ProfilePaneController extends DisposableController with AutoDisposeControllerMixin { ProfilePaneController({required this.mode, AdaptedProfile? profile}) - : assert(profile == null || mode != DevToolsMode.connected) { + : assert( + (mode == DevToolsMode.connected && profile == null) || + (mode == DevToolsMode.offlineData && profile != null), + ) { if (profile != null) { - _currentAllocationProfile.value = profile; - _initializeSelection(); + _currentAllocationProfile.value = AdaptedProfile.withNewFilter( + profile, + classFilter.value, + _rootPackage, + ); } } @@ -45,6 +51,14 @@ class ProfilePaneController extends DisposableController ValueListenable get currentAllocationProfile => _currentAllocationProfile; final _currentAllocationProfile = ValueNotifier(null); + void _setProfile(AllocationProfile profile) { + _currentAllocationProfile.value = AdaptedProfile.fromAllocationProfile( + profile, + classFilter.value, + _rootPackage, + ); + _initializeSelection(); + } /// Specifies if the allocation profile should be refreshed when a GC event /// is received. @@ -73,28 +87,32 @@ class ProfilePaneController extends DisposableController late final _rootPackage = serviceConnection.serviceManager.rootInfoNow().package; - bool _initialized = false; + final Completer _initialized = Completer(); void initialize() { - if (_initialized) { + if (_initialized.isCompleted) { return; } - - autoDisposeStreamSubscription( - serviceConnection.serviceManager.service!.onGCEvent.listen((event) { - if (refreshOnGc.value) { + if (mode == DevToolsMode.connected) { + autoDisposeStreamSubscription( + serviceConnection.serviceManager.service!.onGCEvent.listen((event) { + if (refreshOnGc.value) { + unawaited(refresh()); + } + }), + ); + addAutoDisposeListener( + serviceConnection.serviceManager.isolateManager.selectedIsolate, + () { unawaited(refresh()); - } - }), - ); - addAutoDisposeListener( - serviceConnection.serviceManager.isolateManager.selectedIsolate, - () { - unawaited(refresh()); - }, - ); - unawaited(refresh()); - _initialized = true; + }, + ); + unawaited(refresh()); + } + + _initializeSelection(); + + _initialized.complete(); } @visibleForTesting @@ -111,6 +129,7 @@ class ProfilePaneController extends DisposableController /// Clear the current allocation profile and request an updated version from /// the VM service. Future refresh() async { + print('!!! refreshing'); final service = serviceConnection.serviceManager.service; if (service == null) return; _currentAllocationProfile.value = null; @@ -119,13 +138,8 @@ class ProfilePaneController extends DisposableController serviceConnection.serviceManager.isolateManager.selectedIsolate.value; if (isolate == null) return; - final allocationProfile = await service.getAllocationProfile(isolate.id!); - _currentAllocationProfile.value = AdaptedProfile.fromAllocationProfile( - allocationProfile, - classFilter.value, - _rootPackage, - ); - _initializeSelection(); + final profile = await service.getAllocationProfile(isolate.id!); + _setProfile(profile); } void _initializeSelection() {