MCPcopy Index your code
hub / github.com/angular/angular / collectEnvInjectors

Function collectEnvInjectors

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

* Collects and serializes all environment injectors found in the hierarchy of the given * `rootLViews`. * * Injectors have pointers to their parents, but not their children, so walking "down" the * hierarchy is not a generally supported operation. * * The function walks down the `LView` hierar

(rootLViews: LView[])

Source from the content-addressed store, hash-verified

155 * injector tree.
156 */
157function collectEnvInjectors(rootLViews: LView[]): SerializedInjector {
158 const serializedEnvInjectorMap = new Map<Injector, SerializedInjector>();
159 let rootEnvInjector: SerializedInjector | undefined = undefined;
160
161 /**
162 * Serialize all the ancestors of the given injector and return
163 * its serialized version.
164 *
165 * @param injector The environment injector to start from.
166 * @returns The serialized form of the input {@link Injector}.
167 */
168 function serializeAncestors(injector: Injector): SerializedInjector {
169 const existing = serializedEnvInjectorMap.get(injector);
170 if (existing) return existing;
171
172 const serialized = serializeInjector(injector);
173 serializedEnvInjectorMap.set(injector, serialized);
174
175 const parentInjector = getParentEnvInjector(injector);
176 if (parentInjector) {
177 // Recursively process the parent and attach ourselves as a child.
178 const parentSerialized = serializeAncestors(parentInjector);
179 parentSerialized.children.push(serialized);
180 } else {
181 // If there is no parent, this is a root environment injector.
182 if (!rootEnvInjector) {
183 rootEnvInjector = serialized;
184 } else if (rootEnvInjector !== serialized) {
185 throw new Error('Expected only one root environment injector, but found multiple.', {
186 cause: {firstRoot: rootEnvInjector, secondRoot: serialized},
187 });
188 }
189 }
190
191 return serialized;
192 }
193
194 // Process all descendant environment injectors.
195 for (const rootLView of rootLViews) {
196 for (const [, lView] of walkLViewDirectives(rootLView)) {
197 serializeAncestors(lView[INJECTOR]);
198 }
199 }
200
201 if (!rootEnvInjector) {
202 throw new Error('Expected a root environment injector but did not find one.');
203 }
204
205 return rootEnvInjector;
206}
207
208/**
209 * Checks if `node` is a descendant of `ancestor` within the SAME view.

Callers 1

discoverDiGraphFunction · 0.85

Calls 2

walkLViewDirectivesFunction · 0.90
serializeAncestorsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…