X Tutup
Skip to content

Commit 71bb4b3

Browse files
committed
feat(change_detection): generate checkNoChanges only in dev mode
1 parent a2bb81c commit 71bb4b3

File tree

11 files changed

+68
-31
lines changed

11 files changed

+68
-31
lines changed

modules/angular2/src/change_detection/abstract_change_detector.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isPresent} from 'angular2/src/facade/lang';
1+
import {isPresent, BaseException} from 'angular2/src/facade/lang';
22
import {List, ListWrapper} from 'angular2/src/facade/collection';
33
import {ChangeDetectorRef} from './change_detector_ref';
44
import {ChangeDetector} from './interfaces';
@@ -38,11 +38,11 @@ export class AbstractChangeDetector implements ChangeDetector {
3838

3939
remove(): void { this.parent.removeChild(this); }
4040

41-
detectChanges(): void { this._detectChanges(false); }
41+
detectChanges(): void { this.runDetectChanges(false); }
4242

43-
checkNoChanges(): void { this._detectChanges(true); }
43+
checkNoChanges(): void { throw new BaseException("Not implemented"); }
4444

45-
_detectChanges(throwOnChange: boolean): void {
45+
runDetectChanges(throwOnChange: boolean): void {
4646
if (this.mode === DETACHED || this.mode === CHECKED) return;
4747

4848
this.detectChangesInRecords(throwOnChange);
@@ -67,14 +67,14 @@ export class AbstractChangeDetector implements ChangeDetector {
6767
_detectChangesInLightDomChildren(throwOnChange: boolean): void {
6868
var c = this.lightDomChildren;
6969
for (var i = 0; i < c.length; ++i) {
70-
c[i]._detectChanges(throwOnChange);
70+
c[i].runDetectChanges(throwOnChange);
7171
}
7272
}
7373

7474
_detectChangesInShadowDomChildren(throwOnChange: boolean): void {
7575
var c = this.shadowDomChildren;
7676
for (var i = 0; i < c.length; ++i) {
77-
c[i]._detectChanges(throwOnChange);
77+
c[i].runDetectChanges(throwOnChange);
7878
}
7979
}
8080

modules/angular2/src/change_detection/change_detection_jit_generator.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export class ChangeDetectorJITGenerator {
3535
_names: CodegenNameUtil;
3636

3737
constructor(public id: string, public changeDetectionStrategy: string,
38-
public records: List<ProtoRecord>, public directiveRecords: List<any>) {
38+
public records: List<ProtoRecord>, public directiveRecords: List<any>,
39+
private generateCheckNoChanges: boolean) {
3940
this._names = new CodegenNameUtil(this.records, this.directiveRecords, 'this._', UTIL);
4041
}
4142

@@ -80,6 +81,8 @@ export class ChangeDetectorJITGenerator {
8081
${ALREADY_CHECKED_ACCESSOR} = true;
8182
}
8283
84+
${this._genCheckNoChanges(typeName)}
85+
8386
${typeName}.prototype.callOnAllChangesDone = function() {
8487
${this._genCallOnAllChangesDoneBody()}
8588
}
@@ -109,6 +112,7 @@ export class ChangeDetectorJITGenerator {
109112
return new ${typeName}(dispatcher, protos, directiveRecords);
110113
}
111114
`;
115+
112116
return new Function('AbstractChangeDetector', 'ChangeDetectionUtil', 'protos',
113117
'directiveRecords', classDefinition)(
114118
AbstractChangeDetector, ChangeDetectionUtil, this.records, this.directiveRecords);
@@ -330,11 +334,23 @@ export class ChangeDetectorJITGenerator {
330334
}
331335

332336
_genThrowOnChangeCheck(oldValue: string, newValue: string): string {
333-
return `
334-
if(throwOnChange) {
335-
${UTIL}.throwOnChange(${CURRENT_PROTO}, ${UTIL}.simpleChange(${oldValue}, ${newValue}));
336-
}
337-
`;
337+
if (this.generateCheckNoChanges) {
338+
return `
339+
if(throwOnChange) {
340+
${UTIL}.throwOnChange(${CURRENT_PROTO}, ${UTIL}.simpleChange(${oldValue}, ${newValue}));
341+
}
342+
`;
343+
} else {
344+
return '';
345+
}
346+
}
347+
348+
_genCheckNoChanges(typeName: string): string {
349+
if (this.generateCheckNoChanges) {
350+
return `${typeName}.prototype.checkNoChanges = function() { this.runDetectChanges(true); }`;
351+
} else {
352+
return '';
353+
}
338354
}
339355

340356
_genAddToChanges(r: ProtoRecord): string {

modules/angular2/src/change_detection/dynamic_change_detector.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
6565

6666
hydrated(): boolean { return this.values[0] !== null; }
6767

68+
checkNoChanges(): void { this.runDetectChanges(true); }
69+
6870
detectChangesInRecords(throwOnChange: boolean) {
6971
if (!this.hydrated()) {
7072
ChangeDetectionUtil.throwDehydrated();

modules/angular2/src/change_detection/interfaces.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ export interface ProtoChangeDetector { instantiate(dispatcher: any): ChangeDetec
6363
export class ChangeDetectorDefinition {
6464
constructor(public id: string, public strategy: string, public variableNames: List<string>,
6565
public bindingRecords: List<BindingRecord>,
66-
public directiveRecords: List<DirectiveRecord>) {}
66+
public directiveRecords: List<DirectiveRecord>,
67+
public generateCheckNoChanges: boolean) {}
6768
}

modules/angular2/src/change_detection/jit_proto_change_detector.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export class JitProtoChangeDetector implements ProtoChangeDetector {
2323
(b) => { recordBuilder.add(b, definition.variableNames); });
2424
var records = coalesce(recordBuilder.records);
2525
return new ChangeDetectorJITGenerator(definition.id, definition.strategy, records,
26-
this.definition.directiveRecords)
26+
this.definition.directiveRecords,
27+
this.definition.generateCheckNoChanges)
2728
.generate();
2829
}
2930
}

modules/angular2/src/core/compiler/proto_view_factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Injectable} from 'angular2/di';
22

33
import {List, ListWrapper, MapWrapper} from 'angular2/src/facade/collection';
4-
import {isPresent, isBlank, BaseException} from 'angular2/src/facade/lang';
4+
import {isPresent, isBlank, BaseException, assertionsEnabled} from 'angular2/src/facade/lang';
55
import {reflector} from 'angular2/src/reflection/reflection';
66

77
import {
@@ -246,7 +246,7 @@ function _getChangeDetectorDefinitions(
246246
var id = `${hostComponentMetadata.id}_${typeString}_${pvWithIndex.index}`;
247247
var variableNames = nestedPvVariableNames[pvWithIndex.index];
248248
return new ChangeDetectorDefinition(id, strategyName, variableNames, bindingRecords,
249-
directiveRecords);
249+
directiveRecords, assertionsEnabled());
250250
});
251251
}
252252

modules/angular2/src/transform/template_compiler/change_detector_codegen.dart

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ class _CodegenState {
7474
final List<ProtoRecord> _records;
7575
final List<DirectiveRecord> _directiveRecords;
7676
final CodegenNameUtil _names;
77+
final bool _generateCheckNoChanges;
7778

7879
_CodegenState._(this._changeDetectorDefId, this._contextTypeName,
7980
this._changeDetectorTypeName, String changeDetectionStrategy,
80-
List<ProtoRecord> records, List<DirectiveRecord> directiveRecords)
81+
List<ProtoRecord> records, List<DirectiveRecord> directiveRecords, this._generateCheckNoChanges)
8182
: _records = records,
8283
_directiveRecords = directiveRecords,
8384
_names = new CodegenNameUtil(records, directiveRecords, '_', _UTIL),
@@ -91,7 +92,7 @@ class _CodegenState {
9192
.forEach((rec) => protoRecords.add(rec, def.variableNames));
9293
var records = coalesce(protoRecords.records);
9394
return new _CodegenState._(def.id, typeName, changeDetectorTypeName,
94-
def.strategy, records, def.directiveRecords);
95+
def.strategy, records, def.directiveRecords, def.generateCheckNoChanges);
9596
}
9697

9798
void _writeToBuf(StringBuffer buf) {
@@ -138,6 +139,8 @@ class _CodegenState {
138139
$_ALREADY_CHECKED_ACCESSOR = true;
139140
}
140141
142+
${_genCheckNoChanges()}
143+
141144
void callOnAllChangesDone() {
142145
${_getCallOnAllChangesDoneBody()}
143146
}
@@ -393,12 +396,24 @@ class _CodegenState {
393396
}
394397

395398
String _genThrowOnChangeCheck(String oldValue, String newValue) {
396-
return '''
397-
if(throwOnChange) {
398-
$_UTIL.throwOnChange(
399-
$_CURRENT_PROTO, $_UTIL.simpleChange(${oldValue}, ${newValue}));
400-
}
401-
''';
399+
if (this._generateCheckNoChanges) {
400+
return '''
401+
if(throwOnChange) {
402+
$_UTIL.throwOnChange(
403+
$_CURRENT_PROTO, $_UTIL.simpleChange(${oldValue}, ${newValue}));
404+
}
405+
''';
406+
} else {
407+
return "";
408+
}
409+
}
410+
411+
String _genCheckNoChanges() {
412+
if (this._generateCheckNoChanges) {
413+
return 'void checkNoChanges() { this.runDetectChanges(true); }';
414+
} else {
415+
return '';
416+
}
402417
}
403418

404419
String _genAddToChanges(ProtoRecord r) {

modules/angular2/test/change_detection/change_detection_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function main() {
2323

2424
beforeEach(() => {
2525
proto = new SpyProtoChangeDetector();
26-
def = new ChangeDetectorDefinition('id', null, [], [], []);
26+
def = new ChangeDetectorDefinition('id', null, [], [], [], true);
2727
});
2828

2929
it("should return a proto change detector when one is available", () => {

modules/angular2/test/change_detection/change_detector_config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function getDefinition(id: string): TestDefinition {
6969
var bindingRecords = _createBindingRecords(id);
7070
var directiveRecords = [];
7171
let cdDef = new ChangeDetectorDefinition(id, strategy, variableBindings, bindingRecords,
72-
directiveRecords);
72+
directiveRecords, true);
7373
testDef = new TestDefinition(id, cdDef, null);
7474
}
7575
if (isBlank(testDef)) {
@@ -107,7 +107,7 @@ class _ExpressionWithLocals {
107107
var bindingRecords = _createBindingRecords(this._expression);
108108
var directiveRecords = [];
109109
return new ChangeDetectorDefinition('(empty id)', strategy, variableBindings, bindingRecords,
110-
directiveRecords);
110+
directiveRecords, true);
111111
}
112112

113113
/**
@@ -151,7 +151,7 @@ class _ExpressionWithMode {
151151
directiveRecords = [];
152152
}
153153
return new ChangeDetectorDefinition('(empty id)', this._strategy, variableBindings,
154-
bindingRecords, directiveRecords);
154+
bindingRecords, directiveRecords, true);
155155
}
156156

157157
/**
@@ -174,7 +174,7 @@ class _DirectiveUpdating {
174174
var variableBindings = [];
175175

176176
return new ChangeDetectorDefinition('(empty id)', strategy, variableBindings,
177-
this._bindingRecords, this._directiveRecords);
177+
this._bindingRecords, this._directiveRecords, true);
178178
}
179179

180180
static updateA(expression: string, dirRecord): BindingRecord {

modules/angular2/test/transform/integration/two_annotations_files/expected/bar.ng_deps.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ class _MyComponent_ChangeDetector0 extends _gen.AbstractChangeDetector {
9191
_alreadyChecked = true;
9292
}
9393

94+
void checkNoChanges() {this.runDetectChanges(true);}
95+
9496
void callOnAllChangesDone() {
9597
dispatcher.notifyOnAllChangesDone();
9698
}

0 commit comments

Comments
 (0)
X Tutup