Skip to content

Commit 0c600cf

Browse files
committed
refactor(core): introduce ComponentFactory.
Each compile template now exposes a `<CompName>NgFactory` variable with an instance of a `ComponentFactory`. Calling `ComponentFactory.create` returns a `ComponentRef` that can be used directly. BREAKING CHANGE: - `Compiler` is renamed to `ComponentResolver`, `Compiler.compileInHost` has been renamed to `ComponentResolver.resolveComponent`. - `ComponentRef.dispose` is renamed to `ComponentRef.destroy` - `ViewContainerRef.createHostView` is renamed to `ViewContainerRef.createComponent` - `ComponentFixture_` has been removed, the class `ComponentFixture` can now be created directly as it is no more using private APIs.
1 parent 4140405 commit 0c600cf

File tree

66 files changed

+609
-847
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+609
-847
lines changed

modules/angular2/src/compiler/compiler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export {TEMPLATE_TRANSFORMS} from 'angular2/src/compiler/template_parser';
44
export {CompilerConfig, RenderTypes} from './config';
55
export * from './compile_metadata';
66
export * from './offline_compiler';
7+
export {RuntimeCompiler} from './runtime_compiler';
78
export * from 'angular2/src/compiler/url_resolver';
89
export * from 'angular2/src/compiler/xhr';
910

@@ -20,7 +21,7 @@ import {RuntimeMetadataResolver} from 'angular2/src/compiler/runtime_metadata';
2021
import {StyleCompiler} from 'angular2/src/compiler/style_compiler';
2122
import {ViewCompiler} from 'angular2/src/compiler/view_compiler/view_compiler';
2223
import {CompilerConfig} from './config';
23-
import {Compiler} from 'angular2/src/core/linker/compiler';
24+
import {ComponentResolver} from 'angular2/src/core/linker/component_resolver';
2425
import {RuntimeCompiler} from 'angular2/src/compiler/runtime_compiler';
2526
import {ElementSchemaRegistry} from 'angular2/src/compiler/schema/element_schema_registry';
2627
import {DomElementSchemaRegistry} from 'angular2/src/compiler/schema/dom_element_schema_registry';
@@ -51,7 +52,7 @@ export const COMPILER_PROVIDERS: Array<Type | Provider | any[]> = CONST_EXPR([
5152
ViewCompiler,
5253
new Provider(CompilerConfig, {useFactory: _createCompilerConfig, deps: []}),
5354
RuntimeCompiler,
54-
new Provider(Compiler, {useExisting: RuntimeCompiler}),
55+
new Provider(ComponentResolver, {useExisting: RuntimeCompiler}),
5556
DomElementSchemaRegistry,
5657
new Provider(ElementSchemaRegistry, {useExisting: DomElementSchemaRegistry}),
5758
UrlResolver,

modules/angular2/src/compiler/offline_compiler.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ import {TemplateParser} from './template_parser';
1313
import {DirectiveNormalizer} from './directive_normalizer';
1414
import {OutputEmitter} from './output/abstract_emitter';
1515
import * as o from './output/output_ast';
16-
import {HostViewFactory} from 'angular2/src/core/linker/view';
16+
import {ComponentFactory} from 'angular2/src/core/linker/component_factory';
1717

1818
import {
1919
MODULE_SUFFIX,
2020
} from './util';
2121

22-
var _HOST_VIEW_FACTORY_IDENTIFIER = new CompileIdentifierMetadata({
23-
name: 'HostViewFactory',
24-
runtime: HostViewFactory,
25-
moduleUrl: `asset:angular2/lib/src/core/linker/view${MODULE_SUFFIX}`
22+
var _COMPONENT_FACTORY_IDENTIFIER = new CompileIdentifierMetadata({
23+
name: 'ComponentFactory',
24+
runtime: ComponentFactory,
25+
moduleUrl: `asset:angular2/lib/src/core/linker/component_factory${MODULE_SUFFIX}`
2626
});
2727

2828
export class SourceModule {
@@ -59,17 +59,20 @@ export class OfflineCompiler {
5959
exportedVars.push(compViewFactoryVar);
6060

6161
var hostMeta = createHostComponentMeta(compMeta.type, compMeta.selector);
62-
var compHostViewFactoryVar = this._compileComponent(hostMeta, [compMeta], [], statements);
63-
var hostViewFactoryVar = `hostViewFactory_${compMeta.type.name}`;
64-
statements.push(
65-
o.variable(hostViewFactoryVar)
66-
.set(o.importExpr(_HOST_VIEW_FACTORY_IDENTIFIER)
67-
.instantiate(
68-
[o.literal(compMeta.selector), o.variable(compHostViewFactoryVar)],
69-
o.importType(_HOST_VIEW_FACTORY_IDENTIFIER, null,
70-
[o.TypeModifier.Const])))
71-
.toDeclStmt(null, [o.StmtModifier.Final]));
72-
exportedVars.push(hostViewFactoryVar);
62+
var hostViewFactoryVar = this._compileComponent(hostMeta, [compMeta], [], statements);
63+
var compFactoryVar = `${compMeta.type.name}NgFactory`;
64+
statements.push(o.variable(compFactoryVar)
65+
.set(o.importExpr(_COMPONENT_FACTORY_IDENTIFIER)
66+
.instantiate(
67+
[
68+
o.literal(compMeta.selector),
69+
o.variable(hostViewFactoryVar),
70+
o.importExpr(compMeta.type)
71+
],
72+
o.importType(_COMPONENT_FACTORY_IDENTIFIER, null,
73+
[o.TypeModifier.Const])))
74+
.toDeclStmt(null, [o.StmtModifier.Final]));
75+
exportedVars.push(compFactoryVar);
7376
});
7477
return this._codegenSourceModule(moduleUrl, statements, exportedVars);
7578
}

modules/angular2/src/compiler/output/interpretive_view.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {isPresent} from 'angular2/src/facade/lang';
22
import {AppView} from 'angular2/src/core/linker/view';
3+
import {AppElement} from 'angular2/src/core/linker/element';
34
import {BaseException} from 'angular2/src/facade/exceptions';
45
import {InstanceFactory, DynamicInstance} from './output_interpreter';
56

@@ -19,12 +20,12 @@ class _InterpretiveAppView extends AppView<any> implements DynamicInstance {
1920
super(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9],
2021
args[10]);
2122
}
22-
createInternal(rootSelector: string): void {
23+
createInternal(rootSelector: string | any): AppElement {
2324
var m = this.methods.get('createInternal');
2425
if (isPresent(m)) {
25-
m(rootSelector);
26+
return m(rootSelector);
2627
} else {
27-
super.createInternal(rootSelector);
28+
return super.createInternal(rootSelector);
2829
}
2930
}
3031
injectorGetInternal(token: any, nodeIndex: number, notFoundResult: any): any {

modules/angular2/src/compiler/runtime_compiler.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ import {ViewCompiler} from './view_compiler/view_compiler';
4646
import {TemplateParser} from './template_parser';
4747
import {DirectiveNormalizer} from './directive_normalizer';
4848
import {RuntimeMetadataResolver} from './runtime_metadata';
49-
import {HostViewFactory} from 'angular2/src/core/linker/view';
50-
import {HostViewFactoryRef, HostViewFactoryRef_} from 'angular2/src/core/linker/view_ref';
51-
import {Compiler, Compiler_} from 'angular2/src/core/linker/compiler';
49+
import {ComponentFactory} from 'angular2/src/core/linker/component_factory';
50+
import {
51+
ComponentResolver,
52+
ReflectorComponentResolver
53+
} from 'angular2/src/core/linker/component_resolver';
5254

5355
import {CompilerConfig} from './config';
5456
import * as ir from './output/output_ast';
@@ -64,7 +66,7 @@ import {XHR} from 'angular2/src/compiler/xhr';
6466
* ready for linking into an application.
6567
*/
6668
@Injectable()
67-
export class RuntimeCompiler extends Compiler_ {
69+
export class RuntimeCompiler implements ComponentResolver {
6870
private _styleCache: Map<string, Promise<string>> = new Map<string, Promise<string>>();
6971
private _hostCacheKeys = new Map<Type, any>();
7072
private _compiledTemplateCache = new Map<any, CompiledTemplate>();
@@ -74,11 +76,9 @@ export class RuntimeCompiler extends Compiler_ {
7476
private _templateNormalizer: DirectiveNormalizer,
7577
private _templateParser: TemplateParser, private _styleCompiler: StyleCompiler,
7678
private _viewCompiler: ViewCompiler, private _xhr: XHR,
77-
private _genConfig: CompilerConfig) {
78-
super();
79-
}
79+
private _genConfig: CompilerConfig) {}
8080

81-
compileInHost(componentType: Type): Promise<HostViewFactoryRef_> {
81+
resolveComponent(componentType: Type): Promise<ComponentFactory> {
8282
var compMeta: CompileDirectiveMetadata =
8383
this._runtimeMetadataResolver.getDirectiveMetadata(componentType);
8484
var hostCacheKey = this._hostCacheKeys.get(componentType);
@@ -92,8 +92,8 @@ export class RuntimeCompiler extends Compiler_ {
9292
this._loadAndCompileComponent(hostCacheKey, hostMeta, [compMeta], [], []);
9393
}
9494
return this._compiledTemplateDone.get(hostCacheKey)
95-
.then((compiledTemplate: CompiledTemplate) => new HostViewFactoryRef_(
96-
new HostViewFactory(compMeta.selector, compiledTemplate.viewFactory)));
95+
.then((compiledTemplate: CompiledTemplate) => new ComponentFactory(
96+
compMeta.selector, compiledTemplate.viewFactory, componentType));
9797
}
9898

9999
clearCache() {

modules/angular2/src/compiler/view_compiler/test.js

Lines changed: 0 additions & 77 deletions
This file was deleted.

modules/angular2/src/compiler/view_compiler/view_builder.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import {getViewFactoryName, createFlatArray, createDiTokenExpression} from './ut
4343

4444
import {ViewType} from 'angular2/src/core/linker/view_type';
4545
import {ViewEncapsulation} from 'angular2/src/core/metadata/view';
46-
import {HOST_VIEW_ELEMENT_NAME} from 'angular2/src/core/linker/view';
4746

4847
import {
4948
CompileIdentifierMetadata,
@@ -185,17 +184,13 @@ class ViewBuilderVisitor implements TemplateAstVisitor {
185184
var nodeIndex = this.view.nodes.length;
186185
var createRenderNodeExpr;
187186
var debugContextExpr = this.view.createMethod.resetDebugInfoExpr(nodeIndex, ast);
188-
var createElementExpr = ViewProperties.renderer.callMethod(
189-
'createElement',
190-
[this._getParentRenderNode(parent), o.literal(ast.name), debugContextExpr]);
191187
if (nodeIndex === 0 && this.view.viewType === ViewType.HOST) {
192-
createRenderNodeExpr =
193-
rootSelectorVar.identical(o.NULL_EXPR)
194-
.conditional(createElementExpr,
195-
ViewProperties.renderer.callMethod('selectRootElement',
196-
[rootSelectorVar, debugContextExpr]));
188+
createRenderNodeExpr = o.THIS_EXPR.callMethod(
189+
'selectOrCreateHostElement', [o.literal(ast.name), rootSelectorVar, debugContextExpr]);
197190
} else {
198-
createRenderNodeExpr = createElementExpr;
191+
createRenderNodeExpr = ViewProperties.renderer.callMethod(
192+
'createElement',
193+
[this._getParentRenderNode(parent), o.literal(ast.name), debugContextExpr]);
199194
}
200195
var fieldName = `_el_${nodeIndex}`;
201196
this.view.fields.push(
@@ -342,9 +337,6 @@ function _readHtmlAndDirectiveVariables(elementExportAsVars: VariableAst[],
342337
elementExportAsVars.forEach((varAst) => {
343338
variables[varAst.name] = isPresent(component) ? identifierToken(component.type) : null;
344339
});
345-
if (viewType === ViewType.HOST) {
346-
variables[HOST_VIEW_ELEMENT_NAME] = null;
347-
}
348340
return variables;
349341
}
350342

@@ -444,7 +436,7 @@ function createViewClass(view: CompileView, renderCompTypeVar: o.ReadVarExpr,
444436

445437
var viewMethods = [
446438
new o.ClassMethod('createInternal', [new o.FnParam(rootSelectorVar.name, o.STRING_TYPE)],
447-
generateCreateMethod(view)),
439+
generateCreateMethod(view), o.importType(Identifiers.AppElement)),
448440
new o.ClassMethod(
449441
'injectorGetInternal',
450442
[
@@ -519,6 +511,12 @@ function generateCreateMethod(view: CompileView): o.Statement[] {
519511
.toDeclStmt(o.importType(view.genConfig.renderTypes.renderNode), [o.StmtModifier.Final])
520512
];
521513
}
514+
var resultExpr: o.Expression;
515+
if (view.viewType === ViewType.HOST) {
516+
resultExpr = (<CompileElement>view.nodes[0]).getOrCreateAppElement();
517+
} else {
518+
resultExpr = o.NULL_EXPR;
519+
}
522520
return parentRenderNodeStmts.concat(view.createMethod.finish())
523521
.concat([
524522
o.THIS_EXPR.callMethod('init',
@@ -529,7 +527,8 @@ function generateCreateMethod(view: CompileView): o.Statement[] {
529527
o.literalArr(view.disposables),
530528
o.literalArr(view.subscriptions)
531529
])
532-
.toStmt()
530+
.toStmt(),
531+
new o.ReturnStatement(resultExpr)
533532
]);
534533
}
535534

modules/angular2/src/core/application_common_providers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {
1313
} from './change_detection/change_detection';
1414
import {AppViewManager} from './linker/view_manager';
1515
import {AppViewManager_} from "./linker/view_manager";
16-
import {Compiler} from './linker/compiler';
17-
import {Compiler_} from "./linker/compiler";
16+
import {ComponentResolver} from './linker/component_resolver';
17+
import {ReflectorComponentResolver} from "./linker/component_resolver";
1818
import {DynamicComponentLoader} from './linker/dynamic_component_loader';
1919
import {DynamicComponentLoader_} from "./linker/dynamic_component_loader";
2020

@@ -25,7 +25,7 @@ var __unused: Type; // avoid unused import when Type union types are erased
2525
* application, regardless of the platform it runs onto.
2626
*/
2727
export const APPLICATION_COMMON_PROVIDERS: Array<Type | Provider | any[]> = CONST_EXPR([
28-
new Provider(Compiler, {useClass: Compiler_}),
28+
new Provider(ComponentResolver, {useClass: ReflectorComponentResolver}),
2929
APP_ID_RANDOM_PROVIDER,
3030
new Provider(AppViewManager, {useClass: AppViewManager_}),
3131
new Provider(IterableDiffers, {useValue: defaultIterableDiffers}),

modules/angular2/src/core/application_ref.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import {
1818
import {PromiseWrapper, PromiseCompleter, ObservableWrapper} from 'angular2/src/facade/async';
1919
import {ListWrapper} from 'angular2/src/facade/collection';
2020
import {TestabilityRegistry, Testability} from 'angular2/src/core/testability/testability';
21-
import {
22-
ComponentRef,
23-
DynamicComponentLoader
24-
} from 'angular2/src/core/linker/dynamic_component_loader';
21+
import {DynamicComponentLoader} from 'angular2/src/core/linker/dynamic_component_loader';
22+
import {ComponentRef} from 'angular2/src/core/linker/component_factory';
2523
import {
2624
BaseException,
2725
WrappedException,
@@ -32,7 +30,6 @@ import {Console} from 'angular2/src/core/console';
3230
import {wtfLeave, wtfCreateScope, WtfScopeFn} from './profile/profile';
3331
import {ChangeDetectorRef} from 'angular2/src/core/change_detection/change_detector_ref';
3432
import {lockMode} from 'angular2/src/facade/lang';
35-
import {ElementRef_} from 'angular2/src/core/linker/element_ref';
3633

3734
/**
3835
* Construct providers specific to an individual root component.
@@ -457,8 +454,7 @@ export class ApplicationRef_ extends ApplicationRef {
457454

458455
/** @internal */
459456
_loadComponent(componentRef: ComponentRef): void {
460-
var appChangeDetector = (<ElementRef_>componentRef.location).internalElement.parentView;
461-
this._changeDetectorRefs.push(appChangeDetector.ref);
457+
this._changeDetectorRefs.push(componentRef.changeDetectorRef);
462458
this.tick();
463459
this._rootComponents.push(componentRef);
464460
this._bootstrapListeners.forEach((listener) => listener(componentRef));
@@ -469,8 +465,7 @@ export class ApplicationRef_ extends ApplicationRef {
469465
if (!ListWrapper.contains(this._rootComponents, componentRef)) {
470466
return;
471467
}
472-
this.unregisterChangeDetector(
473-
(<ElementRef_>componentRef.location).internalElement.parentView.ref);
468+
this.unregisterChangeDetector(componentRef.changeDetectorRef);
474469
ListWrapper.remove(this._rootComponents, componentRef);
475470
}
476471

@@ -498,7 +493,7 @@ export class ApplicationRef_ extends ApplicationRef {
498493

499494
dispose(): void {
500495
// TODO(alxhub): Dispose of the NgZone.
501-
ListWrapper.clone(this._rootComponents).forEach((ref) => ref.dispose());
496+
ListWrapper.clone(this._rootComponents).forEach((ref) => ref.destroy());
502497
this._disposeListeners.forEach((dispose) => dispose());
503498
this._platform._applicationDisposed(this);
504499
}

modules/angular2/src/core/debug/debug_renderer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export class DebugDomRootRenderer implements RootRenderer {
2525
export class DebugDomRenderer implements Renderer {
2626
constructor(private _delegate: Renderer) {}
2727

28-
selectRootElement(selector: string, debugInfo: RenderDebugInfo): any {
29-
var nativeEl = this._delegate.selectRootElement(selector, debugInfo);
28+
selectRootElement(selectorOrNode: string | any, debugInfo: RenderDebugInfo): any {
29+
var nativeEl = this._delegate.selectRootElement(selectorOrNode, debugInfo);
3030
var debugEl = new DebugElement(nativeEl, null, debugInfo);
3131
indexDebugNode(debugEl);
3232
return nativeEl;

0 commit comments

Comments
 (0)