Skip to content

Commit 92bb8d9

Browse files
iterianiAndrewKushnir
authored andcommitted
refactor(core): Use the retrieve method in the implementation of injectInjectorOnly (angular#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 angular#60192
1 parent 3602c53 commit 92bb8d9

File tree

16 files changed

+35
-31
lines changed

16 files changed

+35
-31
lines changed

goldens/public-api/core/primitives/di/index.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
export function getCurrentInjector(): Injector | undefined | null;
99

1010
// @public
11-
export interface InjectionToken<T> extends Type<T> {
11+
export interface InjectionToken<T> {
1212
// (undocumented)
1313
ɵprov: ɵɵInjectableDeclaration<T>;
1414
}

packages/core/primitives/di/src/injection_token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ export interface ɵɵInjectableDeclaration<T> {
4949
*
5050
* @publicApi
5151
*/
52-
export interface InjectionToken<T> extends Type<T> {
52+
export interface InjectionToken<T> {
5353
ɵprov: ɵɵInjectableDeclaration<T>;
5454
}

packages/core/src/di/injector_compatibility.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import type {HostAttributeToken} from './host_attribute_token';
2727
import {
2828
Injector as PrimitivesInjector,
2929
NotFound,
30-
NOT_FOUND,
3130
InjectionToken as PrimitivesInjectionToken,
3231
getCurrentInjector,
3332
} from '@angular/core/primitives/di';
33+
import {InjectionToken} from './injection_token';
3434

3535
const _THROW_IF_NOT_FOUND = {};
3636
export const THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND;
@@ -44,11 +44,25 @@ export {getCurrentInjector, setCurrentInjector} from '@angular/core/primitives/d
4444
*/
4545
const DI_DECORATOR_FLAG = '__NG_DI_FLAG__';
4646

47+
/**
48+
* A wrapper around an `Injector` that implements the `PrimitivesInjector` interface.
49+
*
50+
* This is used to allow the `inject` function to be used with the new primitives-based DI system.
51+
*/
4752
export class RetrievingInjector implements PrimitivesInjector {
4853
constructor(readonly injector: Injector) {}
4954
retrieve<T>(token: PrimitivesInjectionToken<T>, options: unknown): T | NotFound {
50-
const ngOptions = options as InjectOptions;
51-
return this.injector.get(token, ngOptions.optional ? NOT_FOUND : THROW_IF_NOT_FOUND, ngOptions);
55+
let flags: InjectFlags;
56+
if (options && (options as {flags: InjectFlags}).flags) {
57+
flags = (options as {flags: InjectFlags}).flags;
58+
} else {
59+
flags = convertToBitFlags(options as InjectOptions | undefined) || InjectFlags.Default;
60+
}
61+
return this.injector.get(
62+
token as unknown as InjectionToken<T>,
63+
flags & InjectFlags.Optional ? null : undefined,
64+
flags,
65+
) as T;
5266
}
5367
}
5468

@@ -64,23 +78,17 @@ export function injectInjectorOnly<T>(
6478
token: ProviderToken<T>,
6579
flags = InjectFlags.Default,
6680
): T | null {
67-
if (getCurrentInjector() === undefined) {
81+
const currentInjector = getCurrentInjector();
82+
if (currentInjector === undefined) {
6883
throw new RuntimeError(
6984
RuntimeErrorCode.MISSING_INJECTION_CONTEXT,
7085
ngDevMode &&
7186
`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\`.`,
7287
);
73-
} else if (getCurrentInjector() === null) {
88+
} else if (currentInjector === null) {
7489
return injectRootLimpMode(token, undefined, flags);
7590
} else {
76-
const currentInjector = getCurrentInjector();
77-
let injector: Injector;
78-
if (currentInjector instanceof RetrievingInjector) {
79-
injector = currentInjector.injector;
80-
} else {
81-
injector = currentInjector as unknown as Injector;
82-
}
83-
const value = injector.get(token, flags & InjectFlags.Optional ? null : undefined, flags);
91+
const value = currentInjector.retrieve(token as PrimitivesInjectionToken<T>, {flags}) as T;
8492
ngDevMode && emitInjectEvent(token as Type<unknown>, value, flags);
8593
return value;
8694
}

packages/core/src/di/r3_injector.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,17 @@ export class R3Injector extends EnvironmentInjector implements PrimitivesInjecto
242242
}
243243

244244
retrieve<T>(token: PrimitivesInjectionToken<T>, options?: unknown): T | NotFound {
245-
const ngOptions = options as InjectOptions;
246-
return this.get(token, ngOptions.optional ? NOT_FOUND : THROW_IF_NOT_FOUND, ngOptions);
245+
let flags: InjectFlags;
246+
if (options && (options as {flags: InjectFlags}).flags) {
247+
flags = (options as {flags: InjectFlags}).flags;
248+
} else {
249+
flags = convertToBitFlags(options as InjectOptions | undefined) || InjectFlags.Default;
250+
}
251+
return this.get(
252+
token as unknown as InjectionToken<T>,
253+
flags & InjectFlags.Optional ? null : undefined,
254+
flags,
255+
);
247256
}
248257

249258
/**

packages/core/test/bundling/animations-standalone/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@
107107
"NG_INJ_DEF",
108108
"NG_PIPE_DEF",
109109
"NG_PROV_DEF",
110-
"NOT_FOUND",
111110
"NOT_FOUND2",
112111
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
113112
"NOT_YET",

packages/core/test/bundling/animations/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
"NG_MOD_DEF",
115115
"NG_PIPE_DEF",
116116
"NG_PROV_DEF",
117-
"NOT_FOUND",
118117
"NOT_FOUND2",
119118
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
120119
"NOT_YET",

packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
"NG_MOD_DEF",
8484
"NG_PIPE_DEF",
8585
"NG_PROV_DEF",
86-
"NOT_FOUND",
8786
"NOT_FOUND2",
8887
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
8988
"NOT_YET",

packages/core/test/bundling/defer/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@
112112
"NG_TEMPLATE_SELECTOR",
113113
"NG_TEMP_TOKEN_PATH",
114114
"NG_TOKEN_PATH",
115-
"NOT_FOUND",
116115
"NOT_FOUND2",
117116
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
118117
"NOT_YET",

packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@
119119
"NG_PROV_DEF",
120120
"NG_VALIDATORS",
121121
"NG_VALUE_ACCESSOR",
122-
"NOT_FOUND",
123122
"NOT_FOUND2",
124123
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
125124
"NOT_YET",

packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@
111111
"NG_PROV_DEF",
112112
"NG_VALIDATORS",
113113
"NG_VALUE_ACCESSOR",
114-
"NOT_FOUND",
115114
"NOT_FOUND2",
116115
"NOT_FOUND_CHECK_ONLY_ELEMENT_INJECTOR",
117116
"NOT_YET",

0 commit comments

Comments
 (0)