Skip to content

Commit

Permalink
feat(devtools): Use Chrome DevTools Performance extension API
Browse files Browse the repository at this point in the history
Incorporate feedback form previous commit:

Remove listeners when component is distroyed
Rename track name to Angular DevTools
Use optional chaining deeper in chrome devtools performance namespace
  • Loading branch information
and-oli committed Jun 7, 2024
1 parent bafbe0d commit 225535e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
14 changes: 11 additions & 3 deletions devtools/projects/ng-devtools-backend/src/lib/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const endMark = (nodeName: string, method: Method) => {
extensionName: 'Angular',
},
color: 'primary',
track: '🅰️ Angular Extension',
track: '🅰️ Angular DevTools',
},
},
};
Expand All @@ -58,9 +58,17 @@ const endMark = (nodeName: string, method: Method) => {
};

let timingAPIFlag = false;
let lastTimingAPIFlagStatus = false;

export const enableTimingAPI = () => (timingAPIFlag = true);
export const disableTimingAPI = () => (timingAPIFlag = false);
export const enableTimingAPI = () => {
lastTimingAPIFlagStatus = timingAPIFlag;
timingAPIFlag = true;
};
// If the recording was stopped from Chrome profiler (Performance panel)
// restore keep the value of the timings API flag as the user had set it
// in Angular DevTools before the recording started.
export const disableTimingAPI = (forBrowserProfiler: boolean = false) =>
(timingAPIFlag = forBrowserProfiler && lastTimingAPIFlagStatus);

const timingAPIEnabled = () => timingAPIFlag;

Expand Down
7 changes: 6 additions & 1 deletion devtools/projects/protocol/src/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,12 @@ export interface Events {
removeComponentHighlight: () => void;

enableTimingAPI: () => void;
disableTimingAPI: () => void;
// forBrowserProfiler is used to determine if the event is dispatched
// because the Chrome profiler (Performance panel) stopped recording
// In this case, we need to ensure we keep the value of the
// timings API flag as the user had set it in Angular DevTools before
// the recording started.
disableTimingAPI: (forBrowserProfiler: boolean) => void;

// todo: type properly
getInjectorProviders: (injector: SerializedInjector) => void;
Expand Down
24 changes: 15 additions & 9 deletions devtools/projects/shell-browser/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,29 @@ import {Events, MessageBus} from 'protocol';
export class AppComponent implements OnInit {
private _cd = inject(ChangeDetectorRef);
private readonly _messageBus = inject<MessageBus<Events>>(MessageBus);

private onProfilingStartedListener = (() => {
this._messageBus.emit('enableTimingAPI');
}).bind(this);
private onProfilingStoppedListener = (() => {
this._messageBus.emit('disableTimingAPI');
}).bind(this);
ngOnInit(): void {
chrome.devtools.network.onNavigated.addListener(() => {
window.location.reload();
});
// At the moment the chrome.devtools.performance namespace does not
// have an entry in DefinitelyTyped so this is a temporary
// workaround to prevent TypeScript failures while the cooresponding
// have an entry in DefinitelyTyped, so this is a temporary
// workaround to prevent TypeScript failures while the corresponding
// type is added upstream.
const chromeDevToolsPerformance = (chrome.devtools as any).performance;
chromeDevToolsPerformance?.onProfilingStarted.addListener(() => {
this._messageBus.emit('enableTimingAPI');
});
chromeDevToolsPerformance?.onProfilingStopped.addListener(() => {
this._messageBus.emit('disableTimingAPI');
});
chromeDevToolsPerformance?.onProfilingStarted?.addListener?.(this.onProfilingStartedListener);
chromeDevToolsPerformance?.onProfilingStopped?.addListener?.(this.onProfilingStoppedListener);

this._cd.detectChanges();
}
ngOnDestroy(): void {
const chromeDevToolsPerformance = (chrome.devtools as any).performance;
chromeDevToolsPerformance?.onProfilingStarted?.addListener?.(this.onProfilingStartedListener);
chromeDevToolsPerformance?.onProfilingStopped?.addListener?.(this.onProfilingStoppedListener);
}
}

0 comments on commit 225535e

Please sign in to comment.