Skip to content

Commit

Permalink
feat(core): create injector debugging APIs (#48639)
Browse files Browse the repository at this point in the history
This commit introduces 3 new APIs.

getDependenciesFromInstantiation:
- Given an injector and a token that was instantiated by that injector, discover all of the things were injected in the instance of that token
- This API is meant to enable recursive inspection of dependencies. Dependencies returned by this API include which injector they were providedIn, which enables the continous use of getDependenciesFromInstantiation to determine the dependencies of dependencies

getInjectorProviders:
- Given an injector, discover all of the providers that were configured in that injector.
- This API returns information on the configured providers of an injector, including the import path that leads to the container that the provider originated from (NgModule or standalone directive). This enables fine grained inspection to determine where a specific provider comes from.

getInjectorParent:
- Given an injector, discover the parent of that injector.
- This function is meant to be used recursively to discover the entire resolution path from a starting injector to the NullInjector.

These APIs were designed to be used together. For example, getInjectorParent can be used to discover the structure of an injector hierarchy. Once that's done, getInjectorProviders can be used to determine the providers of each injector in that hierarchy.

Another example: getDependenciesFromInstantiation can be used to discover the dependencies of a specific injector constructed instance. From there, we can use getInjectorParent to discover the injector resolution path and map each dependency to a path from the starting injector to the injector that it was provided in.

PR Close #48639
  • Loading branch information
AleksanderBodurri authored and alxhub committed Aug 1, 2023
1 parent 4d92080 commit 98d262f
Show file tree
Hide file tree
Showing 2 changed files with 541 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/core/src/render3/util/global_utils.ts
Expand Up @@ -7,9 +7,12 @@
*/
import {assertDefined} from '../../util/assert';
import {global} from '../../util/global';
import {setupFrameworkInjectorProfiler} from '../debug/framework_injector_profiler';
import {setProfiler} from '../profiler';

import {applyChanges} from './change_detection_utils';
import {getComponent, getContext, getDirectiveMetadata, getDirectives, getHostElement, getInjector, getListeners, getOwningComponent, getRootComponents} from './discovery_utils';
import {getDependenciesFromInjectable, getInjectorProviders, getInjectorResolutionPath} from './injector_discovery_utils';



Expand Down Expand Up @@ -41,6 +44,10 @@ export function publishDefaultGlobalUtils() {
if (!_published) {
_published = true;

setupFrameworkInjectorProfiler();
publishGlobalUtil('ɵgetDependenciesFromInjectable', getDependenciesFromInjectable);
publishGlobalUtil('ɵgetInjectorProviders', getInjectorProviders);
publishGlobalUtil('ɵgetInjectorResolutionPath', getInjectorResolutionPath);
/**
* Warning: this function is *INTERNAL* and should not be relied upon in application's code.
* The contract of the function might be changed in any release and/or the function can be
Expand Down

0 comments on commit 98d262f

Please sign in to comment.