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

Function collectEnvInjectors

packages/core/src/debug/ai/di_graph.ts:159–208  ·  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

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