Skip to content

Commit d260ca3

Browse files
AleksanderBodurrimmalerba
authored andcommitted
feat(core): emit template function for template related profiler hooks (#60174)
Previously, the profiler would only emit the specific template event and context when a template is created/updated, but not the template function related to the event. This commit emits this function by using the third argument of the profiler function, which previously was only used for lifecycle hooks and output listeners. This commit also renames this arg to eventFn to express that it varies depending on the event type emitting from the profiler. Note: this change is fully backwards compatible, since previously these template events did not use the third arg of the profiler function. PR Close #60174
1 parent a3575e2 commit d260ca3

File tree

5 files changed

+41
-50
lines changed

5 files changed

+41
-50
lines changed

devtools/projects/ng-devtools-backend/src/lib/hooks/profiler/native.ts

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ import {IdentityTracker, NodeArray} from '../identity-tracker';
1515

1616
import {getLifeCycleName, Hooks, Profiler} from './shared';
1717

18-
type ProfilerCallback = (
19-
event: ɵProfilerEvent,
20-
instanceOrLView: {} | null,
21-
hookOrListener: any,
22-
) => void;
18+
type ProfilerCallback = (event: ɵProfilerEvent, instanceOrLView: {} | null, eventFn: any) => void;
2319

2420
/** Implementation of Profiler that utilizes framework APIs fire profiler hooks. */
2521
export class NgProfiler extends Profiler {
@@ -29,22 +25,20 @@ export class NgProfiler extends Profiler {
2925

3026
constructor(config: Partial<Hooks> = {}) {
3127
super(config);
32-
this._setProfilerCallback(
33-
(event: ɵProfilerEvent, instanceOrLView: {} | null, hookOrListener: any) => {
34-
if (this[event] === undefined) {
35-
return;
36-
}
37-
38-
this[event](instanceOrLView, hookOrListener);
39-
},
40-
);
28+
this._setProfilerCallback((event: ɵProfilerEvent, instanceOrLView: {} | null, eventFn: any) => {
29+
if (this[event] === undefined) {
30+
return;
31+
}
32+
33+
this[event](instanceOrLView, eventFn);
34+
});
4135
this._initialize();
4236
}
4337

4438
private _initialize(): void {
4539
ngDebugClient().ɵsetProfiler(
46-
(event: ɵProfilerEvent, instanceOrLView: {} | null = null, hookOrListener: any) =>
47-
this._callbacks.forEach((cb) => cb(event, instanceOrLView, hookOrListener)),
40+
(event: ɵProfilerEvent, instanceOrLView: {} | null = null, eventFn: any) =>
41+
this._callbacks.forEach((cb) => cb(event, instanceOrLView, eventFn)),
4842
);
4943
}
5044

@@ -74,107 +68,107 @@ export class NgProfiler extends Profiler {
7468
});
7569
}
7670

77-
[ɵProfilerEvent.BootstrapApplicationStart](_directive: any, _hookOrListener: any): void {
71+
[ɵProfilerEvent.BootstrapApplicationStart](_directive: any, _eventFn: any): void {
7872
// todo: implement
7973
return;
8074
}
8175

82-
[ɵProfilerEvent.BootstrapApplicationEnd](_directive: any, _hookOrListener: any): void {
76+
[ɵProfilerEvent.BootstrapApplicationEnd](_directive: any, _eventFn: any): void {
8377
// todo: implement
8478
return;
8579
}
8680

87-
[ɵProfilerEvent.BootstrapComponentStart](_directive: any, _hookOrListener: any): void {
81+
[ɵProfilerEvent.BootstrapComponentStart](_directive: any, _eventFn: any): void {
8882
// todo: implement
8983
return;
9084
}
9185

92-
[ɵProfilerEvent.BootstrapComponentEnd](_directive: any, _hookOrListener: any): void {
86+
[ɵProfilerEvent.BootstrapComponentEnd](_directive: any, _eventFn: any): void {
9387
// todo: implement
9488
return;
9589
}
9690

97-
[ɵProfilerEvent.ChangeDetectionStart](_directive: any, _hookOrListener: any): void {
91+
[ɵProfilerEvent.ChangeDetectionStart](_directive: any, _eventFn: any): void {
9892
// todo: implement
9993
return;
10094
}
10195

102-
[ɵProfilerEvent.ChangeDetectionEnd](_directive: any, _hookOrListener: any): void {
96+
[ɵProfilerEvent.ChangeDetectionEnd](_directive: any, _eventFn: any): void {
10397
// todo: implement
10498
return;
10599
}
106100

107-
[ɵProfilerEvent.ChangeDetectionSyncStart](_directive: any, _hookOrListener: any): void {
101+
[ɵProfilerEvent.ChangeDetectionSyncStart](_directive: any, _eventFn: any): void {
108102
// todo: implement
109103
return;
110104
}
111105

112-
[ɵProfilerEvent.ChangeDetectionSyncEnd](_directive: any, _hookOrListener: any): void {
106+
[ɵProfilerEvent.ChangeDetectionSyncEnd](_directive: any, _eventFn: any): void {
113107
// todo: implement
114108
return;
115109
}
116110

117-
[ɵProfilerEvent.AfterRenderHooksStart](_directive: any, _hookOrListener: any): void {
111+
[ɵProfilerEvent.AfterRenderHooksStart](_directive: any, _eventFn: any): void {
118112
// todo: implement
119113
return;
120114
}
121115

122-
[ɵProfilerEvent.AfterRenderHooksEnd](_directive: any, _hookOrListener: any): void {
116+
[ɵProfilerEvent.AfterRenderHooksEnd](_directive: any, _eventFn: any): void {
123117
// todo: implement
124118
return;
125119
}
126120

127-
[ɵProfilerEvent.ComponentStart](_directive: any, _hookOrListener: any): void {
121+
[ɵProfilerEvent.ComponentStart](_directive: any, _eventFn: any): void {
128122
// todo: implement
129123
return;
130124
}
131125

132-
[ɵProfilerEvent.ComponentEnd](_directive: any, _hookOrListener: any): void {
126+
[ɵProfilerEvent.ComponentEnd](_directive: any, _eventFn: any): void {
133127
// todo: implement
134128
return;
135129
}
136130

137-
[ɵProfilerEvent.DeferBlockStateStart](_directive: any, _hookOrListener: any): void {
131+
[ɵProfilerEvent.DeferBlockStateStart](_directive: any, _eventFn: any): void {
138132
// todo: implement
139133
return;
140134
}
141135

142-
[ɵProfilerEvent.DeferBlockStateEnd](_directive: any, _hookOrListener: any): void {
136+
[ɵProfilerEvent.DeferBlockStateEnd](_directive: any, _eventFn: any): void {
143137
// todo: implement
144138
return;
145139
}
146140

147-
[ɵProfilerEvent.DynamicComponentStart](_directive: any, _hookOrListener: any): void {
141+
[ɵProfilerEvent.DynamicComponentStart](_directive: any, _eventFn: any): void {
148142
// todo: implement
149143
return;
150144
}
151145

152-
[ɵProfilerEvent.DynamicComponentEnd](_directive: any, _hookOrListener: any): void {
146+
[ɵProfilerEvent.DynamicComponentEnd](_directive: any, _eventFn: any): void {
153147
// todo: implement
154148
return;
155149
}
156150

157-
[ɵProfilerEvent.HostBindingsUpdateStart](_directive: any, _hookOrListener: any): void {
151+
[ɵProfilerEvent.HostBindingsUpdateStart](_directive: any, _eventFn: any): void {
158152
// todo: implement
159153
return;
160154
}
161155

162-
[ɵProfilerEvent.HostBindingsUpdateEnd](_directive: any, _hookOrListener: any): void {
156+
[ɵProfilerEvent.HostBindingsUpdateEnd](_directive: any, _eventFn: any): void {
163157
// todo: implement
164158
return;
165159
}
166160

167-
[ɵProfilerEvent.TemplateCreateStart](_directive: any, _hookOrListener: any): void {
161+
[ɵProfilerEvent.TemplateCreateStart](_directive: any, _eventFn: any): void {
168162
// todo: implement
169163
return;
170164
}
171165

172-
[ɵProfilerEvent.TemplateCreateEnd](_directive: any, _hookOrListener: any): void {
166+
[ɵProfilerEvent.TemplateCreateEnd](_directive: any, _eventFn: any): void {
173167
// todo: implement
174168
return;
175169
}
176170

177-
[ɵProfilerEvent.TemplateUpdateStart](context: any, _hookOrListener: any): void {
171+
[ɵProfilerEvent.TemplateUpdateStart](context: any, _eventFn: any): void {
178172
if (!this._inChangeDetection) {
179173
this._inChangeDetection = true;
180174
runOutsideAngular(() => {
@@ -208,7 +202,7 @@ export class NgProfiler extends Profiler {
208202
);
209203
}
210204

211-
[ɵProfilerEvent.TemplateUpdateEnd](context: any, _hookOrListener: any): void {
205+
[ɵProfilerEvent.TemplateUpdateEnd](context: any, _eventFn: any): void {
212206
const position = this._tracker.getDirectivePosition(context);
213207
const id = this._tracker.getDirectiveId(context);
214208

packages/core/src/render3/instructions/shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ export function executeTemplate<T>(
9696
const preHookType = isUpdatePhase
9797
? ProfilerEvent.TemplateUpdateStart
9898
: ProfilerEvent.TemplateCreateStart;
99-
profiler(preHookType, context as unknown as {});
99+
profiler(preHookType, context as unknown as {}, templateFn);
100100
templateFn(rf, context);
101101
} finally {
102102
setSelectedIndex(prevSelectedIndex);
103103

104104
const postHookType = isUpdatePhase
105105
? ProfilerEvent.TemplateUpdateEnd
106106
: ProfilerEvent.TemplateCreateEnd;
107-
profiler(postHookType, context as unknown as {});
107+
profiler(postHookType, context as unknown as {}, templateFn);
108108
}
109109
}
110110

packages/core/src/render3/profiler.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ export const setProfiler = (profiler: Profiler | null) => {
2929
*
3030
* @param event ProfilerEvent corresponding to the execution context
3131
* @param instance component instance
32-
* @param hookOrListener lifecycle hook function or output listener. The value depends on the
33-
* execution context
32+
* @param eventFn function associated with event.
33+
* For example a template function, lifecycle hook, or output listener.
34+
* The value depends on the execution context
3435
* @returns
3536
*/
36-
export const profiler: Profiler = function (event, instance = null, hookOrListener) {
37+
export const profiler: Profiler = function (event, instance = null, eventFn) {
3738
if (profilerCallback != null /* both `null` and `undefined` */) {
38-
profilerCallback(event, instance, hookOrListener);
39+
profilerCallback(event, instance, eventFn);
3940
}
4041
};

packages/core/src/render3/profiler_types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,5 @@ export const enum ProfilerEvent {
160160
* Profiler function which the runtime will invoke before and after user code.
161161
*/
162162
export interface Profiler {
163-
(event: ProfilerEvent, instance?: {} | null, hookOrListener?: (e?: any) => any): void;
163+
(event: ProfilerEvent, instance?: {} | null, eventFn?: Function): void;
164164
}

packages/core/test/acceptance/profiler_spec.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,7 @@ describe('profiler', () => {
443443
return true;
444444
}
445445

446-
profile = (
447-
event: ProfilerEvent,
448-
instance?: {} | null,
449-
hookOrListener?: (e?: any) => any,
450-
): void => {
446+
profile = (event: ProfilerEvent, instance?: {} | null, eventFn?: Function): void => {
451447
this.events.push(event);
452448
};
453449
}

0 commit comments

Comments
 (0)