New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): introduce EnvironmentProviders wrapper type #47669
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
02fde2b
refactor(core): define shape of EnvironmentProviders interface
alxhub 4c71d6a
refactor(core): support `EnvironmentProviders` types internally
alxhub 6010c0f
feat(core): introduce EnvironmentProviders wrapper type
alxhub 640e596
feat(router): prevent `provideRouter()` from usage in @Component
alxhub File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -21,9 +21,19 @@ import {resolveForwardRef} from './forward_ref'; | |||||||||
import {ENVIRONMENT_INITIALIZER} from './initializer_token'; | ||||||||||
import {ɵɵinject as inject} from './injector_compatibility'; | ||||||||||
import {getInjectorDef, InjectorType, InjectorTypeWithProviders} from './interface/defs'; | ||||||||||
import {ClassProvider, ConstructorProvider, ExistingProvider, FactoryProvider, ImportedNgModuleProviders, ModuleWithProviders, Provider, StaticClassProvider, TypeProvider, ValueProvider} from './interface/provider'; | ||||||||||
import {ClassProvider, ConstructorProvider, EnvironmentProviders, ExistingProvider, FactoryProvider, ImportedNgModuleProviders, InternalEnvironmentProviders, isEnvironmentProviders, ModuleWithProviders, Provider, StaticClassProvider, TypeProvider, ValueProvider} from './interface/provider'; | ||||||||||
import {INJECTOR_DEF_TYPES} from './internal_tokens'; | ||||||||||
|
||||||||||
/** | ||||||||||
* Wrap an array of `Provider`s into `EnvironmentProviders`, preventing them from being accidentally | ||||||||||
* referenced in `@Component in a component injector. | ||||||||||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
*/ | ||||||||||
export function makeEnvironmentProviders(providers: Provider[]): EnvironmentProviders { | ||||||||||
return { | ||||||||||
ɵproviders: providers, | ||||||||||
} as unknown as EnvironmentProviders; | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* A source of providers for the `importProvidersFrom` function. | ||||||||||
* | ||||||||||
|
@@ -74,9 +84,11 @@ export type ImportProvidersSource = | |||||||||
* @publicApi | ||||||||||
* @developerPreview | ||||||||||
*/ | ||||||||||
export function importProvidersFrom(...sources: ImportProvidersSource[]): | ||||||||||
ImportedNgModuleProviders { | ||||||||||
return {ɵproviders: internalImportProvidersFrom(true, sources)}; | ||||||||||
export function importProvidersFrom(...sources: ImportProvidersSource[]): EnvironmentProviders { | ||||||||||
return { | ||||||||||
ɵproviders: internalImportProvidersFrom(true, sources), | ||||||||||
ɵfromNgModule: true, | ||||||||||
} as InternalEnvironmentProviders; | ||||||||||
} | ||||||||||
|
||||||||||
export function internalImportProvidersFrom( | ||||||||||
|
@@ -118,7 +130,7 @@ function processInjectorTypesWithProviders( | |||||||||
typesWithProviders: InjectorTypeWithProviders<unknown>[], providersOut: Provider[]): void { | ||||||||||
for (let i = 0; i < typesWithProviders.length; i++) { | ||||||||||
const {ngModule, providers} = typesWithProviders[i]; | ||||||||||
deepForEach(providers!, provider => { | ||||||||||
deepForEachProvider(providers! as Array<Provider|InternalEnvironmentProviders>, provider => { | ||||||||||
ngDevMode && validateProvider(provider, providers || EMPTY_ARRAY, ngModule); | ||||||||||
providersOut.push(provider); | ||||||||||
}); | ||||||||||
|
@@ -251,12 +263,12 @@ export function walkProviderTree( | |||||||||
} | ||||||||||
|
||||||||||
// Next, include providers listed on the definition itself. | ||||||||||
const defProviders = injDef.providers; | ||||||||||
const defProviders = injDef.providers as Array<SingleProvider|InternalEnvironmentProviders>; | ||||||||||
if (defProviders != null && !isDuplicate) { | ||||||||||
const injectorType = container as InjectorType<any>; | ||||||||||
deepForEach(defProviders, provider => { | ||||||||||
ngDevMode && validateProvider(provider, defProviders as SingleProvider[], injectorType); | ||||||||||
providersOut.push(provider); | ||||||||||
deepForEachProvider(defProviders, provider => { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious if we can type it here (vs adding
Suggested change
|
||||||||||
ngDevMode && validateProvider(provider as SingleProvider, defProviders, injectorType); | ||||||||||
providersOut.push(provider as SingleProvider); | ||||||||||
}); | ||||||||||
} | ||||||||||
} else { | ||||||||||
|
@@ -270,7 +282,8 @@ export function walkProviderTree( | |||||||||
} | ||||||||||
|
||||||||||
function validateProvider( | ||||||||||
provider: SingleProvider, providers: SingleProvider[], containerType: Type<unknown>): void { | ||||||||||
provider: SingleProvider, providers: Array<SingleProvider|InternalEnvironmentProviders>, | ||||||||||
containerType: Type<unknown>): void { | ||||||||||
if (isTypeProvider(provider) || isValueProvider(provider) || isFactoryProvider(provider) || | ||||||||||
isExistingProvider(provider)) { | ||||||||||
return; | ||||||||||
|
@@ -284,6 +297,21 @@ function validateProvider( | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
function deepForEachProvider( | ||||||||||
providers: Array<Provider|InternalEnvironmentProviders>, | ||||||||||
fn: (provider: SingleProvider) => void): void { | ||||||||||
for (let provider of providers) { | ||||||||||
if (isEnvironmentProviders(provider)) { | ||||||||||
provider = provider.ɵproviders; | ||||||||||
} | ||||||||||
if (Array.isArray(provider)) { | ||||||||||
deepForEachProvider(provider, fn); | ||||||||||
} else { | ||||||||||
fn(provider); | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
export const USE_VALUE = | ||||||||||
getClosureSafeProperty<ValueProvider>({provide: String, useValue: getClosureSafeProperty}); | ||||||||||
|
||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: may be add a quick comment on why we need this type (I think it's covered in the
ɵfromNgModule
field description, may be just add a version of it as a comment about this function)?