2017-05-11 10:26:02 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright Google Inc. 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
|
|
|
|
|
*/
|
|
|
|
|
|
2018-04-13 23:02:29 -07:00
|
|
|
import {InjectableDef} from '../di/defs';
|
2017-09-24 12:11:36 -07:00
|
|
|
import {resolveForwardRef} from '../di/forward_ref';
|
2018-02-16 08:45:21 -08:00
|
|
|
import {INJECTOR, InjectFlags, Injector, setCurrentInjector} from '../di/injector';
|
2018-03-07 15:10:38 -08:00
|
|
|
import {APP_ROOT} from '../di/scope';
|
2017-05-11 10:26:02 -07:00
|
|
|
import {NgModuleRef} from '../linker/ng_module_factory';
|
2017-12-06 10:13:50 +01:00
|
|
|
import {stringify} from '../util';
|
2017-05-11 10:26:02 -07:00
|
|
|
|
2018-03-07 15:10:38 -08:00
|
|
|
import {DepDef, DepFlags, NgModuleData, NgModuleDefinition, NgModuleProviderDef, NodeFlags} from './types';
|
2017-05-15 13:12:10 -07:00
|
|
|
import {splitDepsDsl, tokenKey} from './util';
|
2017-05-11 10:26:02 -07:00
|
|
|
|
2017-10-04 09:13:22 -07:00
|
|
|
const UNDEFINED_VALUE = new Object();
|
2017-05-11 10:26:02 -07:00
|
|
|
|
|
|
|
|
const InjectorRefTokenKey = tokenKey(Injector);
|
2018-03-26 16:22:46 -07:00
|
|
|
const INJECTORRefTokenKey = tokenKey(INJECTOR);
|
2017-05-11 10:26:02 -07:00
|
|
|
const NgModuleRefTokenKey = tokenKey(NgModuleRef);
|
|
|
|
|
|
|
|
|
|
export function moduleProvideDef(
|
|
|
|
|
flags: NodeFlags, token: any, value: any,
|
|
|
|
|
deps: ([DepFlags, any] | any)[]): NgModuleProviderDef {
|
2017-09-24 12:11:36 -07:00
|
|
|
// Need to resolve forwardRefs as e.g. for `useValue` we
|
|
|
|
|
// lowered the expression and then stopped evaluating it,
|
|
|
|
|
// i.e. also didn't unwrap it.
|
|
|
|
|
value = resolveForwardRef(value);
|
2017-12-06 10:13:50 +01:00
|
|
|
const depDefs = splitDepsDsl(deps, stringify(token));
|
2017-05-11 10:26:02 -07:00
|
|
|
return {
|
|
|
|
|
// will bet set by the module definition
|
|
|
|
|
index: -1,
|
|
|
|
|
deps: depDefs, flags, token, value
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function moduleDef(providers: NgModuleProviderDef[]): NgModuleDefinition {
|
|
|
|
|
const providersByKey: {[key: string]: NgModuleProviderDef} = {};
|
2018-02-02 09:31:58 -08:00
|
|
|
const modules = [];
|
2018-02-12 15:23:46 -08:00
|
|
|
let isRoot: boolean = false;
|
2017-05-11 10:26:02 -07:00
|
|
|
for (let i = 0; i < providers.length; i++) {
|
|
|
|
|
const provider = providers[i];
|
2018-03-07 15:10:38 -08:00
|
|
|
if (provider.token === APP_ROOT) {
|
2018-02-12 15:23:46 -08:00
|
|
|
isRoot = true;
|
|
|
|
|
}
|
2018-02-02 09:31:58 -08:00
|
|
|
if (provider.flags & NodeFlags.TypeNgModule) {
|
|
|
|
|
modules.push(provider.token);
|
|
|
|
|
}
|
2017-05-11 10:26:02 -07:00
|
|
|
provider.index = i;
|
|
|
|
|
providersByKey[tokenKey(provider.token)] = provider;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
// Will be filled later...
|
|
|
|
|
factory: null,
|
|
|
|
|
providersByKey,
|
2018-02-02 09:31:58 -08:00
|
|
|
providers,
|
|
|
|
|
modules,
|
2018-02-12 15:23:46 -08:00
|
|
|
isRoot,
|
2017-05-11 10:26:02 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function initNgModule(data: NgModuleData) {
|
|
|
|
|
const def = data._def;
|
|
|
|
|
const providers = data._providers = new Array(def.providers.length);
|
|
|
|
|
for (let i = 0; i < def.providers.length; i++) {
|
|
|
|
|
const provDef = def.providers[i];
|
2017-10-04 09:13:22 -07:00
|
|
|
if (!(provDef.flags & NodeFlags.LazyProvider)) {
|
2018-04-26 06:25:30 -07:00
|
|
|
// Make sure the provider has not been already initialized outside this loop.
|
|
|
|
|
if (providers[i] === undefined) {
|
|
|
|
|
providers[i] = _createProviderInstance(data, provDef);
|
|
|
|
|
}
|
2017-10-04 09:13:22 -07:00
|
|
|
}
|
2017-05-11 10:26:02 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function resolveNgModuleDep(
|
|
|
|
|
data: NgModuleData, depDef: DepDef, notFoundValue: any = Injector.THROW_IF_NOT_FOUND): any {
|
2018-04-03 09:54:58 -07:00
|
|
|
const former = setCurrentInjector(data);
|
|
|
|
|
try {
|
|
|
|
|
if (depDef.flags & DepFlags.Value) {
|
|
|
|
|
return depDef.token;
|
2017-05-11 10:26:02 -07:00
|
|
|
}
|
2018-04-03 09:54:58 -07:00
|
|
|
if (depDef.flags & DepFlags.Optional) {
|
|
|
|
|
notFoundValue = null;
|
|
|
|
|
}
|
|
|
|
|
if (depDef.flags & DepFlags.SkipSelf) {
|
|
|
|
|
return data._parent.get(depDef.token, notFoundValue);
|
|
|
|
|
}
|
|
|
|
|
const tokenKey = depDef.tokenKey;
|
|
|
|
|
switch (tokenKey) {
|
|
|
|
|
case InjectorRefTokenKey:
|
|
|
|
|
case INJECTORRefTokenKey:
|
|
|
|
|
case NgModuleRefTokenKey:
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
const providerDef = data._def.providersByKey[tokenKey];
|
|
|
|
|
if (providerDef) {
|
|
|
|
|
let providerInstance = data._providers[providerDef.index];
|
|
|
|
|
if (providerInstance === undefined) {
|
|
|
|
|
providerInstance = data._providers[providerDef.index] =
|
|
|
|
|
_createProviderInstance(data, providerDef);
|
|
|
|
|
}
|
|
|
|
|
return providerInstance === UNDEFINED_VALUE ? undefined : providerInstance;
|
|
|
|
|
} else if (depDef.token.ngInjectableDef && targetsModule(data, depDef.token.ngInjectableDef)) {
|
2018-04-13 23:02:29 -07:00
|
|
|
const injectableDef = depDef.token.ngInjectableDef as InjectableDef<any>;
|
2018-04-03 09:54:58 -07:00
|
|
|
const key = tokenKey;
|
|
|
|
|
const index = data._providers.length;
|
|
|
|
|
data._def.providersByKey[depDef.tokenKey] = {
|
|
|
|
|
flags: NodeFlags.TypeFactoryProvider | NodeFlags.LazyProvider,
|
|
|
|
|
value: injectableDef.factory,
|
|
|
|
|
deps: [], index,
|
|
|
|
|
token: depDef.token,
|
|
|
|
|
};
|
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
|
|
|
data._providers[index] = UNDEFINED_VALUE;
|
|
|
|
|
return (
|
|
|
|
|
data._providers[index] =
|
|
|
|
|
_createProviderInstance(data, data._def.providersByKey[depDef.tokenKey]));
|
2018-06-14 10:36:20 -07:00
|
|
|
} else if (depDef.flags & DepFlags.Self) {
|
|
|
|
|
return notFoundValue;
|
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
|
|
|
}
|
2018-04-03 09:54:58 -07:00
|
|
|
return data._parent.get(depDef.token, notFoundValue);
|
|
|
|
|
} finally {
|
|
|
|
|
setCurrentInjector(former);
|
2017-05-11 10:26:02 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 15:23:46 -08:00
|
|
|
function moduleTransitivelyPresent(ngModule: NgModuleData, scope: any): boolean {
|
|
|
|
|
return ngModule._def.modules.indexOf(scope) > -1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 23:02:29 -07:00
|
|
|
function targetsModule(ngModule: NgModuleData, def: InjectableDef<any>): boolean {
|
2018-03-07 15:10:38 -08:00
|
|
|
return def.providedIn != null && (moduleTransitivelyPresent(ngModule, def.providedIn) ||
|
|
|
|
|
def.providedIn === 'root' && ngModule._def.isRoot);
|
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
|
|
|
}
|
2017-05-11 10:26:02 -07:00
|
|
|
|
|
|
|
|
function _createProviderInstance(ngModule: NgModuleData, providerDef: NgModuleProviderDef): any {
|
2017-10-04 09:13:22 -07:00
|
|
|
let injectable: any;
|
2017-05-11 10:26:02 -07:00
|
|
|
switch (providerDef.flags & NodeFlags.Types) {
|
|
|
|
|
case NodeFlags.TypeClassProvider:
|
2017-10-04 09:13:22 -07:00
|
|
|
injectable = _createClass(ngModule, providerDef.value, providerDef.deps);
|
|
|
|
|
break;
|
2017-05-11 10:26:02 -07:00
|
|
|
case NodeFlags.TypeFactoryProvider:
|
2017-10-04 09:13:22 -07:00
|
|
|
injectable = _callFactory(ngModule, providerDef.value, providerDef.deps);
|
|
|
|
|
break;
|
2017-05-11 10:26:02 -07:00
|
|
|
case NodeFlags.TypeUseExistingProvider:
|
2017-10-04 09:13:22 -07:00
|
|
|
injectable = resolveNgModuleDep(ngModule, providerDef.deps[0]);
|
|
|
|
|
break;
|
2017-05-11 10:26:02 -07:00
|
|
|
case NodeFlags.TypeValueProvider:
|
2017-10-04 09:13:22 -07:00
|
|
|
injectable = providerDef.value;
|
|
|
|
|
break;
|
2017-05-11 10:26:02 -07:00
|
|
|
}
|
2018-05-07 10:47:59 -07:00
|
|
|
|
|
|
|
|
// The read of `ngOnDestroy` here is slightly expensive as it's megamorphic, so it should be
|
|
|
|
|
// avoided if possible. The sequence of checks here determines whether ngOnDestroy needs to be
|
|
|
|
|
// checked. It might not if the `injectable` isn't an object or if NodeFlags.OnDestroy is already
|
|
|
|
|
// set (ngOnDestroy was detected statically).
|
|
|
|
|
if (injectable !== UNDEFINED_VALUE && injectable != null && typeof injectable === 'object' &&
|
|
|
|
|
!(providerDef.flags & NodeFlags.OnDestroy) && typeof injectable.ngOnDestroy === 'function') {
|
|
|
|
|
providerDef.flags |= NodeFlags.OnDestroy;
|
|
|
|
|
}
|
2017-10-04 09:13:22 -07:00
|
|
|
return injectable === undefined ? UNDEFINED_VALUE : injectable;
|
2017-05-11 10:26:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _createClass(ngModule: NgModuleData, ctor: any, deps: DepDef[]): any {
|
|
|
|
|
const len = deps.length;
|
|
|
|
|
switch (len) {
|
|
|
|
|
case 0:
|
2017-09-22 14:29:16 -07:00
|
|
|
return new ctor();
|
2017-05-11 10:26:02 -07:00
|
|
|
case 1:
|
2017-09-22 14:29:16 -07:00
|
|
|
return new ctor(resolveNgModuleDep(ngModule, deps[0]));
|
2017-05-11 10:26:02 -07:00
|
|
|
case 2:
|
2017-09-22 14:29:16 -07:00
|
|
|
return new ctor(resolveNgModuleDep(ngModule, deps[0]), resolveNgModuleDep(ngModule, deps[1]));
|
2017-05-11 10:26:02 -07:00
|
|
|
case 3:
|
2017-09-22 14:29:16 -07:00
|
|
|
return new ctor(
|
2017-05-11 10:26:02 -07:00
|
|
|
resolveNgModuleDep(ngModule, deps[0]), resolveNgModuleDep(ngModule, deps[1]),
|
|
|
|
|
resolveNgModuleDep(ngModule, deps[2]));
|
|
|
|
|
default:
|
|
|
|
|
const depValues = new Array(len);
|
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
|
depValues[i] = resolveNgModuleDep(ngModule, deps[i]);
|
|
|
|
|
}
|
2017-09-22 14:29:16 -07:00
|
|
|
return new ctor(...depValues);
|
2017-05-11 10:26:02 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _callFactory(ngModule: NgModuleData, factory: any, deps: DepDef[]): any {
|
|
|
|
|
const len = deps.length;
|
|
|
|
|
switch (len) {
|
|
|
|
|
case 0:
|
2017-09-22 14:29:16 -07:00
|
|
|
return factory();
|
2017-05-11 10:26:02 -07:00
|
|
|
case 1:
|
2017-09-22 14:29:16 -07:00
|
|
|
return factory(resolveNgModuleDep(ngModule, deps[0]));
|
2017-05-11 10:26:02 -07:00
|
|
|
case 2:
|
2017-09-22 14:29:16 -07:00
|
|
|
return factory(resolveNgModuleDep(ngModule, deps[0]), resolveNgModuleDep(ngModule, deps[1]));
|
2017-05-11 10:26:02 -07:00
|
|
|
case 3:
|
2017-09-22 14:29:16 -07:00
|
|
|
return factory(
|
2017-05-11 10:26:02 -07:00
|
|
|
resolveNgModuleDep(ngModule, deps[0]), resolveNgModuleDep(ngModule, deps[1]),
|
|
|
|
|
resolveNgModuleDep(ngModule, deps[2]));
|
|
|
|
|
default:
|
|
|
|
|
const depValues = Array(len);
|
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
|
depValues[i] = resolveNgModuleDep(ngModule, deps[i]);
|
|
|
|
|
}
|
2017-09-22 14:29:16 -07:00
|
|
|
return factory(...depValues);
|
2017-05-11 10:26:02 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function callNgModuleLifecycle(ngModule: NgModuleData, lifecycles: NodeFlags) {
|
|
|
|
|
const def = ngModule._def;
|
2018-05-07 10:47:59 -07:00
|
|
|
const destroyed = new Set<any>();
|
2017-05-11 10:26:02 -07:00
|
|
|
for (let i = 0; i < def.providers.length; i++) {
|
|
|
|
|
const provDef = def.providers[i];
|
|
|
|
|
if (provDef.flags & NodeFlags.OnDestroy) {
|
|
|
|
|
const instance = ngModule._providers[i];
|
2017-10-04 09:13:22 -07:00
|
|
|
if (instance && instance !== UNDEFINED_VALUE) {
|
2018-05-07 10:47:59 -07:00
|
|
|
const onDestroy: Function|undefined = instance.ngOnDestroy;
|
|
|
|
|
if (typeof onDestroy === 'function' && !destroyed.has(instance)) {
|
|
|
|
|
onDestroy.apply(instance);
|
|
|
|
|
destroyed.add(instance);
|
|
|
|
|
}
|
2017-05-11 10:26:02 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|