refactor(change_detection): rename group memento into directive memento

This commit is contained in:
vsavkin 2015-02-06 13:35:05 -08:00
parent 1ec796a601
commit ab733bd80e
6 changed files with 55 additions and 59 deletions

View File

@ -156,7 +156,7 @@ function notifyTemplate(index:number):string{
return ` return `
if (${CHANGES_LOCAL} && ${CHANGES_LOCAL}.length > 0) { if (${CHANGES_LOCAL} && ${CHANGES_LOCAL}.length > 0) {
if(throwOnChange) ${UTIL}.throwOnChange(${PROTOS_ACCESSOR}[${index}], ${CHANGES_LOCAL}[0]); if(throwOnChange) ${UTIL}.throwOnChange(${PROTOS_ACCESSOR}[${index}], ${CHANGES_LOCAL}[0]);
${DISPATCHER_ACCESSOR}.onRecordChange(${PROTOS_ACCESSOR}[${index}].groupMemento, ${CHANGES_LOCAL}); ${DISPATCHER_ACCESSOR}.onRecordChange(${PROTOS_ACCESSOR}[${index}].directiveMemento, ${CHANGES_LOCAL});
${CHANGES_LOCAL} = null; ${CHANGES_LOCAL} = null;
} }
`; `;
@ -388,7 +388,7 @@ export class ChangeDetectorJITGenerator {
} }
genNotify(r):string{ genNotify(r):string{
return r.lastInGroup ? notifyTemplate(r.selfIndex - 1) : ''; return r.lastInDirective ? notifyTemplate(r.selfIndex - 1) : '';
} }
genArgs(r:ProtoRecord):string { genArgs(r:ProtoRecord):string {

View File

@ -47,10 +47,10 @@ function _selfRecord(r:ProtoRecord, contextIndex:number, selfIndex:number):Proto
contextIndex, contextIndex,
selfIndex, selfIndex,
r.bindingMemento, r.bindingMemento,
r.groupMemento, r.directiveMemento,
r.expressionAsString, r.expressionAsString,
r.lastInBinding, r.lastInBinding,
r.lastInGroup r.lastInDirective
); );
} }
@ -75,10 +75,10 @@ function _replaceIndices(r:ProtoRecord, selfIndex:number, indexMap:Map) {
contextIndex, contextIndex,
selfIndex, selfIndex,
r.bindingMemento, r.bindingMemento,
r.groupMemento, r.directiveMemento,
r.expressionAsString, r.expressionAsString,
r.lastInBinding, r.lastInBinding,
r.lastInGroup r.lastInDirective
); );
} }

View File

@ -54,22 +54,19 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
var protos:List<ProtoRecord> = this.protos; var protos:List<ProtoRecord> = this.protos;
var updatedRecords = null; var updatedRecords = null;
var currentGroup = null;
for (var i = 0; i < protos.length; ++i) { for (var i = 0; i < protos.length; ++i) {
var proto:ProtoRecord = protos[i]; var proto:ProtoRecord = protos[i];
var change = this._check(proto); var change = this._check(proto);
if (isPresent(change)) { if (isPresent(change)) {
currentGroup = proto.groupMemento;
var record = ChangeDetectionUtil.changeRecord(proto.bindingMemento, change); var record = ChangeDetectionUtil.changeRecord(proto.bindingMemento, change);
updatedRecords = ChangeDetectionUtil.addRecord(updatedRecords, record); updatedRecords = ChangeDetectionUtil.addRecord(updatedRecords, record);
} }
if (proto.lastInGroup && isPresent(updatedRecords)) { if (proto.lastInDirective && isPresent(updatedRecords)) {
if (throwOnChange) ChangeDetectionUtil.throwOnChange(proto, updatedRecords[0]); if (throwOnChange) ChangeDetectionUtil.throwOnChange(proto, updatedRecords[0]);
this.dispatcher.onRecordChange(currentGroup, updatedRecords); this.dispatcher.onRecordChange(proto.directiveMemento, updatedRecords);
updatedRecords = null; updatedRecords = null;
} }
} }

View File

@ -45,7 +45,7 @@ export const CHECK_ALWAYS="ALWAYS_CHECK";
export const DETACHED="DETACHED"; export const DETACHED="DETACHED";
export class ChangeDispatcher { export class ChangeDispatcher {
onRecordChange(groupMemento, records:List<ChangeRecord>) {} onRecordChange(directiveMemento, records:List<ChangeRecord>) {}
} }
export class ChangeDetector { export class ChangeDetector {

View File

@ -53,9 +53,9 @@ export class ProtoRecord {
contextIndex:number; contextIndex:number;
selfIndex:number; selfIndex:number;
bindingMemento:any; bindingMemento:any;
groupMemento:any; directiveMemento:any;
lastInBinding:boolean; lastInBinding:boolean;
lastInGroup:boolean; lastInDirective:boolean;
expressionAsString:string; expressionAsString:string;
constructor(mode:number, constructor(mode:number,
@ -66,10 +66,10 @@ export class ProtoRecord {
contextIndex:number, contextIndex:number,
selfIndex:number, selfIndex:number,
bindingMemento:any, bindingMemento:any,
groupMemento:any, directiveMemento:any,
expressionAsString:string, expressionAsString:string,
lastInBinding:boolean, lastInBinding:boolean,
lastInGroup:boolean) { lastInDirective:boolean) {
this.mode = mode; this.mode = mode;
this.name = name; this.name = name;
@ -79,9 +79,9 @@ export class ProtoRecord {
this.contextIndex = contextIndex; this.contextIndex = contextIndex;
this.selfIndex = selfIndex; this.selfIndex = selfIndex;
this.bindingMemento = bindingMemento; this.bindingMemento = bindingMemento;
this.groupMemento = groupMemento; this.directiveMemento = directiveMemento;
this.lastInBinding = lastInBinding; this.lastInBinding = lastInBinding;
this.lastInGroup = lastInGroup; this.lastInDirective = lastInDirective;
this.expressionAsString = expressionAsString; this.expressionAsString = expressionAsString;
} }
@ -93,7 +93,7 @@ export class ProtoRecord {
} }
export class ProtoChangeDetector { export class ProtoChangeDetector {
addAst(ast:AST, bindingMemento:any, groupMemento:any = null, structural:boolean = false){} addAst(ast:AST, bindingMemento:any, directiveMemento:any = null, structural:boolean = false){}
instantiate(dispatcher:any, formatters:Map):ChangeDetector{ instantiate(dispatcher:any, formatters:Map):ChangeDetector{
return null; return null;
} }
@ -108,8 +108,8 @@ export class DynamicProtoChangeDetector extends ProtoChangeDetector {
this._recordBuilder = new ProtoRecordBuilder(); this._recordBuilder = new ProtoRecordBuilder();
} }
addAst(ast:AST, bindingMemento:any, groupMemento:any = null, structural:boolean = false) { addAst(ast:AST, bindingMemento:any, directiveMemento:any = null, structural:boolean = false) {
this._recordBuilder.addAst(ast, bindingMemento, groupMemento, structural); this._recordBuilder.addAst(ast, bindingMemento, directiveMemento, structural);
} }
instantiate(dispatcher:any, formatters:Map) { instantiate(dispatcher:any, formatters:Map) {
@ -135,8 +135,8 @@ export class JitProtoChangeDetector extends ProtoChangeDetector {
this._recordBuilder = new ProtoRecordBuilder(); this._recordBuilder = new ProtoRecordBuilder();
} }
addAst(ast:AST, bindingMemento:any, groupMemento:any = null, structural:boolean = false) { addAst(ast:AST, bindingMemento:any, directiveMemento:any = null, structural:boolean = false) {
this._recordBuilder.addAst(ast, bindingMemento, groupMemento, structural); this._recordBuilder.addAst(ast, bindingMemento, directiveMemento, structural);
} }
instantiate(dispatcher:any, formatters:Map) { instantiate(dispatcher:any, formatters:Map) {
@ -161,19 +161,19 @@ class ProtoRecordBuilder {
this.records = []; this.records = [];
} }
addAst(ast:AST, bindingMemento:any, groupMemento:any = null, structural:boolean = false) { addAst(ast:AST, bindingMemento:any, directiveMemento:any = null, structural:boolean = false) {
if (structural) ast = new Structural(ast); if (structural) ast = new Structural(ast);
var last = ListWrapper.last(this.records); var last = ListWrapper.last(this.records);
if (isPresent(last) && last.groupMemento == groupMemento) { if (isPresent(last) && last.directiveMemento == directiveMemento) {
last.lastInGroup = false; last.lastInDirective = false;
} }
var pr = _ConvertAstIntoProtoRecords.convert(ast, bindingMemento, groupMemento, this.records.length); var pr = _ConvertAstIntoProtoRecords.convert(ast, bindingMemento, directiveMemento, this.records.length);
if (! ListWrapper.isEmpty(pr)) { if (! ListWrapper.isEmpty(pr)) {
var last = ListWrapper.last(pr); var last = ListWrapper.last(pr);
last.lastInBinding = true; last.lastInBinding = true;
last.lastInGroup = true; last.lastInDirective = true;
this.records = ListWrapper.concat(this.records, pr); this.records = ListWrapper.concat(this.records, pr);
} }
@ -183,20 +183,20 @@ class ProtoRecordBuilder {
class _ConvertAstIntoProtoRecords { class _ConvertAstIntoProtoRecords {
protoRecords:List; protoRecords:List;
bindingMemento:any; bindingMemento:any;
groupMemento:any; directiveMemento:any;
contextIndex:number; contextIndex:number;
expressionAsString:string; expressionAsString:string;
constructor(bindingMemento:any, groupMemento:any, contextIndex:number, expressionAsString:string) { constructor(bindingMemento:any, directiveMemento:any, contextIndex:number, expressionAsString:string) {
this.protoRecords = []; this.protoRecords = [];
this.bindingMemento = bindingMemento; this.bindingMemento = bindingMemento;
this.groupMemento = groupMemento; this.directiveMemento = directiveMemento;
this.contextIndex = contextIndex; this.contextIndex = contextIndex;
this.expressionAsString = expressionAsString; this.expressionAsString = expressionAsString;
} }
static convert(ast:AST, bindingMemento:any, groupMemento:any, contextIndex:number) { static convert(ast:AST, bindingMemento:any, directiveMemento:any, contextIndex:number) {
var c = new _ConvertAstIntoProtoRecords(bindingMemento, groupMemento, contextIndex, ast.toString()); var c = new _ConvertAstIntoProtoRecords(bindingMemento, directiveMemento, contextIndex, ast.toString());
ast.visit(c); ast.visit(c);
return c.protoRecords; return c.protoRecords;
} }
@ -292,7 +292,7 @@ class _ConvertAstIntoProtoRecords {
var selfIndex = ++ this.contextIndex; var selfIndex = ++ this.contextIndex;
ListWrapper.push(this.protoRecords, ListWrapper.push(this.protoRecords,
new ProtoRecord(type, name, funcOrValue, args, fixedArgs, context, selfIndex, new ProtoRecord(type, name, funcOrValue, args, fixedArgs, context, selfIndex,
this.bindingMemento, this.groupMemento, this.expressionAsString, false, false)); this.bindingMemento, this.directiveMemento, this.expressionAsString, false, false));
return selfIndex; return selfIndex;
} }
} }

View File

@ -195,10 +195,10 @@ export class View {
this._dehydrateContext(); this._dehydrateContext();
} }
onRecordChange(groupMemento, records:List) { onRecordChange(directiveMemento, records:List) {
this._invokeMementos(records); this._invokeMementos(records);
if (groupMemento instanceof DirectivePropertyGroupMemento) { if (directiveMemento instanceof DirectiveMemento) {
this._notifyDirectiveAboutChanges(groupMemento, records); this._notifyDirectiveAboutChanges(directiveMemento, records);
} }
} }
@ -208,9 +208,9 @@ export class View {
} }
} }
_notifyDirectiveAboutChanges(groupMemento, records:List) { _notifyDirectiveAboutChanges(directiveMemento, records:List) {
var dir = groupMemento.directive(this.elementInjectors); var dir = directiveMemento.directive(this.elementInjectors);
var binding = groupMemento.directiveBinding(this.elementInjectors); var binding = directiveMemento.directiveBinding(this.elementInjectors);
if (binding.callOnChange) { if (binding.callOnChange) {
dir.onChange(this._collectChanges(records)); dir.onChange(this._collectChanges(records));
@ -220,13 +220,12 @@ export class View {
// dispatch to element injector or text nodes based on context // dispatch to element injector or text nodes based on context
_invokeMementoFor(record:ChangeRecord) { _invokeMementoFor(record:ChangeRecord) {
var memento = record.bindingMemento; var memento = record.bindingMemento;
if (memento instanceof DirectivePropertyMemento) { if (memento instanceof DirectiveBindingMemento) {
// we know that it is DirectivePropertyMemento var directiveMemento:DirectiveBindingMemento = memento;
var directiveMemento:DirectivePropertyMemento = memento;
directiveMemento.invoke(record, this.elementInjectors); directiveMemento.invoke(record, this.elementInjectors);
} else if (memento instanceof ElementPropertyMemento) { } else if (memento instanceof ElementBindingMemento) {
var elementMemento:ElementPropertyMemento = memento; var elementMemento:ElementBindingMemento = memento;
elementMemento.invoke(record, this.bindElements); elementMemento.invoke(record, this.bindElements);
} else { } else {
@ -451,7 +450,7 @@ export class ProtoView {
} }
ListWrapper.push(elBinder.textNodeIndices, indexInParent); ListWrapper.push(elBinder.textNodeIndices, indexInParent);
var memento = this.textNodesWithBindingCount++; var memento = this.textNodesWithBindingCount++;
this.protoChangeDetector.addAst(expression, memento, memento); this.protoChangeDetector.addAst(expression, memento);
} }
/** /**
@ -463,8 +462,8 @@ export class ProtoView {
elBinder.hasElementPropertyBindings = true; elBinder.hasElementPropertyBindings = true;
this.elementsWithBindingCount++; this.elementsWithBindingCount++;
} }
var memento = new ElementPropertyMemento(this.elementsWithBindingCount-1, setterName, setter); var memento = new ElementBindingMemento(this.elementsWithBindingCount-1, setterName, setter);
this.protoChangeDetector.addAst(expression, memento, memento); this.protoChangeDetector.addAst(expression, memento);
} }
/** /**
@ -488,14 +487,14 @@ export class ProtoView {
setter:SetterFn, setter:SetterFn,
isContentWatch: boolean) { isContentWatch: boolean) {
var expMemento = new DirectivePropertyMemento( var bindingMemento = new DirectiveBindingMemento(
this.elementBinders.length-1, this.elementBinders.length-1,
directiveIndex, directiveIndex,
setterName, setterName,
setter setter
); );
var groupMemento = DirectivePropertyGroupMemento.get(expMemento); var directiveMemento = DirectiveMemento.get(bindingMemento);
this.protoChangeDetector.addAst(expression, expMemento, groupMemento, isContentWatch); this.protoChangeDetector.addAst(expression, bindingMemento, directiveMemento, isContentWatch);
} }
// Create a rootView as if the compiler encountered <rootcmp></rootcmp>, // Create a rootView as if the compiler encountered <rootcmp></rootcmp>,
@ -519,7 +518,7 @@ export class ProtoView {
} }
} }
export class ElementPropertyMemento { export class ElementBindingMemento {
_elementIndex:int; _elementIndex:int;
_setterName:string; _setterName:string;
_setter:SetterFn; _setter:SetterFn;
@ -535,7 +534,7 @@ export class ElementPropertyMemento {
} }
} }
export class DirectivePropertyMemento { export class DirectiveBindingMemento {
_elementInjectorIndex:int; _elementInjectorIndex:int;
_directiveIndex:int; _directiveIndex:int;
_setterName:string; _setterName:string;
@ -558,9 +557,9 @@ export class DirectivePropertyMemento {
} }
} }
var _groups = MapWrapper.create(); var _directiveMementos = MapWrapper.create();
class DirectivePropertyGroupMemento { class DirectiveMemento {
_elementInjectorIndex:number; _elementInjectorIndex:number;
_directiveIndex:number; _directiveIndex:number;
@ -569,15 +568,15 @@ class DirectivePropertyGroupMemento {
this._directiveIndex = directiveIndex; this._directiveIndex = directiveIndex;
} }
static get(memento:DirectivePropertyMemento) { static get(memento:DirectiveBindingMemento) {
var elementInjectorIndex = memento._elementInjectorIndex; var elementInjectorIndex = memento._elementInjectorIndex;
var directiveIndex = memento._directiveIndex; var directiveIndex = memento._directiveIndex;
var id = elementInjectorIndex * 100 + directiveIndex; var id = elementInjectorIndex * 100 + directiveIndex;
if (!MapWrapper.contains(_groups, id)) { if (!MapWrapper.contains(_directiveMementos, id)) {
MapWrapper.set(_groups, id, new DirectivePropertyGroupMemento(elementInjectorIndex, directiveIndex)); MapWrapper.set(_directiveMementos, id, new DirectiveMemento(elementInjectorIndex, directiveIndex));
} }
return MapWrapper.get(_groups, id); return MapWrapper.get(_directiveMementos, id);
} }
directive(elementInjectors:List<ElementInjector>) { directive(elementInjectors:List<ElementInjector>) {