From 709df12b107be257d468fe1ed7111cd585314fa1 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Mon, 2 Feb 2015 10:11:45 -0800 Subject: [PATCH] feat(change_detection): do not register a change from switching from null to null --- modules/change_detection/src/array_changes.js | 4 ++ .../src/change_detection_util.js | 39 ++++++++----------- .../change_detection/src/keyvalue_changes.js | 4 ++ .../test/change_detection_spec.js | 13 +++++-- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/modules/change_detection/src/array_changes.js b/modules/change_detection/src/array_changes.js index 78d616f224..cb2d9ac79d 100644 --- a/modules/change_detection/src/array_changes.js +++ b/modules/change_detection/src/array_changes.js @@ -52,6 +52,10 @@ export class ArrayChanges { return isListLikeIterable(obj); } + supportsObj(obj):boolean { + return ArrayChanges.supports(obj); + } + get collection() { return this._collection; } diff --git a/modules/change_detection/src/change_detection_util.js b/modules/change_detection/src/change_detection_util.js index eca1bb474e..31491c2200 100644 --- a/modules/change_detection/src/change_detection_util.js +++ b/modules/change_detection/src/change_detection_util.js @@ -85,6 +85,10 @@ function _changeRecord(bindingMemento, change) { var _singleElementList = [null]; +function _isBlank(val):boolean { + return isBlank(val) || val === uninitialized; +} + export class ChangeDetectionUtil { static unitialized() { return uninitialized; @@ -146,7 +150,13 @@ export class ChangeDetectionUtil { } static structuralCheck(self, context) { - if (isBlank(self) || self === uninitialized) { + if (_isBlank(self) && _isBlank(context)) { + return null; + } else if (_isBlank(context)) { + return new SimpleChange(null, null); + } + + if (_isBlank(self)) { if (ArrayChanges.supports(context)) { self = new ArrayChanges(); } else if (KeyValueChanges.supports(context)) { @@ -154,29 +164,14 @@ export class ChangeDetectionUtil { } } - if (isBlank(context) || context === uninitialized) { - return new SimpleChange(null, null); + if (isBlank(self) || !self.supportsObj(context)) { + throw new BaseException(`Unsupported type (${context})`); + } + if (self.check(context)) { + return new SimpleChange(null, self); // TODO: don't wrap and return self instead } else { - if (ArrayChanges.supports(context)) { - - if (self.check(context)) { - return new SimpleChange(null, self); // TODO: don't wrap and return self instead - } else { - return null; - } - - } else if (KeyValueChanges.supports(context)) { - - if (self.check(context)) { - return new SimpleChange(null, self); // TODO: don't wrap and return self instead - } else { - return null; - } - - } else { - throw new BaseException(`Unsupported type (${context})`); - } + return null; } } diff --git a/modules/change_detection/src/keyvalue_changes.js b/modules/change_detection/src/keyvalue_changes.js index 6420e0a0a0..bbef6e5ebd 100644 --- a/modules/change_detection/src/keyvalue_changes.js +++ b/modules/change_detection/src/keyvalue_changes.js @@ -30,6 +30,10 @@ export class KeyValueChanges { return obj instanceof Map || isJsObject(obj); } + supportsObj(obj):boolean { + return KeyValueChanges.supports(obj); + } + get isDirty():boolean { return this._additionsHead !== null || this._changesHead !== null || diff --git a/modules/change_detection/test/change_detection_spec.js b/modules/change_detection/test/change_detection_spec.js index 7e92333686..cd9eb10155 100644 --- a/modules/change_detection/test/change_detection_spec.js +++ b/modules/change_detection/test/change_detection_spec.js @@ -283,7 +283,7 @@ export function main() { }); describe("collections", () => { - it("should support null values", () => { + it("should not register a change when going from null to null", () => { var context = new TestData(null); var c = createChangeDetector('a', 'a', context, null, true); @@ -291,8 +291,15 @@ export function main() { var dispatcher = c["dispatcher"]; cd.detectChanges(); - expect(dispatcher.log).toEqual(['a=null']); - dispatcher.clear(); + expect(dispatcher.log).toEqual([]); + }); + + it("should register changes when switching from null to collection and back", () => { + var context = new TestData(null); + + var c = createChangeDetector('a', 'a', context, null, true); + var cd = c["changeDetector"]; + var dispatcher = c["dispatcher"]; context.a = [0]; cd.detectChanges();