-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MapDiffer, cleanup MapChangeRecord (#8)
* Fix bug in list_differ * Add MapDiffer and refactor MapChangeRecord
- Loading branch information
1 parent
5d9eec7
commit 81bbeab
Showing
12 changed files
with
271 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
part of observable.src.differs; | ||
|
||
/// Determines differences between two maps, returning [MapChangeRecord]s. | ||
/// | ||
/// While [MapChangeRecord] has more information and can be replayed they carry | ||
/// a more significant cost to calculate and create and should only be used when | ||
/// the details in the record will actually be used. | ||
/// | ||
/// See also [EqualityDiffer] for a simpler comparison. | ||
class MapDiffer<K, V> implements Differ<Map<K, V>> { | ||
const MapDiffer(); | ||
|
||
@override | ||
List<MapChangeRecord<K, V>> diff(Map<K, V> oldValue, Map<K, V> newValue) { | ||
if (identical(oldValue, newValue)) { | ||
return ChangeRecord.NONE; | ||
} | ||
final changes = <MapChangeRecord<K, V>>[]; | ||
oldValue.forEach((oldK, oldV) { | ||
final newV = newValue[oldK]; | ||
if (newV == null && !newValue.containsKey(oldK)) { | ||
changes.add(new MapChangeRecord<K, V>.remove(oldK, oldV)); | ||
} else if (newV != oldV) { | ||
changes.add(new MapChangeRecord<K, V>(oldK, oldV, newV)); | ||
} | ||
}); | ||
newValue.forEach((newK, newV) { | ||
if (!oldValue.containsKey(newK)) { | ||
changes.add(new MapChangeRecord<K, V>.insert(newK, newV)); | ||
} | ||
}); | ||
return freezeInDevMode(changes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
List/*<E>*/ freezeInDevMode/*<E>*/(List/*<E>*/ list) { | ||
if (list == null) return const []; | ||
assert(() { | ||
list = new List/*<E>*/ .unmodifiable(list); | ||
return true; | ||
}); | ||
return list; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
part of observable.src.records; | ||
|
||
/// A [ChangeRecord] that denotes adding, removing, or updating a map. | ||
class MapChangeRecord<K, V> implements ChangeRecord { | ||
/// The map key that changed. | ||
final K key; | ||
|
||
/// The previous value associated with this key. | ||
/// | ||
/// Is always `null` if [isInsert]. | ||
final V oldValue; | ||
|
||
/// The new value associated with this key. | ||
/// | ||
/// Is always `null` if [isRemove]. | ||
final V newValue; | ||
|
||
/// True if this key was inserted. | ||
final bool isInsert; | ||
|
||
/// True if this key was removed. | ||
final bool isRemove; | ||
|
||
/// Create an update record of [key] from [oldValue] to [newValue]. | ||
const MapChangeRecord(this.key, this.oldValue, this.newValue) | ||
: isInsert = false, | ||
isRemove = false; | ||
|
||
/// Create an insert record of [key] and [newValue]. | ||
const MapChangeRecord.insert(this.key, this.newValue) | ||
: isInsert = true, | ||
isRemove = false, | ||
oldValue = null; | ||
|
||
/// Create a remove record of [key] with a former [oldValue]. | ||
const MapChangeRecord.remove(this.key, this.oldValue) | ||
: isInsert = false, | ||
isRemove = true, | ||
newValue = null; | ||
|
||
/// Apply this change record to [map]. | ||
void apply(Map<K, V> map) { | ||
if (isRemove) { | ||
map.remove(key); | ||
} else { | ||
map[key] = newValue; | ||
} | ||
} | ||
|
||
@override | ||
bool operator ==(Object o) { | ||
if (o is MapChangeRecord<K, V>) { | ||
return key == o.key && | ||
oldValue == o.oldValue && | ||
newValue == o.newValue && | ||
isInsert == o.isInsert && | ||
isRemove == o.isRemove; | ||
} | ||
return false; | ||
} | ||
|
||
@override | ||
int get hashCode { | ||
return hashObjects([ | ||
key, | ||
oldValue, | ||
newValue, | ||
isInsert, | ||
isRemove, | ||
]); | ||
} | ||
|
||
@override | ||
String toString() { | ||
final kind = isInsert ? 'insert' : isRemove ? 'remove' : 'set'; | ||
return '#<MapChangeRecord $kind $key from $oldValue to $newValue'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: observable | ||
version: 0.15.0+2 | ||
version: 0.16.0 | ||
author: Dart Team <[email protected]> | ||
description: Support for marking objects as observable | ||
homepage: https://github.com/dart-lang/observable | ||
|
Oops, something went wrong.