MCPcopy Create free account
hub / github.com/angular/angular / walkElementInjectors

Function walkElementInjectors

packages/core/src/debug/ai/di_graph.ts:98–142  ·  view source on GitHub ↗

* Traverses all directive-hosting nodes in the `rootLView` hierarchy and builds a tree of * serialized element injectors. * * This function uses `walkLViewDirectives` to visit nodes in depth-first order and a stack * to reconstruct the hierarchical tree of injectors, handling both same-view and

(rootLView: LView)

Source from the content-addressed store, hash-verified

96 * @returns The root {@link SerializedInjector} object.
97 */
98function walkElementInjectors(rootLView: LView): SerializedInjector {
99 // Assert that we were given a root `LView` rather than a random component.
100 // A root component actually gets two `LView` objects, the "root `LView`" with
101 // `type === TViewType.Root` and then an `LView` for the component itself as a child.
102 if (rootLView[TVIEW].type !== TViewType.Root) {
103 throw new Error(`Expected a root LView but got type: \`${rootLView[TVIEW].type}\`.`);
104 }
105
106 // Track the injectors we're currently processing.
107 const stack: Array<[TNode, LView, SerializedInjector]> = [];
108
109 // By constraining `rootLView` to only accepting root `LView` objects, we don't have to
110 // process `rootLView` itself, knowing that it won't be a component or directive.
111 // We can just check its descendants.
112 for (const [tNode, lView] of walkLViewDirectives(rootLView)) {
113 const injector = new NodeInjector(tNode as TDirectiveHostNode, lView);
114 const serialized = serializeInjector(injector);
115
116 // Look for our nearest ancestor in the stack.
117 while (stack.length > 0) {
118 const [lastTNode, lastLView, lastInjector] = stack[stack.length - 1];
119
120 const isDescendantInSameView = isTNodeDescendant(tNode, lastTNode);
121 const isDescendantInDifferentView = isLViewDescendantOfTNode(lView, lastLView, lastTNode);
122 if (isDescendantInSameView || isDescendantInDifferentView) {
123 // This injector is a child of the current last injector in the stack.
124 lastInjector.children.push(serialized);
125 break;
126 } else {
127 stack.pop();
128 }
129 }
130
131 // Future injectors might be children of this one.
132 stack.push([tNode, lView, serialized]);
133 }
134
135 // Since all component/directive LViews are descendants of the root LView, the first
136 // item on the stack must still remain and will be the root injector.
137 if (stack.length === 0) {
138 throw new Error(`Expected at least one component/directive in the root \`LView\`.`);
139 }
140 const [, , rootInjector] = stack[0];
141 return rootInjector;
142}
143
144/**
145 * Collects and serializes all environment injectors found in the hierarchy of the given

Callers 1

discoverDiGraphFunction · 0.85

Calls 6

walkLViewDirectivesFunction · 0.90
serializeInjectorFunction · 0.90
isTNodeDescendantFunction · 0.85
isLViewDescendantOfTNodeFunction · 0.85
popMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected