feat(ivy): enable inheritance of factory functions in definitions (#25392)

This commit creates an API for factory functions which allows them
to be inherited from one another. To do so, it differentiates between
the factory function as a wrapper for a constructor and the factory
function in ngInjectableDefs which is determined by a default
provider.

The new form is:

factory: (t?) => new (t || SomeType)(inject(Dep1), inject(Dep2))

The 't' parameter allows for constructor inheritance. A subclass with
no declared constructor inherits its constructor from the superclass.
With the 't' parameter, a subclass can call the superclass' factory
function and use it to create an instance of the subclass.

For @Injectables with configured providers, the factory function is
of the form:

factory: (t?) => t ? constructorInject(t) : provider();

where constructorInject(t) creates an instance of 't' using the
naturally declared constructor of the type, and where provider()
creates an instance of the base type using the special declared
provider on @Injectable.

PR Close #25392
This commit is contained in:
Alex Rickabaugh
2018-07-16 16:36:31 -07:00
committed by Ben Lesh
parent fba276d3d1
commit 5be186035f
29 changed files with 451 additions and 205 deletions

View File

@ -38,7 +38,7 @@ export interface R3DirectiveMetadata {
/**
* Dependencies of the directive's constructor.
*/
deps: R3DependencyMetadata[];
deps: R3DependencyMetadata[]|null;
/**
* Unparsed selector of the directive, or `null` if there was no selector.
@ -177,6 +177,7 @@ export interface R3QueryMetadata {
export interface R3DirectiveDef {
expression: o.Expression;
type: o.Type;
statements: o.Statement[];
}
/**
@ -185,4 +186,5 @@ export interface R3DirectiveDef {
export interface R3ComponentDef {
expression: o.Expression;
type: o.Type;
statements: o.Statement[];
}

View File

@ -29,7 +29,7 @@ import {CONTEXT_NAME, DefinitionMap, RENDER_FLAGS, TEMPORARY_NAME, asLiteral, co
function baseDirectiveFields(
meta: R3DirectiveMetadata, constantPool: ConstantPool,
bindingParser: BindingParser): DefinitionMap {
bindingParser: BindingParser): {definitionMap: DefinitionMap, statements: o.Statement[]} {
const definitionMap = new DefinitionMap();
// e.g. `type: MyDirective`
@ -40,13 +40,13 @@ function baseDirectiveFields(
// e.g. `factory: () => new MyApp(injectElementRef())`
definitionMap.set('factory', compileFactoryFunction({
name: meta.name,
fnOrClass: meta.type,
deps: meta.deps,
useNew: true,
injectFn: R3.directiveInject,
}));
const result = compileFactoryFunction({
name: meta.name,
type: meta.type,
deps: meta.deps,
injectFn: R3.directiveInject,
});
definitionMap.set('factory', result.factory);
definitionMap.set('contentQueries', createContentQueriesFunction(meta, constantPool));
@ -80,7 +80,7 @@ function baseDirectiveFields(
definitionMap.set('features', o.literalArr(features));
}
return definitionMap;
return {definitionMap, statements: result.statements};
}
/**
@ -89,7 +89,7 @@ function baseDirectiveFields(
export function compileDirectiveFromMetadata(
meta: R3DirectiveMetadata, constantPool: ConstantPool,
bindingParser: BindingParser): R3DirectiveDef {
const definitionMap = baseDirectiveFields(meta, constantPool, bindingParser);
const {definitionMap, statements} = baseDirectiveFields(meta, constantPool, bindingParser);
const expression = o.importExpr(R3.defineDirective).callFn([definitionMap.toLiteralMap()]);
// On the type side, remove newlines from the selector as it will need to fit into a TypeScript
@ -100,7 +100,7 @@ export function compileDirectiveFromMetadata(
typeWithParameters(meta.type, meta.typeArgumentCount),
new o.ExpressionType(o.literal(selectorForType))
]));
return {expression, type};
return {expression, type, statements};
}
/**
@ -109,7 +109,7 @@ export function compileDirectiveFromMetadata(
export function compileComponentFromMetadata(
meta: R3ComponentMetadata, constantPool: ConstantPool,
bindingParser: BindingParser): R3ComponentDef {
const definitionMap = baseDirectiveFields(meta, constantPool, bindingParser);
const {definitionMap, statements} = baseDirectiveFields(meta, constantPool, bindingParser);
const selector = meta.selector && CssSelector.parse(meta.selector);
const firstSelector = selector && selector[0];
@ -180,7 +180,7 @@ export function compileComponentFromMetadata(
new o.ExpressionType(o.literal(selectorForType))
]));
return {expression, type};
return {expression, type, statements};
}
/**