Skip to content

Commit 00664c3

Browse files
vicbDiana Salsbury
authored and
Diana Salsbury
committed
fix(dccd): fix removals reporting
closes dart-archive#1097
1 parent 7b6e3ab commit 00664c3

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/change_detection/dirty_checking_change_detector.dart

+2-3
Original file line numberDiff line numberDiff line change
@@ -1140,9 +1140,6 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
11401140
var prev = record._prevRemoved;
11411141
var next = record._nextRemoved;
11421142

1143-
assert((record._prevRemoved = null) == null);
1144-
assert((record._nextRemoved = null) == null);
1145-
11461143
if (prev == null) {
11471144
_removalsHead = next;
11481145
} else {
@@ -1247,10 +1244,12 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
12471244
ItemRecord<V> _addToRemovals(ItemRecord<V> record) {
12481245
_removedItems.put(record);
12491246
record.currentIndex = null;
1247+
record._nextRemoved = null;
12501248

12511249
if (_removalsTail == null) {
12521250
assert(_removalsHead == null);
12531251
_removalsTail = _removalsHead = record;
1252+
record._prevRemoved = null;
12541253
} else {
12551254
assert(_removalsTail._nextRemoved == null);
12561255
assert(record._nextRemoved == null);

test/change_detection/dirty_checking_change_detector_spec.dart

+25
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,31 @@ void testWithGetterFactory(FieldGetterFactory getterFactory) {
288288
moves: ['2[1 -> 0]', '1[0 -> 1]'],
289289
removals: []));
290290
});
291+
292+
it('should handle swapping elements correctly - gh1097', () {
293+
// This test would only have failed in non-checked mode only
294+
var list = ['a', 'b', 'c'];
295+
var record = detector.watch(list, null, null);
296+
var iterator = detector.collectChanges()..moveNext();
297+
298+
list..clear()..addAll(['b', 'a', 'c']);
299+
iterator = detector.collectChanges()..moveNext();
300+
expect(iterator.current.currentValue, toEqualCollectionRecord(
301+
collection: ['b[1 -> 0]', 'a[0 -> 1]', 'c'],
302+
previous: ['a[0 -> 1]', 'b[1 -> 0]', 'c'],
303+
additions: [],
304+
moves: ['b[1 -> 0]', 'a[0 -> 1]', 'c'],
305+
removals: []));
306+
307+
list..clear()..addAll(['b', 'c', 'a']);
308+
iterator = detector.collectChanges()..moveNext();
309+
expect(iterator.current.currentValue, toEqualCollectionRecord(
310+
collection: ['b', 'c[2 -> 1]', 'a[1 -> 2]'],
311+
previous: ['b', 'a[1 -> 2]', 'c[2 -> 1]'],
312+
additions: [],
313+
moves: ['c[2 -> 1]', 'a[1 -> 2]'],
314+
removals: []));
315+
});
291316
});
292317

293318
it('should detect changes in list', () {

0 commit comments

Comments
 (0)