Skip to content

Commit d15fda3

Browse files
Revert "refactor(core): allow multiple DI profilers (angular#60562)" (angular#60611)
This reverts commit b21c6e5. PR Close angular#60611
1 parent e40b5c9 commit d15fda3

File tree

2 files changed

+13
-102
lines changed

2 files changed

+13
-102
lines changed

packages/core/src/render3/debug/injector_profiler.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -194,55 +194,33 @@ export function setInjectorProfilerContext(context: InjectorProfilerContext) {
194194
return previous;
195195
}
196196

197-
const injectorProfilerCallbacks: InjectorProfiler[] = [];
198-
199-
const NOOP_PROFILER_REMOVAL = () => {};
200-
201-
function removeProfiler(profiler: InjectorProfiler) {
202-
const profilerIdx = injectorProfilerCallbacks.indexOf(profiler);
203-
if (profilerIdx !== -1) {
204-
injectorProfilerCallbacks.splice(profilerIdx, 1);
205-
}
206-
}
197+
let injectorProfilerCallback: InjectorProfiler | null = null;
207198

208199
/**
209-
* Adds a callback function which will be invoked during certain DI events within the
210-
* runtime (for example: injecting services, creating injectable instances, configuring providers).
211-
* Multiple profiler callbacks can be set: in this case profiling events are
212-
* reported to every registered callback.
200+
* Sets the callback function which will be invoked during certain DI events within the
201+
* runtime (for example: injecting services, creating injectable instances, configuring providers)
213202
*
214203
* Warning: this function is *INTERNAL* and should not be relied upon in application's code.
215204
* The contract of the function might be changed in any release and/or the function can be removed
216205
* completely.
217206
*
218207
* @param profiler function provided by the caller or null value to disable profiling.
219-
* @returns a cleanup function that, when invoked, removes a given profiler callback.
220208
*/
221-
export function setInjectorProfiler(injectorProfiler: InjectorProfiler | null): () => void {
209+
export const setInjectorProfiler = (injectorProfiler: InjectorProfiler | null) => {
222210
!ngDevMode && throwError('setInjectorProfiler should never be called in production mode');
223-
224-
if (injectorProfiler !== null) {
225-
if (!injectorProfilerCallbacks.includes(injectorProfiler)) {
226-
injectorProfilerCallbacks.push(injectorProfiler);
227-
}
228-
return () => removeProfiler(injectorProfiler);
229-
} else {
230-
injectorProfilerCallbacks.length = 0;
231-
return NOOP_PROFILER_REMOVAL;
232-
}
233-
}
211+
injectorProfilerCallback = injectorProfiler;
212+
};
234213

235214
/**
236215
* Injector profiler function which emits on DI events executed by the runtime.
237216
*
238217
* @param event InjectorProfilerEvent corresponding to the DI event being emitted
239218
*/
240-
export function injectorProfiler(event: InjectorProfilerEvent): void {
219+
function injectorProfiler(event: InjectorProfilerEvent): void {
241220
!ngDevMode && throwError('Injector profiler should never be called in production mode');
242221

243-
for (let i = 0; i < injectorProfilerCallbacks.length; i++) {
244-
const injectorProfilerCallback = injectorProfilerCallbacks[i];
245-
injectorProfilerCallback(event);
222+
if (injectorProfilerCallback != null /* both `null` and `undefined` */) {
223+
injectorProfilerCallback!(event);
246224
}
247225
}
248226

packages/core/test/acceptance/injector_profiler_spec.ts

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
afterRender,
1212
ClassProvider,
1313
Component,
14-
createEnvironmentInjector,
1514
Directive,
1615
ElementRef,
1716
inject,
@@ -44,8 +43,6 @@ import {
4443
InjectorProfilerEventType,
4544
ProviderConfiguredEvent,
4645
setInjectorProfiler,
47-
injectorProfiler,
48-
InjectorProfilerContext,
4946
} from '../../src/render3/debug/injector_profiler';
5047
import {getNodeInjectorLView, NodeInjector} from '../../src/render3/di';
5148
import {
@@ -106,7 +103,7 @@ describe('setProfiler', () => {
106103
});
107104
});
108105

109-
afterEach(() => setInjectorProfiler(null));
106+
afterAll(() => setInjectorProfiler(null));
110107

111108
it('should emit DI events when a component contains a provider and injects it', () => {
112109
class MyService {}
@@ -385,70 +382,6 @@ describe('setProfiler', () => {
385382
});
386383
});
387384

388-
describe('profiler activation and removal', () => {
389-
class SomeClass {}
390-
391-
const fakeContext: InjectorProfilerContext = {
392-
injector: Injector.create({providers: []}),
393-
token: SomeClass,
394-
};
395-
396-
const fakeEvent: InjectorCreatedInstanceEvent = {
397-
type: InjectorProfilerEventType.InstanceCreatedByInjector,
398-
context: fakeContext,
399-
instance: {value: new SomeClass()},
400-
};
401-
402-
it('should allow adding and removing multiple profilers', () => {
403-
const events: string[] = [];
404-
const r1 = setInjectorProfiler((e) => events.push('P1: ' + e.type));
405-
const r2 = setInjectorProfiler((e) => events.push('P2: ' + e.type));
406-
407-
injectorProfiler(fakeEvent);
408-
expect(events).toEqual(['P1: 1', 'P2: 1']);
409-
410-
r1();
411-
injectorProfiler(fakeEvent);
412-
expect(events).toEqual(['P1: 1', 'P2: 1', 'P2: 1']);
413-
414-
r2();
415-
injectorProfiler(fakeEvent);
416-
expect(events).toEqual(['P1: 1', 'P2: 1', 'P2: 1']);
417-
});
418-
419-
it('should not add / remove the same profiler twice', () => {
420-
const events: string[] = [];
421-
const p1 = (e: InjectorProfilerEvent) => events.push('P1: ' + e.type);
422-
const r1 = setInjectorProfiler(p1);
423-
const r2 = setInjectorProfiler(p1);
424-
425-
injectorProfiler(fakeEvent);
426-
expect(events).toEqual(['P1: 1']);
427-
428-
r1();
429-
injectorProfiler(fakeEvent);
430-
expect(events).toEqual(['P1: 1']);
431-
432-
// subsequent removals should be noop
433-
r1();
434-
r2();
435-
});
436-
437-
it('should clear all profilers when passing null', () => {
438-
const events: string[] = [];
439-
setInjectorProfiler((e) => events.push('P1: ' + e.type));
440-
setInjectorProfiler((e) => events.push('P2: ' + e.type));
441-
442-
injectorProfiler(fakeEvent);
443-
expect(events).toEqual(['P1: 1', 'P2: 1']);
444-
445-
// clear all profilers
446-
setInjectorProfiler(null);
447-
injectorProfiler(fakeEvent);
448-
expect(events).toEqual(['P1: 1', 'P2: 1']);
449-
});
450-
});
451-
452385
describe('getInjectorMetadata', () => {
453386
it('should be able to determine injector type and name', fakeAsync(() => {
454387
class MyServiceA {}
@@ -554,7 +487,7 @@ describe('getInjectorMetadata', () => {
554487

555488
describe('getInjectorProviders', () => {
556489
beforeEach(() => setupFrameworkInjectorProfiler());
557-
afterEach(() => setInjectorProfiler(null));
490+
afterAll(() => setInjectorProfiler(null));
558491

559492
it('should be able to get the providers from a components injector', () => {
560493
class MyService {}
@@ -1020,7 +953,7 @@ describe('getInjectorProviders', () => {
1020953

1021954
describe('getDependenciesFromInjectable', () => {
1022955
beforeEach(() => setupFrameworkInjectorProfiler());
1023-
afterEach(() => setInjectorProfiler(null));
956+
afterAll(() => setInjectorProfiler(null));
1024957

1025958
it('should be able to determine which injector dependencies come from', fakeAsync(() => {
1026959
class MyService {}
@@ -1311,7 +1244,7 @@ describe('getDependenciesFromInjectable', () => {
13111244

13121245
describe('getInjectorResolutionPath', () => {
13131246
beforeEach(() => setupFrameworkInjectorProfiler());
1314-
afterEach(() => setInjectorProfiler(null));
1247+
afterAll(() => setInjectorProfiler(null));
13151248

13161249
it('should be able to inspect injector hierarchy structure', fakeAsync(() => {
13171250
class MyServiceA {}

0 commit comments

Comments
 (0)