refactor(change_detection): generate handleEvent only when necessary

This commit is contained in:
vsavkin 2015-08-16 18:29:43 -07:00 committed by Victor Savkin
parent 6c9e712c34
commit 823fa4689e
6 changed files with 42 additions and 25 deletions

View File

@ -67,7 +67,13 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
remove(): void { this.parent.removeChild(this); }
handleEvent(eventName: string, elIndex: number, locals: Locals): boolean {
throw new BaseException("Not implemented");
var res = this.handleEventInternal(eventName, elIndex, locals);
this.markPathToRootAsCheckOnce();
return res;
}
handleEventInternal(eventName: string, elIndex: number, locals: Locals): boolean {
return false;
}
detectChanges(): void { this.runDetectChanges(false); }

View File

@ -50,13 +50,6 @@ export class ChangeDetectorJITGenerator {
${this._typeName}.prototype = Object.create(${ABSTRACT_CHANGE_DETECTOR}.prototype);
${this._typeName}.prototype.handleEvent = function(eventName, elIndex, locals) {
var ${this._names.getPreventDefaultAccesor()} = false;
${this._names.genInitEventLocals()}
${this._genHandleEvent()}
return ${this._names.getPreventDefaultAccesor()};
}
${this._typeName}.prototype.detectChangesInRecordsInternal = function(throwOnChange) {
${this._names.genInitLocals()}
var ${IS_CHANGED_LOCAL} = false;
@ -67,6 +60,8 @@ export class ChangeDetectorJITGenerator {
${this._names.getAlreadyCheckedName()} = true;
}
${this._maybeGenHandleEventInternal()}
${this._genCheckNoChanges()}
${this._maybeGenCallOnAllChangesDone()}
@ -84,8 +79,20 @@ export class ChangeDetectorJITGenerator {
AbstractChangeDetector, ChangeDetectionUtil, this.records, this.directiveRecords);
}
_genHandleEvent(): string {
return this.eventBindings.map(eb => this._genEventBinding(eb)).join("\n");
_maybeGenHandleEventInternal(): string {
if (this.eventBindings.length > 0) {
var handlers = this.eventBindings.map(eb => this._genEventBinding(eb)).join("\n");
return `
${this._typeName}.prototype.handleEvent = function(eventName, elIndex, locals) {
var ${this._names.getPreventDefaultAccesor()} = false;
${this._names.genInitEventLocals()}
${handlers}
return ${this._names.getPreventDefaultAccesor()};
}
`;
} else {
return '';
}
}
_genEventBinding(eb: EventBinding): string {

View File

@ -31,7 +31,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
this.dehydrateDirectives(false);
}
handleEvent(eventName: string, elIndex: number, locals: Locals): boolean {
handleEventInternal(eventName: string, elIndex: number, locals: Locals): boolean {
var preventDefault = false;
this._matchingEventBindings(eventName, elIndex)
@ -41,6 +41,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
preventDefault = true;
}
});
return preventDefault;
}

View File

@ -125,13 +125,6 @@ class _CodegenState {
dehydrateDirectives(false);
}
bool handleEvent(eventName, elIndex, locals) {
var ${_names.getPreventDefaultAccesor()} = false;
${_names.genInitEventLocals()}
${_genHandleEvent()}
return ${this._names.getPreventDefaultAccesor()};
}
void detectChangesInRecordsInternal(throwOnChange) {
${_names.genInitLocals()}
var $_IS_CHANGED_LOCAL = false;
@ -142,6 +135,8 @@ class _CodegenState {
${_names.getAlreadyCheckedName()} = true;
}
${_maybeGenHandleEventInternal()}
${_genCheckNoChanges()}
${_maybeGenCallOnAllChangesDone()}
@ -161,8 +156,20 @@ class _CodegenState {
''');
}
String _genHandleEvent() {
return _eventBindings.map((eb) => _genEventBinding(eb)).join("\n");
String _maybeGenHandleEventInternal() {
if (_eventBindings.length > 0) {
var handlers = _eventBindings.map((eb) => _genEventBinding(eb)).join("\n");
return '''
handleEventInternal(eventName, elIndex, locals) {
var ${this._names.getPreventDefaultAccesor()} = false;
${this._names.genInitEventLocals()}
${handlers}
return ${this._names.getPreventDefaultAccesor()};
}
''';
} else {
return '';
}
}
String _genEventBinding(EventBinding eb) {

View File

@ -899,6 +899,7 @@ export function main() {
res = val.changeDetector.handleEvent("event", 0, locals);
expect(res).toBe(false);
});
});
});
});

View File

@ -36,11 +36,6 @@ class _MyComponent_ChangeDetector0
dehydrateDirectives(false);
}
bool handleEvent(eventName, elIndex, locals) {
var preventDefault = false;
return preventDefault;
}
void detectChangesInRecordsInternal(throwOnChange) {
var l_context = this.context, l_myNum0, c_myNum0, l_interpolate1;
c_myNum0 = false;