Skip to content

Commit 7ae23ad

Browse files
committed
feat(core): speed up view creation via code gen for view factories.
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
1 parent a08f50b commit 7ae23ad

File tree

191 files changed

+6379
-10135
lines changed

Some content is hidden

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

191 files changed

+6379
-10135
lines changed

modules/angular2/src/common/directives/ng_class.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ export class NgClass implements DoCheck, OnDestroy {
169169
if (className.indexOf(' ') > -1) {
170170
var classes = className.split(/\s+/g);
171171
for (var i = 0, len = classes.length; i < len; i++) {
172-
this._renderer.setElementClass(this._ngEl, classes[i], enabled);
172+
this._renderer.setElementClass(this._ngEl.nativeElement, classes[i], enabled);
173173
}
174174
} else {
175-
this._renderer.setElementClass(this._ngEl, className, enabled);
175+
this._renderer.setElementClass(this._ngEl.nativeElement, className, enabled);
176176
}
177177
}
178178
}

modules/angular2/src/common/directives/ng_for.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
IterableDiffers,
77
ViewContainerRef,
88
TemplateRef,
9-
ViewRef
9+
EmbeddedViewRef
1010
} from 'angular2/core';
1111
import {isPresent, isBlank} from 'angular2/src/facade/lang';
1212

@@ -110,7 +110,8 @@ export class NgFor implements DoCheck {
110110
}
111111

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

@@ -153,7 +154,7 @@ export class NgFor implements DoCheck {
153154
}
154155

155156
class RecordViewTuple {
156-
view: ViewRef;
157+
view: EmbeddedViewRef;
157158
record: any;
158159
constructor(record, view) {
159160
this.record = record;

modules/angular2/src/common/directives/ng_style.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@ export class NgStyle implements DoCheck {
9292
}
9393

9494
private _setStyle(name: string, val: string): void {
95-
this._renderer.setElementStyle(this._ngEl, name, val);
95+
this._renderer.setElementStyle(this._ngEl.nativeElement, name, val);
9696
}
9797
}

modules/angular2/src/common/forms/directives/checkbox_value_accessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class CheckboxControlValueAccessor implements ControlValueAccessor {
2727
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {}
2828

2929
writeValue(value: any): void {
30-
this._renderer.setElementProperty(this._elementRef, 'checked', value);
30+
this._renderer.setElementProperty(this._elementRef.nativeElement, 'checked', value);
3131
}
3232
registerOnChange(fn: (_: any) => {}): void { this.onChange = fn; }
3333
registerOnTouched(fn: () => {}): void { this.onTouched = fn; }

modules/angular2/src/common/forms/directives/default_value_accessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class DefaultValueAccessor implements ControlValueAccessor {
3131

3232
writeValue(value: any): void {
3333
var normalizedValue = isBlank(value) ? '' : value;
34-
this._renderer.setElementProperty(this._elementRef, 'value', normalizedValue);
34+
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', normalizedValue);
3535
}
3636

3737
registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }

modules/angular2/src/common/forms/directives/number_value_accessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class NumberValueAccessor implements ControlValueAccessor {
3131
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {}
3232

3333
writeValue(value: number): void {
34-
this._renderer.setElementProperty(this._elementRef, 'value', value);
34+
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', value);
3535
}
3636

3737
registerOnChange(fn: (_: number) => void): void {

modules/angular2/src/common/forms/directives/select_control_value_accessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
5151

5252
writeValue(value: any): void {
5353
this.value = value;
54-
this._renderer.setElementProperty(this._elementRef, 'value', value);
54+
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', value);
5555
}
5656

5757
registerOnChange(fn: () => any): void { this.onChange = fn; }

modules/angular2/src/common/forms/directives/shared.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export function selectValueAccessor(dir: NgControl,
8080
var defaultAccessor;
8181
var builtinAccessor;
8282
var customAccessor;
83-
8483
valueAccessors.forEach(v => {
8584
if (v instanceof DefaultValueAccessor) {
8685
defaultAccessor = v;

modules/angular2/src/compiler/change_definition_factory.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ListWrapper} from 'angular2/src/facade/collection';
1+
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
22
import {isPresent, isBlank} from 'angular2/src/facade/lang';
33
import {reflector} from 'angular2/src/core/reflection/reflection';
44

@@ -43,7 +43,7 @@ export function createChangeDetectorDefinitions(
4343

4444
class ProtoViewVisitor implements TemplateAstVisitor {
4545
viewIndex: number;
46-
boundTextCount: number = 0;
46+
nodeCount: number = 0;
4747
boundElementCount: number = 0;
4848
variableNames: string[] = [];
4949
bindingRecords: BindingRecord[] = [];
@@ -57,6 +57,7 @@ class ProtoViewVisitor implements TemplateAstVisitor {
5757
}
5858

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

7576
visitElement(ast: ElementAst, context: any): any {
77+
this.nodeCount++;
7678
if (ast.isBound()) {
7779
this.boundElementCount++;
7880
}
@@ -132,14 +134,20 @@ class ProtoViewVisitor implements TemplateAstVisitor {
132134
}
133135
visitAttr(ast: AttrAst, context: any): any { return null; }
134136
visitBoundText(ast: BoundTextAst, context: any): any {
135-
var boundTextIndex = this.boundTextCount++;
136-
this.bindingRecords.push(BindingRecord.createForTextNode(ast.value, boundTextIndex));
137+
var nodeIndex = this.nodeCount++;
138+
this.bindingRecords.push(BindingRecord.createForTextNode(ast.value, nodeIndex));
139+
return null;
140+
}
141+
visitText(ast: TextAst, context: any): any {
142+
this.nodeCount++;
137143
return null;
138144
}
139-
visitText(ast: TextAst, context: any): any { return null; }
140145
visitDirective(ast: DirectiveAst, directiveIndexAsNumber: number): any {
141146
var directiveIndex = new DirectiveIndex(this.boundElementCount - 1, directiveIndexAsNumber);
142147
var directiveMetadata = ast.directive;
148+
var outputsArray = [];
149+
StringMapWrapper.forEach(ast.directive.outputs, (eventName, dirProperty) => outputsArray.push(
150+
[dirProperty, eventName]));
143151
var directiveRecord = new DirectiveRecord({
144152
directiveIndex: directiveIndex,
145153
callAfterContentInit:
@@ -153,7 +161,9 @@ class ProtoViewVisitor implements TemplateAstVisitor {
153161
callOnChanges: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnChanges) !== -1,
154162
callDoCheck: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.DoCheck) !== -1,
155163
callOnInit: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnInit) !== -1,
156-
changeDetection: directiveMetadata.changeDetection
164+
callOnDestroy: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnDestroy) !== -1,
165+
changeDetection: directiveMetadata.changeDetection,
166+
outputs: outputsArray
157167
});
158168
this.directiveRecords.push(directiveRecord);
159169

modules/angular2/src/compiler/change_detector_compiler.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {SourceExpressions, moduleRef} from './source_module';
33
import {
44
ChangeDetectorJITGenerator
55
} from 'angular2/src/core/change_detection/change_detection_jit_generator';
6+
import {AbstractChangeDetector} from 'angular2/src/core/change_detection/abstract_change_detector';
7+
import {ChangeDetectionUtil} from 'angular2/src/core/change_detection/change_detection_util';
8+
import {ChangeDetectorState} from 'angular2/src/core/change_detection/constants';
69

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

29+
export const CHANGE_DETECTION_JIT_IMPORTS = CONST_EXPR({
30+
'AbstractChangeDetector': AbstractChangeDetector,
31+
'ChangeDetectionUtil': ChangeDetectionUtil,
32+
'ChangeDetectorState': ChangeDetectorState
33+
});
34+
2635
var ABSTRACT_CHANGE_DETECTOR_MODULE = moduleRef(
2736
`package:angular2/src/core/change_detection/abstract_change_detector${MODULE_SUFFIX}`);
2837
var UTIL_MODULE =
@@ -45,14 +54,8 @@ export class ChangeDetectionCompiler {
4554
}
4655

4756
private _createChangeDetectorFactory(definition: ChangeDetectorDefinition): Function {
48-
if (IS_DART || !this._genConfig.useJit) {
49-
var proto = new DynamicProtoChangeDetector(definition);
50-
return (dispatcher) => proto.instantiate(dispatcher);
51-
} else {
52-
return new ChangeDetectorJITGenerator(definition, UTIL, ABSTRACT_CHANGE_DETECTOR,
53-
CHANGE_DETECTOR_STATE)
54-
.generate();
55-
}
57+
var proto = new DynamicProtoChangeDetector(definition);
58+
return () => proto.instantiate();
5659
}
5760

5861
compileComponentCodeGen(componentType: CompileTypeMetadata, strategy: ChangeDetectionStrategy,
@@ -81,7 +84,7 @@ export class ChangeDetectionCompiler {
8184
definition, `${UTIL_MODULE}${UTIL}`,
8285
`${ABSTRACT_CHANGE_DETECTOR_MODULE}${ABSTRACT_CHANGE_DETECTOR}`,
8386
`${CONSTANTS_MODULE}${CHANGE_DETECTOR_STATE}`);
84-
factories.push(`function(dispatcher) { return new ${codegen.typeName}(dispatcher); }`);
87+
factories.push(`function() { return new ${codegen.typeName}(); }`);
8588
sourcePart = codegen.generateSource();
8689
}
8790
index++;

0 commit comments

Comments
 (0)