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

fix(change_detection): should properly support objects with equality #735

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}