Skip to content

Commit fb4ae8b

Browse files
vicbDiana Salsbury
authored and
Diana Salsbury
committed
fix(dccd): fix false positive for collection moves
Closes dart-archive#1105
1 parent 0c4091a commit fb4ae8b

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
@@ -1110,7 +1110,7 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
11101110
record = _reinsertAfter(reinsertRecord, record._prev, index);
11111111
} else if (record.currentIndex != index) {
11121112
record.currentIndex = index;
1113-
_addToMoves(record);
1113+
_addToMoves(record, index);
11141114
}
11151115
return record;
11161116
}
@@ -1152,14 +1152,14 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
11521152
}
11531153

11541154
_insertAfter(record, prevRecord, index);
1155-
_addToMoves(record);
1155+
_addToMoves(record, index);
11561156
return record;
11571157
}
11581158

11591159
ItemRecord<V> _moveAfter(ItemRecord<V> record, ItemRecord<V> prevRecord, int index) {
11601160
_unlink(record);
11611161
_insertAfter(record, prevRecord, index);
1162-
_addToMoves(record);
1162+
_addToMoves(record, index);
11631163
return record;
11641164
}
11651165

@@ -1228,8 +1228,11 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
12281228
return record;
12291229
}
12301230

1231-
ItemRecord<V> _addToMoves(ItemRecord<V> record) {
1231+
ItemRecord<V> _addToMoves(ItemRecord<V> record, int toIndex) {
12321232
assert(record._nextMoved == null);
1233+
1234+
if (record.previousIndex == toIndex) return record;
1235+
12331236
if (_movesTail == null) {
12341237
assert(_movesHead == null);
12351238
_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']);
@@ -532,6 +532,21 @@ void testWithGetterFactory(FieldGetterFactory getterFactory) {
532532
moves: ['(1)a-a[0 -> 1]'],
533533
removals: []));
534534
});
535+
536+
it('should not report unnecessary moves', () {
537+
var list = ['a', 'b', 'c'];
538+
var record = detector.watch(list, null, null);
539+
var iterator = detector.collectChanges()..moveNext();
540+
541+
list..clear()..addAll(['b', 'a', 'c']);
542+
iterator = detector.collectChanges()..moveNext();
543+
expect(iterator.current.currentValue, toEqualCollectionRecord(
544+
collection: ['b[1 -> 0]', 'a[0 -> 1]', 'c'],
545+
previous: ['a[0 -> 1]', 'b[1 -> 0]', 'c'],
546+
additions: [],
547+
moves: ['b[1 -> 0]', 'a[0 -> 1]'],
548+
removals: []));
549+
});
535550
});
536551

537552
describe('map watching', () {

0 commit comments

Comments
 (0)