Skip to content

Commit

Permalink
Update profile_pane_controller.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
polina-c committed Apr 26, 2024
1 parent 980af0d commit e91c289
Showing 1 changed file with 41 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}

Expand All @@ -45,6 +51,14 @@ class ProfilePaneController extends DisposableController
ValueListenable<AdaptedProfile?> get currentAllocationProfile =>
_currentAllocationProfile;
final _currentAllocationProfile = ValueNotifier<AdaptedProfile?>(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.
Expand Down Expand Up @@ -73,28 +87,32 @@ class ProfilePaneController extends DisposableController
late final _rootPackage =
serviceConnection.serviceManager.rootInfoNow().package;

bool _initialized = false;
final Completer<void> _initialized = Completer<void>();

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
Expand All @@ -111,6 +129,7 @@ class ProfilePaneController extends DisposableController
/// Clear the current allocation profile and request an updated version from
/// the VM service.
Future<void> refresh() async {
print('!!! refreshing');
final service = serviceConnection.serviceManager.service;
if (service == null) return;
_currentAllocationProfile.value = null;
Expand All @@ -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() {
Expand Down

0 comments on commit e91c289

Please sign in to comment.