@@ -10,6 +10,7 @@ type ProfilerCallback = (event: ɵProfilerEvent, instanceOrLView: {}, hookOrList
10
10
export class NgProfiler extends Profiler {
11
11
private _tracker = IdentityTracker . getInstance ( ) ;
12
12
private _callbacks : ProfilerCallback [ ] = [ ] ;
13
+ private _lastDirectiveInstance : { } | null = null ;
13
14
14
15
constructor ( config : Partial < Hooks > = { } ) {
15
16
super ( config ) ;
@@ -66,7 +67,7 @@ export class NgProfiler extends Profiler {
66
67
return ;
67
68
}
68
69
69
- [ ɵProfilerEvent . TemplateUpdateStart ] ( directive : any , _hookOrListener : any ) : void {
70
+ [ ɵProfilerEvent . TemplateUpdateStart ] ( context : any , _hookOrListener : any ) : void {
70
71
if ( ! this . _inChangeDetection ) {
71
72
this . _inChangeDetection = true ;
72
73
runOutsideAngular ( ( ) => {
@@ -77,19 +78,44 @@ export class NgProfiler extends Profiler {
77
78
} ) ;
78
79
}
79
80
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
+ }
82
95
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
+ ) ;
84
102
}
85
103
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 ) ;
89
107
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 ;
92
111
}
112
+
113
+ this . _onChangeDetectionEnd (
114
+ this . _lastDirectiveInstance ,
115
+ getDirectiveHostElement ( this . _lastDirectiveInstance ) ,
116
+ this . _tracker . getDirectiveId ( this . _lastDirectiveInstance ) ,
117
+ this . _tracker . getDirectivePosition ( this . _lastDirectiveInstance )
118
+ ) ;
93
119
}
94
120
95
121
[ ɵProfilerEvent . LifecycleHookStart ] ( directive : any , hook : any ) : void {
0 commit comments