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

Commit 156d638

Browse files
vicbmhevery
authored andcommitted
fix(change detector): fix the handling of NaN & string values for maps
1 parent 07f9b24 commit 156d638

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/change_detection/dirty_checking_change_detector.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,13 @@ class _MapChangeRecord<K, V> implements MapChangeRecord<K, V> {
451451
if (oldSeqRecord != null && key == oldSeqRecord.key) {
452452
newSeqRecord = oldSeqRecord;
453453
if (!identical(value, oldSeqRecord._currentValue)) {
454-
oldSeqRecord._previousValue = oldSeqRecord._currentValue;
454+
var prev = oldSeqRecord._previousValue = oldSeqRecord._currentValue;
455455
oldSeqRecord._currentValue = value;
456-
_addToChanges(oldSeqRecord);
456+
if (!((value is String && prev is String && value == prev) ||
457+
(value is num && value.isNaN && prev is num && prev.isNaN))) {
458+
// Check string by value rather than reference
459+
_addToChanges(oldSeqRecord);
460+
}
457461
}
458462
} else {
459463
seqChanged = true;

test/change_detection/dirty_checking_change_detector_spec.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,31 @@ main() => describe('DirtyCheckingChangeDetector', () {
395395
changes: [],
396396
removals: ['a[A -> null]', 'd[D -> null]']));
397397
});
398+
399+
it('should test string keys by value rather than by reference', () {
400+
var map = {'foo': 0};
401+
detector..watch(map, null, null)..collectChanges();
402+
403+
map['f' + 'oo'] = 0;
404+
405+
expect(detector.collectChanges()).toEqual(null);
406+
});
407+
408+
it('should test string values by value rather than by reference', () {
409+
var map = {'foo': 'bar'};
410+
detector..watch(map, null, null)..collectChanges();
411+
412+
map['foo'] = 'b' + 'ar';
413+
414+
expect(detector.collectChanges()).toEqual(null);
415+
});
416+
417+
it('should not see a NaN value as a change', () {
418+
var map = {'foo': double.NAN};
419+
var record = detector..watch(map, null, null)..collectChanges();
420+
421+
expect(detector.collectChanges()).toEqual(null);
422+
});
398423
});
399424

400425
describe('DuplicateMap', () {

0 commit comments

Comments
 (0)