fix(change_detection): expose values when detecting changes in key-value pairs

Fixes #1118

Closes #1123
This commit is contained in:
Pawel Kozlowski 2015-03-26 19:19:10 +01:00
parent b09624024b
commit 5306b6dd0c
2 changed files with 33 additions and 15 deletions

View File

@ -107,9 +107,9 @@ export class KeyValueChanges extends Pipe {
var newSeqRecord; var newSeqRecord;
if (oldSeqRecord !== null && key === oldSeqRecord.key) { if (oldSeqRecord !== null && key === oldSeqRecord.key) {
newSeqRecord = oldSeqRecord; newSeqRecord = oldSeqRecord;
if (!looseIdentical(value, oldSeqRecord._currentValue)) { if (!looseIdentical(value, oldSeqRecord.currentValue)) {
oldSeqRecord._previousValue = oldSeqRecord._currentValue; oldSeqRecord.previousValue = oldSeqRecord.currentValue;
oldSeqRecord._currentValue = value; oldSeqRecord.currentValue = value;
this._addToChanges(oldSeqRecord); this._addToChanges(oldSeqRecord);
} }
} else { } else {
@ -124,7 +124,7 @@ export class KeyValueChanges extends Pipe {
} else { } else {
newSeqRecord = new KVChangeRecord(key); newSeqRecord = new KVChangeRecord(key);
MapWrapper.set(records, key, newSeqRecord); MapWrapper.set(records, key, newSeqRecord);
newSeqRecord._currentValue = value; newSeqRecord.currentValue = value;
this._addToAdditions(newSeqRecord); this._addToAdditions(newSeqRecord);
} }
} }
@ -158,11 +158,11 @@ export class KeyValueChanges extends Pipe {
} }
for (record = this._changesHead; record !== null; record = record._nextChanged) { for (record = this._changesHead; record !== null; record = record._nextChanged) {
record._previousValue = record._currentValue; record.previousValue = record.currentValue;
} }
for (record = this._additionsHead; record != null; record = record._nextAdded) { for (record = this._additionsHead; record != null; record = record._nextAdded) {
record._previousValue = record._currentValue; record.previousValue = record.currentValue;
} }
// todo(vicb) once assert is supported // todo(vicb) once assert is supported
@ -215,8 +215,8 @@ export class KeyValueChanges extends Pipe {
} }
for (var rec:KVChangeRecord = this._removalsHead; rec !== null; rec = rec._nextRemoved) { for (var rec:KVChangeRecord = this._removalsHead; rec !== null; rec = rec._nextRemoved) {
rec._previousValue = rec._currentValue; rec.previousValue = rec.currentValue;
rec._currentValue = null; rec.currentValue = null;
MapWrapper.delete(this._records, rec.key); MapWrapper.delete(this._records, rec.key);
} }
} }
@ -351,8 +351,8 @@ export class KeyValueChanges extends Pipe {
export class KVChangeRecord { export class KVChangeRecord {
key; key;
_previousValue; previousValue;
_currentValue; currentValue;
_nextPrevious:KVChangeRecord; _nextPrevious:KVChangeRecord;
_next:KVChangeRecord; _next:KVChangeRecord;
@ -363,8 +363,8 @@ export class KVChangeRecord {
constructor(key) { constructor(key) {
this.key = key; this.key = key;
this._previousValue = null; this.previousValue = null;
this._currentValue = null; this.currentValue = null;
this._nextPrevious = null; this._nextPrevious = null;
this._next = null; this._next = null;
@ -375,9 +375,9 @@ export class KVChangeRecord {
} }
toString():string { toString():string {
return looseIdentical(this._previousValue, this._currentValue) ? return looseIdentical(this.previousValue, this.currentValue) ?
stringify(this.key) : stringify(this.key) :
(stringify(this.key) + '[' + stringify(this._previousValue) + '->' + (stringify(this.key) + '[' + stringify(this.previousValue) + '->' +
stringify(this._currentValue) + ']'); stringify(this.currentValue) + ']');
} }
} }

View File

@ -54,6 +54,24 @@ export function main() {
})); }));
}); });
it('should expose previous and current value', () => {
var previous, current;
MapWrapper.set(m, 1, 10);
changes.check(m);
MapWrapper.set(m, 1, 20);
changes.check(m);
changes.forEachChangedItem((record) => {
previous = record.previousValue;
current = record.currentValue;
})
expect(previous).toEqual(10);
expect(current).toEqual(20);
});
it('should do basic map watching', () => { it('should do basic map watching', () => {
changes.check(m); changes.check(m);