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); return isListLikeIterable(obj);
} }
supportsObj(obj):boolean {
return ArrayChanges.supports(obj);
}
get collection() { get collection() {
return this._collection; return this._collection;
} }

View File

@ -85,6 +85,10 @@ function _changeRecord(bindingMemento, change) {
var _singleElementList = [null]; var _singleElementList = [null];
function _isBlank(val):boolean {
return isBlank(val) || val === uninitialized;
}
export class ChangeDetectionUtil { export class ChangeDetectionUtil {
static unitialized() { static unitialized() {
return uninitialized; return uninitialized;
@ -146,7 +150,13 @@ export class ChangeDetectionUtil {
} }
static structuralCheck(self, context) { 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)) { if (ArrayChanges.supports(context)) {
self = new ArrayChanges(); self = new ArrayChanges();
} else if (KeyValueChanges.supports(context)) { } else if (KeyValueChanges.supports(context)) {
@ -154,29 +164,14 @@ export class ChangeDetectionUtil {
} }
} }
if (isBlank(context) || context === uninitialized) { if (isBlank(self) || !self.supportsObj(context)) {
return new SimpleChange(null, null); throw new BaseException(`Unsupported type (${context})`);
}
if (self.check(context)) {
return new SimpleChange(null, self); // TODO: don't wrap and return self instead
} else { } else {
if (ArrayChanges.supports(context)) { return null;
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})`);
}
} }
} }

View File

@ -30,6 +30,10 @@ export class KeyValueChanges {
return obj instanceof Map || isJsObject(obj); return obj instanceof Map || isJsObject(obj);
} }
supportsObj(obj):boolean {
return KeyValueChanges.supports(obj);
}
get isDirty():boolean { get isDirty():boolean {
return this._additionsHead !== null || return this._additionsHead !== null ||
this._changesHead !== null || this._changesHead !== null ||

View File

@ -283,7 +283,7 @@ export function main() {
}); });
describe("collections", () => { 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 context = new TestData(null);
var c = createChangeDetector('a', 'a', context, null, true); var c = createChangeDetector('a', 'a', context, null, true);
@ -291,8 +291,15 @@ export function main() {
var dispatcher = c["dispatcher"]; var dispatcher = c["dispatcher"];
cd.detectChanges(); cd.detectChanges();
expect(dispatcher.log).toEqual(['a=null']); expect(dispatcher.log).toEqual([]);
dispatcher.clear(); });
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]; context.a = [0];
cd.detectChanges(); cd.detectChanges();