Skip to content

Commit 8be6e38

Browse files
crisbetoAndrewKushnir
authored andcommitted
refactor(core): replace internal usages of InjectFlags (angular#60318)
Replaces all the places where we use `InjectFlags` internally with the `InternalInjectFlags` which has the benefit of being a `const` enum and allowing us to consolidate the flags and object literal eventually. PR Close angular#60318
1 parent 611baaf commit 8be6e38

File tree

25 files changed

+99
-146
lines changed

25 files changed

+99
-146
lines changed

packages/core/src/change_detection/change_detector_ref.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {InjectFlags, InternalInjectFlags} from '../di/interface/injector';
9+
import {InternalInjectFlags} from '../di/interface/injector';
1010
import {TNode, TNodeType} from '../render3/interfaces/node';
1111
import {isComponentHost} from '../render3/interfaces/type_checks';
1212
import {DECLARATION_COMPONENT_VIEW, LView} from '../render3/interfaces/view';
@@ -122,11 +122,12 @@ export abstract class ChangeDetectorRef {
122122
* @internal
123123
* @nocollapse
124124
*/
125-
static __NG_ELEMENT_ID__: (flags: InjectFlags) => ChangeDetectorRef = injectChangeDetectorRef;
125+
static __NG_ELEMENT_ID__: (flags: InternalInjectFlags) => ChangeDetectorRef =
126+
injectChangeDetectorRef;
126127
}
127128

128129
/** Returns a ChangeDetectorRef (a.k.a. a ViewRef) */
129-
export function injectChangeDetectorRef(flags: InjectFlags): ChangeDetectorRef {
130+
export function injectChangeDetectorRef(flags: InternalInjectFlags): ChangeDetectorRef {
130131
return createViewRef(
131132
getCurrentTNode()!,
132133
getLView(),

packages/core/src/di/host_tag_name_token.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {TNode, TNodeType} from '../render3/interfaces/node';
1111
import {getCurrentTNode} from '../render3/state';
1212

1313
import {InjectionToken} from './injection_token';
14-
import {InjectFlags} from './interface/injector';
14+
import {InternalInjectFlags} from './interface/injector';
1515

1616
/**
1717
* A token that can be used to inject the tag name of the host node.
@@ -39,7 +39,7 @@ export const HOST_TAG_NAME = new InjectionToken<string>(ngDevMode ? 'HOST_TAG_NA
3939
// HOST_TAG_NAME should be resolved at the current node, similar to e.g. ElementRef,
4040
// so we manually specify __NG_ELEMENT_ID__ here, instead of using a factory.
4141
// tslint:disable-next-line:no-toplevel-property-access
42-
(HOST_TAG_NAME as any).__NG_ELEMENT_ID__ = (flags: InjectFlags) => {
42+
(HOST_TAG_NAME as any).__NG_ELEMENT_ID__ = (flags: InternalInjectFlags) => {
4343
const tNode = getCurrentTNode();
4444
if (tNode === null) {
4545
throw new RuntimeError(
@@ -52,7 +52,7 @@ export const HOST_TAG_NAME = new InjectionToken<string>(ngDevMode ? 'HOST_TAG_NA
5252
if (tNode.type & TNodeType.Element) {
5353
return tNode.value;
5454
}
55-
if (flags & InjectFlags.Optional) {
55+
if (flags & InternalInjectFlags.Optional) {
5656
return null;
5757
}
5858
throw new RuntimeError(

packages/core/src/di/inject_switch.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
import {throwProviderNotFoundError} from '../render3/errors_di';
1010
import {assertNotEqual} from '../util/assert';
11-
import {stringify} from '../util/stringify';
1211

1312
import {getInjectableDef, ɵɵInjectableDeclaration} from './interface/defs';
14-
import {InjectFlags} from './interface/injector';
13+
import {InternalInjectFlags} from './interface/injector';
1514
import {ProviderToken} from './provider_token';
1615

1716
/**
@@ -24,7 +23,7 @@ import {ProviderToken} from './provider_token';
2423
* 2. To maintain tree shake-ability we don't want to bring in unnecessary code.
2524
*/
2625
let _injectImplementation:
27-
| (<T>(token: ProviderToken<T>, flags?: InjectFlags) => T | null)
26+
| (<T>(token: ProviderToken<T>, flags?: InternalInjectFlags) => T | null)
2827
| undefined;
2928
export function getInjectImplementation() {
3029
return _injectImplementation;
@@ -34,8 +33,8 @@ export function getInjectImplementation() {
3433
* Sets the current inject implementation.
3534
*/
3635
export function setInjectImplementation(
37-
impl: (<T>(token: ProviderToken<T>, flags?: InjectFlags) => T | null) | undefined,
38-
): (<T>(token: ProviderToken<T>, flags?: InjectFlags) => T | null) | undefined {
36+
impl: (<T>(token: ProviderToken<T>, flags?: InternalInjectFlags) => T | null) | undefined,
37+
): (<T>(token: ProviderToken<T>, flags?: InternalInjectFlags) => T | null) | undefined {
3938
const previous = _injectImplementation;
4039
_injectImplementation = impl;
4140
return previous;
@@ -51,15 +50,15 @@ export function setInjectImplementation(
5150
export function injectRootLimpMode<T>(
5251
token: ProviderToken<T>,
5352
notFoundValue: T | undefined,
54-
flags: InjectFlags,
53+
flags: InternalInjectFlags,
5554
): T | null {
5655
const injectableDef: ɵɵInjectableDeclaration<T> | null = getInjectableDef(token);
5756
if (injectableDef && injectableDef.providedIn == 'root') {
5857
return injectableDef.value === undefined
5958
? (injectableDef.value = injectableDef.factory())
6059
: injectableDef.value;
6160
}
62-
if (flags & InjectFlags.Optional) return null;
61+
if (flags & InternalInjectFlags.Optional) return null;
6362
if (notFoundValue !== undefined) return notFoundValue;
6463
throwProviderNotFoundError(token, 'Injector');
6564
}
@@ -72,7 +71,7 @@ export function injectRootLimpMode<T>(
7271
* @param fn Function which it should not equal to
7372
*/
7473
export function assertInjectImplementationNotEqual(
75-
fn: <T>(token: ProviderToken<T>, flags?: InjectFlags) => T | null,
74+
fn: <T>(token: ProviderToken<T>, flags?: InternalInjectFlags) => T | null,
7675
) {
7776
ngDevMode &&
7877
assertNotEqual(_injectImplementation, fn, 'Calling ɵɵinject would cause infinite recursion');

packages/core/src/di/injector_compatibility.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ import {stringify} from '../util/stringify';
1616
import {resolveForwardRef} from './forward_ref';
1717
import {getInjectImplementation, injectRootLimpMode} from './inject_switch';
1818
import type {Injector} from './injector';
19-
import {
20-
DecoratorFlags,
21-
InjectFlags,
22-
InjectOptions,
23-
InternalInjectFlags,
24-
} from './interface/injector';
19+
import {DecoratorFlags, InternalInjectFlags, InjectOptions} from './interface/injector';
2520
import {ProviderToken} from './provider_token';
2621
import type {HostAttributeToken} from './host_attribute_token';
2722
import {
@@ -52,12 +47,12 @@ const DI_DECORATOR_FLAG = '__NG_DI_FLAG__';
5247
export class RetrievingInjector implements PrimitivesInjector {
5348
constructor(readonly injector: Injector) {}
5449
retrieve<T>(token: PrimitivesInjectionToken<T>, options: unknown): T | NotFound {
55-
const flags: InjectFlags =
56-
convertToBitFlags(options as InjectOptions | undefined) || InjectFlags.Default;
50+
const flags: InternalInjectFlags =
51+
convertToBitFlags(options as InjectOptions | undefined) || InternalInjectFlags.Default;
5752
return (this.injector as BackwardsCompatibleInjector).get(
5853
token as unknown as InjectionToken<T>,
5954
// When a dependency is requested with an optional flag, DI returns null as the default value.
60-
flags & InjectFlags.Optional ? null : undefined,
55+
flags & InternalInjectFlags.Optional ? null : undefined,
6156
flags,
6257
) as T;
6358
}
@@ -74,14 +69,21 @@ export const SOURCE = '__source';
7469
* removed once we consolidate the flags and the object literal approach.
7570
*/
7671
export type BackwardsCompatibleInjector = Injector & {
77-
get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectFlags | InjectOptions): T;
72+
get<T>(
73+
token: ProviderToken<T>,
74+
notFoundValue?: T,
75+
options?: InternalInjectFlags | InjectOptions,
76+
): T;
7877
};
7978

8079
export function injectInjectorOnly<T>(token: ProviderToken<T>): T;
81-
export function injectInjectorOnly<T>(token: ProviderToken<T>, flags?: InjectFlags): T | null;
8280
export function injectInjectorOnly<T>(
8381
token: ProviderToken<T>,
84-
flags = InjectFlags.Default,
82+
flags?: InternalInjectFlags,
83+
): T | null;
84+
export function injectInjectorOnly<T>(
85+
token: ProviderToken<T>,
86+
flags = InternalInjectFlags.Default,
8587
): T | null {
8688
const currentInjector = getCurrentInjector();
8789
if (currentInjector === undefined) {
@@ -113,16 +115,16 @@ export function injectInjectorOnly<T>(
113115
* @publicApi This instruction has been emitted by ViewEngine for some time and is deployed to npm.
114116
*/
115117
export function ɵɵinject<T>(token: ProviderToken<T>): T;
116-
export function ɵɵinject<T>(token: ProviderToken<T>, flags?: InjectFlags): T | null;
118+
export function ɵɵinject<T>(token: ProviderToken<T>, flags?: InternalInjectFlags): T | null;
117119
export function ɵɵinject(token: HostAttributeToken): string;
118-
export function ɵɵinject(token: HostAttributeToken, flags?: InjectFlags): string | null;
120+
export function ɵɵinject(token: HostAttributeToken, flags?: InternalInjectFlags): string | null;
119121
export function ɵɵinject<T>(
120122
token: ProviderToken<T> | HostAttributeToken,
121-
flags?: InjectFlags,
123+
flags?: InternalInjectFlags,
122124
): string | null;
123125
export function ɵɵinject<T>(
124126
token: ProviderToken<T> | HostAttributeToken,
125-
flags = InjectFlags.Default,
127+
flags = InternalInjectFlags.Default,
126128
): T | null {
127129
return (getInjectImplementation() || injectInjectorOnly)(
128130
resolveForwardRef(token as Type<T>),
@@ -279,8 +281,8 @@ export function inject<T>(token: ProviderToken<T> | HostAttributeToken, options?
279281

280282
// Converts object-based DI flags (`InjectOptions`) to bit flags (`InjectFlags`).
281283
export function convertToBitFlags(
282-
flags: InjectOptions | InjectFlags | undefined,
283-
): InjectFlags | undefined {
284+
flags: InjectOptions | InternalInjectFlags | undefined,
285+
): InternalInjectFlags | undefined {
284286
if (typeof flags === 'undefined' || typeof flags === 'number') {
285287
return flags;
286288
}
@@ -292,11 +294,11 @@ export function convertToBitFlags(
292294
((flags.optional && InternalInjectFlags.Optional) as number) |
293295
((flags.host && InternalInjectFlags.Host) as number) |
294296
((flags.self && InternalInjectFlags.Self) as number) |
295-
((flags.skipSelf && InternalInjectFlags.SkipSelf) as number)) as InjectFlags;
297+
((flags.skipSelf && InternalInjectFlags.SkipSelf) as number)) as InternalInjectFlags;
296298
}
297299

298300
// Converts bitflags to inject options
299-
function convertToInjectOptions(flags: InjectFlags): InjectOptions {
301+
function convertToInjectOptions(flags: InternalInjectFlags): InjectOptions {
300302
return {
301303
optional: !!(flags & InternalInjectFlags.Optional),
302304
host: !!(flags & InternalInjectFlags.Host),
@@ -317,7 +319,7 @@ export function injectArgs(types: (ProviderToken<any> | any[])[]): any[] {
317319
);
318320
}
319321
let type: Type<any> | undefined = undefined;
320-
let flags: InjectFlags = InjectFlags.Default;
322+
let flags: InternalInjectFlags = InternalInjectFlags.Default;
321323

322324
for (let j = 0; j < arg.length; j++) {
323325
const meta = arg[j];

packages/core/src/di/interface/injector.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,6 @@ export const enum DecoratorFlags {
1515
Inject = -1,
1616
}
1717

18-
/**
19-
* Injection flags for DI.
20-
*
21-
* @publicApi
22-
* @deprecated use an options object for [`inject`](api/core/inject) instead.
23-
*/
24-
export enum InjectFlags {
25-
// TODO(alxhub): make this 'const' (and remove `InternalInjectFlags` enum) when ngc no longer
26-
// writes exports of it into ngfactory files.
27-
28-
/** Check self and check parent injector if needed */
29-
Default = 0b0000,
30-
31-
/**
32-
* Specifies that an injector should retrieve a dependency from any injector until reaching the
33-
* host element of the current component. (Only used with Element Injector)
34-
*/
35-
Host = 0b0001,
36-
37-
/** Don't ascend to ancestors of the node requesting injection. */
38-
Self = 0b0010,
39-
40-
/** Skip the node that is requesting injection. */
41-
SkipSelf = 0b0100,
42-
43-
/** Inject `defaultValue` instead if token not found. */
44-
Optional = 0b1000,
45-
}
46-
4718
/**
4819
* This enum is an exact copy of the `InjectFlags` enum above, but the difference is that this is a
4920
* const enum, so actual enum values would be inlined in generated code. The `InjectFlags` enum can

packages/core/src/di/r3_injector.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import {
5252
InjectorType,
5353
ɵɵInjectableDeclaration,
5454
} from './interface/defs';
55-
import {InjectFlags, InjectOptions} from './interface/injector';
55+
import {InternalInjectFlags, InjectOptions} from './interface/injector';
5656
import {
5757
ClassProvider,
5858
ConstructorProvider,
@@ -236,12 +236,12 @@ export class R3Injector extends EnvironmentInjector implements PrimitivesInjecto
236236
}
237237

238238
retrieve<T>(token: PrimitivesInjectionToken<T>, options?: unknown): T | NotFound {
239-
const flags: InjectFlags =
240-
convertToBitFlags(options as InjectOptions | undefined) || InjectFlags.Default;
239+
const flags: InternalInjectFlags =
240+
convertToBitFlags(options as InjectOptions | undefined) || InternalInjectFlags.Default;
241241
return (this as BackwardsCompatibleInjector).get(
242242
token as unknown as InjectionToken<T>,
243243
// When a dependency is requested with an optional flag, DI returns null as the default value.
244-
flags & InjectFlags.Optional ? null : undefined,
244+
flags & InternalInjectFlags.Optional ? null : undefined,
245245
flags,
246246
)!;
247247
}
@@ -316,7 +316,7 @@ export class R3Injector extends EnvironmentInjector implements PrimitivesInjecto
316316
return (token as any)[NG_ENV_ID](this);
317317
}
318318

319-
const flags = convertToBitFlags(options) as InjectFlags;
319+
const flags = convertToBitFlags(options) as InternalInjectFlags;
320320

321321
// Set the injection context.
322322
let prevInjectContext: InjectorProfilerContext;
@@ -327,7 +327,7 @@ export class R3Injector extends EnvironmentInjector implements PrimitivesInjecto
327327
const previousInjectImplementation = setInjectImplementation(undefined);
328328
try {
329329
// Check for the SkipSelf flag.
330-
if (!(flags & InjectFlags.SkipSelf)) {
330+
if (!(flags & InternalInjectFlags.SkipSelf)) {
331331
// SkipSelf isn't set, check if the record belongs to this injector.
332332
let record: Record<T> | undefined | null = this.records.get(token);
333333
if (record === undefined) {
@@ -358,11 +358,13 @@ export class R3Injector extends EnvironmentInjector implements PrimitivesInjecto
358358

359359
// Select the next injector based on the Self flag - if self is set, the next injector is
360360
// the NullInjector, otherwise it's the parent.
361-
const nextInjector = !(flags & InjectFlags.Self) ? this.parent : getNullInjector();
361+
const nextInjector = !(flags & InternalInjectFlags.Self) ? this.parent : getNullInjector();
362362
// Set the notFoundValue based on the Optional flag - if optional is set and notFoundValue
363363
// is undefined, the value is null, otherwise it's the notFoundValue.
364364
notFoundValue =
365-
flags & InjectFlags.Optional && notFoundValue === THROW_IF_NOT_FOUND ? null : notFoundValue;
365+
flags & InternalInjectFlags.Optional && notFoundValue === THROW_IF_NOT_FOUND
366+
? null
367+
: notFoundValue;
366368
return nextInjector.get(token, notFoundValue);
367369
} catch (e: any) {
368370
if (e.name === 'NullInjectorError') {

packages/core/src/render3/debug/injector_profiler.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {FactoryProvider, ProviderToken} from '../../di';
1010
import {resolveForwardRef} from '../../di/forward_ref';
1111
import {InjectionToken} from '../../di/injection_token';
1212
import type {Injector} from '../../di/injector';
13-
import {InjectFlags, InjectOptions, InternalInjectFlags} from '../../di/interface/injector';
13+
import {InjectOptions, InternalInjectFlags} from '../../di/interface/injector';
1414
import type {SingleProvider} from '../../di/provider_collection';
1515
import {Type} from '../../interface/type';
1616
import {throwError} from '../../util/assert';
@@ -163,7 +163,7 @@ export interface InjectedService {
163163
/**
164164
* Flags that this service was injected with
165165
*/
166-
flags?: InternalInjectFlags | InjectFlags | InjectOptions;
166+
flags?: InternalInjectFlags | InjectOptions;
167167

168168
/**
169169
* Injector that this service was provided in.
@@ -303,7 +303,11 @@ export function emitInstanceCreatedByInjectorEvent(instance: unknown): void {
303303
* @param value the instance of the injected service (i.e the result of `inject(token)`)
304304
* @param flags the flags that the token was injected with
305305
*/
306-
export function emitInjectEvent(token: Type<unknown>, value: unknown, flags: InjectFlags): void {
306+
export function emitInjectEvent(
307+
token: Type<unknown>,
308+
value: unknown,
309+
flags: InternalInjectFlags,
310+
): void {
307311
!ngDevMode && throwError('Injector profiler should never be called in production mode');
308312

309313
injectorProfiler({

0 commit comments

Comments
 (0)