feat(change_detection): do not register a change from switching from null to null

This commit is contained in:
vsavkin 2015-02-02 10:11:45 -08:00 committed by Alex Eagle
parent f014b53a4c
commit 709df12b10
4 changed files with 35 additions and 25 deletions

View File

@ -52,6 +52,10 @@ export class ArrayChanges {
return isListLikeIterable(obj);
}
supportsObj(obj):boolean {
return ArrayChanges.supports(obj);
}
get collection() {
return this._collection;
}

View File

@ -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;
}
}

View File

@ -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 ||

View File

@ -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();