Skip to content

Commit 69926dd

Browse files
committed
refactor(change_detection): introduce enum ChangeDetectionStrategy
BREAKING CHANGE Closes angular#2497 - change detection strategy type changes from string to ChangeDetectionStrategy - CHECK_ONCE => ChangeDetectionStrategy.CheckOnce - CHECKED => ChangeDetectionStrategy.Checked - CHECK_ALWAYS => ChangeDetectionStrategy.CheckAlways - DETACHED => ChangeDetectionStrategy.Detached - ON_PUSH => ChangeDetectionStrategy.OnPush - DEFAULT => ChangeDetectionStrategy.Default - ON_PUSH_OBSERVE => ChangeDetectionStrategy.OnPushObserve
1 parent e41d745 commit 69926dd

35 files changed

+394
-371
lines changed

modules/angular2/change_detection.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
*/
66

77
export {
8-
CHECK_ONCE,
9-
CHECK_ALWAYS,
10-
DETACHED,
11-
CHECKED,
12-
ON_PUSH,
13-
DEFAULT,
8+
ChangeDetectionStrategy,
149

1510
ExpressionChangedAfterItHasBeenCheckedException,
1611
ChangeDetectionError,

modules/angular2/src/core/change_detection/abstract_change_detector.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ import {
1212
} from './exceptions';
1313
import {BindingTarget} from './binding_record';
1414
import {Locals} from './parser/locals';
15-
import {CHECK_ALWAYS, CHECK_ONCE, CHECKED, DETACHED} from './constants';
15+
import {ChangeDetectionStrategy} from './constants';
1616
import {wtfCreateScope, wtfLeave, WtfScopeFn} from '../profile/profile';
1717
import {isObservable} from './observable_facade';
18-
import {ON_PUSH_OBSERVE} from './constants';
1918

2019

2120
var _scope_check: WtfScopeFn = wtfCreateScope(`ChangeDetector#check(ascii id, bool throwOnChange)`);
@@ -36,7 +35,7 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
3635
alreadyChecked: any = false;
3736
context: T;
3837
locals: Locals = null;
39-
mode: string = null;
38+
mode: ChangeDetectionStrategy = null;
4039
pipes: Pipes = null;
4140
propertyBindingIndex: number;
4241

@@ -46,7 +45,7 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
4645

4746
constructor(public id: string, public dispatcher: ChangeDispatcher,
4847
public numberOfPropertyProtoRecords: number, public bindingTargets: BindingTarget[],
49-
public directiveIndices: DirectiveIndex[], public strategy: string) {
48+
public directiveIndices: DirectiveIndex[], public strategy: ChangeDetectionStrategy) {
5049
this.ref = new ChangeDetectorRef(this);
5150
}
5251

@@ -79,14 +78,16 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
7978
checkNoChanges(): void { throw new BaseException("Not implemented"); }
8079

8180
runDetectChanges(throwOnChange: boolean): void {
82-
if (StringWrapper.equals(this.mode, DETACHED) || StringWrapper.equals(this.mode, CHECKED))
81+
if (this.mode === ChangeDetectionStrategy.Detached ||
82+
this.mode === ChangeDetectionStrategy.Checked)
8383
return;
8484
var s = _scope_check(this.id, throwOnChange);
8585
this.detectChangesInRecords(throwOnChange);
8686
this._detectChangesInLightDomChildren(throwOnChange);
8787
if (throwOnChange === false) this.callOnAllChangesDone();
8888
this._detectChangesInShadowDomChildren(throwOnChange);
89-
if (StringWrapper.equals(this.mode, CHECK_ONCE)) this.mode = CHECKED;
89+
if (this.mode === ChangeDetectionStrategy.CheckOnce)
90+
this.mode = ChangeDetectionStrategy.Checked;
9091
wtfLeave(s);
9192
}
9293

@@ -121,7 +122,7 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
121122
this.mode = ChangeDetectionUtil.changeDetectionMode(this.strategy);
122123
this.context = context;
123124

124-
if (StringWrapper.equals(this.strategy, ON_PUSH_OBSERVE)) {
125+
if (this.strategy === ChangeDetectionStrategy.OnPushObserve) {
125126
this.observeComponent(context);
126127
}
127128

@@ -140,7 +141,7 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
140141
this.dehydrateDirectives(true);
141142

142143
// This is an experimental feature. Works only in Dart.
143-
if (StringWrapper.equals(this.strategy, ON_PUSH_OBSERVE)) {
144+
if (this.strategy === ChangeDetectionStrategy.OnPushObserve) {
144145
this._unsubsribeFromObservables();
145146
}
146147

@@ -171,12 +172,12 @@ export class AbstractChangeDetector<T> implements ChangeDetector {
171172
}
172173
}
173174

174-
markAsCheckOnce(): void { this.mode = CHECK_ONCE; }
175+
markAsCheckOnce(): void { this.mode = ChangeDetectionStrategy.CheckOnce; }
175176

176177
markPathToRootAsCheckOnce(): void {
177178
var c: ChangeDetector = this;
178-
while (isPresent(c) && !StringWrapper.equals(c.mode, DETACHED)) {
179-
if (StringWrapper.equals(c.mode, CHECKED)) c.mode = CHECK_ONCE;
179+
while (isPresent(c) && c.mode !== ChangeDetectionStrategy.Detached) {
180+
if (c.mode === ChangeDetectionStrategy.Checked) c.mode = ChangeDetectionStrategy.CheckOnce;
180181
c = c.parent;
181182
}
182183
}

modules/angular2/src/core/change_detection/change_detection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export {
4848
DebugContext,
4949
ChangeDetectorGenConfig
5050
} from './interfaces';
51-
export {CHECK_ONCE, CHECK_ALWAYS, DETACHED, CHECKED, ON_PUSH, DEFAULT} from './constants';
51+
export {ChangeDetectionStrategy} from './constants';
5252
export {DynamicProtoChangeDetector} from './proto_change_detector';
5353
export {BindingRecord, BindingTarget} from './binding_record';
5454
export {DirectiveIndex, DirectiveRecord} from './directive_record';

modules/angular2/src/core/change_detection/change_detection_jit_generator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {codify} from './codegen_facade';
1212
import {EventBinding} from './event_binding';
1313
import {BindingTarget} from './binding_record';
1414
import {ChangeDetectorGenConfig} from './interfaces';
15+
import {ChangeDetectionStrategy} from './constants';
16+
1517

1618

1719
/**
@@ -33,7 +35,7 @@ export class ChangeDetectorJITGenerator {
3335
_names: CodegenNameUtil;
3436
_typeName: string;
3537

36-
constructor(private id: string, private changeDetectionStrategy: string,
38+
constructor(private id: string, private changeDetectionStrategy: ChangeDetectionStrategy,
3739
private records: List<ProtoRecord>, private propertyBindingTargets: BindingTarget[],
3840
private eventBindings: EventBinding[], private directiveRecords: List<any>,
3941
private genConfig: ChangeDetectorGenConfig) {

modules/angular2/src/core/change_detection/change_detection_util.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ import {
88
} from 'angular2/src/core/facade/lang';
99
import {List, ListWrapper, MapWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection';
1010
import {ProtoRecord} from './proto_record';
11-
import {
12-
CHECK_ALWAYS,
13-
CHECK_ONCE,
14-
CHECKED,
15-
DETACHED,
16-
isDefaultChangeDetectionStrategy
17-
} from './constants';
11+
import {ChangeDetectionStrategy, isDefaultChangeDetectionStrategy} from './constants';
1812
import {implementsOnDestroy} from './pipe_lifecycle_reflector';
1913
import {BindingTarget} from './binding_record';
2014
import {DirectiveIndex} from './directive_record';
@@ -180,8 +174,9 @@ export class ChangeDetectionUtil {
180174
}
181175
}
182176

183-
static changeDetectionMode(strategy: string): string {
184-
return isDefaultChangeDetectionStrategy(strategy) ? CHECK_ALWAYS : CHECK_ONCE;
177+
static changeDetectionMode(strategy: ChangeDetectionStrategy): ChangeDetectionStrategy {
178+
return isDefaultChangeDetectionStrategy(strategy) ? ChangeDetectionStrategy.CheckAlways :
179+
ChangeDetectionStrategy.CheckOnce;
185180
}
186181

187182
static simpleChange(previousValue: any, currentValue: any): SimpleChange {

modules/angular2/src/core/change_detection/change_detector_ref.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ChangeDetector} from './interfaces';
2-
import {CHECK_ONCE, DETACHED, CHECK_ALWAYS} from './constants';
2+
import {ChangeDetectionStrategy} from './constants';
33

44
/**
55
* Controls change detection.
@@ -14,7 +14,7 @@ export class ChangeDetectorRef {
1414
constructor(private _cd: ChangeDetector) {}
1515

1616
/**
17-
* Request to check all ON_PUSH ancestors.
17+
* Request to check all OnPush ancestors.
1818
*/
1919
requestCheck(): void { this._cd.markPathToRootAsCheckOnce(); }
2020

@@ -23,7 +23,7 @@ export class ChangeDetectorRef {
2323
*
2424
* The detached change detector will not be checked until it is reattached.
2525
*/
26-
detach(): void { this._cd.mode = DETACHED; }
26+
detach(): void { this._cd.mode = ChangeDetectionStrategy.Detached; }
2727

2828
/**
2929
* Reattach the change detector to the change detector tree.
@@ -33,7 +33,7 @@ export class ChangeDetectorRef {
3333
* next change detection run.
3434
*/
3535
reattach(): void {
36-
this._cd.mode = CHECK_ALWAYS;
36+
this._cd.mode = ChangeDetectionStrategy.CheckAlways;
3737
this.requestCheck();
3838
}
3939
}

modules/angular2/src/core/change_detection/codegen_logic_util.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import {codify, combineGeneratedStrings, rawString} from './codegen_facade';
1111
import {ProtoRecord, RecordType} from './proto_record';
1212
import {BindingTarget} from './binding_record';
1313
import {DirectiveRecord} from './directive_record';
14-
import {ON_PUSH_OBSERVE} from './constants';
14+
import {ChangeDetectionStrategy} from './constants';
1515

1616
/**
1717
* Class responsible for providing change detection logic for chagne detector classes.
1818
*/
1919
export class CodegenLogicUtil {
2020
constructor(private _names: CodegenNameUtil, private _utilName: string,
21-
private _changeDetection: string) {}
21+
private _changeDetection: ChangeDetectionStrategy) {}
2222

2323
/**
2424
* Generates a statement which updates the local variable representing `protoRec` with the current
@@ -119,7 +119,7 @@ export class CodegenLogicUtil {
119119

120120
_observe(exp: string, rec: ProtoRecord): string {
121121
// This is an experimental feature. Works only in Dart.
122-
if (StringWrapper.equals(this._changeDetection, ON_PUSH_OBSERVE)) {
122+
if (this._changeDetection === ChangeDetectionStrategy.OnPushObserve) {
123123
return `this.observeValue(${exp}, ${rec.selfIndex})`;
124124
} else {
125125
return exp;
@@ -165,7 +165,7 @@ export class CodegenLogicUtil {
165165

166166
private _genReadDirective(index: number) {
167167
// This is an experimental feature. Works only in Dart.
168-
if (StringWrapper.equals(this._changeDetection, ON_PUSH_OBSERVE)) {
168+
if (this._changeDetection === ChangeDetectionStrategy.OnPushObserve) {
169169
return `this.observeDirective(this.getDirectiveFor(directives, ${index}), ${index})`;
170170
} else {
171171
return `this.getDirectiveFor(directives, ${index})`;
Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
11
// TODO:vsavkin Use enums after switching to TypeScript
22
import {StringWrapper, normalizeBool, isBlank} from 'angular2/src/core/facade/lang';
33

4-
/**
5-
* CHECK_ONCE means that after calling detectChanges the mode of the change detector
6-
* will become CHECKED.
7-
*/
8-
export const CHECK_ONCE: string = "CHECK_ONCE";
9-
10-
/**
11-
* CHECKED means that the change detector should be skipped until its mode changes to
12-
* CHECK_ONCE or CHECK_ALWAYS.
13-
*/
14-
export const CHECKED: string = "CHECKED";
15-
16-
/**
17-
* CHECK_ALWAYS means that after calling detectChanges the mode of the change detector
18-
* will remain CHECK_ALWAYS.
19-
*/
20-
export const CHECK_ALWAYS: string = "ALWAYS_CHECK";
21-
22-
/**
23-
* DETACHED means that the change detector sub tree is not a part of the main tree and
24-
* should be skipped.
25-
*/
26-
export const DETACHED: string = "DETACHED";
27-
28-
/**
29-
* ON_PUSH means that the change detector's mode will be set to CHECK_ONCE during hydration.
30-
*/
31-
export const ON_PUSH: string = "ON_PUSH";
32-
33-
/**
34-
* DEFAULT means that the change detector's mode will be set to CHECK_ALWAYS during hydration.
35-
*/
36-
export const DEFAULT: string = "DEFAULT";
37-
38-
export function isDefaultChangeDetectionStrategy(changeDetectionStrategy: string): boolean {
39-
return isBlank(changeDetectionStrategy) || StringWrapper.equals(changeDetectionStrategy, DEFAULT);
4+
export enum ChangeDetectionStrategy {
5+
/**
6+
* `CheckedOnce` means that after calling detectChanges the mode of the change detector
7+
* will become `Checked`.
8+
*/
9+
CheckOnce,
10+
11+
/**
12+
* `Checked` means that the change detector should be skipped until its mode changes to
13+
* `CheckOnce`.
14+
*/
15+
Checked,
16+
17+
/**
18+
* `CheckAlways` means that after calling detectChanges the mode of the change detector
19+
* will remain `CheckAlways`.
20+
*/
21+
CheckAlways,
22+
23+
/**
24+
* `Detached` means that the change detector sub tree is not a part of the main tree and
25+
* should be skipped.
26+
*/
27+
Detached,
28+
29+
/**
30+
* `OnPush` means that the change detector's mode will be set to `CheckOnce` during hydration.
31+
*/
32+
OnPush,
33+
34+
/**
35+
* `Default` means that the change detector's mode will be set to `CheckAlways` during hydration.
36+
*/
37+
Default,
38+
39+
/**
40+
* This is an experimental feature. Works only in Dart.
41+
*/
42+
OnPushObserve
4043
}
4144

42-
43-
/**
44-
* This is an experimental feature. Works only in Dart.
45-
*/
46-
export const ON_PUSH_OBSERVE = "ON_PUSH_OBSERVE";
45+
export function isDefaultChangeDetectionStrategy(changeDetectionStrategy: ChangeDetectionStrategy):
46+
boolean {
47+
return isBlank(changeDetectionStrategy) ||
48+
changeDetectionStrategy === ChangeDetectionStrategy.Default;
49+
}

modules/angular2/src/core/change_detection/directive_record.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {StringWrapper, normalizeBool, isBlank} from 'angular2/src/core/facade/lang';
2-
import {isDefaultChangeDetectionStrategy} from './constants';
2+
import {isDefaultChangeDetectionStrategy, ChangeDetectionStrategy} from './constants';
33

44
export class DirectiveIndex {
55
constructor(public elementIndex: number, public directiveIndex: number) {}
@@ -13,7 +13,7 @@ export class DirectiveRecord {
1313
callOnChange: boolean;
1414
callOnCheck: boolean;
1515
callOnInit: boolean;
16-
changeDetection: string;
16+
changeDetection: ChangeDetectionStrategy;
1717

1818
constructor({directiveIndex, callOnAllChangesDone, callOnChange, callOnCheck, callOnInit,
1919
changeDetection}: {
@@ -22,7 +22,7 @@ export class DirectiveRecord {
2222
callOnChange?: boolean,
2323
callOnCheck?: boolean,
2424
callOnInit?: boolean,
25-
changeDetection?: string
25+
changeDetection?: ChangeDetectionStrategy
2626
} = {}) {
2727
this.directiveIndex = directiveIndex;
2828
this.callOnAllChangesDone = normalizeBool(callOnAllChangesDone);
@@ -35,4 +35,4 @@ export class DirectiveRecord {
3535
isDefaultChangeDetection(): boolean {
3636
return isDefaultChangeDetectionStrategy(this.changeDetection);
3737
}
38-
}
38+
}

modules/angular2/src/core/change_detection/dynamic_change_detector.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {DirectiveRecord, DirectiveIndex} from './directive_record';
1414
import {Locals} from './parser/locals';
1515
import {ChangeDetectorGenConfig} from './interfaces';
1616
import {ChangeDetectionUtil, SimpleChange} from './change_detection_util';
17-
import {ON_PUSH_OBSERVE} from './constants';
17+
import {ChangeDetectionStrategy} from './constants';
1818
import {ProtoRecord, RecordType} from './proto_record';
1919

2020
export class DynamicChangeDetector extends AbstractChangeDetector<any> {
@@ -26,7 +26,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
2626

2727
constructor(id: string, dispatcher: any, numberOfPropertyProtoRecords: number,
2828
propertyBindingTargets: BindingTarget[], directiveIndices: DirectiveIndex[],
29-
strategy: string, private records: ProtoRecord[],
29+
strategy: ChangeDetectionStrategy, private records: ProtoRecord[],
3030
private eventBindings: EventBinding[], private directiveRecords: DirectiveRecord[],
3131
private genConfig: ChangeDetectorGenConfig) {
3232
super(id, dispatcher, numberOfPropertyProtoRecords, propertyBindingTargets, directiveIndices,
@@ -88,7 +88,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
8888
this.values[0] = this.context;
8989
this.directives = directives;
9090

91-
if (StringWrapper.equals(this.strategy, ON_PUSH_OBSERVE)) {
91+
if (this.strategy === ChangeDetectionStrategy.OnPushObserve) {
9292
for (var i = 0; i < this.directiveIndices.length; ++i) {
9393
var index = this.directiveIndices[i];
9494
super.observeDirective(directives.getDirectiveFor(index), i);
@@ -219,7 +219,7 @@ export class DynamicChangeDetector extends AbstractChangeDetector<any> {
219219
}
220220

221221
var currValue = this._calculateCurrValue(proto, values, locals);
222-
if (StringWrapper.equals(this.strategy, ON_PUSH_OBSERVE)) {
222+
if (this.strategy === ChangeDetectionStrategy.OnPushObserve) {
223223
super.observeValue(currValue, proto.selfIndex);
224224
}
225225

0 commit comments

Comments
 (0)