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

Commit bdb5d07

Browse files
vicbchirayuk
authored andcommitted
fix(dccd): fix false positive for collection moves
Closes #1105
1 parent 9b31699 commit bdb5d07

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

lib/change_detection/dirty_checking_change_detector.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,7 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
10871087
record = _reinsertAfter(reinsertRecord, record._prev, index);
10881088
} else if (record.currentIndex != index) {
10891089
record.currentIndex = index;
1090-
_addToMoves(record);
1090+
_addToMoves(record, index);
10911091
}
10921092
return record;
10931093
}
@@ -1129,14 +1129,14 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
11291129
}
11301130

11311131
_insertAfter(record, prevRecord, index);
1132-
_addToMoves(record);
1132+
_addToMoves(record, index);
11331133
return record;
11341134
}
11351135

11361136
ItemRecord<V> _moveAfter(ItemRecord<V> record, ItemRecord<V> prevRecord, int index) {
11371137
_unlink(record);
11381138
_insertAfter(record, prevRecord, index);
1139-
_addToMoves(record);
1139+
_addToMoves(record, index);
11401140
return record;
11411141
}
11421142

@@ -1207,8 +1207,11 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
12071207
return record;
12081208
}
12091209

1210-
ItemRecord<V> _addToMoves(ItemRecord<V> record) {
1210+
ItemRecord<V> _addToMoves(ItemRecord<V> record, int toIndex) {
12111211
assert(record._nextMoved == null);
1212+
1213+
if (record.previousIndex == toIndex) return record;
1214+
12121215
if (_movesTail == null) {
12131216
assert(_movesHead == null);
12141217
_movesTail = _movesHead = record;

test/change_detection/dirty_checking_change_detector_spec.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ void testWithGetterFactory(FieldGetterFactory getterFactory) {
301301
collection: ['b[1 -> 0]', 'a[0 -> 1]', 'c'],
302302
previous: ['a[0 -> 1]', 'b[1 -> 0]', 'c'],
303303
additions: [],
304-
moves: ['b[1 -> 0]', 'a[0 -> 1]', 'c'],
304+
moves: ['b[1 -> 0]', 'a[0 -> 1]'],
305305
removals: []));
306306

307307
list..clear()..addAll(['b', 'c', 'a']);
@@ -547,6 +547,21 @@ void testWithGetterFactory(FieldGetterFactory getterFactory) {
547547
moves: ['(1)a-a[0 -> 1]'],
548548
removals: []));
549549
});
550+
551+
it('should not report unnecessary moves', () {
552+
var list = ['a', 'b', 'c'];
553+
var record = detector.watch(list, null, null);
554+
var iterator = detector.collectChanges()..moveNext();
555+
556+
list..clear()..addAll(['b', 'a', 'c']);
557+
iterator = detector.collectChanges()..moveNext();
558+
expect(iterator.current.currentValue, toEqualCollectionRecord(
559+
collection: ['b[1 -> 0]', 'a[0 -> 1]', 'c'],
560+
previous: ['a[0 -> 1]', 'b[1 -> 0]', 'c'],
561+
additions: [],
562+
moves: ['b[1 -> 0]', 'a[0 -> 1]'],
563+
removals: []));
564+
});
550565
});
551566

552567
describe('map watching', () {

0 commit comments

Comments
 (0)