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

@ -23,6 +23,8 @@ export {
injectViewContainerRef as ɵinjectViewContainerRef,
injectChangeDetectorRef as ɵinjectChangeDetectorRef,
injectAttribute as ɵinjectAttribute,
getFactoryOf as ɵgetFactoryOf,
getInheritedFactory as ɵgetInheritedFactory,
PublicFeature as ɵPublicFeature,
InheritDefinitionFeature as ɵInheritDefinitionFeature,
NgOnChangesFeature as ɵNgOnChangesFeature,

View File

@ -771,6 +771,26 @@ export function getOrCreateTemplateRef<T>(di: LInjector): viewEngine.TemplateRef
return di.templateRef;
}
export function getFactoryOf<T>(type: Type<any>): ((type?: Type<T>) => T)|null {
const typeAny = type as any;
const def = typeAny.ngComponentDef || typeAny.ngDirectiveDef || typeAny.ngPipeDef ||
typeAny.ngInjectableDef || typeAny.ngInjectorDef;
if (def === undefined || def.factory === undefined) {
return null;
}
return def.factory;
}
export function getInheritedFactory<T>(type: Type<any>): (type: Type<T>) => T {
debugger;
const proto = Object.getPrototypeOf(type.prototype).constructor as Type<any>;
const factory = getFactoryOf<T>(proto);
if (factory === null) {
throw new Error(`Type ${proto.name} does not support inheritance`);
}
return factory;
}
class TemplateRef<T> implements viewEngine.TemplateRef<T> {
constructor(
private _declarationParentView: LViewData, readonly elementRef: viewEngine.ElementRef,

View File

@ -14,7 +14,7 @@ import {PublicFeature} from './features/public_feature';
import {ComponentDef, ComponentDefInternal, ComponentTemplate, ComponentType, DirectiveDef, DirectiveDefFlags, DirectiveDefInternal, DirectiveType, PipeDef} from './interfaces/definition';
export {ComponentFactory, ComponentFactoryResolver, ComponentRef} from './component_ref';
export {QUERY_READ_CONTAINER_REF, QUERY_READ_ELEMENT_REF, QUERY_READ_FROM_NODE, QUERY_READ_TEMPLATE_REF, directiveInject, injectAttribute, injectChangeDetectorRef, injectComponentFactoryResolver, injectElementRef, injectTemplateRef, injectViewContainerRef} from './di';
export {QUERY_READ_CONTAINER_REF, QUERY_READ_ELEMENT_REF, QUERY_READ_FROM_NODE, QUERY_READ_TEMPLATE_REF, directiveInject, getFactoryOf, getInheritedFactory, injectAttribute, injectChangeDetectorRef, injectComponentFactoryResolver, injectElementRef, injectTemplateRef, injectViewContainerRef} from './di';
export {RenderFlags} from './interfaces/definition';
export {CssSelectorList} from './interfaces/projection';

View File

@ -75,9 +75,10 @@ export function compileComponent(type: Type<any>, metadata: Component): void {
viewQueries: [],
},
constantPool, makeBindingParser());
const preStatements = [...constantPool.statements, ...res.statements];
def = jitExpression(
res.expression, angularCoreEnv, `ng://${type.name}/ngComponentDef.js`, constantPool);
res.expression, angularCoreEnv, `ng://${type.name}/ngComponentDef.js`, preStatements);
// If component compilation is async, then the @NgModule annotation which declares the
// component may execute and set an ngSelectorScope property on the component type. This
@ -113,7 +114,8 @@ export function compileDirective(type: Type<any>, directive: Directive): void {
const sourceMapUrl = `ng://${type && type.name}/ngDirectiveDef.js`;
const res = compileR3Directive(
directiveMetadata(type, directive), constantPool, makeBindingParser());
def = jitExpression(res.expression, angularCoreEnv, sourceMapUrl, constantPool);
const preStatements = [...constantPool.statements, ...res.statements];
def = jitExpression(res.expression, angularCoreEnv, sourceMapUrl, preStatements);
}
return def;
},

View File

@ -25,6 +25,8 @@ export const angularCoreEnv: {[name: string]: Function} = {
'ɵdefineNgModule': r3.defineNgModule,
'ɵdefinePipe': r3.definePipe,
'ɵdirectiveInject': r3.directiveInject,
'ɵgetFactoryOf': r3.getFactoryOf,
'ɵgetInheritedFactory': r3.getInheritedFactory,
'inject': inject,
'ɵinjectAttribute': r3.injectAttribute,
'ɵinjectChangeDetectorRef': r3.injectChangeDetectorRef,

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Expression, LiteralExpr, R3DependencyMetadata, WrappedNodeExpr, compileInjectable as compileR3Injectable, jitExpression} from '@angular/compiler';
import {Expression, LiteralExpr, R3DependencyMetadata, R3InjectableMetadata, WrappedNodeExpr, compileInjectable as compileR3Injectable, jitExpression} from '@angular/compiler';
import {Injectable} from '../../di/injectable';
import {ClassSansProvider, ExistingSansProvider, FactorySansProvider, StaticClassSansProvider, ValueProvider, ValueSansProvider} from '../../di/provider';
@ -18,6 +18,7 @@ import {NG_INJECTABLE_DEF} from './fields';
import {convertDependencies, reflectDependencies} from './util';
/**
* Compile an Angular injectable according to its `Injectable` metadata, and patch the resulting
* `ngInjectableDef` onto the injectable type.
@ -34,13 +35,11 @@ export function compileInjectable(type: Type<any>, srcMeta?: Injectable): void {
const hasAProvider = isUseClassProvider(meta) || isUseFactoryProvider(meta) ||
isUseValueProvider(meta) || isUseExistingProvider(meta);
let deps: R3DependencyMetadata[]|undefined = undefined;
if (!hasAProvider || (isUseClassProvider(meta) && type === meta.useClass)) {
deps = reflectDependencies(type);
} else if (isUseClassProvider(meta)) {
deps = meta.deps && convertDependencies(meta.deps);
} else if (isUseFactoryProvider(meta)) {
deps = meta.deps && convertDependencies(meta.deps) || [];
const ctorDeps = reflectDependencies(type);
let userDeps: R3DependencyMetadata[]|undefined = undefined;
if ((isUseClassProvider(meta) || isUseFactoryProvider(meta)) && meta.deps !== undefined) {
userDeps = convertDependencies(meta.deps);
}
// Decide which flavor of factory to generate, based on the provider specified.
@ -73,7 +72,7 @@ export function compileInjectable(type: Type<any>, srcMeta?: Injectable): void {
throw new Error(`Unreachable state.`);
}
const {expression} = compileR3Injectable({
const {expression, statements} = compileR3Injectable({
name: type.name,
type: new WrappedNodeExpr(type),
providedIn: computeProvidedIn(meta.providedIn),
@ -81,10 +80,12 @@ export function compileInjectable(type: Type<any>, srcMeta?: Injectable): void {
useFactory,
useValue,
useExisting,
deps,
ctorDeps,
userDeps,
});
def = jitExpression(expression, angularCoreEnv, `ng://${type.name}/ngInjectableDef.js`);
def = jitExpression(
expression, angularCoreEnv, `ng://${type.name}/ngInjectableDef.js`, statements);
}
return def;
},

View File

@ -39,7 +39,7 @@ export function compileNgModule(type: Type<any>, ngModule: NgModule): void {
};
const res = compileR3NgModule(meta);
ngModuleDef =
jitExpression(res.expression, angularCoreEnv, `ng://${type.name}/ngModuleDef.js`);
jitExpression(res.expression, angularCoreEnv, `ng://${type.name}/ngModuleDef.js`, []);
}
return ngModuleDef;
},
@ -60,8 +60,8 @@ export function compileNgModule(type: Type<any>, ngModule: NgModule): void {
]),
};
const res = compileInjector(meta);
ngInjectorDef =
jitExpression(res.expression, angularCoreEnv, `ng://${type.name}/ngInjectorDef.js`);
ngInjectorDef = jitExpression(
res.expression, angularCoreEnv, `ng://${type.name}/ngInjectorDef.js`, res.statements);
}
return ngInjectorDef;
},

View File

@ -32,7 +32,7 @@ export function compilePipe(type: Type<any>, meta: Pipe): void {
pure: meta.pure !== undefined ? meta.pure : true,
});
def = jitExpression(res.expression, angularCoreEnv, sourceMapUrl);
def = jitExpression(res.expression, angularCoreEnv, sourceMapUrl, res.statements);
}
return def;
}