feat(change_detection): generate checkNoChanges only in dev mode
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import {isPresent} from 'angular2/src/facade/lang';
|
||||
import {isPresent, BaseException} from 'angular2/src/facade/lang';
|
||||
import {List, ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {ChangeDetectorRef} from './change_detector_ref';
|
||||
import {ChangeDetector} from './interfaces';
|
||||
@ -38,11 +38,11 @@ export class AbstractChangeDetector implements ChangeDetector {
|
||||
|
||||
remove(): void { this.parent.removeChild(this); }
|
||||
|
||||
detectChanges(): void { this._detectChanges(false); }
|
||||
detectChanges(): void { this.runDetectChanges(false); }
|
||||
|
||||
checkNoChanges(): void { this._detectChanges(true); }
|
||||
checkNoChanges(): void { throw new BaseException("Not implemented"); }
|
||||
|
||||
_detectChanges(throwOnChange: boolean): void {
|
||||
runDetectChanges(throwOnChange: boolean): void {
|
||||
if (this.mode === DETACHED || this.mode === CHECKED) return;
|
||||
|
||||
this.detectChangesInRecords(throwOnChange);
|
||||
@ -67,14 +67,14 @@ export class AbstractChangeDetector implements ChangeDetector {
|
||||
_detectChangesInLightDomChildren(throwOnChange: boolean): void {
|
||||
var c = this.lightDomChildren;
|
||||
for (var i = 0; i < c.length; ++i) {
|
||||
c[i]._detectChanges(throwOnChange);
|
||||
c[i].runDetectChanges(throwOnChange);
|
||||
}
|
||||
}
|
||||
|
||||
_detectChangesInShadowDomChildren(throwOnChange: boolean): void {
|
||||
var c = this.shadowDomChildren;
|
||||
for (var i = 0; i < c.length; ++i) {
|
||||
c[i]._detectChanges(throwOnChange);
|
||||
c[i].runDetectChanges(throwOnChange);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,8 @@ export class ChangeDetectorJITGenerator {
|
||||
_names: CodegenNameUtil;
|
||||
|
||||
constructor(public id: string, public changeDetectionStrategy: string,
|
||||
public records: List<ProtoRecord>, public directiveRecords: List<any>) {
|
||||
public records: List<ProtoRecord>, public directiveRecords: List<any>,
|
||||
private generateCheckNoChanges: boolean) {
|
||||
this._names = new CodegenNameUtil(this.records, this.directiveRecords, 'this._', UTIL);
|
||||
}
|
||||
|
||||
@ -80,6 +81,8 @@ export class ChangeDetectorJITGenerator {
|
||||
${ALREADY_CHECKED_ACCESSOR} = true;
|
||||
}
|
||||
|
||||
${this._genCheckNoChanges(typeName)}
|
||||
|
||||
${typeName}.prototype.callOnAllChangesDone = function() {
|
||||
${this._genCallOnAllChangesDoneBody()}
|
||||
}
|
||||
@ -109,6 +112,7 @@ export class ChangeDetectorJITGenerator {
|
||||
return new ${typeName}(dispatcher, protos, directiveRecords);
|
||||
}
|
||||
`;
|
||||
|
||||
return new Function('AbstractChangeDetector', 'ChangeDetectionUtil', 'protos',
|
||||
'directiveRecords', classDefinition)(
|
||||
AbstractChangeDetector, ChangeDetectionUtil, this.records, this.directiveRecords);
|
||||
@ -330,11 +334,23 @@ export class ChangeDetectorJITGenerator {
|
||||
}
|
||||
|
||||
_genThrowOnChangeCheck(oldValue: string, newValue: string): string {
|
||||
return `
|
||||
if(throwOnChange) {
|
||||
${UTIL}.throwOnChange(${CURRENT_PROTO}, ${UTIL}.simpleChange(${oldValue}, ${newValue}));
|
||||
}
|
||||
`;
|
||||
if (this.generateCheckNoChanges) {
|
||||
return `
|
||||
if(throwOnChange) {
|
||||
${UTIL}.throwOnChange(${CURRENT_PROTO}, ${UTIL}.simpleChange(${oldValue}, ${newValue}));
|
||||
}
|
||||
`;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
_genCheckNoChanges(typeName: string): string {
|
||||
if (this.generateCheckNoChanges) {
|
||||
return `${typeName}.prototype.checkNoChanges = function() { this.runDetectChanges(true); }`;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
_genAddToChanges(r: ProtoRecord): string {
|
||||
|
@ -65,6 +65,8 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
|
||||
|
||||
hydrated(): boolean { return this.values[0] !== null; }
|
||||
|
||||
checkNoChanges(): void { this.runDetectChanges(true); }
|
||||
|
||||
detectChangesInRecords(throwOnChange: boolean) {
|
||||
if (!this.hydrated()) {
|
||||
ChangeDetectionUtil.throwDehydrated();
|
||||
|
@ -63,5 +63,6 @@ export interface ProtoChangeDetector { instantiate(dispatcher: any): ChangeDetec
|
||||
export class ChangeDetectorDefinition {
|
||||
constructor(public id: string, public strategy: string, public variableNames: List<string>,
|
||||
public bindingRecords: List<BindingRecord>,
|
||||
public directiveRecords: List<DirectiveRecord>) {}
|
||||
public directiveRecords: List<DirectiveRecord>,
|
||||
public generateCheckNoChanges: boolean) {}
|
||||
}
|
||||
|
@ -23,7 +23,8 @@ export class JitProtoChangeDetector implements ProtoChangeDetector {
|
||||
(b) => { recordBuilder.add(b, definition.variableNames); });
|
||||
var records = coalesce(recordBuilder.records);
|
||||
return new ChangeDetectorJITGenerator(definition.id, definition.strategy, records,
|
||||
this.definition.directiveRecords)
|
||||
this.definition.directiveRecords,
|
||||
this.definition.generateCheckNoChanges)
|
||||
.generate();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user