From 46c37e50b48d101f223063bf6b24affc230bfa57 Mon Sep 17 00:00:00 2001 From: Shuaib Hasan Akib Date: Tue, 29 Sep 2026 02:25:19 +0600 Subject: [PATCH] fix(devtools): clean up profiler component subscriptions on destroy Cleans up `uploadedData` and message bus subscriptions when the profiler is destroyed to prevent duplicate error dialogs and stale profiler results from accumulating across profiler instances. Adds coverage for subscription cleanup on destroy. --- .../lib/devtools-tabs/profiler/BUILD.bazel | 25 +++++- .../profiler/profiler.component.spec.ts | 79 +++++++++++++++++++ .../profiler/profiler.component.ts | 23 ++++-- 3 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/profiler.component.spec.ts diff --git a/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/BUILD.bazel b/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/BUILD.bazel index a5d5aa696de..4ff6c51ff82 100644 --- a/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/BUILD.bazel +++ b/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/BUILD.bazel @@ -1,4 +1,4 @@ -load("//devtools/tools:defaults.bzl", "ng_project", "sass_binary") +load("//devtools/tools:defaults.bzl", "ng_project", "sass_binary", "ts_test_library", "zoneless_web_test_suite") package(default_visibility = ["//devtools:__subpackages__"]) @@ -30,3 +30,26 @@ ng_project( "//devtools/projects/protocol", ], ) + +ts_test_library( + name = "profiler_test", + srcs = [ + "profiler.component.spec.ts", + ], + visibility = [ + "//visibility:private", + ], + deps = [ + ":profiler", + "//:node_modules/@angular/core", + "//:node_modules/@angular/material", + "//devtools/projects/protocol", + ], +) + +zoneless_web_test_suite( + name = "test", + deps = [ + ":profiler_test", + ], +) diff --git a/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/profiler.component.spec.ts b/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/profiler.component.spec.ts new file mode 100644 index 00000000000..43ca8100287 --- /dev/null +++ b/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/profiler.component.spec.ts @@ -0,0 +1,79 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.dev/license + */ + +import {ComponentFixture, TestBed} from '@angular/core/testing'; +import {MatDialog} from '@angular/material/dialog'; + +import {Events, MessageBus} from '../../../../../protocol'; +import {FileApiService} from './file-api-service'; +import {ProfilerComponent} from './profiler.component'; + +class MessageBusMock implements MessageBus { + readonly emit = jasmine.createSpy('emit'); + readonly once = jasmine.createSpy('once'); + readonly destroy = jasmine.createSpy('destroy'); + private listeners = new Map>(); + + on = jasmine.createSpy('on').and.callFake((topic: keyof Events, cb: Function) => { + let topicCbs = this.listeners.get(topic); + if (!topicCbs) { + topicCbs = new Set(); + this.listeners.set(topic, topicCbs); + } + topicCbs.add(cb); + return () => topicCbs.delete(cb); + }); + + hasListener(topic: keyof Events): boolean { + return (this.listeners.get(topic)?.size ?? 0) > 0; + } +} + +describe('ProfilerComponent', () => { + let messageBus: MessageBusMock; + let dialog: jasmine.SpyObj; + let fixture: ComponentFixture; + + beforeEach(async () => { + messageBus = new MessageBusMock(); + dialog = jasmine.createSpyObj('MatDialog', ['open']); + + TestBed.configureTestingModule({ + imports: [ProfilerComponent], + providers: [ + {provide: MessageBus, useValue: messageBus}, + {provide: MatDialog, useValue: dialog}, + ], + }); + + fixture = TestBed.createComponent(ProfilerComponent); + await fixture.whenStable(); + }); + + it('should unregister the profiler message bus listeners on destroy', () => { + expect(messageBus.hasListener('profilerResults')).toBeTrue(); + expect(messageBus.hasListener('sendProfilerChunk')).toBeTrue(); + + fixture.destroy(); + + expect(messageBus.hasListener('profilerResults')).toBeFalse(); + expect(messageBus.hasListener('sendProfilerChunk')).toBeFalse(); + }); + + it('should stop handling uploaded files once destroyed', () => { + const fileApiService = TestBed.inject(FileApiService); + + fileApiService.uploadedData.next({error: new Error('Invalid file')}); + expect(dialog.open).toHaveBeenCalledTimes(1); + + fixture.destroy(); + fileApiService.uploadedData.next({error: new Error('Invalid file')}); + + expect(dialog.open).toHaveBeenCalledTimes(1); + }); +}); diff --git a/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/profiler.component.ts b/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/profiler.component.ts index 0edbfe65f04..44ff132b4bc 100644 --- a/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/profiler.component.ts +++ b/devtools/projects/ng-devtools/src/lib/devtools-tabs/profiler/profiler.component.ts @@ -6,7 +6,8 @@ * found in the LICENSE file at https://angular.dev/license */ -import {Component, inject, signal} from '@angular/core'; +import {Component, DestroyRef, inject, signal} from '@angular/core'; +import {takeUntilDestroyed} from '@angular/core/rxjs-interop'; import {MatDialog} from '@angular/material/dialog'; import {MatIcon} from '@angular/material/icon'; import {MatTooltip} from '@angular/material/tooltip'; @@ -42,7 +43,9 @@ export class ProfilerComponent { public dialog = inject(MatDialog); constructor() { - this._fileApiService.uploadedData.subscribe((importedFile) => { + // `FileApiService` is provided in root and outlives this component, which is destroyed and + // recreated when another frame is inspected. + this._fileApiService.uploadedData.pipe(takeUntilDestroyed()).subscribe((importedFile) => { if (importedFile.error) { console.error('Could not process uploaded file'); console.error(importedFile.error); @@ -84,16 +87,24 @@ export class ProfilerComponent { } }); - this._messageBus.on('profilerResults', (remainingRecords) => { + const unlistenProfilerResults = this._messageBus.on('profilerResults', (remainingRecords) => { if (remainingRecords.duration > 0 && remainingRecords.source) { this.stream.next([remainingRecords]); this._buffer.push(remainingRecords); } }); - this._messageBus.on('sendProfilerChunk', (chunkOfRecords: ProfilerFrame) => { - this.stream.next([chunkOfRecords]); - this._buffer.push(chunkOfRecords); + const unlistenProfilerChunk = this._messageBus.on( + 'sendProfilerChunk', + (chunkOfRecords: ProfilerFrame) => { + this.stream.next([chunkOfRecords]); + this._buffer.push(chunkOfRecords); + }, + ); + + inject(DestroyRef).onDestroy(() => { + unlistenProfilerResults(); + unlistenProfilerChunk(); }); }