perf(change detection): minimized amount of code in protective try-catch

This commit is contained in:
Stephen Adams 2015-01-07 11:47:25 -08:00 committed by Rado Kirov
parent a6a6273263
commit 1320175646
2 changed files with 48 additions and 42 deletions

View File

@ -19,22 +19,6 @@ class ExpressionChangedAfterItHasBeenChecked extends Error {
} }
} }
export class ChangeDetectionError extends Error {
message:string;
originalException:any;
location:string;
constructor(record:Record, originalException:any) {
this.originalException = originalException;
this.location = record.protoRecord.expressionAsString;
this.message = `${this.originalException} in [${this.location}]`;
}
toString():string {
return this.message;
}
}
export class ChangeDetector { export class ChangeDetector {
_rootRecordRange:RecordRange; _rootRecordRange:RecordRange;
_enforceNoNewChanges:boolean; _enforceNoNewChanges:boolean;
@ -58,7 +42,6 @@ export class ChangeDetector {
var record = this._rootRecordRange.findFirstEnabledRecord(); var record = this._rootRecordRange.findFirstEnabledRecord();
var currentRange, currentGroup; var currentRange, currentGroup;
try {
while (isPresent(record)) { while (isPresent(record)) {
if (record.check()) { if (record.check()) {
count++; count++;
@ -82,9 +65,6 @@ export class ChangeDetector {
record = record.findNextEnabled(); record = record.findNextEnabled();
} }
} catch(e) {
throw new ChangeDetectionError(record, e);
}
return count; return count;
} }

View File

@ -274,12 +274,22 @@ export class Record {
} }
_calculateNewValue() { _calculateNewValue() {
try {
return this.__calculateNewValue();
} catch (e) {
throw new ChangeDetectionError(this, e);
}
}
__calculateNewValue() {
switch (this.getType()) { switch (this.getType()) {
case RECORD_TYPE_PROPERTY: case RECORD_TYPE_PROPERTY:
return this.funcOrValue(this.context); var propertyGetter:Function = this.funcOrValue;
return propertyGetter(this.context);
case RECORD_TYPE_INVOKE_METHOD: case RECORD_TYPE_INVOKE_METHOD:
return this.funcOrValue(this.context, this.args); var methodInvoker:Function = this.funcOrValue;
return methodInvoker(this.context, this.args);
case RECORD_TYPE_INVOKE_CLOSURE: case RECORD_TYPE_INVOKE_CLOSURE:
return FunctionWrapper.apply(this.context, this.args); return FunctionWrapper.apply(this.context, this.args);
@ -477,3 +487,19 @@ function isSame(a, b) {
if ((a !== a) && (b !== b)) return true; if ((a !== a) && (b !== b)) return true;
return false; return false;
} }
export class ChangeDetectionError extends Error {
message:string;
originalException:any;
location:string;
constructor(record:Record, originalException:any) {
this.originalException = originalException;
this.location = record.protoRecord.expressionAsString;
this.message = `${this.originalException} in [${this.location}]`;
}
toString():string {
return this.message;
}
}