2018-04-06 12:53:10 -04: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
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {InjectFlags} from './core';
|
2018-05-09 11:35:25 -04:00
|
|
|
import {Identifiers} from './identifiers';
|
2018-04-06 12:53:10 -04:00
|
|
|
import * as o from './output/output_ast';
|
2018-07-16 19:36:31 -04:00
|
|
|
import {R3DependencyMetadata, R3FactoryDelegateType, R3FactoryMetadata, compileFactoryFunction} from './render3/r3_factory';
|
2018-05-09 11:35:25 -04:00
|
|
|
import {mapToMapExpression} from './render3/util';
|
2018-04-06 12:53:10 -04:00
|
|
|
|
|
|
|
export interface InjectableDef {
|
|
|
|
expression: o.Expression;
|
|
|
|
type: o.Type;
|
2018-07-16 19:36:31 -04:00
|
|
|
statements: o.Statement[];
|
2018-04-06 12:53:10 -04:00
|
|
|
}
|
|
|
|
|
2018-05-09 11:35:25 -04:00
|
|
|
export interface R3InjectableMetadata {
|
2018-04-06 12:53:10 -04:00
|
|
|
name: string;
|
|
|
|
type: o.Expression;
|
2018-07-16 19:36:31 -04:00
|
|
|
ctorDeps: R3DependencyMetadata[]|null;
|
2018-04-06 12:53:10 -04:00
|
|
|
providedIn: o.Expression;
|
|
|
|
useClass?: o.Expression;
|
2018-05-09 11:35:25 -04:00
|
|
|
useFactory?: o.Expression;
|
2018-04-06 12:53:10 -04:00
|
|
|
useExisting?: o.Expression;
|
|
|
|
useValue?: o.Expression;
|
2018-07-16 19:36:31 -04:00
|
|
|
userDeps?: R3DependencyMetadata[];
|
2018-04-06 12:53:10 -04:00
|
|
|
}
|
|
|
|
|
2018-05-09 11:35:25 -04:00
|
|
|
export function compileInjectable(meta: R3InjectableMetadata): InjectableDef {
|
2018-07-16 19:36:31 -04:00
|
|
|
let result: {factory: o.Expression, statements: o.Statement[]}|null = null;
|
2018-05-09 11:35:25 -04:00
|
|
|
|
|
|
|
function makeFn(ret: o.Expression): o.Expression {
|
|
|
|
return o.fn([], [new o.ReturnStatement(ret)], undefined, undefined, `${meta.name}_Factory`);
|
|
|
|
}
|
|
|
|
|
2018-07-16 19:36:31 -04:00
|
|
|
const factoryMeta = {
|
|
|
|
name: meta.name,
|
|
|
|
type: meta.type,
|
|
|
|
deps: meta.ctorDeps,
|
|
|
|
injectFn: Identifiers.inject,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (meta.useClass !== undefined) {
|
|
|
|
// meta.useClass has two modes of operation. Either deps are specified, in which case `new` is
|
|
|
|
// used to instantiate the class with dependencies injected, or deps are not specified and
|
|
|
|
// the factory of the class is used to instantiate it.
|
|
|
|
//
|
|
|
|
// A special case exists for useClass: Type where Type is the injectable type itself, in which
|
|
|
|
// case omitting deps just uses the constructor dependencies instead.
|
2018-05-09 11:35:25 -04:00
|
|
|
|
2018-07-16 19:36:31 -04:00
|
|
|
const useClassOnSelf = meta.useClass.isEquivalent(meta.type);
|
|
|
|
const deps = meta.userDeps || (useClassOnSelf && meta.ctorDeps) || undefined;
|
2018-05-09 11:35:25 -04:00
|
|
|
|
2018-07-16 19:36:31 -04:00
|
|
|
if (deps !== undefined) {
|
|
|
|
// factory: () => new meta.useClass(...deps)
|
|
|
|
result = compileFactoryFunction({
|
|
|
|
...factoryMeta,
|
|
|
|
delegate: meta.useClass,
|
|
|
|
delegateDeps: deps,
|
|
|
|
delegateType: R3FactoryDelegateType.Class,
|
2018-05-09 11:35:25 -04:00
|
|
|
});
|
|
|
|
} else {
|
2018-07-16 19:36:31 -04:00
|
|
|
result = compileFactoryFunction({
|
|
|
|
...factoryMeta,
|
|
|
|
delegate: meta.useClass,
|
|
|
|
delegateType: R3FactoryDelegateType.Factory,
|
|
|
|
});
|
2018-05-09 11:35:25 -04:00
|
|
|
}
|
2018-07-16 19:36:31 -04:00
|
|
|
} else if (meta.useFactory !== undefined) {
|
|
|
|
result = compileFactoryFunction({
|
|
|
|
...factoryMeta,
|
|
|
|
delegate: meta.useFactory,
|
|
|
|
delegateDeps: meta.userDeps || [],
|
|
|
|
delegateType: R3FactoryDelegateType.Function,
|
|
|
|
});
|
2018-04-06 12:53:10 -04:00
|
|
|
} else if (meta.useValue !== undefined) {
|
2018-05-09 11:35:25 -04:00
|
|
|
// Note: it's safe to use `meta.useValue` instead of the `USE_VALUE in meta` check used for
|
|
|
|
// client code because meta.useValue is an Expression which will be defined even if the actual
|
|
|
|
// value is undefined.
|
2018-07-16 19:36:31 -04:00
|
|
|
result = compileFactoryFunction({
|
|
|
|
...factoryMeta,
|
|
|
|
expression: meta.useValue,
|
|
|
|
});
|
2018-04-06 12:53:10 -04:00
|
|
|
} else if (meta.useExisting !== undefined) {
|
2018-05-09 11:35:25 -04:00
|
|
|
// useExisting is an `inject` call on the existing token.
|
2018-07-16 19:36:31 -04:00
|
|
|
result = compileFactoryFunction({
|
|
|
|
...factoryMeta,
|
|
|
|
expression: o.importExpr(Identifiers.inject).callFn([meta.useExisting]),
|
2018-05-09 11:35:25 -04:00
|
|
|
});
|
2018-07-16 19:36:31 -04:00
|
|
|
} else {
|
|
|
|
result = compileFactoryFunction(factoryMeta);
|
2018-04-06 12:53:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const token = meta.type;
|
|
|
|
const providedIn = meta.providedIn;
|
|
|
|
|
2018-05-09 11:35:25 -04:00
|
|
|
const expression = o.importExpr(Identifiers.defineInjectable).callFn([mapToMapExpression(
|
2018-07-16 19:36:31 -04:00
|
|
|
{token, factory: result.factory, providedIn})]);
|
2018-05-09 11:35:25 -04:00
|
|
|
const type = new o.ExpressionType(
|
|
|
|
o.importExpr(Identifiers.InjectableDef, [new o.ExpressionType(meta.type)]));
|
2018-04-06 12:53:10 -04:00
|
|
|
|
|
|
|
return {
|
2018-07-16 19:36:31 -04:00
|
|
|
expression,
|
|
|
|
type,
|
|
|
|
statements: result.statements,
|
2018-04-06 12:53:10 -04:00
|
|
|
};
|
|
|
|
}
|