forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): collect providers from NgModules while rendering
@defer
…
…block Currently, when a `@defer` block contains standalone components that import NgModules with providers, those providers are not available to components declared within the same NgModule. The problem is that the standalone injector is not created for the host component (that hosts this `@defer` block), since dependencies become defer-loaded, thus no information is available at host component creation time. This commit updates the logic to collect all providers from all NgModules used as a dependency for standalone components used within a `@defer` block. When an instance of a defer block is created, a new environment injector instance with those providers is created. Resolves angular#52876.
- Loading branch information
1 parent
74aa8a3
commit 3138f09
Showing
5 changed files
with
210 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {ɵɵdefineInjectable as defineInjectable} from './di/interface/defs'; | ||
import {Provider} from './di/interface/provider'; | ||
import {EnvironmentInjector} from './di/r3_injector'; | ||
import {OnDestroy} from './interface/lifecycle_hooks'; | ||
import {createEnvironmentInjector} from './render3/ng_module_ref'; | ||
|
||
/** | ||
* A service used by the framework to create and cache injector instances. | ||
* | ||
* This service is used to create a single injector instance for each defer | ||
* block definition, to avoid creating an injector for each defer block instance | ||
* of a certain type. | ||
*/ | ||
export class CachedInjectorService implements OnDestroy { | ||
private cachedInjectors = new Map<unknown, EnvironmentInjector|null>(); | ||
|
||
getOrCreateInjector( | ||
key: unknown, parentInjector: EnvironmentInjector, providers: Provider[], | ||
debugName?: string) { | ||
if (!this.cachedInjectors.has(key)) { | ||
const injector = providers.length > 0 ? | ||
createEnvironmentInjector(providers, parentInjector, debugName) : | ||
null; | ||
this.cachedInjectors.set(key, injector); | ||
} | ||
return this.cachedInjectors.get(key)!; | ||
} | ||
|
||
ngOnDestroy() { | ||
try { | ||
for (const injector of this.cachedInjectors.values()) { | ||
if (injector !== null) { | ||
injector.destroy(); | ||
} | ||
} | ||
} finally { | ||
this.cachedInjectors.clear(); | ||
} | ||
} | ||
|
||
/** @nocollapse */ | ||
static ɵprov = /** @pureOrBreakMyCode */ defineInjectable({ | ||
token: CachedInjectorService, | ||
providedIn: 'environment', | ||
factory: () => new CachedInjectorService(), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters