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

Function createDeferBlockInjector

packages/core/src/defer/rendering.ts:107–150  ·  view source on GitHub ↗

* Creates a new injector, which contains providers collected from dependencies (NgModules) of * defer-loaded components. This function detects different types of parent injectors and creates * a new injector based on that.

(
  parentInjector: Injector,
  tDetails: TDeferBlockDetails,
  providers: Provider[],
)

Source from the content-addressed store, hash-verified

105 * a new injector based on that.
106 */
107function createDeferBlockInjector(
108 parentInjector: Injector,
109 tDetails: TDeferBlockDetails,
110 providers: Provider[],
111) {
112 // Check if the parent injector is an instance of a `ChainedInjector`.
113 //
114 // In this case, we retain the shape of the injector and use a newly created
115 // `EnvironmentInjector` as a parent in the `ChainedInjector`. That is needed to
116 // make sure that the primary injector gets consulted first (since it's typically
117 // a NodeInjector) and `EnvironmentInjector` tree is consulted after that.
118 if (parentInjector instanceof ChainedInjector) {
119 const origInjector = parentInjector.injector;
120 // Guaranteed to be an environment injector
121 const parentEnvInjector = parentInjector.parentInjector;
122
123 const envInjector = getOrCreateEnvironmentInjector(parentEnvInjector, tDetails, providers);
124 return new ChainedInjector(origInjector, envInjector);
125 }
126
127 const parentEnvInjector = parentInjector.get(EnvironmentInjector);
128
129 // If the `parentInjector` is *not* an `EnvironmentInjector` - we need to create
130 // a new `ChainedInjector` with the following setup:
131 //
132 // - the provided `parentInjector` becomes a primary injector
133 // - an existing (real) `EnvironmentInjector` becomes a parent injector for
134 // a newly-created one, which contains extra providers
135 //
136 // So the final order in which injectors would be consulted in this case would look like this:
137 //
138 // 1. Provided `parentInjector`
139 // 2. Newly-created `EnvironmentInjector` with extra providers
140 // 3. `EnvironmentInjector` from the `parentInjector`
141 if (parentEnvInjector !== parentInjector) {
142 const envInjector = getOrCreateEnvironmentInjector(parentEnvInjector, tDetails, providers);
143 return new ChainedInjector(parentInjector, envInjector);
144 }
145
146 // The `parentInjector` is an instance of an `EnvironmentInjector`.
147 // No need for special handling, we can use `parentInjector` as a
148 // parent injector directly.
149 return getOrCreateEnvironmentInjector(parentInjector, tDetails, providers);
150}
151
152/** Rendering Helpers */
153

Callers 1

applyDeferBlockStateFunction · 0.85

Calls 2

getMethod · 0.65

Tested by

no test coverage detected