refactor(compiler): remove AppElement.initComponent

This commit is contained in:
Tobias Bosch
2016-11-01 08:21:39 -07:00
committed by vikerman
parent 953cb50fa5
commit 13533d2a30
14 changed files with 80 additions and 53 deletions

View File

@ -20,6 +20,7 @@ import * as console from './console';
import * as debug from './debug/debug_renderer';
import * as reflective_provider from './di/reflective_provider';
import {ComponentStillLoadingError} from './linker/compiler';
import * as component_factory from './linker/component_factory';
import * as component_factory_resolver from './linker/component_factory_resolver';
import * as debug_context from './linker/debug_context';
import * as element from './linker/element';
@ -56,6 +57,7 @@ export var __core_private__: {
_MethodFn?: reflection_types.MethodFn;
CodegenComponentFactoryResolver:
typeof component_factory_resolver.CodegenComponentFactoryResolver,
ComponentRef_: typeof component_factory.ComponentRef_,
_CodegenComponentFactoryResolver?: component_factory_resolver.CodegenComponentFactoryResolver,
AppElement: typeof element.AppElement, _AppElement?: element.AppElement,
AppView: typeof view.AppView, _AppView?: view.AppView<any>,
@ -112,6 +114,7 @@ export var __core_private__: {
LIFECYCLE_HOOKS_VALUES: lifecycle_hooks.LIFECYCLE_HOOKS_VALUES,
ReflectorReader: reflector_reader.ReflectorReader,
CodegenComponentFactoryResolver: component_factory_resolver.CodegenComponentFactoryResolver,
ComponentRef_: component_factory.ComponentRef_,
AppElement: element.AppElement,
AppView: view.AppView,
DebugAppView: view.DebugAppView,

View File

@ -55,7 +55,7 @@ export abstract class ComponentRef<C> {
/**
* The component type.
*/
get componentType(): Type<any> { return unimplemented(); }
get componentType(): Type<C> { return unimplemented(); }
/**
* Destroys the component instance and all of the data structures associated with it.
@ -69,15 +69,19 @@ export abstract class ComponentRef<C> {
}
export class ComponentRef_<C> extends ComponentRef<C> {
constructor(private _hostElement: AppElement, private _componentType: Type<any>) { super(); }
get location(): ElementRef { return this._hostElement.elementRef; }
get injector(): Injector { return this._hostElement.injector; }
get instance(): C { return this._hostElement.component; };
get hostView(): ViewRef { return this._hostElement.parentView.ref; };
get changeDetectorRef(): ChangeDetectorRef { return this._hostElement.parentView.ref; };
get componentType(): Type<any> { return this._componentType; }
constructor(
private _index: number, private _parentView: AppView<any>, private _nativeElement: any,
private _component: C) {
super();
}
get location(): ElementRef { return new ElementRef(this._nativeElement); }
get injector(): Injector { return this._parentView.injector(this._index); }
get instance(): C { return this._component; };
get hostView(): ViewRef { return this._parentView.ref; };
get changeDetectorRef(): ChangeDetectorRef { return this._parentView.ref; };
get componentType(): Type<C> { return <any>this._component.constructor; }
destroy(): void { this._hostElement.parentView.detachAndDestroy(); }
destroy(): void { this._parentView.detachAndDestroy(); }
onDestroy(callback: Function): void { this.hostView.onDestroy(callback); }
}
@ -107,6 +111,9 @@ export class ComponentFactory<C> {
}
// Note: Host views don't need a declarationAppElement!
var hostView: AppView<any> = this._viewFactory(vu, injector, null);
// TODO: implement this in the View class directly?!
// (behind a `if (this.type === ViewType.HOST)`)
// TODO: and pass the projectableNodes into `createHostView`
hostView.visitProjectableNodesInternal =
(nodeIndex: number, ngContentIndex: number, cb: any, ctx: any) => {
const nodes = projectableNodes[ngContentIndex] || [];
@ -114,7 +121,6 @@ export class ComponentFactory<C> {
cb(nodes[i], ctx);
}
};
var hostElement = hostView.create(EMPTY_CONTEXT, rootSelectorOrNode);
return new ComponentRef_<C>(hostElement, this._componentType);
return hostView.createHostView(rootSelectorOrNode);
}
}

View File

@ -23,9 +23,6 @@ import {ViewType} from './view_type';
*/
export class AppElement {
public nestedViews: AppView<any>[];
public componentView: AppView<any>;
public component: any;
constructor(
public index: number, public parentIndex: number, public parentView: AppView<any>,
@ -35,11 +32,6 @@ export class AppElement {
get vcRef(): ViewContainerRef_ { return new ViewContainerRef_(this); }
initComponent(component: any, view: AppView<any>) {
this.component = component;
this.componentView = view;
}
get parentInjector(): Injector { return this.parentView.injector(this.parentIndex); }
get injector(): Injector { return this.parentView.injector(this.index); }

View File

@ -48,7 +48,7 @@ export class TemplateRef_<C> extends TemplateRef<C> {
createEmbeddedView(context: C): EmbeddedViewRef<C> {
var view: AppView<C> = this._viewFactory(
this._appElement.parentView.viewUtils, this._appElement.parentInjector, this._appElement);
view.create(context || <any>{}, null);
view.create(context || <any>{});
return view.ref;
}

View File

@ -14,6 +14,7 @@ import {WtfScopeFn, wtfCreateScope, wtfLeave} from '../profile/profile';
import {RenderComponentType, RenderDebugInfo, Renderer} from '../render/api';
import {AnimationViewContext} from './animation_view_context';
import {ComponentRef} from './component_factory';
import {DebugContext, StaticNodeDebugInfo} from './debug_context';
import {AppElement} from './element';
import {ElementInjector} from './element_injector';
@ -24,6 +25,11 @@ import {ViewUtils, addToArray} from './view_utils';
var _scope_check: WtfScopeFn = wtfCreateScope(`AppView#check(ascii id)`);
/**
* @experimental
*/
const EMPTY_CONTEXT = new Object();
/**
* Cost of making objects: http://jsperf.com/instantiate-size-of-object
*
@ -65,17 +71,22 @@ export abstract class AppView<T> {
get destroyed(): boolean { return this.cdMode === ChangeDetectorStatus.Destroyed; }
create(context: T, rootSelectorOrNode: string|any): AppElement {
create(context: T) {
this.context = context;
return this.createInternal(null);
}
createHostView(rootSelectorOrNode: string|any): ComponentRef<any> {
this.context = <any>EMPTY_CONTEXT;
this._hasExternalHostElement = isPresent(rootSelectorOrNode);
return this.createInternal(rootSelectorOrNode);
}
/**
* Overwritten by implementations.
* Returns the AppElement for the host element for ViewType.HOST.
* Returns the ComponentRef for the host element for ViewType.HOST.
*/
createInternal(rootSelectorOrNode: string|any): AppElement { return null; }
createInternal(rootSelectorOrNode: string|any): ComponentRef<any> { return null; }
init(lastRootNode: any, allNodes: any[], disposables: Function[]) {
this.lastRootNode = lastRootNode;
@ -268,10 +279,20 @@ export class DebugAppView<T> extends AppView<T> {
super(clazz, componentType, type, viewUtils, parentInjector, declarationAppElement, cdMode);
}
create(context: T, rootSelectorOrNode: string|any): AppElement {
create(context: T) {
this._resetDebug();
try {
return super.create(context, rootSelectorOrNode);
return super.create(context);
} catch (e) {
this._rethrowWithContext(e);
throw e;
}
}
createHostView(rootSelectorOrNode: string|any): ComponentRef<any> {
this._resetDebug();
try {
return super.createHostView(rootSelectorOrNode);
} catch (e) {
this._rethrowWithContext(e);
throw e;