Skip to content

Commit cd5d8be

Browse files
committed
feat(change_detection): do not register a change from switching from null to null
1 parent 3ad6285 commit cd5d8be

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

modules/change_detection/src/array_changes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export class ArrayChanges {
5252
return isListLikeIterable(obj);
5353
}
5454

55+
supportsObj(obj):boolean {
56+
return ArrayChanges.supports(obj);
57+
}
58+
5559
get collection() {
5660
return this._collection;
5761
}

modules/change_detection/src/change_detection_util.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ function _changeRecord(bindingMemento, change) {
8585

8686
var _singleElementList = [null];
8787

88+
function _isBlank(val):boolean {
89+
return isBlank(val) || val === uninitialized;
90+
}
91+
8892
export class ChangeDetectionUtil {
8993
static unitialized() {
9094
return uninitialized;
@@ -146,37 +150,28 @@ export class ChangeDetectionUtil {
146150
}
147151

148152
static structuralCheck(self, context) {
149-
if (isBlank(self) || self === uninitialized) {
153+
if (_isBlank(self) && _isBlank(context)) {
154+
return null;
155+
} else if (_isBlank(context)) {
156+
return new SimpleChange(null, null);
157+
}
158+
159+
if (_isBlank(self)) {
150160
if (ArrayChanges.supports(context)) {
151161
self = new ArrayChanges();
152162
} else if (KeyValueChanges.supports(context)) {
153163
self = new KeyValueChanges();
154164
}
155165
}
156166

157-
if (isBlank(context) || context === uninitialized) {
158-
return new SimpleChange(null, null);
167+
if (isBlank(self) || !self.supportsObj(context)) {
168+
throw new BaseException(`Unsupported type (${context})`);
169+
}
159170

171+
if (self.check(context)) {
172+
return new SimpleChange(null, self); // TODO: don't wrap and return self instead
160173
} else {
161-
if (ArrayChanges.supports(context)) {
162-
163-
if (self.check(context)) {
164-
return new SimpleChange(null, self); // TODO: don't wrap and return self instead
165-
} else {
166-
return null;
167-
}
168-
169-
} else if (KeyValueChanges.supports(context)) {
170-
171-
if (self.check(context)) {
172-
return new SimpleChange(null, self); // TODO: don't wrap and return self instead
173-
} else {
174-
return null;
175-
}
176-
177-
} else {
178-
throw new BaseException(`Unsupported type (${context})`);
179-
}
174+
return null;
180175
}
181176
}
182177

modules/change_detection/src/keyvalue_changes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export class KeyValueChanges {
3030
return obj instanceof Map || isJsObject(obj);
3131
}
3232

33+
supportsObj(obj):boolean {
34+
return KeyValueChanges.supports(obj);
35+
}
36+
3337
get isDirty():boolean {
3438
return this._additionsHead !== null ||
3539
this._changesHead !== null ||

modules/change_detection/test/change_detection_spec.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,23 @@ export function main() {
283283
});
284284

285285
describe("collections", () => {
286-
it("should support null values", () => {
286+
it("should not register a change when going from null to null", () => {
287287
var context = new TestData(null);
288288

289289
var c = createChangeDetector('a', 'a', context, null, true);
290290
var cd = c["changeDetector"];
291291
var dispatcher = c["dispatcher"];
292292

293293
cd.detectChanges();
294-
expect(dispatcher.log).toEqual(['a=null']);
295-
dispatcher.clear();
294+
expect(dispatcher.log).toEqual([]);
295+
});
296+
297+
it("should register changes when switching from null to collection and back", () => {
298+
var context = new TestData(null);
299+
300+
var c = createChangeDetector('a', 'a', context, null, true);
301+
var cd = c["changeDetector"];
302+
var dispatcher = c["dispatcher"];
296303

297304
context.a = [0];
298305
cd.detectChanges();

0 commit comments

Comments
 (0)