mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(core): Use the retrieve method in the implementation of injectInjectorOnly (#60192)
This should keep the existing behavior intact. Right now retrieve never returns back NOT_FOUND. This should not be the case, but tests fail if I do add this behavior so itll have to be later. PR Close #60192
This commit is contained in:
parent
3602c536e4
commit
92bb8d974b
16 changed files with 35 additions and 31 deletions
|
|
@ -8,7 +8,7 @@
|
|||
export function getCurrentInjector(): Injector | undefined | null;
|
||||
|
||||
// @public
|
||||
export interface InjectionToken<T> extends Type<T> {
|
||||
export interface InjectionToken<T> {
|
||||
// (undocumented)
|
||||
ɵprov: ɵɵInjectableDeclaration<T>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,6 @@ export interface ɵɵInjectableDeclaration<T> {
|
|||
*
|
||||
* @publicApi
|
||||
*/
|
||||
export interface InjectionToken<T> extends Type<T> {
|
||||
export interface InjectionToken<T> {
|
||||
ɵprov: ɵɵInjectableDeclaration<T>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,10 +27,10 @@ import type {HostAttributeToken} from './host_attribute_token';
|
|||
import {
|
||||
Injector as PrimitivesInjector,
|
||||
NotFound,
|
||||
NOT_FOUND,
|
||||
InjectionToken as PrimitivesInjectionToken,
|
||||
getCurrentInjector,
|
||||
} from '@angular/core/primitives/di';
|
||||
import {InjectionToken} from './injection_token';
|
||||
|
||||
const _THROW_IF_NOT_FOUND = {};
|
||||
export const THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND;
|
||||
|
|
@ -44,11 +44,25 @@ export {getCurrentInjector, setCurrentInjector} from '@angular/core/primitives/d
|
|||
*/
|
||||
const DI_DECORATOR_FLAG = '__NG_DI_FLAG__';
|
||||
|
||||
/**
|
||||
* A wrapper around an `Injector` that implements the `PrimitivesInjector` interface.
|
||||
*
|
||||
* This is used to allow the `inject` function to be used with the new primitives-based DI system.
|
||||
*/
|
||||
export class RetrievingInjector implements PrimitivesInjector {
|
||||
constructor(readonly injector: Injector) {}
|
||||
retrieve<T>(token: PrimitivesInjectionToken<T>, options: unknown): T | NotFound {
|
||||
const ngOptions = options as InjectOptions;
|
||||
return this.injector.get(token, ngOptions.optional ? NOT_FOUND : THROW_IF_NOT_FOUND, ngOptions);
|
||||
let flags: InjectFlags;
|
||||
if (options && (options as {flags: InjectFlags}).flags) {
|
||||
flags = (options as {flags: InjectFlags}).flags;
|
||||
} else {
|
||||
flags = convertToBitFlags(options as InjectOptions | undefined) || InjectFlags.Default;
|
||||
}
|
||||
return this.injector.get(
|
||||
token as unknown as InjectionToken<T>,
|
||||
flags & InjectFlags.Optional ? null : undefined,
|
||||
flags,
|
||||
) as T;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -64,23 +78,17 @@ export function injectInjectorOnly<T>(
|
|||
token: ProviderToken<T>,
|
||||
flags = InjectFlags.Default,
|
||||
): T | null {
|
||||
if (getCurrentInjector() === undefined) {
|
||||
const currentInjector = getCurrentInjector();
|
||||
if (currentInjector === undefined) {
|
||||
throw new RuntimeError(
|
||||
RuntimeErrorCode.MISSING_INJECTION_CONTEXT,
|
||||
ngDevMode &&
|
||||
`The \`${stringify(token)}\` token injection failed. \`inject()\` function must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with \`runInInjectionContext\`.`,
|
||||
);
|
||||
} else if (getCurrentInjector() === null) {
|
||||
} else if (currentInjector === null) {
|
||||
return injectRootLimpMode(token, undefined, flags);
|
||||
} else {
|
||||
const currentInjector = getCurrentInjector();
|
||||
let injector: Injector;
|
||||
if (currentInjector instanceof RetrievingInjector) {
|
||||
injector = currentInjector.injector;
|
||||
} else {
|
||||
injector = currentInjector as unknown as Injector;
|
||||
}
|
||||
const value = injector.get(token, flags & InjectFlags.Optional ? null : undefined, flags);
|
||||
const value = currentInjector.retrieve(token as PrimitivesInjectionToken<T>, {flags}) as T;
|
||||
ngDevMode && emitInjectEvent(token as Type<unknown>, value, flags);
|
||||
return value;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -242,8 +242,17 @@ export class R3Injector extends EnvironmentInjector implements PrimitivesInjecto
|
|||
}
|
||||
|
||||
retrieve<T>(token: PrimitivesInjectionToken<T>, options?: unknown): T | NotFound {
|
||||
const ngOptions = options as InjectOptions;
|
||||
return this.get(token, ngOptions.optional ? NOT_FOUND : THROW_IF_NOT_FOUND, ngOptions);
|
||||
let flags: InjectFlags;
|
||||
if (options && (options as {flags: InjectFlags}).flags) {
|
||||
flags = (options as {flags: InjectFlags}).flags;
|
||||
} else {
|
||||
flags = convertToBitFlags(options as InjectOptions | undefined) || InjectFlags.Default;
|
||||
}
|
||||
return this.get(
|
||||
token as unknown as InjectionToken<T>,
|
||||
flags & InjectFlags.Optional ? null : undefined,
|
||||
flags,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -107,7 +107,6 @@
|
|||
"NG_INJ_DEF",
|
||||
"NG_PIPE_DEF",
|
||||
"NG_PROV_DEF",
|
||||
"NOT_FOUND",
|
||||
"NOT_FOUND2",
|
||||
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
|
||||
"NOT_YET",
|
||||
|
|
|
|||
|
|
@ -114,7 +114,6 @@
|
|||
"NG_MOD_DEF",
|
||||
"NG_PIPE_DEF",
|
||||
"NG_PROV_DEF",
|
||||
"NOT_FOUND",
|
||||
"NOT_FOUND2",
|
||||
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
|
||||
"NOT_YET",
|
||||
|
|
|
|||
|
|
@ -83,7 +83,6 @@
|
|||
"NG_MOD_DEF",
|
||||
"NG_PIPE_DEF",
|
||||
"NG_PROV_DEF",
|
||||
"NOT_FOUND",
|
||||
"NOT_FOUND2",
|
||||
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
|
||||
"NOT_YET",
|
||||
|
|
|
|||
|
|
@ -112,7 +112,6 @@
|
|||
"NG_TEMPLATE_SELECTOR",
|
||||
"NG_TEMP_TOKEN_PATH",
|
||||
"NG_TOKEN_PATH",
|
||||
"NOT_FOUND",
|
||||
"NOT_FOUND2",
|
||||
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
|
||||
"NOT_YET",
|
||||
|
|
|
|||
|
|
@ -119,7 +119,6 @@
|
|||
"NG_PROV_DEF",
|
||||
"NG_VALIDATORS",
|
||||
"NG_VALUE_ACCESSOR",
|
||||
"NOT_FOUND",
|
||||
"NOT_FOUND2",
|
||||
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
|
||||
"NOT_YET",
|
||||
|
|
|
|||
|
|
@ -111,7 +111,6 @@
|
|||
"NG_PROV_DEF",
|
||||
"NG_VALIDATORS",
|
||||
"NG_VALUE_ACCESSOR",
|
||||
"NOT_FOUND",
|
||||
"NOT_FOUND2",
|
||||
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
|
||||
"NOT_YET",
|
||||
|
|
|
|||
|
|
@ -58,7 +58,6 @@
|
|||
"NG_INJ_DEF",
|
||||
"NG_MOD_DEF",
|
||||
"NG_PROV_DEF",
|
||||
"NOT_FOUND",
|
||||
"NOT_FOUND2",
|
||||
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
|
||||
"NOT_YET",
|
||||
|
|
|
|||
|
|
@ -89,7 +89,6 @@
|
|||
"NG_INJ_DEF",
|
||||
"NG_PIPE_DEF",
|
||||
"NG_PROV_DEF",
|
||||
"NOT_FOUND",
|
||||
"NOT_FOUND2",
|
||||
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
|
||||
"NOT_YET",
|
||||
|
|
|
|||
|
|
@ -16,12 +16,10 @@
|
|||
"NG_INJECTOR_DEF",
|
||||
"NG_INJ_DEF",
|
||||
"NG_PROV_DEF",
|
||||
"NOT_FOUND",
|
||||
"NOT_YET",
|
||||
"NULL_INJECTOR",
|
||||
"NullInjector",
|
||||
"R3Injector",
|
||||
"RetrievingInjector",
|
||||
"RuntimeError",
|
||||
"ScopedService",
|
||||
"Subscription",
|
||||
|
|
@ -36,6 +34,7 @@
|
|||
"activeConsumer",
|
||||
"arrRemove",
|
||||
"assertNotDestroyed",
|
||||
"convertToBitFlags",
|
||||
"createErrorClass",
|
||||
"createInjector",
|
||||
"createLFrame",
|
||||
|
|
@ -45,7 +44,6 @@
|
|||
"forEachSingleProvider",
|
||||
"forwardRef",
|
||||
"getClosureSafeProperty",
|
||||
"getCurrentInjector",
|
||||
"getFactoryDef",
|
||||
"getInjectableDef",
|
||||
"getInjectorDef",
|
||||
|
|
|
|||
|
|
@ -124,7 +124,6 @@
|
|||
"NG_MOD_DEF",
|
||||
"NG_PIPE_DEF",
|
||||
"NG_PROV_DEF",
|
||||
"NOT_FOUND",
|
||||
"NOT_FOUND2",
|
||||
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
|
||||
"NOT_YET",
|
||||
|
|
|
|||
|
|
@ -77,7 +77,6 @@
|
|||
"NG_INJ_DEF",
|
||||
"NG_PIPE_DEF",
|
||||
"NG_PROV_DEF",
|
||||
"NOT_FOUND",
|
||||
"NOT_FOUND2",
|
||||
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
|
||||
"NOT_YET",
|
||||
|
|
|
|||
|
|
@ -85,7 +85,6 @@
|
|||
"NG_MOD_DEF",
|
||||
"NG_PIPE_DEF",
|
||||
"NG_PROV_DEF",
|
||||
"NOT_FOUND",
|
||||
"NOT_FOUND2",
|
||||
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
|
||||
"NOT_YET",
|
||||
|
|
|
|||
Loading…
Reference in a new issue