refactor(proto_view_factory): Move getChangeDetectorDefinitions out of ProtoViewFactory

Move `getChangeDetectorDefinitions` out of `ProtoViewFactory` since it
does not depend on any state in that object.
This commit is contained in:
Tim Blasi 2015-05-14 12:43:56 -07:00
parent c397297eef
commit 3644036693
2 changed files with 217 additions and 211 deletions

View File

@ -116,31 +116,13 @@ export class ProtoViewFactory {
this._changeDetection = changeDetection; this._changeDetection = changeDetection;
} }
/**
* Returns the data needed to create ChangeDetectors
* for the given ProtoView and all nested ProtoViews.
*/
getChangeDetectorDefinitions(hostComponentMetadata:renderApi.DirectiveMetadata,
rootRenderProtoView: renderApi.ProtoViewDto, allRenderDirectiveMetadata:List<renderApi.DirectiveMetadata>):List<ChangeDetectorDefinition> {
var nestedPvsWithIndex = this._collectNestedProtoViews(rootRenderProtoView);
var nestedPvVariableBindings = this._collectNestedProtoViewsVariableBindings(nestedPvsWithIndex);
var nestedPvVariableNames = this._collectNestedProtoViewsVariableNames(nestedPvsWithIndex, nestedPvVariableBindings);
return this._getChangeDetectorDefinitions(
hostComponentMetadata,
nestedPvsWithIndex,
nestedPvVariableNames,
allRenderDirectiveMetadata
);
}
createAppProtoViews(hostComponentBinding:DirectiveBinding, createAppProtoViews(hostComponentBinding:DirectiveBinding,
rootRenderProtoView: renderApi.ProtoViewDto, allDirectives:List<DirectiveBinding>):List<AppProtoView> { rootRenderProtoView: renderApi.ProtoViewDto, allDirectives:List<DirectiveBinding>):List<AppProtoView> {
var allRenderDirectiveMetadata = ListWrapper.map(allDirectives, directiveBinding => directiveBinding.metadata); var allRenderDirectiveMetadata = ListWrapper.map(allDirectives, directiveBinding => directiveBinding.metadata);
var nestedPvsWithIndex = this._collectNestedProtoViews(rootRenderProtoView); var nestedPvsWithIndex = _collectNestedProtoViews(rootRenderProtoView);
var nestedPvVariableBindings = this._collectNestedProtoViewsVariableBindings(nestedPvsWithIndex); var nestedPvVariableBindings = _collectNestedProtoViewsVariableBindings(nestedPvsWithIndex);
var nestedPvVariableNames = this._collectNestedProtoViewsVariableNames(nestedPvsWithIndex, nestedPvVariableBindings); var nestedPvVariableNames = _collectNestedProtoViewsVariableNames(nestedPvsWithIndex, nestedPvVariableBindings);
var changeDetectorDefs = this._getChangeDetectorDefinitions( var changeDetectorDefs = _getChangeDetectorDefinitions(
hostComponentBinding.metadata, nestedPvsWithIndex, nestedPvVariableNames, allRenderDirectiveMetadata hostComponentBinding.metadata, nestedPvsWithIndex, nestedPvVariableNames, allRenderDirectiveMetadata
); );
var protoChangeDetectors = ListWrapper.map( var protoChangeDetectors = ListWrapper.map(
@ -148,7 +130,7 @@ export class ProtoViewFactory {
); );
var appProtoViews = ListWrapper.createFixedSize(nestedPvsWithIndex.length); var appProtoViews = ListWrapper.createFixedSize(nestedPvsWithIndex.length);
ListWrapper.forEach(nestedPvsWithIndex, (pvWithIndex) => { ListWrapper.forEach(nestedPvsWithIndex, (pvWithIndex) => {
var appProtoView = this._createAppProtoView( var appProtoView = _createAppProtoView(
pvWithIndex.renderProtoView, pvWithIndex.renderProtoView,
protoChangeDetectors[pvWithIndex.index], protoChangeDetectors[pvWithIndex.index],
nestedPvVariableBindings[pvWithIndex.index], nestedPvVariableBindings[pvWithIndex.index],
@ -162,8 +144,32 @@ export class ProtoViewFactory {
}); });
return appProtoViews; return appProtoViews;
} }
}
_collectNestedProtoViews(renderProtoView:renderApi.ProtoViewDto, parentIndex:number = null, boundElementIndex = null, result:List<RenderProtoViewWithIndex> = null):List<RenderProtoViewWithIndex> { /**
* Returns the data needed to create ChangeDetectors
* for the given ProtoView and all nested ProtoViews.
*/
export function getChangeDetectorDefinitions(
hostComponentMetadata:renderApi.DirectiveMetadata,
rootRenderProtoView: renderApi.ProtoViewDto,
allRenderDirectiveMetadata:List<renderApi.DirectiveMetadata>): List<ChangeDetectorDefinition> {
var nestedPvsWithIndex = _collectNestedProtoViews(rootRenderProtoView);
var nestedPvVariableBindings = _collectNestedProtoViewsVariableBindings(nestedPvsWithIndex);
var nestedPvVariableNames = _collectNestedProtoViewsVariableNames(nestedPvsWithIndex, nestedPvVariableBindings);
return _getChangeDetectorDefinitions(
hostComponentMetadata,
nestedPvsWithIndex,
nestedPvVariableNames,
allRenderDirectiveMetadata
);
}
function _collectNestedProtoViews(renderProtoView:renderApi.ProtoViewDto,
parentIndex:number = null,
boundElementIndex = null,
result:List<RenderProtoViewWithIndex> = null): List<RenderProtoViewWithIndex> {
if (isBlank(result)) { if (isBlank(result)) {
result = []; result = [];
} }
@ -172,14 +178,14 @@ export class ProtoViewFactory {
var childBoundElementIndex = 0; var childBoundElementIndex = 0;
ListWrapper.forEach(renderProtoView.elementBinders, (elementBinder) => { ListWrapper.forEach(renderProtoView.elementBinders, (elementBinder) => {
if (isPresent(elementBinder.nestedProtoView)) { if (isPresent(elementBinder.nestedProtoView)) {
this._collectNestedProtoViews(elementBinder.nestedProtoView, currentIndex, childBoundElementIndex, result); _collectNestedProtoViews(elementBinder.nestedProtoView, currentIndex, childBoundElementIndex, result);
} }
childBoundElementIndex++; childBoundElementIndex++;
}); });
return result; return result;
} }
_getChangeDetectorDefinitions( function _getChangeDetectorDefinitions(
hostComponentMetadata:renderApi.DirectiveMetadata, hostComponentMetadata:renderApi.DirectiveMetadata,
nestedPvsWithIndex: List<RenderProtoViewWithIndex>, nestedPvsWithIndex: List<RenderProtoViewWithIndex>,
nestedPvVariableNames: List<List<string>>, nestedPvVariableNames: List<List<string>>,
@ -205,7 +211,7 @@ export class ProtoViewFactory {
}); });
} }
_createAppProtoView( function _createAppProtoView(
renderProtoView: renderApi.ProtoViewDto, renderProtoView: renderApi.ProtoViewDto,
protoChangeDetector: ProtoChangeDetector, protoChangeDetector: ProtoChangeDetector,
variableBindings: Map<string, string>, variableBindings: Map<string, string>,
@ -215,21 +221,21 @@ export class ProtoViewFactory {
var protoView = new AppProtoView(renderProtoView.render, protoChangeDetector, variableBindings); var protoView = new AppProtoView(renderProtoView.render, protoChangeDetector, variableBindings);
// TODO: vsavkin refactor to pass element binders into proto view // TODO: vsavkin refactor to pass element binders into proto view
this._createElementBinders(protoView, elementBinders, allDirectives); _createElementBinders(protoView, elementBinders, allDirectives);
this._bindDirectiveEvents(protoView, elementBinders); _bindDirectiveEvents(protoView, elementBinders);
return protoView; return protoView;
} }
_collectNestedProtoViewsVariableBindings( function _collectNestedProtoViewsVariableBindings(
nestedPvsWithIndex: List<RenderProtoViewWithIndex> nestedPvsWithIndex: List<RenderProtoViewWithIndex>
):List<Map<string, string>> { ):List<Map<string, string>> {
return ListWrapper.map(nestedPvsWithIndex, (pvWithIndex) => { return ListWrapper.map(nestedPvsWithIndex, (pvWithIndex) => {
return this._createVariableBindings(pvWithIndex.renderProtoView); return _createVariableBindings(pvWithIndex.renderProtoView);
}); });
} }
_createVariableBindings(renderProtoView):Map { function _createVariableBindings(renderProtoView):Map {
var variableBindings = MapWrapper.create(); var variableBindings = MapWrapper.create();
MapWrapper.forEach(renderProtoView.variableBindings, (mappedName, varName) => { MapWrapper.forEach(renderProtoView.variableBindings, (mappedName, varName) => {
MapWrapper.set(variableBindings, varName, mappedName); MapWrapper.set(variableBindings, varName, mappedName);
@ -242,21 +248,21 @@ export class ProtoViewFactory {
return variableBindings; return variableBindings;
} }
_collectNestedProtoViewsVariableNames( function _collectNestedProtoViewsVariableNames(
nestedPvsWithIndex: List<RenderProtoViewWithIndex>, nestedPvsWithIndex: List<RenderProtoViewWithIndex>,
nestedPvVariableBindings:List<Map<string, string>> nestedPvVariableBindings:List<Map<string, string>>
):List<List<string>> { ):List<List<string>> {
var nestedPvVariableNames = ListWrapper.createFixedSize(nestedPvsWithIndex.length); var nestedPvVariableNames = ListWrapper.createFixedSize(nestedPvsWithIndex.length);
ListWrapper.forEach(nestedPvsWithIndex, (pvWithIndex) => { ListWrapper.forEach(nestedPvsWithIndex, (pvWithIndex) => {
var parentVariableNames = isPresent(pvWithIndex.parentIndex) ? nestedPvVariableNames[pvWithIndex.parentIndex] : null; var parentVariableNames = isPresent(pvWithIndex.parentIndex) ? nestedPvVariableNames[pvWithIndex.parentIndex] : null;
nestedPvVariableNames[pvWithIndex.index] = this._createVariableNames( nestedPvVariableNames[pvWithIndex.index] = _createVariableNames(
parentVariableNames, nestedPvVariableBindings[pvWithIndex.index] parentVariableNames, nestedPvVariableBindings[pvWithIndex.index]
); );
}); });
return nestedPvVariableNames; return nestedPvVariableNames;
} }
_createVariableNames(parentVariableNames, variableBindings):List { function _createVariableNames(parentVariableNames, variableBindings):List {
var variableNames = isPresent(parentVariableNames) ? ListWrapper.clone(parentVariableNames) : []; var variableNames = isPresent(parentVariableNames) ? ListWrapper.clone(parentVariableNames) : [];
MapWrapper.forEach(variableBindings, (local, v) => { MapWrapper.forEach(variableBindings, (local, v) => {
ListWrapper.push(variableNames, local); ListWrapper.push(variableNames, local);
@ -264,12 +270,12 @@ export class ProtoViewFactory {
return variableNames; return variableNames;
} }
_createElementBinders(protoView, elementBinders, allDirectiveBindings) { function _createElementBinders(protoView, elementBinders, allDirectiveBindings) {
for (var i=0; i<elementBinders.length; i++) { for (var i=0; i<elementBinders.length; i++) {
var renderElementBinder = elementBinders[i]; var renderElementBinder = elementBinders[i];
var dirs = elementBinders[i].directives; var dirs = elementBinders[i].directives;
var parentPeiWithDistance = this._findParentProtoElementInjectorWithDistance( var parentPeiWithDistance = _findParentProtoElementInjectorWithDistance(
i, protoView.elementBinders, elementBinders); i, protoView.elementBinders, elementBinders);
var directiveBindings = ListWrapper.map(dirs, (dir) => allDirectiveBindings[dir.directiveIndex] ); var directiveBindings = ListWrapper.map(dirs, (dir) => allDirectiveBindings[dir.directiveIndex] );
var componentDirectiveBinding = null; var componentDirectiveBinding = null;
@ -278,14 +284,14 @@ export class ProtoViewFactory {
componentDirectiveBinding = directiveBindings[0]; componentDirectiveBinding = directiveBindings[0];
} }
} }
var protoElementInjector = this._createProtoElementInjector( var protoElementInjector = _createProtoElementInjector(
i, parentPeiWithDistance, renderElementBinder, componentDirectiveBinding, directiveBindings); i, parentPeiWithDistance, renderElementBinder, componentDirectiveBinding, directiveBindings);
this._createElementBinder(protoView, i, renderElementBinder, protoElementInjector, componentDirectiveBinding); _createElementBinder(protoView, i, renderElementBinder, protoElementInjector, componentDirectiveBinding);
} }
} }
_findParentProtoElementInjectorWithDistance(binderIndex, elementBinders, renderElementBinders) { function _findParentProtoElementInjectorWithDistance(binderIndex, elementBinders, renderElementBinders) {
var distance = 0; var distance = 0;
do { do {
var renderElementBinder = renderElementBinders[binderIndex]; var renderElementBinder = renderElementBinders[binderIndex];
@ -301,7 +307,7 @@ export class ProtoViewFactory {
return new ParentProtoElementInjectorWithDistance(null, -1); return new ParentProtoElementInjectorWithDistance(null, -1);
} }
_createProtoElementInjector(binderIndex, parentPeiWithDistance, renderElementBinder, componentDirectiveBinding, directiveBindings) { function _createProtoElementInjector(binderIndex, parentPeiWithDistance, renderElementBinder, componentDirectiveBinding, directiveBindings) {
var protoElementInjector = null; var protoElementInjector = null;
// Create a protoElementInjector for any element that either has bindings *or* has one // Create a protoElementInjector for any element that either has bindings *or* has one
// or more var- defined. Elements with a var- defined need a their own element injector // or more var- defined. Elements with a var- defined need a their own element injector
@ -328,7 +334,7 @@ export class ProtoViewFactory {
return protoElementInjector; return protoElementInjector;
} }
_createElementBinder(protoView, boundElementIndex, renderElementBinder, protoElementInjector, componentDirectiveBinding) { function _createElementBinder(protoView, boundElementIndex, renderElementBinder, protoElementInjector, componentDirectiveBinding) {
var parent = null; var parent = null;
if (renderElementBinder.parentIndex !== -1) { if (renderElementBinder.parentIndex !== -1) {
parent = protoView.elementBinders[renderElementBinder.parentIndex]; parent = protoView.elementBinders[renderElementBinder.parentIndex];
@ -351,7 +357,7 @@ export class ProtoViewFactory {
return elBinder; return elBinder;
} }
_bindDirectiveEvents(protoView, elementBinders:List<renderApi.ElementBinder>) { function _bindDirectiveEvents(protoView, elementBinders:List<renderApi.ElementBinder>) {
for (var boundElementIndex = 0; boundElementIndex < elementBinders.length; ++boundElementIndex) { for (var boundElementIndex = 0; boundElementIndex < elementBinders.length; ++boundElementIndex) {
var dirs = elementBinders[boundElementIndex].directives; var dirs = elementBinders[boundElementIndex].directives;
for (var i = 0; i < dirs.length; i++) { for (var i = 0; i < dirs.length; i++) {
@ -362,7 +368,7 @@ export class ProtoViewFactory {
} }
} }
} }
}
class RenderProtoViewWithIndex { class RenderProtoViewWithIndex {
renderProtoView:renderApi.ProtoViewDto; renderProtoView:renderApi.ProtoViewDto;

View File

@ -17,7 +17,7 @@ import {isBlank} from 'angular2/src/facade/lang';
import {MapWrapper} from 'angular2/src/facade/collection'; import {MapWrapper} from 'angular2/src/facade/collection';
import {ChangeDetection, ChangeDetectorDefinition} from 'angular2/change_detection'; import {ChangeDetection, ChangeDetectorDefinition} from 'angular2/change_detection';
import {ProtoViewFactory} from 'angular2/src/core/compiler/proto_view_factory'; import {ProtoViewFactory, getChangeDetectorDefinitions} from 'angular2/src/core/compiler/proto_view_factory';
import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations'; import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations';
import {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver'; import {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver';
import {DirectiveBinding} from 'angular2/src/core/compiler/element_injector'; import {DirectiveBinding} from 'angular2/src/core/compiler/element_injector';
@ -45,7 +45,7 @@ export function main() {
it('should create a ChangeDetectorDefinition for the root render proto view', () => { it('should create a ChangeDetectorDefinition for the root render proto view', () => {
var renderPv = createRenderProtoView(); var renderPv = createRenderProtoView();
var defs = protoViewFactory.getChangeDetectorDefinitions(bindDirective(MainComponent).metadata, var defs = getChangeDetectorDefinitions(bindDirective(MainComponent).metadata,
renderPv, []); renderPv, []);
expect(defs.length).toBe(1); expect(defs.length).toBe(1);
expect(defs[0].id).toEqual('MainComponent_comp_0'); expect(defs[0].id).toEqual('MainComponent_comp_0');