Skip to content

Commit 0a40cee

Browse files
committed
fix(dccd): fix removals reporting
closes dart-archive#1097
1 parent 3e16015 commit 0a40cee

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
@@ -1142,9 +1142,6 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
11421142
var prev = record._prevRemoved;
11431143
var next = record._nextRemoved;
11441144

1145-
assert((record._prevRemoved = null) == null);
1146-
assert((record._nextRemoved = null) == null);
1147-
11481145
if (prev == null) {
11491146
_removalsHead = next;
11501147
} else {
@@ -1255,10 +1252,12 @@ class _CollectionChangeRecord<V> implements CollectionChangeRecord<V> {
12551252
ItemRecord<V> _addToRemovals(ItemRecord<V> record) {
12561253
_removedItems.put(record);
12571254
record.currentIndex = null;
1255+
record._nextRemoved = null;
12581256

12591257
if (_removalsTail == null) {
12601258
assert(_removalsHead == null);
12611259
_removalsTail = _removalsHead = record;
1260+
record._prevRemoved = null;
12621261
} else {
12631262
assert(_removalsTail._nextRemoved == null);
12641263
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)