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); } remove(): void { this.parent.removeChild(this); }
handleEvent(eventName: string, elIndex: number, locals: Locals): boolean { 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); } 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 = 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._typeName}.prototype.detectChangesInRecordsInternal = function(throwOnChange) {
${this._names.genInitLocals()} ${this._names.genInitLocals()}
var ${IS_CHANGED_LOCAL} = false; var ${IS_CHANGED_LOCAL} = false;
@ -67,6 +60,8 @@ export class ChangeDetectorJITGenerator {
${this._names.getAlreadyCheckedName()} = true; ${this._names.getAlreadyCheckedName()} = true;
} }
${this._maybeGenHandleEventInternal()}
${this._genCheckNoChanges()} ${this._genCheckNoChanges()}
${this._maybeGenCallOnAllChangesDone()} ${this._maybeGenCallOnAllChangesDone()}
@ -84,8 +79,20 @@ export class ChangeDetectorJITGenerator {
AbstractChangeDetector, ChangeDetectionUtil, this.records, this.directiveRecords); AbstractChangeDetector, ChangeDetectionUtil, this.records, this.directiveRecords);
} }
_genHandleEvent(): string { _maybeGenHandleEventInternal(): string {
return this.eventBindings.map(eb => this._genEventBinding(eb)).join("\n"); 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 { _genEventBinding(eb: EventBinding): string {

View File

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

View File

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

View File

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

View File

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