Skip to content

Commit

Permalink
feat(core): speed up view creation via code gen for view factories.
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
- Platform pipes can only contain types and arrays of types,
  but no bindings any more.
- When using transformers, platform pipes need to be specified explicitly
  in the pubspec.yaml via the new config option
  `platform_pipes`.
- `Compiler.compileInHost` now returns a `HostViewFactoryRef`
- Component view is not yet created when component constructor is called.
  -> use `onInit` lifecycle callback to access the view of a component
- `ViewRef#setLocal` has been moved to new type `EmbeddedViewRef`
- `internalView` is gone, use `EmbeddedViewRef.rootNodes` to access
  the root nodes of an embedded view
- `renderer.setElementProperty`, `..setElementStyle`, `..setElementAttribute` now
  take a native element instead of an ElementRef
- `Renderer` interface now operates on plain native nodes,
  instead of `RenderElementRef`s or `RenderViewRef`s

Closes #5993
  • Loading branch information
tbosch committed Jan 5, 2016
1 parent a08f50b commit 7ae23ad
Show file tree
Hide file tree
Showing 191 changed files with 6,379 additions and 10,135 deletions.
4 changes: 2 additions & 2 deletions modules/angular2/src/common/directives/ng_class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ export class NgClass implements DoCheck, OnDestroy {
if (className.indexOf(' ') > -1) {
var classes = className.split(/\s+/g);
for (var i = 0, len = classes.length; i < len; i++) {
this._renderer.setElementClass(this._ngEl, classes[i], enabled);
this._renderer.setElementClass(this._ngEl.nativeElement, classes[i], enabled);
}
} else {
this._renderer.setElementClass(this._ngEl, className, enabled);
this._renderer.setElementClass(this._ngEl.nativeElement, className, enabled);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions modules/angular2/src/common/directives/ng_for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
IterableDiffers,
ViewContainerRef,
TemplateRef,
ViewRef
EmbeddedViewRef
} from 'angular2/core';
import {isPresent, isBlank} from 'angular2/src/facade/lang';

Expand Down Expand Up @@ -110,7 +110,8 @@ export class NgFor implements DoCheck {
}

for (var i = 0, ilen = this._viewContainer.length; i < ilen; i++) {
this._viewContainer.get(i).setLocal('last', i === ilen - 1);
var viewRef = <EmbeddedViewRef>this._viewContainer.get(i);
viewRef.setLocal('last', i === ilen - 1);
}
}

Expand Down Expand Up @@ -153,7 +154,7 @@ export class NgFor implements DoCheck {
}

class RecordViewTuple {
view: ViewRef;
view: EmbeddedViewRef;
record: any;
constructor(record, view) {
this.record = record;
Expand Down
2 changes: 1 addition & 1 deletion modules/angular2/src/common/directives/ng_style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ export class NgStyle implements DoCheck {
}

private _setStyle(name: string, val: string): void {
this._renderer.setElementStyle(this._ngEl, name, val);
this._renderer.setElementStyle(this._ngEl.nativeElement, name, val);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class CheckboxControlValueAccessor implements ControlValueAccessor {
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {}

writeValue(value: any): void {
this._renderer.setElementProperty(this._elementRef, 'checked', value);
this._renderer.setElementProperty(this._elementRef.nativeElement, 'checked', value);
}
registerOnChange(fn: (_: any) => {}): void { this.onChange = fn; }
registerOnTouched(fn: () => {}): void { this.onTouched = fn; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class DefaultValueAccessor implements ControlValueAccessor {

writeValue(value: any): void {
var normalizedValue = isBlank(value) ? '' : value;
this._renderer.setElementProperty(this._elementRef, 'value', normalizedValue);
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', normalizedValue);
}

registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class NumberValueAccessor implements ControlValueAccessor {
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {}

writeValue(value: number): void {
this._renderer.setElementProperty(this._elementRef, 'value', value);
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', value);
}

registerOnChange(fn: (_: number) => void): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class SelectControlValueAccessor implements ControlValueAccessor {

writeValue(value: any): void {
this.value = value;
this._renderer.setElementProperty(this._elementRef, 'value', value);
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', value);
}

registerOnChange(fn: () => any): void { this.onChange = fn; }
Expand Down
1 change: 0 additions & 1 deletion modules/angular2/src/common/forms/directives/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export function selectValueAccessor(dir: NgControl,
var defaultAccessor;
var builtinAccessor;
var customAccessor;

valueAccessors.forEach(v => {
if (v instanceof DefaultValueAccessor) {
defaultAccessor = v;
Expand Down
22 changes: 16 additions & 6 deletions modules/angular2/src/compiler/change_definition_factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ListWrapper} from 'angular2/src/facade/collection';
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
import {isPresent, isBlank} from 'angular2/src/facade/lang';
import {reflector} from 'angular2/src/core/reflection/reflection';

Expand Down Expand Up @@ -43,7 +43,7 @@ export function createChangeDetectorDefinitions(

class ProtoViewVisitor implements TemplateAstVisitor {
viewIndex: number;
boundTextCount: number = 0;
nodeCount: number = 0;
boundElementCount: number = 0;
variableNames: string[] = [];
bindingRecords: BindingRecord[] = [];
Expand All @@ -57,6 +57,7 @@ class ProtoViewVisitor implements TemplateAstVisitor {
}

visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any {
this.nodeCount++;
this.boundElementCount++;
templateVisitAll(this, ast.outputs);
for (var i = 0; i < ast.directives.length; i++) {
Expand All @@ -73,6 +74,7 @@ class ProtoViewVisitor implements TemplateAstVisitor {
}

visitElement(ast: ElementAst, context: any): any {
this.nodeCount++;
if (ast.isBound()) {
this.boundElementCount++;
}
Expand Down Expand Up @@ -132,14 +134,20 @@ class ProtoViewVisitor implements TemplateAstVisitor {
}
visitAttr(ast: AttrAst, context: any): any { return null; }
visitBoundText(ast: BoundTextAst, context: any): any {
var boundTextIndex = this.boundTextCount++;
this.bindingRecords.push(BindingRecord.createForTextNode(ast.value, boundTextIndex));
var nodeIndex = this.nodeCount++;
this.bindingRecords.push(BindingRecord.createForTextNode(ast.value, nodeIndex));
return null;
}
visitText(ast: TextAst, context: any): any {
this.nodeCount++;
return null;
}
visitText(ast: TextAst, context: any): any { return null; }
visitDirective(ast: DirectiveAst, directiveIndexAsNumber: number): any {
var directiveIndex = new DirectiveIndex(this.boundElementCount - 1, directiveIndexAsNumber);
var directiveMetadata = ast.directive;
var outputsArray = [];
StringMapWrapper.forEach(ast.directive.outputs, (eventName, dirProperty) => outputsArray.push(
[dirProperty, eventName]));
var directiveRecord = new DirectiveRecord({
directiveIndex: directiveIndex,
callAfterContentInit:
Expand All @@ -153,7 +161,9 @@ class ProtoViewVisitor implements TemplateAstVisitor {
callOnChanges: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnChanges) !== -1,
callDoCheck: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.DoCheck) !== -1,
callOnInit: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnInit) !== -1,
changeDetection: directiveMetadata.changeDetection
callOnDestroy: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnDestroy) !== -1,
changeDetection: directiveMetadata.changeDetection,
outputs: outputsArray
});
this.directiveRecords.push(directiveRecord);

Expand Down
21 changes: 12 additions & 9 deletions modules/angular2/src/compiler/change_detector_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {SourceExpressions, moduleRef} from './source_module';
import {
ChangeDetectorJITGenerator
} from 'angular2/src/core/change_detection/change_detection_jit_generator';
import {AbstractChangeDetector} from 'angular2/src/core/change_detection/abstract_change_detector';
import {ChangeDetectionUtil} from 'angular2/src/core/change_detection/change_detection_util';
import {ChangeDetectorState} from 'angular2/src/core/change_detection/constants';

import {createChangeDetectorDefinitions} from './change_definition_factory';
import {IS_DART, isJsObject, CONST_EXPR} from 'angular2/src/facade/lang';
Expand All @@ -23,6 +26,12 @@ const ABSTRACT_CHANGE_DETECTOR = "AbstractChangeDetector";
const UTIL = "ChangeDetectionUtil";
const CHANGE_DETECTOR_STATE = "ChangeDetectorState";

export const CHANGE_DETECTION_JIT_IMPORTS = CONST_EXPR({
'AbstractChangeDetector': AbstractChangeDetector,
'ChangeDetectionUtil': ChangeDetectionUtil,
'ChangeDetectorState': ChangeDetectorState
});

var ABSTRACT_CHANGE_DETECTOR_MODULE = moduleRef(
`package:angular2/src/core/change_detection/abstract_change_detector${MODULE_SUFFIX}`);
var UTIL_MODULE =
Expand All @@ -45,14 +54,8 @@ export class ChangeDetectionCompiler {
}

private _createChangeDetectorFactory(definition: ChangeDetectorDefinition): Function {
if (IS_DART || !this._genConfig.useJit) {
var proto = new DynamicProtoChangeDetector(definition);
return (dispatcher) => proto.instantiate(dispatcher);
} else {
return new ChangeDetectorJITGenerator(definition, UTIL, ABSTRACT_CHANGE_DETECTOR,
CHANGE_DETECTOR_STATE)
.generate();
}
var proto = new DynamicProtoChangeDetector(definition);
return () => proto.instantiate();
}

compileComponentCodeGen(componentType: CompileTypeMetadata, strategy: ChangeDetectionStrategy,
Expand Down Expand Up @@ -81,7 +84,7 @@ export class ChangeDetectionCompiler {
definition, `${UTIL_MODULE}${UTIL}`,
`${ABSTRACT_CHANGE_DETECTOR_MODULE}${ABSTRACT_CHANGE_DETECTOR}`,
`${CONSTANTS_MODULE}${CHANGE_DETECTOR_STATE}`);
factories.push(`function(dispatcher) { return new ${codegen.typeName}(dispatcher); }`);
factories.push(`function() { return new ${codegen.typeName}(); }`);
sourcePart = codegen.generateSource();
}
index++;
Expand Down
Loading

0 comments on commit 7ae23ad

Please sign in to comment.