Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
fix(change_detection): should properly support objects with equality
Browse files Browse the repository at this point in the history
Closes #735
Fixes #670
  • Loading branch information
pavelgj authored and mhevery committed Mar 14, 2014
1 parent 9b24338 commit 9b480da
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/change_detection/dirty_checking_change_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1226,8 +1226,8 @@ class _DuplicateItemRecordList {
ItemRecord get(key, int hideIndex) {
ItemRecord record = head;
while (record != null) {
if (hideIndex == null ||
hideIndex < record.currentIndex && identical(record.item, key)) {
if ((hideIndex == null || hideIndex < record.currentIndex) &&
identical(record.item, key)) {
return record;
}
record = record._nextDupRec;
Expand Down
51 changes: 51 additions & 0 deletions test/change_detection/dirty_checking_change_detector_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,37 @@ void main() {
moves: ['2[0 -> 1]', '3[1 -> 2]', '4[2 -> 3]'],
removals: []));
});

it('should properly support objects with equality', () {
FooBar.fooIds = 0;
var list = [new FooBar('a', 'a'), new FooBar('a', 'a')];
var record = detector.watch(list, null, 'handler');
var iterator;

iterator = detector.collectChanges()..moveNext();
expect(iterator.current.currentValue, toEqualCollectionRecord(
collection: ['(0)a-a[null -> 0]', '(1)a-a[null -> 1]'],
additions: ['(0)a-a[null -> 0]', '(1)a-a[null -> 1]'],
moves: [],
removals: []));
detector.collectChanges();

list.removeRange(0, 1);
iterator = detector.collectChanges()..moveNext();
expect(iterator.current.currentValue, toEqualCollectionRecord(
collection: ['(1)a-a[1 -> 0]'],
additions: [],
moves: ['(1)a-a[1 -> 0]'],
removals: ['(0)a-a[0 -> null]']));

list.insert(0, new FooBar('a', 'a'));
iterator = detector.collectChanges()..moveNext();
expect(iterator.current.currentValue, toEqualCollectionRecord(
collection: ['(2)a-a[null -> 0]', '(1)a-a[0 -> 1]'],
additions: ['(2)a-a[null -> 0]'],
moves: ['(1)a-a[0 -> 1]'],
removals: []));
});
});

describe('map watching', () {
Expand Down Expand Up @@ -823,3 +854,23 @@ class MapRecordMatcher extends Matcher {
return equals;
}
}


class FooBar {
static int fooIds = 0;

int id;
String foo, bar;

FooBar(this.foo, this.bar) {
id = fooIds++;
}

bool operator==(other) =>
other is FooBar && foo == other.foo && bar == other.bar;

int get hashCode =>
foo.hashCode ^ bar.hashCode;

toString() => '($id)$foo-$bar';
}

0 comments on commit 9b480da

Please sign in to comment.