Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions goldens/public-api/core/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ export declare interface ConstructorSansProvider {
export declare type ContentChild = Query;

export declare interface ContentChildDecorator {
(selector: Type<any> | InjectionToken<unknown> | Function | string, opts?: {
(selector: ProviderToken<unknown> | Function | string, opts?: {
read?: any;
static?: boolean;
}): any;
new (selector: Type<any> | InjectionToken<unknown> | Function | string, opts?: {
new (selector: ProviderToken<unknown> | Function | string, opts?: {
read?: any;
static?: boolean;
}): ContentChild;
Expand All @@ -172,12 +172,12 @@ export declare interface ContentChildDecorator {
export declare type ContentChildren = Query;

export declare interface ContentChildrenDecorator {
(selector: Type<any> | InjectionToken<unknown> | Function | string, opts?: {
(selector: ProviderToken<unknown> | Function | string, opts?: {
descendants?: boolean;
emitDistinctChangesOnly?: boolean;
read?: any;
}): any;
new (selector: Type<any> | InjectionToken<unknown> | Function | string, opts?: {
new (selector: ProviderToken<unknown> | Function | string, opts?: {
descendants?: boolean;
emitDistinctChangesOnly?: boolean;
read?: any;
Expand Down Expand Up @@ -448,7 +448,7 @@ export declare class InjectionToken<T> {
}

export declare abstract class Injector {
abstract get<T>(token: Type<T> | AbstractType<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
/** @deprecated */ abstract get(token: any, notFoundValue?: any): any;
static NULL: Injector;
static THROW_IF_NOT_FOUND: {};
Expand Down Expand Up @@ -680,8 +680,8 @@ export declare function ɵɵdefineInjectable<T>(opts: {
}): unknown;

/** @codeGenApi */
export declare function ɵɵinject<T>(token: Type<T> | AbstractType<T> | InjectionToken<T>): T;
export declare function ɵɵinject<T>(token: Type<T> | AbstractType<T> | InjectionToken<T>, flags?: InjectFlags): T | null;
export declare function ɵɵinject<T>(token: ProviderToken<T>): T;
export declare function ɵɵinject<T>(token: ProviderToken<T>, flags?: InjectFlags): T | null;

/** @codeGenApi */
export declare function ɵɵinjectAttribute(attrNameToInject: string): string | null;
Expand Down Expand Up @@ -725,6 +725,8 @@ export declare interface Predicate<T> {

export declare type Provider = TypeProvider | ValueProvider | ClassProvider | ConstructorProvider | ExistingProvider | FactoryProvider | any[];

export declare type ProviderToken<T> = Type<T> | AbstractType<T> | InjectionToken<T>;

export declare interface Query {
descendants: boolean;
emitDistinctChangesOnly: boolean;
Expand Down Expand Up @@ -989,11 +991,11 @@ export declare const VERSION: Version;
export declare type ViewChild = Query;

export declare interface ViewChildDecorator {
(selector: Type<any> | InjectionToken<unknown> | Function | string, opts?: {
(selector: ProviderToken<unknown> | Function | string, opts?: {
read?: any;
static?: boolean;
}): any;
new (selector: Type<any> | InjectionToken<unknown> | Function | string, opts?: {
new (selector: ProviderToken<unknown> | Function | string, opts?: {
read?: any;
static?: boolean;
}): ViewChild;
Expand All @@ -1002,11 +1004,11 @@ export declare interface ViewChildDecorator {
export declare type ViewChildren = Query;

export declare interface ViewChildrenDecorator {
(selector: Type<any> | InjectionToken<unknown> | Function | string, opts?: {
(selector: ProviderToken<unknown> | Function | string, opts?: {
read?: any;
emitDistinctChangesOnly?: boolean;
}): any;
new (selector: Type<any> | InjectionToken<unknown> | Function | string, opts?: {
new (selector: ProviderToken<unknown> | Function | string, opts?: {
read?: any;
emitDistinctChangesOnly?: boolean;
}): ViewChildren;
Expand Down
12 changes: 6 additions & 6 deletions goldens/public-api/core/testing/testing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ export declare interface TestBed {
configureTestingModule(moduleDef: TestModuleMetadata): void;
createComponent<T>(component: Type<T>): ComponentFixture<T>;
execute(tokens: any[], fn: Function, context?: any): any;
/** @deprecated */ get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
/** @deprecated */ get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
/** @deprecated */ get(token: any, notFoundValue?: any): any;
initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): void;
inject<T>(token: Type<T> | InjectionToken<T> | AbstractType<T>, notFoundValue?: T, flags?: InjectFlags): T;
inject<T>(token: Type<T> | InjectionToken<T> | AbstractType<T>, notFoundValue: null, flags?: InjectFlags): T | null;
inject<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
inject<T>(token: ProviderToken<T>, notFoundValue: null, flags?: InjectFlags): T | null;
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void;
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void;
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void;
Expand Down Expand Up @@ -99,11 +99,11 @@ export declare interface TestBedStatic {
}): TestBedStatic;
configureTestingModule(moduleDef: TestModuleMetadata): TestBedStatic;
createComponent<T>(component: Type<T>): ComponentFixture<T>;
/** @deprecated */ get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
/** @deprecated */ get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): any;
/** @deprecated */ get(token: any, notFoundValue?: any): any;
initTestEnvironment(ngModule: Type<any> | Type<any>[], platform: PlatformRef, aotSummaries?: () => any[]): TestBed;
inject<T>(token: Type<T> | InjectionToken<T> | AbstractType<T>, notFoundValue?: T, flags?: InjectFlags): T;
inject<T>(token: Type<T> | InjectionToken<T> | AbstractType<T>, notFoundValue: null, flags?: InjectFlags): T | null;
inject<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
inject<T>(token: ProviderToken<T>, notFoundValue: null, flags?: InjectFlags): T | null;
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): TestBedStatic;
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): TestBedStatic;
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): TestBedStatic;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/di/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export {ɵɵdefineInjectable, defineInjectable, ɵɵdefineInjector, InjectableTy
export {forwardRef, resolveForwardRef, ForwardRefFn} from './forward_ref';
export {Injectable, InjectableDecorator, InjectableProvider} from './injectable';
export {Injector} from './injector';
export {ProviderToken} from './provider_token';
export {ɵɵinject, inject, ɵɵinvalidFactoryDep} from './injector_compatibility';
export {INJECTOR} from './injector_token';
export {ReflectiveInjector} from './reflective_injector';
Expand Down
18 changes: 7 additions & 11 deletions packages/core/src/di/inject_switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* found in the LICENSE file at https://angular.io/license
*/

import {AbstractType, Type} from '../interface/type';
import {throwProviderNotFoundError} from '../render3/errors_di';
import {assertNotEqual} from '../util/assert';
import {stringify} from '../util/stringify';
import {InjectionToken} from './injection_token';

import {getInjectableDef, ɵɵInjectableDeclaration} from './interface/defs';
import {InjectFlags} from './interface/injector';
import {ProviderToken} from './provider_token';


/**
Expand All @@ -24,8 +24,7 @@ import {InjectFlags} from './interface/injector';
* 1. `Injector` should not depend on ivy logic.
* 2. To maintain tree shake-ability we don't want to bring in unnecessary code.
*/
let _injectImplementation:
(<T>(token: Type<T>|AbstractType<T>|InjectionToken<T>, flags?: InjectFlags) => T | null)|
let _injectImplementation: (<T>(token: ProviderToken<T>, flags?: InjectFlags) => T | null)|
undefined;
export function getInjectImplementation() {
return _injectImplementation;
Expand All @@ -36,10 +35,8 @@ export function getInjectImplementation() {
* Sets the current inject implementation.
*/
export function setInjectImplementation(
impl: (<T>(token: Type<T>|AbstractType<T>|InjectionToken<T>, flags?: InjectFlags) => T | null)|
undefined):
(<T>(token: Type<T>|AbstractType<T>|InjectionToken<T>, flags?: InjectFlags) => T | null)|
undefined {
impl: (<T>(token: ProviderToken<T>, flags?: InjectFlags) => T | null)|
undefined): (<T>(token: ProviderToken<T>, flags?: InjectFlags) => T | null)|undefined {
const previous = _injectImplementation;
_injectImplementation = impl;
return previous;
Expand All @@ -54,8 +51,7 @@ export function setInjectImplementation(
* injectable definition.
*/
export function injectRootLimpMode<T>(
token: Type<T>|AbstractType<T>|InjectionToken<T>, notFoundValue: T|undefined,
flags: InjectFlags): T|null {
token: ProviderToken<T>, notFoundValue: T|undefined, flags: InjectFlags): T|null {
const injectableDef: ɵɵInjectableDeclaration<T>|null = getInjectableDef(token);
if (injectableDef && injectableDef.providedIn == 'root') {
return injectableDef.value === undefined ? injectableDef.value = injectableDef.factory() :
Expand All @@ -75,7 +71,7 @@ export function injectRootLimpMode<T>(
* @param fn Function which it should not equal to
*/
export function assertInjectImplementationNotEqual(
fn: (<T>(token: Type<T>|AbstractType<T>|InjectionToken<T>, flags?: InjectFlags) => T | null)) {
fn: (<T>(token: ProviderToken<T>, flags?: InjectFlags) => T | null)) {
ngDevMode &&
assertNotEqual(_injectImplementation, fn, 'Calling ɵɵinject would cause infinite recursion');
}
12 changes: 5 additions & 7 deletions packages/core/src/di/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/

import {AbstractType, Type} from '../interface/type';
import {stringify} from '../util/stringify';

import {resolveForwardRef} from './forward_ref';
import {InjectionToken} from './injection_token';
import {catchInjectorError, formatError, NG_TEMP_TOKEN_PATH, setCurrentInjector, THROW_IF_NOT_FOUND, USE_VALUE, ɵɵinject} from './injector_compatibility';
import {InjectorMarkers} from './injector_marker';
import {INJECTOR} from './injector_token';
Expand All @@ -18,6 +17,7 @@ import {InjectFlags} from './interface/injector';
import {ConstructorProvider, ExistingProvider, FactoryProvider, StaticClassProvider, StaticProvider, ValueProvider} from './interface/provider';
import {Inject, Optional, Self, SkipSelf} from './metadata';
import {NullInjector} from './null_injector';
import {ProviderToken} from './provider_token';
import {createInjector} from './r3_injector';
import {INJECTOR_SCOPE} from './scope';

Expand Down Expand Up @@ -66,10 +66,9 @@ export abstract class Injector {
* @returns The instance from the injector if defined, otherwise the `notFoundValue`.
* @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
*/
abstract get<T>(
token: Type<T>|AbstractType<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
/**
* @deprecated from v4.0.0 use Type<T>, AbstractType<T> or InjectionToken<T>
* @deprecated from v4.0.0 use ProviderToken<T>
* @suppress {duplicate}
*/
abstract get(token: any, notFoundValue?: any): any;
Expand Down Expand Up @@ -156,8 +155,7 @@ export class StaticInjector implements Injector {
this.scope = recursivelyProcessProviders(records, providers);
}

get<T>(token: Type<T>|AbstractType<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags):
T;
get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
get(token: any, notFoundValue?: any): any;
get(token: any, notFoundValue?: any, flags: InjectFlags = InjectFlags.Default): any {
const records = this._records;
Expand Down
23 changes: 10 additions & 13 deletions packages/core/src/di/injector_compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

import '../util/ng_dev_mode';

import {AbstractType, Type} from '../interface/type';
import {Type} from '../interface/type';
import {getClosureSafeProperty} from '../util/property';
import {stringify} from '../util/stringify';

import {resolveForwardRef} from './forward_ref';
import {getInjectImplementation, injectRootLimpMode} from './inject_switch';
import {InjectionToken} from './injection_token';
import {Injector} from './injector';
import {DecoratorFlags, InjectFlags, InternalInjectFlags} from './interface/injector';
import {ValueProvider} from './interface/provider';
import {ProviderToken} from './provider_token';


const _THROW_IF_NOT_FOUND = {};
Expand Down Expand Up @@ -53,11 +53,10 @@ export function setCurrentInjector(injector: Injector|null|undefined): Injector|
return former;
}

export function injectInjectorOnly<T>(token: Type<T>|AbstractType<T>|InjectionToken<T>): T;
export function injectInjectorOnly<T>(
token: Type<T>|AbstractType<T>|InjectionToken<T>, flags?: InjectFlags): T|null;
export function injectInjectorOnly<T>(
token: Type<T>|AbstractType<T>|InjectionToken<T>, flags = InjectFlags.Default): T|null {
export function injectInjectorOnly<T>(token: ProviderToken<T>): T;
export function injectInjectorOnly<T>(token: ProviderToken<T>, flags?: InjectFlags): T|null;
export function injectInjectorOnly<T>(token: ProviderToken<T>, flags = InjectFlags.Default): T|
null {
if (_currentInjector === undefined) {
throw new Error(`inject() must be called from an injection context`);
} else if (_currentInjector === null) {
Expand All @@ -80,11 +79,9 @@ export function injectInjectorOnly<T>(
* @codeGenApi
* @publicApi This instruction has been emitted by ViewEngine for some time and is deployed to npm.
*/
export function ɵɵinject<T>(token: Type<T>|AbstractType<T>|InjectionToken<T>): T;
export function ɵɵinject<T>(
token: Type<T>|AbstractType<T>|InjectionToken<T>, flags?: InjectFlags): T|null;
export function ɵɵinject<T>(
token: Type<T>|AbstractType<T>|InjectionToken<T>, flags = InjectFlags.Default): T|null {
export function ɵɵinject<T>(token: ProviderToken<T>): T;
export function ɵɵinject<T>(token: ProviderToken<T>, flags?: InjectFlags): T|null;
export function ɵɵinject<T>(token: ProviderToken<T>, flags = InjectFlags.Default): T|null {
return (getInjectImplementation() || injectInjectorOnly)(resolveForwardRef(token), flags);
}

Expand Down Expand Up @@ -138,7 +135,7 @@ Please check that 1) the type for the parameter at index ${
*/
export const inject = ɵɵinject;

export function injectArgs(types: (Type<any>|InjectionToken<any>|any[])[]): any[] {
export function injectArgs(types: (ProviderToken<any>|any[])[]): any[] {
const args: any[] = [];
for (let i = 0; i < types.length; i++) {
const arg = resolveForwardRef(types[i]);
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/di/provider_token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @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 {AbstractType, Type} from '../interface/type';
import {InjectionToken} from './injection_token';

/**
* @description
*
* Token that can be used to retrieve an instance from an injector or through a query.
*
* @publicApi
*/
export type ProviderToken<T> = Type<T>|AbstractType<T>|InjectionToken<T>;
15 changes: 7 additions & 8 deletions packages/core/src/di/r3_injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import '../util/ng_dev_mode';

import {OnDestroy} from '../interface/lifecycle_hooks';
import {AbstractType, Type} from '../interface/type';
import {Type} from '../interface/type';
import {FactoryFn, getFactoryDef} from '../render3/definition_factory';
import {throwCyclicDependencyError, throwInvalidProviderError, throwMixedMultiProviderError} from '../render3/errors_di';
import {deepForEach, newArray} from '../util/array_utils';
Expand All @@ -25,6 +25,7 @@ import {getInheritedInjectableDef, getInjectableDef, getInjectorDef, InjectorTyp
import {InjectFlags} from './interface/injector';
import {ClassProvider, ConstructorProvider, ExistingProvider, FactoryProvider, StaticClassProvider, StaticProvider, TypeProvider, ValueProvider} from './interface/provider';
import {NullInjector} from './null_injector';
import {ProviderToken} from './provider_token';
import {INJECTOR_SCOPE} from './scope';


Expand Down Expand Up @@ -102,7 +103,7 @@ export class R3Injector {
* - `null` value implies that we don't have the record. Used by tree-shakable injectors
* to prevent further searches.
*/
private records = new Map<Type<any>|AbstractType<any>|InjectionToken<any>, Record<any>|null>();
private records = new Map<ProviderToken<any>, Record<any>|null>();

/**
* The transitive set of `InjectorType`s which define this injector.
Expand Down Expand Up @@ -180,7 +181,7 @@ export class R3Injector {
}

get<T>(
token: Type<T>|AbstractType<T>|InjectionToken<T>, notFoundValue: any = THROW_IF_NOT_FOUND,
token: ProviderToken<T>, notFoundValue: any = THROW_IF_NOT_FOUND,
flags = InjectFlags.Default): T {
this.assertNotDestroyed();
// Set the injection context.
Expand Down Expand Up @@ -404,7 +405,7 @@ export class R3Injector {
this.records.set(token, record);
}

private hydrate<T>(token: Type<T>|AbstractType<T>|InjectionToken<T>, record: Record<T>): T {
private hydrate<T>(token: ProviderToken<T>, record: Record<T>): T {
if (ngDevMode && record.value === CIRCULAR) {
throwCyclicDependencyError(stringify(token));
} else if (record.value === NOT_YET) {
Expand All @@ -430,8 +431,7 @@ export class R3Injector {
}
}

function injectableDefOrInjectorDefFactory(token: Type<any>|AbstractType<any>|
InjectionToken<any>): FactoryFn<any> {
function injectableDefOrInjectorDefFactory(token: ProviderToken<any>): FactoryFn<any> {
// Most tokens will have an injectable def directly on them, which specifies a factory directly.
const injectableDef = getInjectableDef(token);
const factory = injectableDef !== null ? injectableDef.factory : getFactoryDef(token);
Expand Down Expand Up @@ -560,8 +560,7 @@ function hasOnDestroy(value: any): value is OnDestroy {
typeof (value as OnDestroy).ngOnDestroy === 'function';
}

function couldBeInjectableType(value: any): value is Type<any>|AbstractType<any>|
InjectionToken<any> {
function couldBeInjectableType(value: any): value is ProviderToken<any> {
return (typeof value === 'function') ||
(typeof value === 'object' && value instanceof InjectionToken);
}
Loading