@@ -10,6 +10,7 @@ type ProfilerCallback = (event: ɵProfilerEvent, instanceOrLView: {}, hookOrList
1010export class NgProfiler extends Profiler {
1111 private _tracker = IdentityTracker . getInstance ( ) ;
1212 private _callbacks : ProfilerCallback [ ] = [ ] ;
13+ private _lastDirectiveInstance : { } | null = null ;
1314
1415 constructor ( config : Partial < Hooks > = { } ) {
1516 super ( config ) ;
@@ -66,7 +67,7 @@ export class NgProfiler extends Profiler {
6667 return ;
6768 }
6869
69- [ ɵProfilerEvent . TemplateUpdateStart ] ( directive : any , _hookOrListener : any ) : void {
70+ [ ɵProfilerEvent . TemplateUpdateStart ] ( context : any , _hookOrListener : any ) : void {
7071 if ( ! this . _inChangeDetection ) {
7172 this . _inChangeDetection = true ;
7273 runOutsideAngular ( ( ) => {
@@ -77,19 +78,44 @@ export class NgProfiler extends Profiler {
7778 } ) ;
7879 }
7980
80- const position = this . _tracker . getDirectivePosition ( directive ) ;
81- const id = this . _tracker . getDirectiveId ( directive ) ;
81+ const position = this . _tracker . getDirectivePosition ( context ) ;
82+ const id = this . _tracker . getDirectiveId ( context ) ;
83+
84+ // If we can find the position and the ID we assume that this is a component instance.
85+ // Alternatively, if we can't find the ID or the position, we assume that this is a
86+ // context of an embedded view (for example, NgForOfContext, NgIfContext, or a custom one).
87+ if ( position !== undefined && id !== undefined ) {
88+ this . _lastDirectiveInstance = context ;
89+ }
90+
91+ if ( id !== undefined && position !== undefined ) {
92+ this . _onChangeDetectionStart ( context , getDirectiveHostElement ( context ) , id , position ) ;
93+ return ;
94+ }
8295
83- this . _onChangeDetectionStart ( directive , getDirectiveHostElement ( directive ) , id , position ) ;
96+ this . _onChangeDetectionStart (
97+ this . _lastDirectiveInstance ,
98+ getDirectiveHostElement ( this . _lastDirectiveInstance ) ,
99+ this . _tracker . getDirectiveId ( this . _lastDirectiveInstance ) ,
100+ this . _tracker . getDirectivePosition ( this . _lastDirectiveInstance )
101+ ) ;
84102 }
85103
86- [ ɵProfilerEvent . TemplateUpdateEnd ] ( directive : any , _hookOrListener : any ) : void {
87- const position = this . _tracker . getDirectivePosition ( directive ) ;
88- const id = this . _tracker . getDirectiveId ( directive ) ;
104+ [ ɵProfilerEvent . TemplateUpdateEnd ] ( context : any , _hookOrListener : any ) : void {
105+ const position = this . _tracker . getDirectivePosition ( context ) ;
106+ const id = this . _tracker . getDirectiveId ( context ) ;
89107
90- if ( this . _tracker . hasDirective ( directive ) && id !== undefined && position !== undefined ) {
91- this . _onChangeDetectionEnd ( directive , getDirectiveHostElement ( directive ) , id , position ) ;
108+ if ( this . _tracker . hasDirective ( context ) && id !== undefined && position !== undefined ) {
109+ this . _onChangeDetectionEnd ( context , getDirectiveHostElement ( context ) , id , position ) ;
110+ return ;
92111 }
112+
113+ this . _onChangeDetectionEnd (
114+ this . _lastDirectiveInstance ,
115+ getDirectiveHostElement ( this . _lastDirectiveInstance ) ,
116+ this . _tracker . getDirectiveId ( this . _lastDirectiveInstance ) ,
117+ this . _tracker . getDirectivePosition ( this . _lastDirectiveInstance )
118+ ) ;
93119 }
94120
95121 [ ɵProfilerEvent . LifecycleHookStart ] ( directive : any , hook : any ) : void {
0 commit comments