perf(change detection): minimized amount of code in protective try-catch
This commit is contained in:
parent
a6a6273263
commit
1320175646
|
@ -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,32 +42,28 @@ 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++;
|
if (record.terminatesExpression()) {
|
||||||
if (record.terminatesExpression()) {
|
if (throwOnChange) throw new ExpressionChangedAfterItHasBeenChecked(record);
|
||||||
if (throwOnChange) throw new ExpressionChangedAfterItHasBeenChecked(record);
|
currentRange = record.recordRange;
|
||||||
currentRange = record.recordRange;
|
currentGroup = record.groupMemento();
|
||||||
currentGroup = record.groupMemento();
|
updatedRecords = this._addRecord(updatedRecords, record);
|
||||||
updatedRecords = this._addRecord(updatedRecords, record);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPresent(updatedRecords)) {
|
|
||||||
var nextEnabled = record.nextEnabled;
|
|
||||||
if (isBlank(nextEnabled) || // we have reached the last enabled record
|
|
||||||
currentRange != nextEnabled.recordRange || // the next record is in a different range
|
|
||||||
currentGroup != nextEnabled.groupMemento()) { // the next record is in a different group
|
|
||||||
currentRange.dispatcher.onRecordChange(currentGroup, updatedRecords);
|
|
||||||
updatedRecords = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
record = record.findNextEnabled();
|
|
||||||
}
|
}
|
||||||
} catch(e) {
|
|
||||||
throw new ChangeDetectionError(record, e);
|
if (isPresent(updatedRecords)) {
|
||||||
|
var nextEnabled = record.nextEnabled;
|
||||||
|
if (isBlank(nextEnabled) || // we have reached the last enabled record
|
||||||
|
currentRange != nextEnabled.recordRange || // the next record is in a different range
|
||||||
|
currentGroup != nextEnabled.groupMemento()) { // the next record is in a different group
|
||||||
|
currentRange.dispatcher.onRecordChange(currentGroup, updatedRecords);
|
||||||
|
updatedRecords = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record = record.findNextEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue