Skip to content

Commit 99cc8a2

Browse files
committed
fix(components): nest components checked inside a hook under that hook
A component checked while a hook ran (a RouterOutlet's ngOnInit, say) got its span depth from the component stack alone, so it shared a row with the hook in the flame graph and nested wrongly in the Perfetto trace. Component spans now count the open hooks, as hook spans do. Refs #341
1 parent ff656c2 commit 99cc8a2

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎packages/devtools/src/__tests__/cd-timing.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,36 @@ describe('lifecycle hook timing', () => {
9696
expect(result.components[0]).toMatchObject({ name: 'Shell', ms: 4.5 });
9797
});
9898

99+
it('nests a component checked inside a running hook under that hook', () => {
100+
const { clock, recorder, snap } = recorderAt();
101+
const shell = new Shell();
102+
const row = new Row();
103+
const outlet = new Row();
104+
recorder.onEvent(E.ChangeDetectionStart);
105+
recorder.onEvent(E.ComponentStart);
106+
clock.advance(0.5);
107+
recorder.onEvent(E.LifecycleHookStart, outlet, Row.prototype.ngOnInit);
108+
clock.advance(1);
109+
recorder.onEvent(E.ComponentStart);
110+
clock.advance(2);
111+
recorder.onEvent(E.ComponentEnd, row);
112+
recorder.onEvent(E.LifecycleHookEnd, outlet, Row.prototype.ngOnInit);
113+
recorder.onEvent(E.ComponentEnd, shell);
114+
recorder.onEvent(E.ChangeDetectionEnd);
115+
const result = snap();
116+
const names = result.spanNames!;
117+
const depths = result.cycles[0]!.spans!.map(([name, , , depth, kind]) => [
118+
names[name],
119+
depth,
120+
kind,
121+
]);
122+
expect(depths).toEqual([
123+
['Shell', 0, 'c'],
124+
['Row.ngOnInit', 1, 'h'],
125+
['Row', 2, 'c'],
126+
]);
127+
});
128+
99129
it('counts a hook run by another hook once in the cycle total', () => {
100130
const { clock, recorder, snap } = recorderAt();
101131
const row = new Row();

‎packages/devtools/src/cd-recorder.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ export function createCdRecorder(options: {
304304
label: '',
305305
at: frame.start,
306306
ms: total,
307-
depth: stack.length,
307+
// A component checked inside a running hook (a RouterOutlet's
308+
// ngOnInit, say) nests under that hook, as hook spans do.
309+
depth: stack.length + hookStack.length,
308310
kind: CD_SPAN_KIND.component,
309311
});
310312
const check = current.checks.get(ctor);

0 commit comments

Comments
 (0)