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

@ -110,37 +110,19 @@ class BindingRecordsCreator {
@Injectable()
export class ProtoViewFactory {
_changeDetection:ChangeDetection;
_changeDetection: ChangeDetection;
constructor(changeDetection:ChangeDetection) {
constructor(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,
rootRenderProtoView: renderApi.ProtoViewDto, allDirectives:List<DirectiveBinding>):List<AppProtoView> {
var allRenderDirectiveMetadata = ListWrapper.map(allDirectives, directiveBinding => directiveBinding.metadata );
var nestedPvsWithIndex = this._collectNestedProtoViews(rootRenderProtoView);
var nestedPvVariableBindings = this._collectNestedProtoViewsVariableBindings(nestedPvsWithIndex);
var nestedPvVariableNames = this._collectNestedProtoViewsVariableNames(nestedPvsWithIndex, nestedPvVariableBindings);
var changeDetectorDefs = this._getChangeDetectorDefinitions(
var allRenderDirectiveMetadata = ListWrapper.map(allDirectives, directiveBinding => directiveBinding.metadata);
var nestedPvsWithIndex = _collectNestedProtoViews(rootRenderProtoView);
var nestedPvVariableBindings = _collectNestedProtoViewsVariableBindings(nestedPvsWithIndex);
var nestedPvVariableNames = _collectNestedProtoViewsVariableNames(nestedPvsWithIndex, nestedPvVariableBindings);
var changeDetectorDefs = _getChangeDetectorDefinitions(
hostComponentBinding.metadata, nestedPvsWithIndex, nestedPvVariableNames, allRenderDirectiveMetadata
);
var protoChangeDetectors = ListWrapper.map(
@ -148,7 +130,7 @@ export class ProtoViewFactory {
);
var appProtoViews = ListWrapper.createFixedSize(nestedPvsWithIndex.length);
ListWrapper.forEach(nestedPvsWithIndex, (pvWithIndex) => {
var appProtoView = this._createAppProtoView(
var appProtoView = _createAppProtoView(
pvWithIndex.renderProtoView,
protoChangeDetectors[pvWithIndex.index],
nestedPvVariableBindings[pvWithIndex.index],
@ -162,8 +144,32 @@ export class ProtoViewFactory {
});
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)) {
result = [];
}
@ -172,14 +178,14 @@ export class ProtoViewFactory {
var childBoundElementIndex = 0;
ListWrapper.forEach(renderProtoView.elementBinders, (elementBinder) => {
if (isPresent(elementBinder.nestedProtoView)) {
this._collectNestedProtoViews(elementBinder.nestedProtoView, currentIndex, childBoundElementIndex, result);
_collectNestedProtoViews(elementBinder.nestedProtoView, currentIndex, childBoundElementIndex, result);
}
childBoundElementIndex++;
});
return result;
}
}
_getChangeDetectorDefinitions(
function _getChangeDetectorDefinitions(
hostComponentMetadata:renderApi.DirectiveMetadata,
nestedPvsWithIndex: List<RenderProtoViewWithIndex>,
nestedPvVariableNames: List<List<string>>,
@ -203,9 +209,9 @@ export class ProtoViewFactory {
var variableNames = nestedPvVariableNames[pvWithIndex.index];
return new ChangeDetectorDefinition(id, strategyName, variableNames, bindingRecords, directiveRecords);
});
}
}
_createAppProtoView(
function _createAppProtoView(
renderProtoView: renderApi.ProtoViewDto,
protoChangeDetector: ProtoChangeDetector,
variableBindings: Map<string, string>,
@ -215,21 +221,21 @@ export class ProtoViewFactory {
var protoView = new AppProtoView(renderProtoView.render, protoChangeDetector, variableBindings);
// TODO: vsavkin refactor to pass element binders into proto view
this._createElementBinders(protoView, elementBinders, allDirectives);
this._bindDirectiveEvents(protoView, elementBinders);
_createElementBinders(protoView, elementBinders, allDirectives);
_bindDirectiveEvents(protoView, elementBinders);
return protoView;
}
}
_collectNestedProtoViewsVariableBindings(
function _collectNestedProtoViewsVariableBindings(
nestedPvsWithIndex: List<RenderProtoViewWithIndex>
):List<Map<string, string>> {
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();
MapWrapper.forEach(renderProtoView.variableBindings, (mappedName, varName) => {
MapWrapper.set(variableBindings, varName, mappedName);
@ -240,36 +246,36 @@ export class ProtoViewFactory {
});
});
return variableBindings;
}
}
_collectNestedProtoViewsVariableNames(
function _collectNestedProtoViewsVariableNames(
nestedPvsWithIndex: List<RenderProtoViewWithIndex>,
nestedPvVariableBindings:List<Map<string, string>>
):List<List<string>> {
var nestedPvVariableNames = ListWrapper.createFixedSize(nestedPvsWithIndex.length);
ListWrapper.forEach(nestedPvsWithIndex, (pvWithIndex) => {
var parentVariableNames = isPresent(pvWithIndex.parentIndex) ? nestedPvVariableNames[pvWithIndex.parentIndex] : null;
nestedPvVariableNames[pvWithIndex.index] = this._createVariableNames(
nestedPvVariableNames[pvWithIndex.index] = _createVariableNames(
parentVariableNames, nestedPvVariableBindings[pvWithIndex.index]
);
});
return nestedPvVariableNames;
}
}
_createVariableNames(parentVariableNames, variableBindings):List {
function _createVariableNames(parentVariableNames, variableBindings):List {
var variableNames = isPresent(parentVariableNames) ? ListWrapper.clone(parentVariableNames) : [];
MapWrapper.forEach(variableBindings, (local, v) => {
ListWrapper.push(variableNames, local);
});
return variableNames;
}
}
_createElementBinders(protoView, elementBinders, allDirectiveBindings) {
function _createElementBinders(protoView, elementBinders, allDirectiveBindings) {
for (var i=0; i<elementBinders.length; i++) {
var renderElementBinder = elementBinders[i];
var dirs = elementBinders[i].directives;
var parentPeiWithDistance = this._findParentProtoElementInjectorWithDistance(
var parentPeiWithDistance = _findParentProtoElementInjectorWithDistance(
i, protoView.elementBinders, elementBinders);
var directiveBindings = ListWrapper.map(dirs, (dir) => allDirectiveBindings[dir.directiveIndex] );
var componentDirectiveBinding = null;
@ -278,14 +284,14 @@ export class ProtoViewFactory {
componentDirectiveBinding = directiveBindings[0];
}
}
var protoElementInjector = this._createProtoElementInjector(
var protoElementInjector = _createProtoElementInjector(
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;
do {
var renderElementBinder = renderElementBinders[binderIndex];
@ -299,9 +305,9 @@ export class ProtoViewFactory {
}
} while (binderIndex !== -1);
return new ParentProtoElementInjectorWithDistance(null, -1);
}
}
_createProtoElementInjector(binderIndex, parentPeiWithDistance, renderElementBinder, componentDirectiveBinding, directiveBindings) {
function _createProtoElementInjector(binderIndex, parentPeiWithDistance, renderElementBinder, componentDirectiveBinding, directiveBindings) {
var protoElementInjector = null;
// 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
@ -326,9 +332,9 @@ export class ProtoViewFactory {
}
}
return protoElementInjector;
}
}
_createElementBinder(protoView, boundElementIndex, renderElementBinder, protoElementInjector, componentDirectiveBinding) {
function _createElementBinder(protoView, boundElementIndex, renderElementBinder, protoElementInjector, componentDirectiveBinding) {
var parent = null;
if (renderElementBinder.parentIndex !== -1) {
parent = protoView.elementBinders[renderElementBinder.parentIndex];
@ -349,9 +355,9 @@ export class ProtoViewFactory {
MapWrapper.set(protoView.protoLocals, mappedName, null);
});
return elBinder;
}
}
_bindDirectiveEvents(protoView, elementBinders:List<renderApi.ElementBinder>) {
function _bindDirectiveEvents(protoView, elementBinders:List<renderApi.ElementBinder>) {
for (var boundElementIndex = 0; boundElementIndex < elementBinders.length; ++boundElementIndex) {
var dirs = elementBinders[boundElementIndex].directives;
for (var i = 0; i < dirs.length; i++) {
@ -361,9 +367,9 @@ export class ProtoViewFactory {
protoView.bindEvent(directiveBinder.eventBindings, boundElementIndex, i);
}
}
}
}
class RenderProtoViewWithIndex {
renderProtoView:renderApi.ProtoViewDto;
index:number;

View File

@ -17,7 +17,7 @@ import {isBlank} from 'angular2/src/facade/lang';
import {MapWrapper} from 'angular2/src/facade/collection';
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 {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver';
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', () => {
var renderPv = createRenderProtoView();
var defs = protoViewFactory.getChangeDetectorDefinitions(bindDirective(MainComponent).metadata,
var defs = getChangeDetectorDefinitions(bindDirective(MainComponent).metadata,
renderPv, []);
expect(defs.length).toBe(1);
expect(defs[0].id).toEqual('MainComponent_comp_0');