2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 12:08:49 -07:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-06-23 09:47:54 -07:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2019-08-19 15:05:29 -07:00
|
|
|
import {AbstractType, Type} from '../interface/type';
|
2019-01-09 13:49:16 -08:00
|
|
|
import {stringify} from '../util/stringify';
|
2017-07-27 13:49:33 -07:00
|
|
|
import {resolveForwardRef} from './forward_ref';
|
2017-01-03 16:54:46 -08:00
|
|
|
import {InjectionToken} from './injection_token';
|
2020-11-09 21:09:18 -08:00
|
|
|
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';
|
2019-08-22 19:19:41 -07:00
|
|
|
import {getInjectableDef, ɵɵdefineInjectable} from './interface/defs';
|
2019-01-11 16:07:01 -08:00
|
|
|
import {InjectFlags} from './interface/injector';
|
|
|
|
|
import {ConstructorProvider, ExistingProvider, FactoryProvider, StaticClassProvider, StaticProvider, ValueProvider} from './interface/provider';
|
2017-07-27 13:49:33 -07:00
|
|
|
import {Inject, Optional, Self, SkipSelf} from './metadata';
|
2020-11-09 21:09:18 -08:00
|
|
|
import {NullInjector} from './null_injector';
|
2019-04-30 20:24:00 -07:00
|
|
|
import {createInjector} from './r3_injector';
|
2019-08-22 19:19:41 -07:00
|
|
|
import {INJECTOR_SCOPE} from './scope';
|
2015-09-02 10:21:28 -07:00
|
|
|
|
2019-04-30 20:24:00 -07:00
|
|
|
export function INJECTOR_IMPL__PRE_R3__(
|
2020-04-13 16:40:21 -07:00
|
|
|
providers: StaticProvider[], parent: Injector|undefined, name: string) {
|
2019-04-30 20:24:00 -07:00
|
|
|
return new StaticInjector(providers, parent, name);
|
|
|
|
|
}
|
2018-02-16 08:45:21 -08:00
|
|
|
|
2019-04-30 20:24:00 -07:00
|
|
|
export function INJECTOR_IMPL__POST_R3__(
|
2020-04-13 16:40:21 -07:00
|
|
|
providers: StaticProvider[], parent: Injector|undefined, name: string) {
|
2019-04-30 20:24:00 -07:00
|
|
|
return createInjector({name: name}, parent, providers, name);
|
2016-06-28 09:54:42 -07:00
|
|
|
}
|
|
|
|
|
|
2019-04-30 20:24:00 -07:00
|
|
|
export const INJECTOR_IMPL = INJECTOR_IMPL__PRE_R3__;
|
|
|
|
|
|
2016-05-25 15:00:05 -07:00
|
|
|
/**
|
2019-12-03 12:41:21 -08:00
|
|
|
* Concrete injectors implement this interface. Injectors are configured
|
|
|
|
|
* with [providers](guide/glossary#provider) that associate
|
|
|
|
|
* dependencies of various types with [injection tokens](guide/glossary#di-token).
|
2018-04-05 10:16:31 +01:00
|
|
|
*
|
2019-12-03 12:41:21 -08:00
|
|
|
* @see ["DI Providers"](guide/dependency-injection-providers).
|
|
|
|
|
* @see `StaticProvider`
|
2016-09-14 09:43:01 -07:00
|
|
|
*
|
2018-05-18 16:13:00 +01:00
|
|
|
* @usageNotes
|
2019-12-03 12:41:21 -08:00
|
|
|
*
|
|
|
|
|
* The following example creates a service injector instance.
|
|
|
|
|
*
|
|
|
|
|
* {@example core/di/ts/provider_spec.ts region='ConstructorProvider'}
|
|
|
|
|
*
|
|
|
|
|
* ### Usage example
|
2016-09-14 09:43:01 -07:00
|
|
|
*
|
|
|
|
|
* {@example core/di/ts/injector_spec.ts region='Injector'}
|
|
|
|
|
*
|
|
|
|
|
* `Injector` returns itself when given `Injector` as a token:
|
2018-05-18 16:13:00 +01:00
|
|
|
*
|
2016-09-14 09:43:01 -07:00
|
|
|
* {@example core/di/ts/injector_spec.ts region='injectInjector'}
|
|
|
|
|
*
|
2018-10-19 16:27:04 +01:00
|
|
|
* @publicApi
|
2016-05-25 15:00:05 -07:00
|
|
|
*/
|
2016-01-06 14:13:44 -08:00
|
|
|
export abstract class Injector {
|
2019-04-30 20:24:00 -07:00
|
|
|
static THROW_IF_NOT_FOUND = THROW_IF_NOT_FOUND;
|
2018-02-16 08:45:21 -08:00
|
|
|
static NULL: Injector = new NullInjector();
|
2015-10-10 22:11:13 -07:00
|
|
|
|
2015-06-29 11:15:49 -07:00
|
|
|
/**
|
2015-09-21 14:19:03 -07:00
|
|
|
* Retrieves an instance from the injector based on the provided token.
|
2018-06-27 22:08:48 +02:00
|
|
|
* @returns The instance from the injector if defined, otherwise the `notFoundValue`.
|
|
|
|
|
* @throws When the `notFoundValue` is `undefined` or `Injector.THROW_IF_NOT_FOUND`.
|
2015-06-29 11:15:49 -07:00
|
|
|
*/
|
2019-08-19 15:05:29 -07:00
|
|
|
abstract get<T>(
|
2020-07-07 01:27:23 +00:00
|
|
|
token: Type<T>|AbstractType<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): T;
|
2017-01-03 16:54:46 -08:00
|
|
|
/**
|
2020-07-07 01:27:23 +00:00
|
|
|
* @deprecated from v4.0.0 use Type<T>, AbstractType<T> or InjectionToken<T>
|
2017-01-30 16:25:15 -08:00
|
|
|
* @suppress {duplicate}
|
2017-01-03 16:54:46 -08:00
|
|
|
*/
|
2017-01-25 22:32:32 -08:00
|
|
|
abstract get(token: any, notFoundValue?: any): any;
|
2017-07-27 13:49:33 -07:00
|
|
|
|
2017-12-06 10:13:50 +01:00
|
|
|
/**
|
|
|
|
|
* @deprecated from v5 use the new signature Injector.create(options)
|
|
|
|
|
*/
|
|
|
|
|
static create(providers: StaticProvider[], parent?: Injector): Injector;
|
|
|
|
|
|
2017-07-27 13:49:33 -07:00
|
|
|
/**
|
2019-12-03 12:41:21 -08:00
|
|
|
* Creates a new injector instance that provides one or more dependencies,
|
|
|
|
|
* according to a given type or types of `StaticProvider`.
|
2017-07-27 13:49:33 -07:00
|
|
|
*
|
2019-12-03 12:41:21 -08:00
|
|
|
* @param options An object with the following properties:
|
|
|
|
|
* * `providers`: An array of providers of the [StaticProvider type](api/core/StaticProvider).
|
|
|
|
|
* * `parent`: (optional) A parent injector.
|
|
|
|
|
* * `name`: (optional) A developer-defined identifying name for the new injector.
|
|
|
|
|
*
|
|
|
|
|
* @returns The new injector instance.
|
2017-07-27 13:49:33 -07:00
|
|
|
*
|
|
|
|
|
*/
|
2019-12-03 12:41:21 -08:00
|
|
|
static create(options: {providers: StaticProvider[], parent?: Injector, name?: string}): Injector;
|
|
|
|
|
|
|
|
|
|
|
2017-12-06 10:13:50 +01:00
|
|
|
static create(
|
|
|
|
|
options: StaticProvider[]|{providers: StaticProvider[], parent?: Injector, name?: string},
|
|
|
|
|
parent?: Injector): Injector {
|
|
|
|
|
if (Array.isArray(options)) {
|
2019-04-30 20:24:00 -07:00
|
|
|
return INJECTOR_IMPL(options, parent, '');
|
2017-12-06 10:13:50 +01:00
|
|
|
} else {
|
2019-04-30 20:24:00 -07:00
|
|
|
return INJECTOR_IMPL(options.providers, options.parent, options.name || '');
|
2017-12-06 10:13:50 +01:00
|
|
|
}
|
2017-07-27 13:49:33 -07:00
|
|
|
}
|
2018-02-16 08:45:21 -08:00
|
|
|
|
2018-11-05 15:10:04 -08:00
|
|
|
/** @nocollapse */
|
2021-03-05 18:20:04 -08:00
|
|
|
static ɵprov = /** @pureOrBreakMyCode */ ɵɵdefineInjectable({
|
2019-06-07 10:12:07 -07:00
|
|
|
token: Injector,
|
2018-02-16 08:45:21 -08:00
|
|
|
providedIn: 'any' as any,
|
2019-05-17 18:49:21 -07:00
|
|
|
factory: () => ɵɵinject(INJECTOR),
|
2018-02-16 08:45:21 -08:00
|
|
|
});
|
2018-10-23 14:28:15 -07:00
|
|
|
|
2019-01-10 23:45:02 -08:00
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
* @nocollapse
|
|
|
|
|
*/
|
2020-11-09 21:09:18 -08:00
|
|
|
static __NG_ELEMENT_ID__ = InjectorMarkers.Injector;
|
2017-07-27 13:49:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const IDENT = function<T>(value: T): T {
|
|
|
|
|
return value;
|
|
|
|
|
};
|
|
|
|
|
const EMPTY = <any[]>[];
|
|
|
|
|
const CIRCULAR = IDENT;
|
|
|
|
|
const MULTI_PROVIDER_FN = function(): any[] {
|
|
|
|
|
return Array.prototype.slice.call(arguments);
|
|
|
|
|
};
|
2019-04-30 20:24:00 -07:00
|
|
|
|
2017-07-27 13:49:33 -07:00
|
|
|
const enum OptionFlags {
|
|
|
|
|
Optional = 1 << 0,
|
|
|
|
|
CheckSelf = 1 << 1,
|
|
|
|
|
CheckParent = 1 << 2,
|
|
|
|
|
Default = CheckSelf | CheckParent
|
|
|
|
|
}
|
|
|
|
|
const NO_NEW_LINE = 'ɵ';
|
|
|
|
|
|
|
|
|
|
export class StaticInjector implements Injector {
|
|
|
|
|
readonly parent: Injector;
|
2017-12-06 10:13:50 +01:00
|
|
|
readonly source: string|null;
|
2019-08-22 19:19:41 -07:00
|
|
|
readonly scope: string|null;
|
2017-07-27 13:49:33 -07:00
|
|
|
|
2019-08-22 19:19:41 -07:00
|
|
|
private _records: Map<any, Record|null>;
|
2017-07-27 13:49:33 -07:00
|
|
|
|
2017-12-06 10:13:50 +01:00
|
|
|
constructor(
|
2019-05-02 16:44:24 +01:00
|
|
|
providers: StaticProvider[], parent: Injector = Injector.NULL, source: string|null = null) {
|
2017-07-27 13:49:33 -07:00
|
|
|
this.parent = parent;
|
2017-12-06 10:13:50 +01:00
|
|
|
this.source = source;
|
2017-07-27 13:49:33 -07:00
|
|
|
const records = this._records = new Map<any, Record>();
|
|
|
|
|
records.set(
|
|
|
|
|
Injector, <Record>{token: Injector, fn: IDENT, deps: EMPTY, value: this, useNew: false});
|
2018-02-16 08:45:21 -08:00
|
|
|
records.set(
|
2018-03-26 16:22:46 -07:00
|
|
|
INJECTOR, <Record>{token: INJECTOR, fn: IDENT, deps: EMPTY, value: this, useNew: false});
|
2019-08-22 19:19:41 -07:00
|
|
|
this.scope = recursivelyProcessProviders(records, providers);
|
2017-07-27 13:49:33 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-07 01:27:23 +00:00
|
|
|
get<T>(token: Type<T>|AbstractType<T>|InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags):
|
|
|
|
|
T;
|
2017-07-27 13:49:33 -07:00
|
|
|
get(token: any, notFoundValue?: any): any;
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
get(token: any, notFoundValue?: any, flags: InjectFlags = InjectFlags.Default): any {
|
2019-08-22 19:19:41 -07:00
|
|
|
const records = this._records;
|
|
|
|
|
let record = records.get(token);
|
|
|
|
|
if (record === undefined) {
|
|
|
|
|
// This means we have never seen this record, see if it is tree shakable provider.
|
|
|
|
|
const injectableDef = getInjectableDef(token);
|
|
|
|
|
if (injectableDef) {
|
2021-04-02 09:00:26 +02:00
|
|
|
const providedIn = injectableDef && resolveForwardRef(injectableDef.providedIn);
|
2019-08-22 19:19:41 -07:00
|
|
|
if (providedIn === 'any' || providedIn != null && providedIn === this.scope) {
|
|
|
|
|
records.set(
|
2020-04-13 16:40:21 -07:00
|
|
|
token,
|
|
|
|
|
record = resolveProvider(
|
|
|
|
|
{provide: token, useFactory: injectableDef.factory, deps: EMPTY}));
|
2019-08-22 19:19:41 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (record === undefined) {
|
|
|
|
|
// Set record to null to make sure that we don't go through expensive lookup above again.
|
|
|
|
|
records.set(token, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let lastInjector = setCurrentInjector(this);
|
2017-07-27 13:49:33 -07:00
|
|
|
try {
|
2019-08-22 19:19:41 -07:00
|
|
|
return tryResolveToken(token, record, records, this.parent, notFoundValue, flags);
|
2017-07-27 13:49:33 -07:00
|
|
|
} catch (e) {
|
2019-01-17 18:48:39 +01:00
|
|
|
return catchInjectorError(e, token, 'StaticInjectorError', this.source);
|
2019-08-22 19:19:41 -07:00
|
|
|
} finally {
|
|
|
|
|
setCurrentInjector(lastInjector);
|
2017-07-27 13:49:33 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
|
const tokens = <string[]>[], records = this._records;
|
|
|
|
|
records.forEach((v, token) => tokens.push(stringify(token)));
|
|
|
|
|
return `StaticInjector[${tokens.join(', ')}]`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SupportedProvider =
|
2020-04-13 16:40:21 -07:00
|
|
|
ValueProvider|ExistingProvider|StaticClassProvider|ConstructorProvider|FactoryProvider;
|
2017-07-27 13:49:33 -07:00
|
|
|
|
|
|
|
|
interface Record {
|
|
|
|
|
fn: Function;
|
|
|
|
|
useNew: boolean;
|
|
|
|
|
deps: DependencyRecord[];
|
|
|
|
|
value: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface DependencyRecord {
|
|
|
|
|
token: any;
|
|
|
|
|
options: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resolveProvider(provider: SupportedProvider): Record {
|
|
|
|
|
const deps = computeDeps(provider);
|
|
|
|
|
let fn: Function = IDENT;
|
|
|
|
|
let value: any = EMPTY;
|
|
|
|
|
let useNew: boolean = false;
|
|
|
|
|
let provide = resolveForwardRef(provider.provide);
|
|
|
|
|
if (USE_VALUE in provider) {
|
|
|
|
|
// We need to use USE_VALUE in provider since provider.useValue could be defined as undefined.
|
|
|
|
|
value = (provider as ValueProvider).useValue;
|
|
|
|
|
} else if ((provider as FactoryProvider).useFactory) {
|
|
|
|
|
fn = (provider as FactoryProvider).useFactory;
|
|
|
|
|
} else if ((provider as ExistingProvider).useExisting) {
|
|
|
|
|
// Just use IDENT
|
|
|
|
|
} else if ((provider as StaticClassProvider).useClass) {
|
|
|
|
|
useNew = true;
|
|
|
|
|
fn = resolveForwardRef((provider as StaticClassProvider).useClass);
|
|
|
|
|
} else if (typeof provide == 'function') {
|
|
|
|
|
useNew = true;
|
|
|
|
|
fn = provide;
|
|
|
|
|
} else {
|
|
|
|
|
throw staticError(
|
|
|
|
|
'StaticProvider does not have [useValue|useFactory|useExisting|useClass] or [provide] is not newable',
|
|
|
|
|
provider);
|
|
|
|
|
}
|
|
|
|
|
return {deps, fn, useNew, value};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function multiProviderMixError(token: any) {
|
|
|
|
|
return staticError('Cannot mix multi providers and regular providers', token);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 19:19:41 -07:00
|
|
|
function recursivelyProcessProviders(records: Map<any, Record>, provider: StaticProvider): string|
|
|
|
|
|
null {
|
|
|
|
|
let scope: string|null = null;
|
2017-07-27 13:49:33 -07:00
|
|
|
if (provider) {
|
|
|
|
|
provider = resolveForwardRef(provider);
|
2019-10-10 12:03:27 +03:00
|
|
|
if (Array.isArray(provider)) {
|
2017-07-27 13:49:33 -07:00
|
|
|
// if we have an array recurse into the array
|
|
|
|
|
for (let i = 0; i < provider.length; i++) {
|
2019-08-22 19:19:41 -07:00
|
|
|
scope = recursivelyProcessProviders(records, provider[i]) || scope;
|
2017-07-27 13:49:33 -07:00
|
|
|
}
|
|
|
|
|
} else if (typeof provider === 'function') {
|
|
|
|
|
// Functions were supported in ReflectiveInjector, but are not here. For safety give useful
|
|
|
|
|
// error messages
|
|
|
|
|
throw staticError('Function/Class not supported', provider);
|
|
|
|
|
} else if (provider && typeof provider === 'object' && provider.provide) {
|
|
|
|
|
// At this point we have what looks like a provider: {provide: ?, ....}
|
|
|
|
|
let token = resolveForwardRef(provider.provide);
|
|
|
|
|
const resolvedProvider = resolveProvider(provider);
|
|
|
|
|
if (provider.multi === true) {
|
|
|
|
|
// This is a multi provider.
|
|
|
|
|
let multiProvider: Record|undefined = records.get(token);
|
|
|
|
|
if (multiProvider) {
|
|
|
|
|
if (multiProvider.fn !== MULTI_PROVIDER_FN) {
|
|
|
|
|
throw multiProviderMixError(token);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Create a placeholder factory which will look up the constituents of the multi provider.
|
|
|
|
|
records.set(token, multiProvider = <Record>{
|
|
|
|
|
token: provider.provide,
|
|
|
|
|
deps: [],
|
|
|
|
|
useNew: false,
|
|
|
|
|
fn: MULTI_PROVIDER_FN,
|
|
|
|
|
value: EMPTY
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
// Treat the provider as the token.
|
|
|
|
|
token = provider;
|
|
|
|
|
multiProvider.deps.push({token, options: OptionFlags.Default});
|
|
|
|
|
}
|
|
|
|
|
const record = records.get(token);
|
|
|
|
|
if (record && record.fn == MULTI_PROVIDER_FN) {
|
|
|
|
|
throw multiProviderMixError(token);
|
|
|
|
|
}
|
2019-08-22 19:19:41 -07:00
|
|
|
if (token === INJECTOR_SCOPE) {
|
|
|
|
|
scope = resolvedProvider.value;
|
|
|
|
|
}
|
2017-07-27 13:49:33 -07:00
|
|
|
records.set(token, resolvedProvider);
|
|
|
|
|
} else {
|
|
|
|
|
throw staticError('Unexpected provider', provider);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-22 19:19:41 -07:00
|
|
|
return scope;
|
2017-07-27 13:49:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function tryResolveToken(
|
2020-04-13 16:40:21 -07:00
|
|
|
token: any, record: Record|undefined|null, records: Map<any, Record|null>, parent: Injector,
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
notFoundValue: any, flags: InjectFlags): any {
|
2017-07-27 13:49:33 -07:00
|
|
|
try {
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
return resolveToken(token, record, records, parent, notFoundValue, flags);
|
2017-07-27 13:49:33 -07:00
|
|
|
} catch (e) {
|
|
|
|
|
// ensure that 'e' is of type Error.
|
|
|
|
|
if (!(e instanceof Error)) {
|
|
|
|
|
e = new Error(e);
|
|
|
|
|
}
|
|
|
|
|
const path: any[] = e[NG_TEMP_TOKEN_PATH] = e[NG_TEMP_TOKEN_PATH] || [];
|
|
|
|
|
path.unshift(token);
|
|
|
|
|
if (record && record.value == CIRCULAR) {
|
|
|
|
|
// Reset the Circular flag.
|
|
|
|
|
record.value = EMPTY;
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resolveToken(
|
2020-04-13 16:40:21 -07:00
|
|
|
token: any, record: Record|undefined|null, records: Map<any, Record|null>, parent: Injector,
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
notFoundValue: any, flags: InjectFlags): any {
|
2017-07-27 13:49:33 -07:00
|
|
|
let value;
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
if (record && !(flags & InjectFlags.SkipSelf)) {
|
2017-07-27 13:49:33 -07:00
|
|
|
// If we don't have a record, this implies that we don't own the provider hence don't know how
|
|
|
|
|
// to resolve it.
|
|
|
|
|
value = record.value;
|
|
|
|
|
if (value == CIRCULAR) {
|
|
|
|
|
throw Error(NO_NEW_LINE + 'Circular dependency');
|
|
|
|
|
} else if (value === EMPTY) {
|
|
|
|
|
record.value = CIRCULAR;
|
|
|
|
|
let obj = undefined;
|
|
|
|
|
let useNew = record.useNew;
|
|
|
|
|
let fn = record.fn;
|
|
|
|
|
let depRecords = record.deps;
|
|
|
|
|
let deps = EMPTY;
|
|
|
|
|
if (depRecords.length) {
|
|
|
|
|
deps = [];
|
|
|
|
|
for (let i = 0; i < depRecords.length; i++) {
|
|
|
|
|
const depRecord: DependencyRecord = depRecords[i];
|
|
|
|
|
const options = depRecord.options;
|
|
|
|
|
const childRecord =
|
|
|
|
|
options & OptionFlags.CheckSelf ? records.get(depRecord.token) : undefined;
|
|
|
|
|
deps.push(tryResolveToken(
|
|
|
|
|
// Current Token to resolve
|
|
|
|
|
depRecord.token,
|
|
|
|
|
// A record which describes how to resolve the token.
|
|
|
|
|
// If undefined, this means we don't have such a record
|
|
|
|
|
childRecord,
|
|
|
|
|
// Other records we know about.
|
|
|
|
|
records,
|
|
|
|
|
// If we don't know how to resolve dependency and we should not check parent for it,
|
|
|
|
|
// than pass in Null injector.
|
2019-05-02 16:44:24 +01:00
|
|
|
!childRecord && !(options & OptionFlags.CheckParent) ? Injector.NULL : parent,
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
options & OptionFlags.Optional ? null : Injector.THROW_IF_NOT_FOUND,
|
|
|
|
|
InjectFlags.Default));
|
2017-07-27 13:49:33 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
record.value = value = useNew ? new (fn as any)(...deps) : fn.apply(obj, deps);
|
|
|
|
|
}
|
feat: change @Injectable() to support tree-shakeable tokens (#22005)
This commit bundles 3 important changes, with the goal of enabling tree-shaking
of services which are never injected. Ordinarily, this tree-shaking is prevented
by the existence of a hard dependency on the service by the module in which it
is declared.
Firstly, @Injectable() is modified to accept a 'scope' parameter, which points
to an @NgModule(). This reverses the dependency edge, permitting the module to
not depend on the service which it "provides".
Secondly, the runtime is modified to understand the new relationship created
above. When a module receives a request to inject a token, and cannot find that
token in its list of providers, it will then look at the token for a special
ngInjectableDef field which indicates which module the token is scoped to. If
that module happens to be in the injector, it will behave as if the token
itself was in the injector to begin with.
Thirdly, the compiler is modified to read the @Injectable() metadata and to
generate the special ngInjectableDef field as part of TS compilation, using the
PartialModules system.
Additionally, this commit adds several unit and integration tests of various
flavors to test this change.
PR Close #22005
2018-02-02 10:33:48 -08:00
|
|
|
} else if (!(flags & InjectFlags.Self)) {
|
|
|
|
|
value = parent.get(token, notFoundValue, InjectFlags.Default);
|
2019-04-10 14:42:41 +03:00
|
|
|
} else if (!(flags & InjectFlags.Optional)) {
|
|
|
|
|
value = Injector.NULL.get(token, notFoundValue);
|
|
|
|
|
} else {
|
|
|
|
|
value = Injector.NULL.get(token, typeof notFoundValue !== 'undefined' ? notFoundValue : null);
|
2017-07-27 13:49:33 -07:00
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function computeDeps(provider: StaticProvider): DependencyRecord[] {
|
|
|
|
|
let deps: DependencyRecord[] = EMPTY;
|
|
|
|
|
const providerDeps: any[] =
|
|
|
|
|
(provider as ExistingProvider & StaticClassProvider & ConstructorProvider).deps;
|
|
|
|
|
if (providerDeps && providerDeps.length) {
|
|
|
|
|
deps = [];
|
|
|
|
|
for (let i = 0; i < providerDeps.length; i++) {
|
|
|
|
|
let options = OptionFlags.Default;
|
|
|
|
|
let token = resolveForwardRef(providerDeps[i]);
|
2019-10-10 12:03:27 +03:00
|
|
|
if (Array.isArray(token)) {
|
2017-07-27 13:49:33 -07:00
|
|
|
for (let j = 0, annotations = token; j < annotations.length; j++) {
|
|
|
|
|
const annotation = annotations[j];
|
|
|
|
|
if (annotation instanceof Optional || annotation == Optional) {
|
|
|
|
|
options = options | OptionFlags.Optional;
|
|
|
|
|
} else if (annotation instanceof SkipSelf || annotation == SkipSelf) {
|
|
|
|
|
options = options & ~OptionFlags.CheckSelf;
|
|
|
|
|
} else if (annotation instanceof Self || annotation == Self) {
|
|
|
|
|
options = options & ~OptionFlags.CheckParent;
|
|
|
|
|
} else if (annotation instanceof Inject) {
|
|
|
|
|
token = (annotation as Inject).token;
|
|
|
|
|
} else {
|
|
|
|
|
token = resolveForwardRef(annotation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
deps.push({token, options});
|
|
|
|
|
}
|
|
|
|
|
} else if ((provider as ExistingProvider).useExisting) {
|
|
|
|
|
const token = resolveForwardRef((provider as ExistingProvider).useExisting);
|
|
|
|
|
deps = [{token, options: OptionFlags.Default}];
|
|
|
|
|
} else if (!providerDeps && !(USE_VALUE in provider)) {
|
|
|
|
|
// useValue & useExisting are the only ones which are exempt from deps all others need it.
|
|
|
|
|
throw staticError('\'deps\' required', provider);
|
|
|
|
|
}
|
|
|
|
|
return deps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function staticError(text: string, obj: any): Error {
|
2019-01-17 18:48:39 +01:00
|
|
|
return new Error(formatError(text, obj, 'StaticInjectorError'));
|
2017-07-27 13:49:33 -07:00
|
|
|
}
|